QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#463960#8551. DFS Order 5PhantomThreshold#WA 44ms14508kbC++172.9kb2024-07-05 16:45:062024-07-05 16:45:06

Judging History

你现在查看的是最新测评结果

  • [2024-07-05 16:45:06]
  • 评测
  • 测评结果:WA
  • 用时:44ms
  • 内存:14508kb
  • [2024-07-05 16:45:06]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;

const int maxn=100000;

namespace BIT{
	int tr[maxn+50];
	inline int lowbit(int x){
		return (x)&(-x);
	}
	void add(int pos,int val){
		for (int i=pos;i<=maxn;i+=lowbit(i)) tr[i]+=val; 
	}
	int query(int pos){
		int ans=0;
		for (int i=pos;i>=1;i-=lowbit(i)) ans+=tr[i];
		return ans;		
	}
}

int n,Q;
vector<int> g[maxn+50];
int len=0;
int l[maxn+50];
int r[maxn+50];
int sz[maxn+50];
int dep[maxn+50];

const int maxl=17;
int fa[maxl+5][maxn+50];

void dfs(int u,int FA){
	fa[0][u]=FA;
	dep[u]=dep[FA]+1;
	l[u]=++len;
	for (auto v:g[u]) if (v!=FA){
		dfs(v,u);	
	}
	r[u]=len;
	sz[u]=r[u]-l[u]+1;
}
void lca_prepare(){
	for (int i=1;i<=maxl;i++){
		for (int j=1;j<=n;j++){
			fa[i][j]=fa[i-1][fa[i-1][j]];
		}
	}
}
tuple<int,int,int> getlca(int x,int y){
	for (int i=maxl;i>=0;i--){
		if (dep[fa[i][y]]>=dep[x]) y=fa[i][y]; 	
	}
	for (int i=maxl;i>=0;i--){
		if (dep[fa[i][x]]>=dep[y]) x=fa[i][x]; 	
	}
	if (x==y) return make_tuple(x,x,x);
	for (int i=maxl;i>=0;i--){
		int fax=fa[i][x];
		int fay=fa[i][y];
		if (fax!=fay){
			x=fax;
			y=fay;
		}
	}
	return make_tuple(fa[0][x],x,y);
}

void add(int x,int val){
	BIT::add(l[x],val);
}
int query(int x){
	return BIT::query(r[x])-BIT::query(l[x]-1);
}

int m;
int b[maxn+50];
int vis[maxn+50];

bool solve(){
	cin >> m;
	for (int i=1;i<=m;i++) cin >> b[i];
	
	bool ans=true;
	int lasti=0;
	int fpos=0;
	
	vector<int> stk;
	vector<int> vislis;
	for (int i=1;i<=m;i++){
	//	cout << "i : " << i << endl;
		if (query(b[i])!=0){
			ans=false;
			break;
		}
		add(b[i],1);
		lasti=i;
		
		if (i==1) continue;
		
		if (fa[0][b[i]]==b[i-1]){
			stk.push_back(b[i]);
		}
		else{
			auto [lca,lx,ly]=getlca(b[i-1],b[i]);
		//	cout << "lca,lx,ly : " << lca << " " << lx << " " << ly << endl;
			if (b[i]!=ly || vis[ly]){
				ans=false;
				break;
			}
			vis[lx]=1;
			vislis.push_back(lx);
			for (;stk.size() && dep[stk.back()]>=dep[lx];){
				int tp=stk.back();
				if (query(tp)!=sz[tp]){
					ans=false;
					break;
				}
				stk.pop_back();
			}
			if (fpos==0 && stk.size()==0){
				fpos=lx;
			}
		//	cout << "fpos : " << fpos << endl;
			if (l[lx]<=l[fpos] && l[fpos]<=r[lx]){
				if (query(lx)-query(fpos)!=(sz[lx]-sz[fpos])-(dep[fpos]-dep[lx])){
					ans=false;
				//	cout << "case 1 " << endl;
				}
			}
			else{
				if (query(lx)!=sz[lx]){
					ans=false;
				//	cout << "case2" << endl;
				}
			}
			stk.push_back(ly);
			if (!ans) break;
		}
	}
	for (auto x:vislis) vis[x]=0;
	for (int i=1;i<=lasti;i++) add(b[i],-1);
	return ans;
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cin >> n >> Q;
	for (int i=1;i<n;i++){
		int x,y;
		cin >> x >> y;
		g[x].push_back(y);
		g[y].push_back(x);
	}
	dfs(1,0);
	lca_prepare();
	for (;Q--;){
		if (solve()) cout << "Yes" << "\n";
		else cout << "No" << "\n";
	}
	return 0;	
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 13360kb

input:

6 7
1 2
1 3
2 4
3 5
2 6
2 4 1
2 4 2
2 4 3
2 4 4
2 4 5
2 4 6
6 1 2 6 4 3 5

output:

No
No
Yes
No
No
Yes
Yes

result:

ok 7 tokens

Test #2:

score: -100
Wrong Answer
time: 44ms
memory: 14508kb

input:

10 100000
7 2
1 7
7 10
8 6
8 7
1 3
4 5
9 5
5 8
8 8 9 7 2 8 1 6 1
4 8 3 5 2
6 7 10 3 9 9 1
1 1
8 10 3 2 9 3 8 7 3
7 5 6 2 8 5 9 1
6 3 4 6 2 1 3
5 8 9 2 4 9
1 3
2 1 5
5 8 5 1 7 9
10 5 2 9 2 6 4 10 6 3 8
3 4 5 8
2 8 4
9 4 10 1 2 4 3 3 6 3
1 3
6 1 1 6 8 3 1
3 7 3 2
3 9 1 5
4 3 7 8 10
9 4 2 3 10 2 5 4 3 ...

output:

No
No
No
Yes
No
No
No
No
Yes
No
No
No
No
No
No
Yes
No
No
No
No
No
No
No
No
No
No
No
No
No
Yes
No
Yes
No
No
No
No
No
No
Yes
No
No
No
No
No
Yes
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
Yes
No
No
Yes
Yes
No
No
No
No
Yes
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
No
N...

result:

wrong answer 30th words differ - expected: 'No', found: 'Yes'