#include <bits/stdc++.h>
using namespace std;
#define int long long
const int maxn = 120005, maxN = 480005, lgn = 17, INF = 1e18;
int n, m;
vector<int> ADJ[maxn], adj[maxn];
pair<int,int> a[maxn];
int sz[maxn], fa[maxn], d[maxn], newid[maxn], lim[maxn], cnter = 0;
void dfs1(int u) {
sz[u] = 1;
int f = -1;
for (int v:ADJ[u]) {
if (sz[v]) {
f = v;
continue;
}
dfs1(v);
sz[u] += sz[v];
}
if (f!=-1) ADJ[u].erase(find(ADJ[u].begin(), ADJ[u].end(), f));
// cout << u << endl;
// for (int v:ADJ[u]) cout << v << " "; cout << endl;
for (int j=1;j<ADJ[u].size();j++) if (sz[ADJ[u][j]] > sz[ADJ[u][0]]) swap(ADJ[u][0], ADJ[u][j]);
}
void dfs2(int u) {
cnter++;
newid[u] = cnter;
for (int v:ADJ[u]) {
d[cnter+1] = d[newid[u]] + 1;
dfs2(v);
fa[newid[v]] = newid[u];
}
lim[newid[u]] = cnter;
}
int S[maxn], T[maxn], vis[maxn], cur = 0;
bool die = false;
vector<int> dep[maxn];
void dfs3(int u) {
vis[u] = cur;
for (int v:dep[u]) {
if (!vis[v]) dfs3(v);
else if (vis[v]==cur) die = true;
}
vis[u] = -1;
}
void solve() {
cin >> n;
for (int i=1;i<=n;i++) sz[i] = 0, adj[i].clear(), ADJ[i].clear();
cnter = 0;
for (int i=1;i<=n-1;i++) {
int u, v;
cin >> u >> v;
ADJ[u].push_back(v);
ADJ[v].push_back(u);
}
fa[1] = 0, d[1] = 0;
dfs1(1); dfs2(1);
for (int i=1;i<=n;i++) S[i] = -1, T[i] = -1;
cin >> m;
for (int i=1;i<=m;i++) {
int s, t;
cin >> s >> t;
s = newid[s], t = newid[t];
a[i] = {s, t};
S[s] = i;
T[t] = i;
}
for (int i=1;i<=m;i++) dep[i].clear();
for (int i=1;i<=m;i++) {
int u = a[i].first, v = a[i].second;
vector<int> path;
while (u != v) {
if (d[u] > d[v]) swap(u, v);
path.push_back(v);
v = fa[v];
}
path.push_back(u);
for (int j:path) {
if (S[j]!=-1) dep[i].push_back(S[j]);
if (T[j]!=-1) dep[T[j]].push_back(i);
}
}
for (int i=1;i<=m;i++) {
sort(dep[i].begin(), dep[i].end());
dep[i].erase(unique(dep[i].begin(), dep[i].end()), dep[i].end());
dep[i].erase(find(dep[i].begin(), dep[i].end(), i));
// for (int j:dep[i]) cout << j << " "; cout << endl;
}
for (int i=1;i<=m;i++) vis[i] = 0;
die = false;
for (int i=1;i<=m;i++) if (!vis[i]) {
cur = i;
dfs3(i);
}
if (die) cout << "No\n";
else cout << "Yes\n";
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
int t;
cin >> t;
while (t--) solve();
}
Compilation message
jail.cpp: In function 'void dfs1(long long int)':
jail.cpp:24:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for (int j=1;j<ADJ[u].size();j++) if (sz[ADJ[u][j]] > sz[ADJ[u][0]]) swap(ADJ[u][0], ADJ[u][j]);
| ~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18268 KB |
Output is correct |
2 |
Correct |
4 ms |
18268 KB |
Output is correct |
3 |
Correct |
4 ms |
18308 KB |
Output is correct |
4 |
Correct |
9 ms |
18264 KB |
Output is correct |
5 |
Correct |
16 ms |
19072 KB |
Output is correct |
6 |
Correct |
3 ms |
18268 KB |
Output is correct |
7 |
Correct |
3 ms |
18268 KB |
Output is correct |
8 |
Correct |
6 ms |
18524 KB |
Output is correct |
9 |
Correct |
143 ms |
23872 KB |
Output is correct |
10 |
Correct |
696 ms |
37620 KB |
Output is correct |
11 |
Correct |
7 ms |
18520 KB |
Output is correct |
12 |
Correct |
53 ms |
19468 KB |
Output is correct |
13 |
Correct |
193 ms |
93080 KB |
Output is correct |
14 |
Correct |
248 ms |
121688 KB |
Output is correct |
15 |
Runtime error |
2314 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18264 KB |
Output is correct |
2 |
Correct |
3 ms |
18268 KB |
Output is correct |
3 |
Correct |
4 ms |
18268 KB |
Output is correct |
4 |
Correct |
3 ms |
18308 KB |
Output is correct |
5 |
Correct |
4 ms |
18272 KB |
Output is correct |
6 |
Correct |
5 ms |
18304 KB |
Output is correct |
7 |
Correct |
5 ms |
18268 KB |
Output is correct |
8 |
Correct |
4 ms |
18268 KB |
Output is correct |
9 |
Correct |
4 ms |
18268 KB |
Output is correct |
10 |
Correct |
3 ms |
18308 KB |
Output is correct |
11 |
Correct |
4 ms |
18268 KB |
Output is correct |
12 |
Correct |
3 ms |
18268 KB |
Output is correct |
13 |
Correct |
3 ms |
18268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18264 KB |
Output is correct |
2 |
Correct |
3 ms |
18268 KB |
Output is correct |
3 |
Correct |
4 ms |
18268 KB |
Output is correct |
4 |
Correct |
3 ms |
18308 KB |
Output is correct |
5 |
Correct |
4 ms |
18272 KB |
Output is correct |
6 |
Correct |
5 ms |
18304 KB |
Output is correct |
7 |
Correct |
5 ms |
18268 KB |
Output is correct |
8 |
Correct |
4 ms |
18268 KB |
Output is correct |
9 |
Correct |
4 ms |
18268 KB |
Output is correct |
10 |
Correct |
3 ms |
18308 KB |
Output is correct |
11 |
Correct |
4 ms |
18268 KB |
Output is correct |
12 |
Correct |
3 ms |
18268 KB |
Output is correct |
13 |
Correct |
3 ms |
18268 KB |
Output is correct |
14 |
Correct |
3 ms |
18292 KB |
Output is correct |
15 |
Correct |
2 ms |
18268 KB |
Output is correct |
16 |
Correct |
3 ms |
18268 KB |
Output is correct |
17 |
Correct |
3 ms |
18264 KB |
Output is correct |
18 |
Correct |
4 ms |
18268 KB |
Output is correct |
19 |
Correct |
3 ms |
18296 KB |
Output is correct |
20 |
Correct |
4 ms |
18272 KB |
Output is correct |
21 |
Correct |
3 ms |
18268 KB |
Output is correct |
22 |
Correct |
4 ms |
18268 KB |
Output is correct |
23 |
Correct |
3 ms |
18264 KB |
Output is correct |
24 |
Correct |
3 ms |
18268 KB |
Output is correct |
25 |
Correct |
4 ms |
18272 KB |
Output is correct |
26 |
Correct |
3 ms |
18520 KB |
Output is correct |
27 |
Correct |
3 ms |
18268 KB |
Output is correct |
28 |
Correct |
3 ms |
18268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18264 KB |
Output is correct |
2 |
Correct |
3 ms |
18268 KB |
Output is correct |
3 |
Correct |
4 ms |
18268 KB |
Output is correct |
4 |
Correct |
3 ms |
18308 KB |
Output is correct |
5 |
Correct |
4 ms |
18272 KB |
Output is correct |
6 |
Correct |
5 ms |
18304 KB |
Output is correct |
7 |
Correct |
5 ms |
18268 KB |
Output is correct |
8 |
Correct |
4 ms |
18268 KB |
Output is correct |
9 |
Correct |
4 ms |
18268 KB |
Output is correct |
10 |
Correct |
3 ms |
18308 KB |
Output is correct |
11 |
Correct |
4 ms |
18268 KB |
Output is correct |
12 |
Correct |
3 ms |
18268 KB |
Output is correct |
13 |
Correct |
3 ms |
18268 KB |
Output is correct |
14 |
Correct |
3 ms |
18292 KB |
Output is correct |
15 |
Correct |
2 ms |
18268 KB |
Output is correct |
16 |
Correct |
3 ms |
18268 KB |
Output is correct |
17 |
Correct |
3 ms |
18264 KB |
Output is correct |
18 |
Correct |
4 ms |
18268 KB |
Output is correct |
19 |
Correct |
3 ms |
18296 KB |
Output is correct |
20 |
Correct |
4 ms |
18272 KB |
Output is correct |
21 |
Correct |
3 ms |
18268 KB |
Output is correct |
22 |
Correct |
4 ms |
18268 KB |
Output is correct |
23 |
Correct |
3 ms |
18264 KB |
Output is correct |
24 |
Correct |
3 ms |
18268 KB |
Output is correct |
25 |
Correct |
4 ms |
18272 KB |
Output is correct |
26 |
Correct |
3 ms |
18520 KB |
Output is correct |
27 |
Correct |
3 ms |
18268 KB |
Output is correct |
28 |
Correct |
3 ms |
18268 KB |
Output is correct |
29 |
Correct |
6 ms |
18524 KB |
Output is correct |
30 |
Correct |
4 ms |
18268 KB |
Output is correct |
31 |
Correct |
4 ms |
18268 KB |
Output is correct |
32 |
Correct |
5 ms |
18308 KB |
Output is correct |
33 |
Correct |
4 ms |
18268 KB |
Output is correct |
34 |
Correct |
4 ms |
18268 KB |
Output is correct |
35 |
Correct |
7 ms |
18268 KB |
Output is correct |
36 |
Correct |
3 ms |
18268 KB |
Output is correct |
37 |
Correct |
3 ms |
18380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18264 KB |
Output is correct |
2 |
Correct |
3 ms |
18268 KB |
Output is correct |
3 |
Correct |
4 ms |
18268 KB |
Output is correct |
4 |
Correct |
3 ms |
18308 KB |
Output is correct |
5 |
Correct |
4 ms |
18272 KB |
Output is correct |
6 |
Correct |
5 ms |
18304 KB |
Output is correct |
7 |
Correct |
5 ms |
18268 KB |
Output is correct |
8 |
Correct |
4 ms |
18268 KB |
Output is correct |
9 |
Correct |
4 ms |
18268 KB |
Output is correct |
10 |
Correct |
3 ms |
18308 KB |
Output is correct |
11 |
Correct |
4 ms |
18268 KB |
Output is correct |
12 |
Correct |
3 ms |
18268 KB |
Output is correct |
13 |
Correct |
3 ms |
18268 KB |
Output is correct |
14 |
Correct |
3 ms |
18292 KB |
Output is correct |
15 |
Correct |
2 ms |
18268 KB |
Output is correct |
16 |
Correct |
3 ms |
18268 KB |
Output is correct |
17 |
Correct |
3 ms |
18264 KB |
Output is correct |
18 |
Correct |
4 ms |
18268 KB |
Output is correct |
19 |
Correct |
3 ms |
18296 KB |
Output is correct |
20 |
Correct |
4 ms |
18272 KB |
Output is correct |
21 |
Correct |
3 ms |
18268 KB |
Output is correct |
22 |
Correct |
4 ms |
18268 KB |
Output is correct |
23 |
Correct |
3 ms |
18264 KB |
Output is correct |
24 |
Correct |
3 ms |
18268 KB |
Output is correct |
25 |
Correct |
4 ms |
18272 KB |
Output is correct |
26 |
Correct |
3 ms |
18520 KB |
Output is correct |
27 |
Correct |
3 ms |
18268 KB |
Output is correct |
28 |
Correct |
3 ms |
18268 KB |
Output is correct |
29 |
Correct |
6 ms |
18524 KB |
Output is correct |
30 |
Correct |
4 ms |
18268 KB |
Output is correct |
31 |
Correct |
4 ms |
18268 KB |
Output is correct |
32 |
Correct |
5 ms |
18308 KB |
Output is correct |
33 |
Correct |
4 ms |
18268 KB |
Output is correct |
34 |
Correct |
4 ms |
18268 KB |
Output is correct |
35 |
Correct |
7 ms |
18268 KB |
Output is correct |
36 |
Correct |
3 ms |
18268 KB |
Output is correct |
37 |
Correct |
3 ms |
18380 KB |
Output is correct |
38 |
Correct |
147 ms |
23948 KB |
Output is correct |
39 |
Correct |
635 ms |
37632 KB |
Output is correct |
40 |
Correct |
63 ms |
22096 KB |
Output is correct |
41 |
Correct |
23 ms |
19804 KB |
Output is correct |
42 |
Correct |
54 ms |
22124 KB |
Output is correct |
43 |
Correct |
19 ms |
19804 KB |
Output is correct |
44 |
Correct |
9 ms |
18972 KB |
Output is correct |
45 |
Correct |
32 ms |
23900 KB |
Output is correct |
46 |
Correct |
36 ms |
23900 KB |
Output is correct |
47 |
Correct |
111 ms |
29552 KB |
Output is correct |
48 |
Correct |
116 ms |
29608 KB |
Output is correct |
49 |
Correct |
44 ms |
24308 KB |
Output is correct |
50 |
Correct |
32 ms |
24156 KB |
Output is correct |
51 |
Correct |
27 ms |
25428 KB |
Output is correct |
52 |
Correct |
27 ms |
25344 KB |
Output is correct |
53 |
Correct |
9 ms |
19036 KB |
Output is correct |
54 |
Correct |
35 ms |
24724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18264 KB |
Output is correct |
2 |
Correct |
3 ms |
18268 KB |
Output is correct |
3 |
Correct |
4 ms |
18268 KB |
Output is correct |
4 |
Correct |
3 ms |
18268 KB |
Output is correct |
5 |
Correct |
7 ms |
18268 KB |
Output is correct |
6 |
Correct |
3 ms |
18268 KB |
Output is correct |
7 |
Correct |
3 ms |
18268 KB |
Output is correct |
8 |
Correct |
3 ms |
18268 KB |
Output is correct |
9 |
Correct |
3 ms |
18300 KB |
Output is correct |
10 |
Correct |
3 ms |
18268 KB |
Output is correct |
11 |
Correct |
3 ms |
18292 KB |
Output is correct |
12 |
Correct |
3 ms |
18268 KB |
Output is correct |
13 |
Correct |
23 ms |
19032 KB |
Output is correct |
14 |
Correct |
24 ms |
19200 KB |
Output is correct |
15 |
Correct |
29 ms |
19028 KB |
Output is correct |
16 |
Correct |
36 ms |
24912 KB |
Output is correct |
17 |
Correct |
108 ms |
36676 KB |
Output is correct |
18 |
Correct |
287 ms |
77488 KB |
Output is correct |
19 |
Correct |
41 ms |
24996 KB |
Output is correct |
20 |
Correct |
47 ms |
25168 KB |
Output is correct |
21 |
Correct |
41 ms |
25172 KB |
Output is correct |
22 |
Correct |
88 ms |
37320 KB |
Output is correct |
23 |
Correct |
76 ms |
37216 KB |
Output is correct |
24 |
Correct |
91 ms |
37600 KB |
Output is correct |
25 |
Correct |
84 ms |
37832 KB |
Output is correct |
26 |
Correct |
88 ms |
37700 KB |
Output is correct |
27 |
Correct |
73 ms |
36548 KB |
Output is correct |
28 |
Correct |
101 ms |
37832 KB |
Output is correct |
29 |
Correct |
72 ms |
35016 KB |
Output is correct |
30 |
Correct |
65 ms |
29940 KB |
Output is correct |
31 |
Correct |
56 ms |
30412 KB |
Output is correct |
32 |
Correct |
52 ms |
29640 KB |
Output is correct |
33 |
Correct |
56 ms |
30156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
18268 KB |
Output is correct |
2 |
Correct |
4 ms |
18268 KB |
Output is correct |
3 |
Correct |
4 ms |
18308 KB |
Output is correct |
4 |
Correct |
9 ms |
18264 KB |
Output is correct |
5 |
Correct |
16 ms |
19072 KB |
Output is correct |
6 |
Correct |
3 ms |
18268 KB |
Output is correct |
7 |
Correct |
3 ms |
18268 KB |
Output is correct |
8 |
Correct |
6 ms |
18524 KB |
Output is correct |
9 |
Correct |
143 ms |
23872 KB |
Output is correct |
10 |
Correct |
696 ms |
37620 KB |
Output is correct |
11 |
Correct |
7 ms |
18520 KB |
Output is correct |
12 |
Correct |
53 ms |
19468 KB |
Output is correct |
13 |
Correct |
193 ms |
93080 KB |
Output is correct |
14 |
Correct |
248 ms |
121688 KB |
Output is correct |
15 |
Runtime error |
2314 ms |
1048576 KB |
Execution killed with signal 9 |
16 |
Halted |
0 ms |
0 KB |
- |