#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++){
unordered_map<int, int> cnt;
for(int x: get_path(s[i], t[i])){
if(ss[x] != -1 && ss[x] != i) cnt[ss[x]]++, sth[i].pb(ss[x]);
if(tt[x] != -1 && tt[x] != i) cnt[tt[x]]++, sth[tt[x]].pb(i);
}
for(auto [ig, x]: cnt) if(x > 1){
cout << "No\n";
return;
}
}
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 |
332 KB |
Output is correct |
5 |
Correct |
27 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
340 KB |
Output is correct |
9 |
Correct |
124 ms |
2776 KB |
Output is correct |
10 |
Correct |
581 ms |
23912 KB |
Output is correct |
11 |
Correct |
5 ms |
324 KB |
Output is correct |
12 |
Correct |
27 ms |
340 KB |
Output is correct |
13 |
Correct |
537 ms |
51500 KB |
Output is correct |
14 |
Correct |
61 ms |
26896 KB |
Output is correct |
15 |
Correct |
52 ms |
24220 KB |
Output is correct |
16 |
Correct |
70 ms |
26968 KB |
Output is correct |
17 |
Execution timed out |
5099 ms |
319908 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
2 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 |
340 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 |
0 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 |
2 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 |
340 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 |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
368 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
212 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 |
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 |
2 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 |
340 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 |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
368 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
3 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
1 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 |
4 ms |
340 KB |
Output is correct |
36 |
Correct |
2 ms |
340 KB |
Output is correct |
37 |
Correct |
2 ms |
340 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 |
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 |
2 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 |
340 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 |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
368 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
3 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
340 KB |
Output is correct |
31 |
Correct |
1 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 |
4 ms |
340 KB |
Output is correct |
36 |
Correct |
2 ms |
340 KB |
Output is correct |
37 |
Correct |
2 ms |
340 KB |
Output is correct |
38 |
Correct |
105 ms |
2708 KB |
Output is correct |
39 |
Correct |
576 ms |
24032 KB |
Output is correct |
40 |
Correct |
26 ms |
1108 KB |
Output is correct |
41 |
Correct |
29 ms |
996 KB |
Output is correct |
42 |
Correct |
22 ms |
1384 KB |
Output is correct |
43 |
Correct |
23 ms |
1108 KB |
Output is correct |
44 |
Correct |
11 ms |
544 KB |
Output is correct |
45 |
Correct |
55 ms |
12064 KB |
Output is correct |
46 |
Correct |
64 ms |
12112 KB |
Output is correct |
47 |
Correct |
107 ms |
17224 KB |
Output is correct |
48 |
Correct |
113 ms |
17240 KB |
Output is correct |
49 |
Correct |
40 ms |
12296 KB |
Output is correct |
50 |
Correct |
47 ms |
12332 KB |
Output is correct |
51 |
Correct |
37 ms |
13628 KB |
Output is correct |
52 |
Correct |
39 ms |
13692 KB |
Output is correct |
53 |
Correct |
10 ms |
1124 KB |
Output is correct |
54 |
Correct |
50 ms |
12064 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
5 ms |
328 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 |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
2 ms |
340 KB |
Output is correct |
13 |
Correct |
15 ms |
344 KB |
Output is correct |
14 |
Correct |
31 ms |
336 KB |
Output is correct |
15 |
Correct |
26 ms |
356 KB |
Output is correct |
16 |
Correct |
54 ms |
12496 KB |
Output is correct |
17 |
Correct |
57 ms |
13396 KB |
Output is correct |
18 |
Correct |
77 ms |
14284 KB |
Output is correct |
19 |
Correct |
60 ms |
12992 KB |
Output is correct |
20 |
Correct |
48 ms |
12892 KB |
Output is correct |
21 |
Correct |
59 ms |
13068 KB |
Output is correct |
22 |
Correct |
60 ms |
13608 KB |
Output is correct |
23 |
Correct |
53 ms |
13232 KB |
Output is correct |
24 |
Correct |
160 ms |
19272 KB |
Output is correct |
25 |
Correct |
97 ms |
15980 KB |
Output is correct |
26 |
Correct |
164 ms |
19356 KB |
Output is correct |
27 |
Correct |
140 ms |
20464 KB |
Output is correct |
28 |
Correct |
130 ms |
21600 KB |
Output is correct |
29 |
Correct |
122 ms |
19440 KB |
Output is correct |
30 |
Correct |
81 ms |
16584 KB |
Output is correct |
31 |
Correct |
84 ms |
16940 KB |
Output is correct |
32 |
Correct |
83 ms |
16464 KB |
Output is correct |
33 |
Correct |
81 ms |
16944 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 |
332 KB |
Output is correct |
5 |
Correct |
27 ms |
212 KB |
Output is correct |
6 |
Correct |
2 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
3 ms |
340 KB |
Output is correct |
9 |
Correct |
124 ms |
2776 KB |
Output is correct |
10 |
Correct |
581 ms |
23912 KB |
Output is correct |
11 |
Correct |
5 ms |
324 KB |
Output is correct |
12 |
Correct |
27 ms |
340 KB |
Output is correct |
13 |
Correct |
537 ms |
51500 KB |
Output is correct |
14 |
Correct |
61 ms |
26896 KB |
Output is correct |
15 |
Correct |
52 ms |
24220 KB |
Output is correct |
16 |
Correct |
70 ms |
26968 KB |
Output is correct |
17 |
Execution timed out |
5099 ms |
319908 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |