#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 120'001;
vector<int> g[MAXN], g2[MAXN];
int tin[MAXN], tout[MAXN], s[MAXN], e[MAXN], par[MAXN], col[MAXN], hv[MAXN], timer;
bool ok;
void dfs(int u, int p = -1) {
tin[u] = timer++;
par[u] = p;
for (int v : g[u]) {
if (v != p) dfs(v, u);
}
tout[u] = timer++;
}
void dfs2(int u) {
col[u] = 1;
for (int v : g2[u]) {
if (col[v] == 0) dfs2(v);
else if (col[v] == 1) ok = false;
}
col[u] = 2;
}
bool is_ancestor(int u, int v) {
return tin[u] <= tin[v] && tout[v] <= tout[u];
}
void solve() {
int n; cin >> n;
ok = true;
fill(s, s+n+1, 0);
fill(e, e+n+1, 0);
fill(col, col+n+1, 0);
fill(hv, hv+n+1, 0);
timer = 1;
for (int i = 1; i <= n; i++) {
g[i].clear();
g2[i].clear();
}
for (int i = 0; i < n-1; i++) {
int u, v; cin >> u >> v;
g[u].emplace_back(v);
g[v].emplace_back(u);
}
dfs(1);
int m; cin >> m;
vector<pair<int, int>> edges(m+1);
for (int i = 1; i <= m; i++) {
cin >> edges[i].first >> edges[i].second;
auto [u, v] = edges[i];
s[u] = e[v] = i;
hv[edges[i].first] = i;
}
auto calc = [&](int u, int v, vector<int> &indices, bool add_lca) {
while (!is_ancestor(u, v)) {
indices.emplace_back(u);
u = par[u];
}
if (add_lca) indices.emplace_back(u);
};
vector<int> cnt(m+1, 0);
for (int i = 1; i <= m; i++) {
vector<int> indices;
{
auto [u, v] = edges[i];
calc(u, v, indices, false);
calc(v, u, indices, true);
}
vector<int> to_rem;
for (int u : indices) {
if (e[u] && e[u] != i) {
to_rem.emplace_back(e[u]);
cnt[e[u]]++;
g2[i].emplace_back(e[u]);
}
if (s[u] && s[u] != i) {
to_rem.emplace_back(s[u]);
cnt[s[u]]++;
g2[s[u]].emplace_back(i);
}
}
for (int i : to_rem) {
if (cnt[i] == 2) ok = false;
else cnt[i] = 0;
}
}
for (int i = 1; i <= m; i++) {
if (col[i] == 0) dfs2(i);
}
cout << (ok?"Yes\n":"No\n");
}
int main() {
ios::sync_with_stdio(0); cin.tie(0);
int t; cin >> t;
while (t--) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9052 KB |
Output is correct |
2 |
Correct |
1 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Correct |
7 ms |
9212 KB |
Output is correct |
5 |
Correct |
15 ms |
9212 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
3 ms |
9308 KB |
Output is correct |
9 |
Correct |
114 ms |
11664 KB |
Output is correct |
10 |
Correct |
563 ms |
21192 KB |
Output is correct |
11 |
Correct |
5 ms |
9048 KB |
Output is correct |
12 |
Correct |
38 ms |
9324 KB |
Output is correct |
13 |
Correct |
137 ms |
49076 KB |
Output is correct |
14 |
Correct |
178 ms |
64180 KB |
Output is correct |
15 |
Execution timed out |
5116 ms |
981288 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9048 KB |
Output is correct |
2 |
Correct |
1 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9052 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
2 ms |
9172 KB |
Output is correct |
9 |
Correct |
2 ms |
9052 KB |
Output is correct |
10 |
Correct |
2 ms |
9052 KB |
Output is correct |
11 |
Correct |
2 ms |
9052 KB |
Output is correct |
12 |
Correct |
1 ms |
9052 KB |
Output is correct |
13 |
Correct |
2 ms |
9052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9048 KB |
Output is correct |
2 |
Correct |
1 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9052 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
2 ms |
9172 KB |
Output is correct |
9 |
Correct |
2 ms |
9052 KB |
Output is correct |
10 |
Correct |
2 ms |
9052 KB |
Output is correct |
11 |
Correct |
2 ms |
9052 KB |
Output is correct |
12 |
Correct |
1 ms |
9052 KB |
Output is correct |
13 |
Correct |
2 ms |
9052 KB |
Output is correct |
14 |
Correct |
2 ms |
9140 KB |
Output is correct |
15 |
Correct |
1 ms |
9052 KB |
Output is correct |
16 |
Correct |
2 ms |
9048 KB |
Output is correct |
17 |
Correct |
2 ms |
9124 KB |
Output is correct |
18 |
Correct |
3 ms |
9052 KB |
Output is correct |
19 |
Correct |
2 ms |
9052 KB |
Output is correct |
20 |
Correct |
2 ms |
9052 KB |
Output is correct |
21 |
Correct |
2 ms |
9052 KB |
Output is correct |
22 |
Correct |
2 ms |
9052 KB |
Output is correct |
23 |
Correct |
2 ms |
9136 KB |
Output is correct |
24 |
Correct |
2 ms |
9048 KB |
Output is correct |
25 |
Correct |
2 ms |
9052 KB |
Output is correct |
26 |
Correct |
2 ms |
9052 KB |
Output is correct |
27 |
Correct |
2 ms |
9052 KB |
Output is correct |
28 |
Correct |
1 ms |
9052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9048 KB |
Output is correct |
2 |
Correct |
1 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9052 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
2 ms |
9172 KB |
Output is correct |
9 |
Correct |
2 ms |
9052 KB |
Output is correct |
10 |
Correct |
2 ms |
9052 KB |
Output is correct |
11 |
Correct |
2 ms |
9052 KB |
Output is correct |
12 |
Correct |
1 ms |
9052 KB |
Output is correct |
13 |
Correct |
2 ms |
9052 KB |
Output is correct |
14 |
Correct |
2 ms |
9140 KB |
Output is correct |
15 |
Correct |
1 ms |
9052 KB |
Output is correct |
16 |
Correct |
2 ms |
9048 KB |
Output is correct |
17 |
Correct |
2 ms |
9124 KB |
Output is correct |
18 |
Correct |
3 ms |
9052 KB |
Output is correct |
19 |
Correct |
2 ms |
9052 KB |
Output is correct |
20 |
Correct |
2 ms |
9052 KB |
Output is correct |
21 |
Correct |
2 ms |
9052 KB |
Output is correct |
22 |
Correct |
2 ms |
9052 KB |
Output is correct |
23 |
Correct |
2 ms |
9136 KB |
Output is correct |
24 |
Correct |
2 ms |
9048 KB |
Output is correct |
25 |
Correct |
2 ms |
9052 KB |
Output is correct |
26 |
Correct |
2 ms |
9052 KB |
Output is correct |
27 |
Correct |
2 ms |
9052 KB |
Output is correct |
28 |
Correct |
1 ms |
9052 KB |
Output is correct |
29 |
Correct |
6 ms |
9304 KB |
Output is correct |
30 |
Correct |
3 ms |
9052 KB |
Output is correct |
31 |
Correct |
4 ms |
9052 KB |
Output is correct |
32 |
Correct |
3 ms |
9052 KB |
Output is correct |
33 |
Correct |
2 ms |
9052 KB |
Output is correct |
34 |
Correct |
2 ms |
9052 KB |
Output is correct |
35 |
Correct |
4 ms |
9052 KB |
Output is correct |
36 |
Correct |
2 ms |
9052 KB |
Output is correct |
37 |
Correct |
2 ms |
9052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9048 KB |
Output is correct |
2 |
Correct |
1 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Correct |
2 ms |
9052 KB |
Output is correct |
5 |
Correct |
2 ms |
9052 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
2 ms |
9172 KB |
Output is correct |
9 |
Correct |
2 ms |
9052 KB |
Output is correct |
10 |
Correct |
2 ms |
9052 KB |
Output is correct |
11 |
Correct |
2 ms |
9052 KB |
Output is correct |
12 |
Correct |
1 ms |
9052 KB |
Output is correct |
13 |
Correct |
2 ms |
9052 KB |
Output is correct |
14 |
Correct |
2 ms |
9140 KB |
Output is correct |
15 |
Correct |
1 ms |
9052 KB |
Output is correct |
16 |
Correct |
2 ms |
9048 KB |
Output is correct |
17 |
Correct |
2 ms |
9124 KB |
Output is correct |
18 |
Correct |
3 ms |
9052 KB |
Output is correct |
19 |
Correct |
2 ms |
9052 KB |
Output is correct |
20 |
Correct |
2 ms |
9052 KB |
Output is correct |
21 |
Correct |
2 ms |
9052 KB |
Output is correct |
22 |
Correct |
2 ms |
9052 KB |
Output is correct |
23 |
Correct |
2 ms |
9136 KB |
Output is correct |
24 |
Correct |
2 ms |
9048 KB |
Output is correct |
25 |
Correct |
2 ms |
9052 KB |
Output is correct |
26 |
Correct |
2 ms |
9052 KB |
Output is correct |
27 |
Correct |
2 ms |
9052 KB |
Output is correct |
28 |
Correct |
1 ms |
9052 KB |
Output is correct |
29 |
Correct |
6 ms |
9304 KB |
Output is correct |
30 |
Correct |
3 ms |
9052 KB |
Output is correct |
31 |
Correct |
4 ms |
9052 KB |
Output is correct |
32 |
Correct |
3 ms |
9052 KB |
Output is correct |
33 |
Correct |
2 ms |
9052 KB |
Output is correct |
34 |
Correct |
2 ms |
9052 KB |
Output is correct |
35 |
Correct |
4 ms |
9052 KB |
Output is correct |
36 |
Correct |
2 ms |
9052 KB |
Output is correct |
37 |
Correct |
2 ms |
9052 KB |
Output is correct |
38 |
Correct |
115 ms |
11600 KB |
Output is correct |
39 |
Correct |
600 ms |
21168 KB |
Output is correct |
40 |
Correct |
49 ms |
10576 KB |
Output is correct |
41 |
Correct |
18 ms |
9304 KB |
Output is correct |
42 |
Correct |
43 ms |
10832 KB |
Output is correct |
43 |
Correct |
14 ms |
9304 KB |
Output is correct |
44 |
Correct |
8 ms |
9308 KB |
Output is correct |
45 |
Correct |
28 ms |
13148 KB |
Output is correct |
46 |
Correct |
28 ms |
13144 KB |
Output is correct |
47 |
Correct |
100 ms |
16416 KB |
Output is correct |
48 |
Correct |
105 ms |
16440 KB |
Output is correct |
49 |
Correct |
39 ms |
13148 KB |
Output is correct |
50 |
Correct |
31 ms |
13144 KB |
Output is correct |
51 |
Correct |
22 ms |
13916 KB |
Output is correct |
52 |
Correct |
23 ms |
14088 KB |
Output is correct |
53 |
Correct |
9 ms |
9332 KB |
Output is correct |
54 |
Correct |
29 ms |
12964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9052 KB |
Output is correct |
2 |
Correct |
1 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Correct |
1 ms |
9052 KB |
Output is correct |
5 |
Correct |
5 ms |
9052 KB |
Output is correct |
6 |
Correct |
2 ms |
9216 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
1 ms |
9052 KB |
Output is correct |
9 |
Correct |
2 ms |
9048 KB |
Output is correct |
10 |
Correct |
2 ms |
9052 KB |
Output is correct |
11 |
Correct |
1 ms |
9052 KB |
Output is correct |
12 |
Correct |
2 ms |
9052 KB |
Output is correct |
13 |
Correct |
17 ms |
9240 KB |
Output is correct |
14 |
Correct |
20 ms |
9052 KB |
Output is correct |
15 |
Correct |
18 ms |
9228 KB |
Output is correct |
16 |
Correct |
33 ms |
13524 KB |
Output is correct |
17 |
Correct |
100 ms |
20308 KB |
Output is correct |
18 |
Correct |
212 ms |
43824 KB |
Output is correct |
19 |
Correct |
41 ms |
13648 KB |
Output is correct |
20 |
Correct |
34 ms |
13568 KB |
Output is correct |
21 |
Correct |
35 ms |
13660 KB |
Output is correct |
22 |
Correct |
81 ms |
19980 KB |
Output is correct |
23 |
Correct |
68 ms |
20044 KB |
Output is correct |
24 |
Correct |
64 ms |
19660 KB |
Output is correct |
25 |
Correct |
65 ms |
19784 KB |
Output is correct |
26 |
Correct |
69 ms |
19664 KB |
Output is correct |
27 |
Correct |
72 ms |
20168 KB |
Output is correct |
28 |
Correct |
72 ms |
27336 KB |
Output is correct |
29 |
Correct |
74 ms |
24264 KB |
Output is correct |
30 |
Correct |
45 ms |
20184 KB |
Output is correct |
31 |
Correct |
45 ms |
20684 KB |
Output is correct |
32 |
Correct |
51 ms |
19404 KB |
Output is correct |
33 |
Correct |
46 ms |
20684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
9052 KB |
Output is correct |
2 |
Correct |
1 ms |
9052 KB |
Output is correct |
3 |
Correct |
2 ms |
9052 KB |
Output is correct |
4 |
Correct |
7 ms |
9212 KB |
Output is correct |
5 |
Correct |
15 ms |
9212 KB |
Output is correct |
6 |
Correct |
2 ms |
9052 KB |
Output is correct |
7 |
Correct |
2 ms |
9052 KB |
Output is correct |
8 |
Correct |
3 ms |
9308 KB |
Output is correct |
9 |
Correct |
114 ms |
11664 KB |
Output is correct |
10 |
Correct |
563 ms |
21192 KB |
Output is correct |
11 |
Correct |
5 ms |
9048 KB |
Output is correct |
12 |
Correct |
38 ms |
9324 KB |
Output is correct |
13 |
Correct |
137 ms |
49076 KB |
Output is correct |
14 |
Correct |
178 ms |
64180 KB |
Output is correct |
15 |
Execution timed out |
5116 ms |
981288 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |