#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
int n, m;
vector<vector<int>> v;
vector<int> s, t;
vector<vector<int>> sth;
vector<int> state;
vector<int> p;
vector<int> d;
bool cycle;
void dfs1(int nd, int ss, int dis){
for(int x: v[nd]) if(x != ss) dfs1(x, nd, dis+1);
p[nd] = ss;
d[nd] = dis;
}
void dfs2(int nd){
if(state[nd] == 1){
cycle = 1;
return;
}
if(state[nd] == 2) return;
state[nd] = 1;
for(int x: sth[nd]) dfs2(x);
state[nd] = 2;
}
void solve(){
cin >> n;
v.assign(n, {});
p.assign(n, 0);
d.assign(n, 0);
sth.assign(n, {});
state.assign(n, 0);
for(int i = 0; i < n-1; i++){
int a, b; cin >> a >> b; a--, b--;
v[a].pb(b);
v[b].pb(a);
}
dfs1(0, -1, 0);
cin >> m;
s.assign(m, 0);
t.assign(m, 0);
for(int i = 0; i < m; i++) cin >> s[i] >> t[i], s[i]--, t[i]--;
vector<int> ss(n, -1), tt(n, -1);
for(int i = 0; i < m; i++) ss[s[i]] = i, tt[t[i]] = i;
auto get_path = [&](int a, int b){
vector<int> rt;
if(d[a] > d[b]) swap(a, b);
while(d[a] != d[b]) rt.pb(b), b = p[b];
while(a != b) rt.pb(a), rt.pb(b), a = p[a], b = p[b];
rt.pb(a);
return rt;
};
for(int i = 0; i < m; i++){
for(int x: get_path(s[i], t[i])){
if(ss[x] != -1 && ss[x] != i) sth[i].pb(ss[x]);
if(tt[x] != -1 && tt[x] != i) sth[tt[x]].pb(i);
}
}
cycle = 0;
for(int i = 0; i < n; i++) if(state[i] == 0) dfs2(i);
cout << (cycle?"No\n":"Yes\n");
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int T; cin >> T; while(T--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
9 ms |
340 KB |
Output is correct |
5 |
Correct |
18 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
4 ms |
340 KB |
Output is correct |
9 |
Correct |
143 ms |
3248 KB |
Output is correct |
10 |
Correct |
574 ms |
23896 KB |
Output is correct |
11 |
Correct |
6 ms |
212 KB |
Output is correct |
12 |
Correct |
52 ms |
424 KB |
Output is correct |
13 |
Correct |
191 ms |
51596 KB |
Output is correct |
14 |
Correct |
222 ms |
66056 KB |
Output is correct |
15 |
Execution timed out |
5050 ms |
711636 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
4 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
3 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
2 ms |
340 KB |
Output is correct |
35 |
Correct |
3 ms |
340 KB |
Output is correct |
36 |
Correct |
2 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
2 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
212 KB |
Output is correct |
29 |
Correct |
4 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
3 ms |
340 KB |
Output is correct |
32 |
Correct |
2 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
2 ms |
340 KB |
Output is correct |
35 |
Correct |
3 ms |
340 KB |
Output is correct |
36 |
Correct |
2 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
137 ms |
3456 KB |
Output is correct |
39 |
Correct |
571 ms |
23940 KB |
Output is correct |
40 |
Correct |
74 ms |
2336 KB |
Output is correct |
41 |
Correct |
28 ms |
992 KB |
Output is correct |
42 |
Correct |
62 ms |
2428 KB |
Output is correct |
43 |
Correct |
22 ms |
1108 KB |
Output is correct |
44 |
Correct |
8 ms |
628 KB |
Output is correct |
45 |
Correct |
49 ms |
12176 KB |
Output is correct |
46 |
Correct |
45 ms |
12092 KB |
Output is correct |
47 |
Correct |
111 ms |
17236 KB |
Output is correct |
48 |
Correct |
126 ms |
17304 KB |
Output is correct |
49 |
Correct |
55 ms |
12316 KB |
Output is correct |
50 |
Correct |
51 ms |
12236 KB |
Output is correct |
51 |
Correct |
39 ms |
13568 KB |
Output is correct |
52 |
Correct |
37 ms |
13616 KB |
Output is correct |
53 |
Correct |
9 ms |
1108 KB |
Output is correct |
54 |
Correct |
83 ms |
12036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
7 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
20 ms |
344 KB |
Output is correct |
14 |
Correct |
28 ms |
212 KB |
Output is correct |
15 |
Correct |
23 ms |
340 KB |
Output is correct |
16 |
Correct |
53 ms |
12484 KB |
Output is correct |
17 |
Correct |
141 ms |
18724 KB |
Output is correct |
18 |
Correct |
369 ms |
40384 KB |
Output is correct |
19 |
Correct |
56 ms |
12624 KB |
Output is correct |
20 |
Correct |
68 ms |
12684 KB |
Output is correct |
21 |
Correct |
57 ms |
12588 KB |
Output is correct |
22 |
Correct |
121 ms |
18776 KB |
Output is correct |
23 |
Correct |
88 ms |
18960 KB |
Output is correct |
24 |
Correct |
90 ms |
18800 KB |
Output is correct |
25 |
Correct |
99 ms |
18992 KB |
Output is correct |
26 |
Correct |
104 ms |
18848 KB |
Output is correct |
27 |
Correct |
120 ms |
19944 KB |
Output is correct |
28 |
Correct |
110 ms |
20948 KB |
Output is correct |
29 |
Correct |
104 ms |
18952 KB |
Output is correct |
30 |
Correct |
78 ms |
16296 KB |
Output is correct |
31 |
Correct |
74 ms |
16416 KB |
Output is correct |
32 |
Correct |
73 ms |
16000 KB |
Output is correct |
33 |
Correct |
78 ms |
16460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
9 ms |
340 KB |
Output is correct |
5 |
Correct |
18 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
4 ms |
340 KB |
Output is correct |
9 |
Correct |
143 ms |
3248 KB |
Output is correct |
10 |
Correct |
574 ms |
23896 KB |
Output is correct |
11 |
Correct |
6 ms |
212 KB |
Output is correct |
12 |
Correct |
52 ms |
424 KB |
Output is correct |
13 |
Correct |
191 ms |
51596 KB |
Output is correct |
14 |
Correct |
222 ms |
66056 KB |
Output is correct |
15 |
Execution timed out |
5050 ms |
711636 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |