#include <bits/stdc++.h>
using namespace std;
int Q, N, M;
int st[120005], ed[120005];
int P[120005][20], D[120005];
int S[120005], T[120005], C[120005];
int timer, tin[120005], out[120005];
vector<int> adj[120005], G[120005];
void dfs(int v, int p) {
P[v][0] = p; D[v] = D[p] + 1;
tin[v] = timer++;
for(auto u : adj[v]) {
if(u == p) continue;
dfs(u, v);
}
out[v] = timer - 1;
}
int lca(int u, int v) {
if(D[u] < D[v]) swap(u, v);
int K = D[u] - D[v];
for(int l = 0; l < 20; l++)
if(K & (1 << l)) u = P[u][l];
if(u == v) return v;
for(int l = 19; l >= 0; l--)
if(P[u][l] != P[v][l])
u = P[u][l], v = P[v][l];
return P[u][0];
}
bool anc(int a, int b) {
return (tin[b] >= tin[a] && tin[b] <= out[a]);
}
bool intersect(int a, int b, int c) {
int p = lca(a, b);
if(c == p) return 1;
return (anc(c, a) ^ anc(c, b));
}
int32_t main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> Q;
while(Q--) {
cin >> N; timer = 0;
for(int l = 0; l < N; l++) {
adj[l].clear();
st[l] = -1, ed[l] = -1;
}
for(int l = 0; l < N - 1; l++) {
int U, V; cin >> U >> V; U--; V--;
adj[U].push_back(V);
adj[V].push_back(U);
}
D[1] = -1; dfs(1, 1);
for(int l = 1; l < 20; l++)
for(int i = 0; i < N; i++)
P[i][l] = P[P[i][l - 1]][l - 1];
cin >> M;
for(int l = 0; l < M; l++) {
cin >> S[l] >> T[l]; S[l]--; T[l]--;
G[l].clear(), C[l] = 0;
st[S[l]] = l, ed[T[l]] = l;
}
for(int l = 0; l < M; l++) {
int p = lca(S[l], T[l]);
vector<int> path, p1;
int x = T[l];
while(x != p) {
p1.push_back(x); x = P[x][0];
}
reverse(p1.begin(), p1.end()); x = S[l];
while(x != p) {
path.push_back(x); x = P[x][0];
}
path.push_back(p);
for(auto u : p1) path.push_back(u);
for(auto u : path) {
if(st[u] != -1 && st[u] != l) G[l].push_back(st[u]), C[st[u]]++;
if(ed[u] != -1 && ed[u] != l) G[ed[u]].push_back(l), C[l]++;
}
}
vector<bool> vis(M, 0); queue<int> q;
for(int l = 0; l < M; l++)
if(C[l] == 0) q.push(l);
while(!q.empty()) {
int a = q.front(); q.pop(); vis[a] = 1;
for(auto u : G[a]) {
C[u]--; if(C[u] == 0) q.push(u);
}
}
bool res = 1;
for(int l = 0; l < M; l++) res &= vis[l];
cout << (res ? "Yes" : "No") << "\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5972 KB |
Output is correct |
2 |
Correct |
5 ms |
5960 KB |
Output is correct |
3 |
Correct |
4 ms |
5972 KB |
Output is correct |
4 |
Correct |
17 ms |
6360 KB |
Output is correct |
5 |
Correct |
24 ms |
6764 KB |
Output is correct |
6 |
Correct |
4 ms |
5976 KB |
Output is correct |
7 |
Correct |
5 ms |
5972 KB |
Output is correct |
8 |
Correct |
7 ms |
6104 KB |
Output is correct |
9 |
Correct |
150 ms |
10348 KB |
Output is correct |
10 |
Correct |
1131 ms |
31500 KB |
Output is correct |
11 |
Correct |
12 ms |
6100 KB |
Output is correct |
12 |
Correct |
57 ms |
7036 KB |
Output is correct |
13 |
Correct |
243 ms |
59384 KB |
Output is correct |
14 |
Correct |
244 ms |
73788 KB |
Output is correct |
15 |
Execution timed out |
5077 ms |
550472 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5972 KB |
Output is correct |
2 |
Correct |
5 ms |
5972 KB |
Output is correct |
3 |
Correct |
6 ms |
5972 KB |
Output is correct |
4 |
Correct |
6 ms |
6072 KB |
Output is correct |
5 |
Correct |
4 ms |
5972 KB |
Output is correct |
6 |
Correct |
4 ms |
5980 KB |
Output is correct |
7 |
Correct |
7 ms |
5972 KB |
Output is correct |
8 |
Correct |
6 ms |
5972 KB |
Output is correct |
9 |
Correct |
6 ms |
5972 KB |
Output is correct |
10 |
Correct |
6 ms |
5972 KB |
Output is correct |
11 |
Correct |
6 ms |
5972 KB |
Output is correct |
12 |
Correct |
3 ms |
5972 KB |
Output is correct |
13 |
Correct |
5 ms |
5972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5972 KB |
Output is correct |
2 |
Correct |
5 ms |
5972 KB |
Output is correct |
3 |
Correct |
6 ms |
5972 KB |
Output is correct |
4 |
Correct |
6 ms |
6072 KB |
Output is correct |
5 |
Correct |
4 ms |
5972 KB |
Output is correct |
6 |
Correct |
4 ms |
5980 KB |
Output is correct |
7 |
Correct |
7 ms |
5972 KB |
Output is correct |
8 |
Correct |
6 ms |
5972 KB |
Output is correct |
9 |
Correct |
6 ms |
5972 KB |
Output is correct |
10 |
Correct |
6 ms |
5972 KB |
Output is correct |
11 |
Correct |
6 ms |
5972 KB |
Output is correct |
12 |
Correct |
3 ms |
5972 KB |
Output is correct |
13 |
Correct |
5 ms |
5972 KB |
Output is correct |
14 |
Correct |
4 ms |
5972 KB |
Output is correct |
15 |
Correct |
5 ms |
5972 KB |
Output is correct |
16 |
Correct |
6 ms |
5980 KB |
Output is correct |
17 |
Correct |
6 ms |
5972 KB |
Output is correct |
18 |
Correct |
5 ms |
5980 KB |
Output is correct |
19 |
Correct |
5 ms |
5972 KB |
Output is correct |
20 |
Correct |
5 ms |
6088 KB |
Output is correct |
21 |
Correct |
4 ms |
5972 KB |
Output is correct |
22 |
Correct |
5 ms |
5972 KB |
Output is correct |
23 |
Correct |
4 ms |
5972 KB |
Output is correct |
24 |
Correct |
4 ms |
5972 KB |
Output is correct |
25 |
Correct |
5 ms |
6080 KB |
Output is correct |
26 |
Correct |
5 ms |
5972 KB |
Output is correct |
27 |
Correct |
5 ms |
6084 KB |
Output is correct |
28 |
Correct |
4 ms |
5968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5972 KB |
Output is correct |
2 |
Correct |
5 ms |
5972 KB |
Output is correct |
3 |
Correct |
6 ms |
5972 KB |
Output is correct |
4 |
Correct |
6 ms |
6072 KB |
Output is correct |
5 |
Correct |
4 ms |
5972 KB |
Output is correct |
6 |
Correct |
4 ms |
5980 KB |
Output is correct |
7 |
Correct |
7 ms |
5972 KB |
Output is correct |
8 |
Correct |
6 ms |
5972 KB |
Output is correct |
9 |
Correct |
6 ms |
5972 KB |
Output is correct |
10 |
Correct |
6 ms |
5972 KB |
Output is correct |
11 |
Correct |
6 ms |
5972 KB |
Output is correct |
12 |
Correct |
3 ms |
5972 KB |
Output is correct |
13 |
Correct |
5 ms |
5972 KB |
Output is correct |
14 |
Correct |
4 ms |
5972 KB |
Output is correct |
15 |
Correct |
5 ms |
5972 KB |
Output is correct |
16 |
Correct |
6 ms |
5980 KB |
Output is correct |
17 |
Correct |
6 ms |
5972 KB |
Output is correct |
18 |
Correct |
5 ms |
5980 KB |
Output is correct |
19 |
Correct |
5 ms |
5972 KB |
Output is correct |
20 |
Correct |
5 ms |
6088 KB |
Output is correct |
21 |
Correct |
4 ms |
5972 KB |
Output is correct |
22 |
Correct |
5 ms |
5972 KB |
Output is correct |
23 |
Correct |
4 ms |
5972 KB |
Output is correct |
24 |
Correct |
4 ms |
5972 KB |
Output is correct |
25 |
Correct |
5 ms |
6080 KB |
Output is correct |
26 |
Correct |
5 ms |
5972 KB |
Output is correct |
27 |
Correct |
5 ms |
6084 KB |
Output is correct |
28 |
Correct |
4 ms |
5968 KB |
Output is correct |
29 |
Correct |
7 ms |
6100 KB |
Output is correct |
30 |
Correct |
6 ms |
6100 KB |
Output is correct |
31 |
Correct |
7 ms |
6108 KB |
Output is correct |
32 |
Correct |
5 ms |
5972 KB |
Output is correct |
33 |
Correct |
4 ms |
5972 KB |
Output is correct |
34 |
Correct |
7 ms |
6100 KB |
Output is correct |
35 |
Correct |
7 ms |
6100 KB |
Output is correct |
36 |
Correct |
6 ms |
6076 KB |
Output is correct |
37 |
Correct |
5 ms |
5972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5972 KB |
Output is correct |
2 |
Correct |
5 ms |
5972 KB |
Output is correct |
3 |
Correct |
6 ms |
5972 KB |
Output is correct |
4 |
Correct |
6 ms |
6072 KB |
Output is correct |
5 |
Correct |
4 ms |
5972 KB |
Output is correct |
6 |
Correct |
4 ms |
5980 KB |
Output is correct |
7 |
Correct |
7 ms |
5972 KB |
Output is correct |
8 |
Correct |
6 ms |
5972 KB |
Output is correct |
9 |
Correct |
6 ms |
5972 KB |
Output is correct |
10 |
Correct |
6 ms |
5972 KB |
Output is correct |
11 |
Correct |
6 ms |
5972 KB |
Output is correct |
12 |
Correct |
3 ms |
5972 KB |
Output is correct |
13 |
Correct |
5 ms |
5972 KB |
Output is correct |
14 |
Correct |
4 ms |
5972 KB |
Output is correct |
15 |
Correct |
5 ms |
5972 KB |
Output is correct |
16 |
Correct |
6 ms |
5980 KB |
Output is correct |
17 |
Correct |
6 ms |
5972 KB |
Output is correct |
18 |
Correct |
5 ms |
5980 KB |
Output is correct |
19 |
Correct |
5 ms |
5972 KB |
Output is correct |
20 |
Correct |
5 ms |
6088 KB |
Output is correct |
21 |
Correct |
4 ms |
5972 KB |
Output is correct |
22 |
Correct |
5 ms |
5972 KB |
Output is correct |
23 |
Correct |
4 ms |
5972 KB |
Output is correct |
24 |
Correct |
4 ms |
5972 KB |
Output is correct |
25 |
Correct |
5 ms |
6080 KB |
Output is correct |
26 |
Correct |
5 ms |
5972 KB |
Output is correct |
27 |
Correct |
5 ms |
6084 KB |
Output is correct |
28 |
Correct |
4 ms |
5968 KB |
Output is correct |
29 |
Correct |
7 ms |
6100 KB |
Output is correct |
30 |
Correct |
6 ms |
6100 KB |
Output is correct |
31 |
Correct |
7 ms |
6108 KB |
Output is correct |
32 |
Correct |
5 ms |
5972 KB |
Output is correct |
33 |
Correct |
4 ms |
5972 KB |
Output is correct |
34 |
Correct |
7 ms |
6100 KB |
Output is correct |
35 |
Correct |
7 ms |
6100 KB |
Output is correct |
36 |
Correct |
6 ms |
6076 KB |
Output is correct |
37 |
Correct |
5 ms |
5972 KB |
Output is correct |
38 |
Correct |
202 ms |
10208 KB |
Output is correct |
39 |
Correct |
1185 ms |
31496 KB |
Output is correct |
40 |
Correct |
123 ms |
9164 KB |
Output is correct |
41 |
Correct |
39 ms |
7964 KB |
Output is correct |
42 |
Correct |
82 ms |
9220 KB |
Output is correct |
43 |
Correct |
39 ms |
8028 KB |
Output is correct |
44 |
Correct |
15 ms |
6484 KB |
Output is correct |
45 |
Correct |
85 ms |
23004 KB |
Output is correct |
46 |
Correct |
78 ms |
22920 KB |
Output is correct |
47 |
Correct |
239 ms |
26344 KB |
Output is correct |
48 |
Correct |
236 ms |
26372 KB |
Output is correct |
49 |
Correct |
85 ms |
23024 KB |
Output is correct |
50 |
Correct |
85 ms |
23036 KB |
Output is correct |
51 |
Correct |
61 ms |
23844 KB |
Output is correct |
52 |
Correct |
74 ms |
23920 KB |
Output is correct |
53 |
Correct |
15 ms |
7384 KB |
Output is correct |
54 |
Correct |
110 ms |
22860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5972 KB |
Output is correct |
2 |
Correct |
5 ms |
6008 KB |
Output is correct |
3 |
Correct |
4 ms |
5972 KB |
Output is correct |
4 |
Correct |
4 ms |
5972 KB |
Output is correct |
5 |
Correct |
9 ms |
6100 KB |
Output is correct |
6 |
Correct |
4 ms |
5972 KB |
Output is correct |
7 |
Correct |
4 ms |
5972 KB |
Output is correct |
8 |
Correct |
4 ms |
5960 KB |
Output is correct |
9 |
Correct |
4 ms |
5972 KB |
Output is correct |
10 |
Correct |
4 ms |
5972 KB |
Output is correct |
11 |
Correct |
4 ms |
5972 KB |
Output is correct |
12 |
Correct |
5 ms |
5972 KB |
Output is correct |
13 |
Correct |
34 ms |
6612 KB |
Output is correct |
14 |
Correct |
43 ms |
6768 KB |
Output is correct |
15 |
Correct |
39 ms |
6752 KB |
Output is correct |
16 |
Correct |
110 ms |
23416 KB |
Output is correct |
17 |
Correct |
234 ms |
29664 KB |
Output is correct |
18 |
Correct |
450 ms |
49916 KB |
Output is correct |
19 |
Correct |
112 ms |
23708 KB |
Output is correct |
20 |
Correct |
117 ms |
23888 KB |
Output is correct |
21 |
Correct |
117 ms |
23764 KB |
Output is correct |
22 |
Correct |
214 ms |
30380 KB |
Output is correct |
23 |
Correct |
160 ms |
30652 KB |
Output is correct |
24 |
Correct |
165 ms |
30568 KB |
Output is correct |
25 |
Correct |
186 ms |
30764 KB |
Output is correct |
26 |
Correct |
204 ms |
30644 KB |
Output is correct |
27 |
Correct |
204 ms |
29948 KB |
Output is correct |
28 |
Correct |
187 ms |
29940 KB |
Output is correct |
29 |
Correct |
189 ms |
29852 KB |
Output is correct |
30 |
Correct |
129 ms |
26340 KB |
Output is correct |
31 |
Correct |
165 ms |
26416 KB |
Output is correct |
32 |
Correct |
171 ms |
26484 KB |
Output is correct |
33 |
Correct |
140 ms |
26336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5972 KB |
Output is correct |
2 |
Correct |
5 ms |
5960 KB |
Output is correct |
3 |
Correct |
4 ms |
5972 KB |
Output is correct |
4 |
Correct |
17 ms |
6360 KB |
Output is correct |
5 |
Correct |
24 ms |
6764 KB |
Output is correct |
6 |
Correct |
4 ms |
5976 KB |
Output is correct |
7 |
Correct |
5 ms |
5972 KB |
Output is correct |
8 |
Correct |
7 ms |
6104 KB |
Output is correct |
9 |
Correct |
150 ms |
10348 KB |
Output is correct |
10 |
Correct |
1131 ms |
31500 KB |
Output is correct |
11 |
Correct |
12 ms |
6100 KB |
Output is correct |
12 |
Correct |
57 ms |
7036 KB |
Output is correct |
13 |
Correct |
243 ms |
59384 KB |
Output is correct |
14 |
Correct |
244 ms |
73788 KB |
Output is correct |
15 |
Execution timed out |
5077 ms |
550472 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |