#include <bits/stdc++.h>
using namespace std;
vector<vector<int> > g, g2;
vector<int> vis;
vector<int> path;
vector<vector<int> > p;
void dfs(int v, int pa)
{
path.push_back(v);
p[v] = path;
for(auto x : g[v])
{
if(x != pa) dfs(x, v);
}
path.pop_back();
}
bool c(int v)
{
vis[v] = 1;
for(auto x : g2[v])
{
//cout << v << " " << x << endl;
if(vis[x] == 1) return 1;
if(vis[x] == 0) if(c(x)) return 1;
}
vis[v] = 2;
return 0;
}
void solve()
{
int n, m;
cin >> n;
g.assign(n + 1, {});
vis.assign(n + 1, 0);
p.assign(n + 1, {});
for(int i = 0; i < n - 1; i++)
{
int x, y;
cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
cin >> m;
g2.assign(m + 1, {});
vector<int> a(m), b(m);
for(int i = 0; i < m; i++) cin >> a[i] >> b[i];
dfs(1, 0);
//return;
vector<vector<int> > inc(n + 1);
for(int i = 0; i < m; i++)
{
vector<int> fi = p[a[i]], se = p[b[i]];
int y = fi.size(), z = se.size();
int be = 0;
while(be < y - 1 && be < z - 1)
{
if(fi[be + 1] == se[be + 1]) be++;
else break;
}
for(int j = be; j < y; j++)
{
inc[fi[j]].push_back(i);
}
for(int j = be; j < z; j++)
{
inc[se[j]].push_back(i);
}
}
for(int i = 0; i < m; i++)
{
for(auto x : inc[a[i]])
{
if(x != i) g2[x].push_back(i);
}
for(auto x : inc[b[i]])
{
if(x != i) g2[i].push_back(x);
}
}
vis.assign(m + 1, 0);
for(int i = 0; i < m; i++)
{
if(vis[i] == 0 && c(i))
{
cout << "No\n";
return;
}
}
cout << "Yes\n";
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int q;
cin >> q;
while(q--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
8 ms |
344 KB |
Output is correct |
5 |
Correct |
17 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
265 ms |
85216 KB |
Output is correct |
10 |
Runtime error |
457 ms |
1048576 KB |
Execution killed with signal 9 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
576 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
568 KB |
Output is correct |
9 |
Correct |
2 ms |
352 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 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 |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
576 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
568 KB |
Output is correct |
9 |
Correct |
2 ms |
352 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 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 |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
708 KB |
Output is correct |
17 |
Correct |
1 ms |
540 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
600 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 |
600 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
576 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
568 KB |
Output is correct |
9 |
Correct |
2 ms |
352 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 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 |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
708 KB |
Output is correct |
17 |
Correct |
1 ms |
540 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
600 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 |
600 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
3 ms |
860 KB |
Output is correct |
30 |
Correct |
2 ms |
352 KB |
Output is correct |
31 |
Correct |
3 ms |
600 KB |
Output is correct |
32 |
Correct |
2 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
2 ms |
604 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 |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
576 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
568 KB |
Output is correct |
9 |
Correct |
2 ms |
352 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
2 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 |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
708 KB |
Output is correct |
17 |
Correct |
1 ms |
540 KB |
Output is correct |
18 |
Correct |
1 ms |
604 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
1 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
600 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 |
600 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
344 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
3 ms |
860 KB |
Output is correct |
30 |
Correct |
2 ms |
352 KB |
Output is correct |
31 |
Correct |
3 ms |
600 KB |
Output is correct |
32 |
Correct |
2 ms |
604 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
468 KB |
Output is correct |
35 |
Correct |
2 ms |
604 KB |
Output is correct |
36 |
Correct |
2 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
271 ms |
85952 KB |
Output is correct |
39 |
Runtime error |
464 ms |
1048576 KB |
Execution killed with signal 9 |
40 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
5 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
604 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 |
21 ms |
984 KB |
Output is correct |
14 |
Correct |
26 ms |
1040 KB |
Output is correct |
15 |
Correct |
20 ms |
860 KB |
Output is correct |
16 |
Correct |
62 ms |
22864 KB |
Output is correct |
17 |
Correct |
124 ms |
35660 KB |
Output is correct |
18 |
Correct |
230 ms |
64052 KB |
Output is correct |
19 |
Correct |
85 ms |
24920 KB |
Output is correct |
20 |
Correct |
67 ms |
24912 KB |
Output is correct |
21 |
Correct |
73 ms |
24880 KB |
Output is correct |
22 |
Correct |
169 ms |
37580 KB |
Output is correct |
23 |
Correct |
88 ms |
37068 KB |
Output is correct |
24 |
Correct |
103 ms |
37172 KB |
Output is correct |
25 |
Correct |
94 ms |
37576 KB |
Output is correct |
26 |
Correct |
95 ms |
37324 KB |
Output is correct |
27 |
Correct |
116 ms |
33992 KB |
Output is correct |
28 |
Correct |
108 ms |
35012 KB |
Output is correct |
29 |
Correct |
108 ms |
33116 KB |
Output is correct |
30 |
Correct |
81 ms |
27704 KB |
Output is correct |
31 |
Correct |
76 ms |
28080 KB |
Output is correct |
32 |
Correct |
84 ms |
27772 KB |
Output is correct |
33 |
Correct |
75 ms |
28056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
8 ms |
344 KB |
Output is correct |
5 |
Correct |
17 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
2 ms |
860 KB |
Output is correct |
9 |
Correct |
265 ms |
85216 KB |
Output is correct |
10 |
Runtime error |
457 ms |
1048576 KB |
Execution killed with signal 9 |
11 |
Halted |
0 ms |
0 KB |
- |