QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#672756#8551. DFS Order 5Sanada_Masayuki#WA 89ms7528kbC++141.4kb2024-10-24 18:45:352024-10-24 18:45:36

Judging History

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

  • [2024-10-24 18:45:36]
  • 评测
  • 测评结果:WA
  • 用时:89ms
  • 内存:7528kb
  • [2024-10-24 18:45:35]
  • 提交

answer

#include<bits/stdc++.h> 
#define rep(i,p,q) for (ll i=(p);i<=(q);i++)
#define ll long long
#define pb push_back
using namespace std;
const int N=1e5+5; int n; int D[N],sz[N],Fa[N][22]; vector<int>E[N]; map<int,int>m;
int lca(int x,int y) {
	int q=0;
	if (D[x]>D[y]) swap(x,y),q=1;
	for (int i=20;~i;i--) if (D[x]<=D[y]-(1<<i)) y=Fa[y][i];
	if (x==y) return 0;
	for (int i=20;~i;i--) if (Fa[x][i]!=Fa[y][i]) x=Fa[x][i],y=Fa[y][i];
	return q?y:x;
}
void dfs(int x,int fa) {
	int i; D[x]=D[fa]+1,sz[x]=1,Fa[x][0]=fa;
	for (int i=1;(1<<i)<=D[x];i++) Fa[x][i]=Fa[Fa[x][i-1]][i-1];
	for (auto y:E[x]) if (y!=fa) dfs(y,x),sz[x]+=sz[y];
}
int check(int x,int y) {
	if (sz[x]>1) return Fa[y][0]==x;
	int l=lca(x,y),p; if (!l) return 0;
	for (p=y;p;p=Fa[p][0]) if (m[p]) return 0;
	m[l]=1;
	return Fa[l][0]==Fa[y][0];
}
int main() {
	int T,x,y,k,q;
	scanf("%d%d",&n,&T);
	rep(i,1,n-1) scanf("%d%d",&x,&y),E[x].pb(y),E[y].pb(x);
	dfs(1,0);
	//rep(i,1,n) printf("%d %d%c",Fa[i][0],sz[i]," \n"[i==n]);
	for (;T--;) {
		set<int>S; m.clear();
		scanf("%d%d",&k,&x),q=1;
		S.insert(x);
		rep(i,2,k) {
			scanf("%d",&y),S.insert(y);
			if (!check(x,y)||S.size()!=i) q=0;
			x=y;
		}
		//rep(i,1,n) printf("%d%c",m[i]," \n"[i==n]);
		puts(q?"Yes":"No");
	}
}
/*
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
*/
/*
7 1
1 2
2 3
2 4
4 5 
4 6
1 7
4 5 6 3 4
*/

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 7528kb

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: 89ms
memory: 6932kb

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
No
No
Yes
No
No
No
No
No
No
Yes
No
No
No
No
No
Yes
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
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 899th words differ - expected: 'No', found: 'Yes'