QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#642115#8551. DFS Order 5youthpaulWA 29ms3556kbC++201.9kb2024-10-15 10:27:092024-10-15 10:27:09

Judging History

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

  • [2024-10-15 10:27:09]
  • 评测
  • 测评结果:WA
  • 用时:29ms
  • 内存:3556kb
  • [2024-10-15 10:27:09]
  • 提交

answer

#include<bits/stdc++.h>
#define fore(i,l,r)	for(int i=(int)(l);i<(int)(r);++i)
#define fi first
#define se second
#define endl '\n'
#define ull unsigned long long
#define ALL(v) v.begin(), v.end()
#define Debug(x, ed) std::cerr << #x << " = " << x << ed;

const int INF=0x3f3f3f3f;
const long long INFLL=1e18;

typedef long long ll;

const int N = 100005;
const int K = 17;

std::vector<int> g[N];
int f[N][K + 1];
int h[N];

void dfs0(int u, int fa){
	h[u] = h[fa] + 1;
	f[u][0] = fa;
	for(int k = 1; (1 << k) <= h[u]; ++k)
		f[u][k] = f[f[u][k - 1]][k - 1];
	for(auto v : g[u])
		if(v != fa)
			dfs0(v, u);
}

int lca(int u, int v){
	if(h[u] < h[v]) std::swap(u, v);
	for(int k = K; k >= 0; --k)
		if(h[u] - (1 << k) >= h[v])
			u = f[u][k];
	if(u == v) return u;
	for(int k = K; k >= 0; --k)
		if(f[u][k] != f[v][k]){
			u = f[u][k];
			v = f[v][k];
		}
	return f[u][0];
}

int main(){
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    std::cout.tie(nullptr);
    int n, q;
    std::cin >> n >> q;
    fore(i, 1, n){
    	int u, v;
    	std::cin >> u >> v;
    	g[u].push_back(v);
    	g[v].push_back(u);
    }
    
    dfs0(1, 0);
    
    std::vector<bool> oc(n + 1);
    
    while(q--){
    	int m;
    	std::cin >> m;
    	std::vector<int> a(m);
    	fore(i, 0, m) std::cin >> a[i];
    	
    	auto check = [&]() -> bool {
    		if(m == 1) return true;
    		oc[a[0]] = oc[1] = true;
    		fore(i, 1, m){
    			int u = a[i - 1], w = a[i];
    			if(oc[w]) return false;
    			oc[w] = true;
    			if(lca(u, f[w][0]) == f[w][0] && lca(u, w) != w)
    				continue;
    			return false;
   		 	}	
   		 	
   		 	return true;
    	};
    	
    	std::cout << (check() ? "Yes" : "No") << endl;
    	for(auto x : a) oc[x] = false;
    }
    
    return 0;
}














详细

Test #1:

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

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: 29ms
memory: 3556kb

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
Yes
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
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
Yes
No
No
No
No
No
No
N...

result:

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