#include <bits/stdc++.h>
using namespace std;
const int maxn = 120'001, mbit = 18;
int n, m;
int dth[maxn];
int S[maxn], T[maxn];
vector <vector <int> > G;
vector <int> Si, Ti;
vector <int> w;
vector <vector <int> > g;
vector <int> stp;
void dfs_pa(int u = 1){
for(auto v : G[u]){
if(v != stp[u]){
stp[v] = u;
dth[v] = dth[u] + 1;
dfs_pa(v);
}
}
}
bool dfs_loop(int u){
if(w[u] == 1) return 1;
if(w[u] == 2) return 0;
w[u] = 1;
for(auto v : g[u]){
if(dfs_loop(v)) return 1;
}
w[u] = 2;
return 0;
}
bool sol(){
cin >> n;
G.assign(n + 1, vector<int>());
stp.assign(n + 1, 0);
for(int i = 1; i < n; i++){
int u, v;
cin >> u >> v;
G[u].push_back(v);
G[v].push_back(u);
}
dth[1] = 0;
dfs_pa();
//===============
cin >> m;
g.assign(m + 1, vector<int>());
Si.assign(n + 1, -1);
Ti.assign(n + 1, -1);
for(int i = 0; i < m; i++){
cin >> S[i] >> T[i];
Si[S[i]] = i;
Ti[T[i]] = i;
}
for(int i = 0; i < m; i++){
int u = S[i], v = T[i];
while(u != v){
if(dth[u] < dth[v]) swap(u, v);
int s = Si[u], t = Ti[u];
if(s != -1 && s != i) g[i].push_back(s);
if(t != -1 && t != i) g[t].push_back(i);
u = stp[u];
}
int s = Si[u], t = Ti[u];
if(s != -1 && s != i) g[i].push_back(s);
if(t != -1 && t != i) g[t].push_back(i);
}
w.assign(m + 1, 0);
for(int i = 0; i < m; i++){
if(dfs_loop(i)) return 0;
}
return 1;
}
int main(){
int Q;
ios::sync_with_stdio(0);
cin.tie(0);
cin >> Q;
while(Q--){
cout << (sol() ? "Yes\n" : "No\n");
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
6 ms |
348 KB |
Output is correct |
5 |
Correct |
11 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
50 ms |
3036 KB |
Output is correct |
10 |
Correct |
179 ms |
17796 KB |
Output is correct |
11 |
Correct |
4 ms |
348 KB |
Output is correct |
12 |
Correct |
22 ms |
492 KB |
Output is correct |
13 |
Correct |
95 ms |
47820 KB |
Output is correct |
14 |
Correct |
108 ms |
62924 KB |
Output is correct |
15 |
Runtime error |
4223 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
528 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
2 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
504 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
604 KB |
Output is correct |
30 |
Correct |
1 ms |
348 KB |
Output is correct |
31 |
Correct |
1 ms |
528 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Correct |
2 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
49 ms |
3152 KB |
Output is correct |
39 |
Correct |
183 ms |
17748 KB |
Output is correct |
40 |
Correct |
28 ms |
2136 KB |
Output is correct |
41 |
Correct |
16 ms |
1016 KB |
Output is correct |
42 |
Correct |
31 ms |
2388 KB |
Output is correct |
43 |
Correct |
15 ms |
856 KB |
Output is correct |
44 |
Correct |
4 ms |
600 KB |
Output is correct |
45 |
Correct |
31 ms |
8788 KB |
Output is correct |
46 |
Correct |
28 ms |
8784 KB |
Output is correct |
47 |
Correct |
46 ms |
12636 KB |
Output is correct |
48 |
Correct |
47 ms |
12636 KB |
Output is correct |
49 |
Correct |
41 ms |
9032 KB |
Output is correct |
50 |
Correct |
29 ms |
9048 KB |
Output is correct |
51 |
Correct |
24 ms |
10076 KB |
Output is correct |
52 |
Correct |
22 ms |
10108 KB |
Output is correct |
53 |
Correct |
5 ms |
1116 KB |
Output is correct |
54 |
Correct |
31 ms |
8948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
3 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
10 ms |
348 KB |
Output is correct |
14 |
Correct |
15 ms |
348 KB |
Output is correct |
15 |
Correct |
12 ms |
348 KB |
Output is correct |
16 |
Correct |
31 ms |
9556 KB |
Output is correct |
17 |
Correct |
65 ms |
16376 KB |
Output is correct |
18 |
Correct |
150 ms |
37316 KB |
Output is correct |
19 |
Correct |
32 ms |
9808 KB |
Output is correct |
20 |
Correct |
37 ms |
10076 KB |
Output is correct |
21 |
Correct |
32 ms |
9808 KB |
Output is correct |
22 |
Correct |
58 ms |
17100 KB |
Output is correct |
23 |
Correct |
54 ms |
17444 KB |
Output is correct |
24 |
Correct |
61 ms |
17360 KB |
Output is correct |
25 |
Correct |
48 ms |
17480 KB |
Output is correct |
26 |
Correct |
54 ms |
17228 KB |
Output is correct |
27 |
Correct |
61 ms |
20156 KB |
Output is correct |
28 |
Correct |
59 ms |
21080 KB |
Output is correct |
29 |
Correct |
62 ms |
19144 KB |
Output is correct |
30 |
Correct |
50 ms |
14580 KB |
Output is correct |
31 |
Correct |
43 ms |
14812 KB |
Output is correct |
32 |
Correct |
42 ms |
14540 KB |
Output is correct |
33 |
Correct |
41 ms |
14796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
6 ms |
348 KB |
Output is correct |
5 |
Correct |
11 ms |
492 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
50 ms |
3036 KB |
Output is correct |
10 |
Correct |
179 ms |
17796 KB |
Output is correct |
11 |
Correct |
4 ms |
348 KB |
Output is correct |
12 |
Correct |
22 ms |
492 KB |
Output is correct |
13 |
Correct |
95 ms |
47820 KB |
Output is correct |
14 |
Correct |
108 ms |
62924 KB |
Output is correct |
15 |
Runtime error |
4223 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |