#include <bits/stdc++.h>
#include <random>
#ifndef LOCAL
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt")
#endif
using namespace std;
typedef long long ll;
typedef double dd;
typedef long double ld;
typedef unsigned int uii;
#define x first
#define y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
void solve();
mt19937_64 mt(1);
int32_t main() {
#ifdef LOCAL
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#else
ios::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
//freopen("amusing.in", "r", stdin);
//freopen("amusing.out", "w", stdout);
#endif
cout << fixed << setprecision(30);
int tests = 1;
cin >> tests;
while (tests--) {
solve();
}
}
int tim = 0;
bool ans = true;
void dfs(int s, int p, vector<vector<int>>& g, vector<int>& tin, vector<int>& tout, vector<int>& pr) {
tin[s] = tim++;
pr[s] = p;
for (int i : g[s]) {
if (i != p) {
dfs(i, s, g, tin, tout, pr);
}
}
tout[s] = tim++;
}
void find_cycle(int s, vector<vector<int>>& g, vector<int>& used) {
used[s] = 1;
for (int i : g[s]) {
if (used[i] == 1) {
ans = false;
}
if (!used[i]) {
find_cycle(i, g, used);
}
}
used[s] = 2;
}
bool a_p_b(int a, int b, vector<int>& tin, vector<int>& tout) {
return tin[a] <= tin[b] && tout[a] >= tout[b];
}
struct path {
int u, v, lca;
};
void solve() {
tim = 0;
int n;
cin >> n;
vector<vector<int>> g(n);
for (int i = 0; i < n - 1; i++) {
int u, v;
cin >> u >> v; v--; u--;
g[v].push_back(u);
g[u].push_back(v);
}
vector<int> s(n, -1), t(n, -1), tin(n), tout(n), pr(n), cnt(n, 0);
dfs(0, -1, g, tin, tout, pr);
int q;
cin >> q;
ans = true;
vector<int> u, used(q, 0);
vector<path> z;
vector<vector<int>> h(q);
for (int i = 0; i < q; i++) {
int a, b;
cin >> a >> b; a--; b--;
s[a] = i;
t[b] = i;
int a1 = a;
while (a1 > 0 && !a_p_b(a1, b, tin, tout)) {
a1 = pr[a1];
}
z.push_back({a, b, a1});
}
for (int ii = 0; ii < q; ii++) {
int a, b;
a = z[ii].u;
b = z[ii].v;
int a1 = a, b1 = b;
while (!a_p_b(a1, b, tin, tout)) {
if (s[a1] != -1) {
cnt[s[a1]] += 2;
u.push_back(s[a1]);
}
if (t[a1] != -1) {
cnt[t[a1]]++;
u.push_back(t[a1]);
}
a1 = pr[a1];
}
while (!a_p_b(b1, a, tin, tout)) {
if (s[b1] != -1) {
cnt[s[b1]] += 2;
u.push_back(s[b1]);
}
if (t[b1] != -1) {
cnt[t[b1]]++;
u.push_back(t[b1]);
}
b1 = pr[b1];
}
if (s[b1] != -1) {
cnt[s[b1]] += 2;
u.push_back(s[b1]);
}
if (t[b1] != -1) {
cnt[t[b1]]++;
u.push_back(t[b1]);
}
for (int i : u) {
if (!cnt[i]) {
continue;
}
if (cnt[i] == 2) {
h[ii].push_back(i);
}
if (cnt[i] == 1) {
h[i].push_back(ii);
}
if (cnt[i] > 2 && i != ii) {
cout << "No\n";
return;
}
cnt[i] = 0;
}
u.clear();
}
ans = true;
for (int i = 0; i < q; i++) {
if (!used[i]) {
find_cycle(i, h, used);
if (!ans) {
cout << "No\n";
return;
}
}
}
if (ans) {
cout << "Yes\n";
} else {
cout << "No\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
544 KB |
Output is correct |
4 |
Correct |
9 ms |
344 KB |
Output is correct |
5 |
Correct |
20 ms |
344 KB |
Output is correct |
6 |
Correct |
2 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
46 ms |
2808 KB |
Output is correct |
10 |
Correct |
171 ms |
22420 KB |
Output is correct |
11 |
Correct |
5 ms |
344 KB |
Output is correct |
12 |
Correct |
28 ms |
344 KB |
Output is correct |
13 |
Correct |
116 ms |
52580 KB |
Output is correct |
14 |
Correct |
57 ms |
24264 KB |
Output is correct |
15 |
Correct |
1884 ms |
23528 KB |
Output is correct |
16 |
Correct |
4496 ms |
26580 KB |
Output is correct |
17 |
Runtime error |
2297 ms |
1048576 KB |
Execution killed with signal 9 |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
344 KB |
Output is correct |
29 |
Correct |
2 ms |
344 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
2 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
2 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
2 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
600 KB |
Output is correct |
4 |
Correct |
1 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
344 KB |
Output is correct |
17 |
Correct |
1 ms |
344 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |
21 |
Correct |
2 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
344 KB |
Output is correct |
26 |
Correct |
1 ms |
344 KB |
Output is correct |
27 |
Correct |
1 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
344 KB |
Output is correct |
29 |
Correct |
2 ms |
344 KB |
Output is correct |
30 |
Correct |
2 ms |
348 KB |
Output is correct |
31 |
Correct |
2 ms |
344 KB |
Output is correct |
32 |
Correct |
1 ms |
344 KB |
Output is correct |
33 |
Correct |
2 ms |
344 KB |
Output is correct |
34 |
Correct |
1 ms |
344 KB |
Output is correct |
35 |
Correct |
2 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
2 ms |
344 KB |
Output is correct |
38 |
Correct |
47 ms |
2852 KB |
Output is correct |
39 |
Correct |
167 ms |
22436 KB |
Output is correct |
40 |
Correct |
29 ms |
968 KB |
Output is correct |
41 |
Correct |
26 ms |
968 KB |
Output is correct |
42 |
Correct |
24 ms |
1224 KB |
Output is correct |
43 |
Correct |
23 ms |
1112 KB |
Output is correct |
44 |
Correct |
7 ms |
600 KB |
Output is correct |
45 |
Correct |
33 ms |
9928 KB |
Output is correct |
46 |
Correct |
37 ms |
9808 KB |
Output is correct |
47 |
Correct |
63 ms |
15512 KB |
Output is correct |
48 |
Correct |
65 ms |
15448 KB |
Output is correct |
49 |
Correct |
29 ms |
10064 KB |
Output is correct |
50 |
Correct |
33 ms |
10064 KB |
Output is correct |
51 |
Correct |
27 ms |
11600 KB |
Output is correct |
52 |
Correct |
26 ms |
11604 KB |
Output is correct |
53 |
Correct |
8 ms |
1116 KB |
Output is correct |
54 |
Correct |
35 ms |
9808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
5 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
344 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
16 ms |
348 KB |
Output is correct |
14 |
Correct |
29 ms |
344 KB |
Output is correct |
15 |
Correct |
23 ms |
352 KB |
Output is correct |
16 |
Correct |
33 ms |
10580 KB |
Output is correct |
17 |
Correct |
49 ms |
12624 KB |
Output is correct |
18 |
Correct |
63 ms |
15304 KB |
Output is correct |
19 |
Correct |
37 ms |
11024 KB |
Output is correct |
20 |
Correct |
36 ms |
11024 KB |
Output is correct |
21 |
Correct |
39 ms |
11052 KB |
Output is correct |
22 |
Correct |
46 ms |
12744 KB |
Output is correct |
23 |
Correct |
44 ms |
12620 KB |
Output is correct |
24 |
Correct |
64 ms |
18308 KB |
Output is correct |
25 |
Correct |
52 ms |
15048 KB |
Output is correct |
26 |
Correct |
67 ms |
18820 KB |
Output is correct |
27 |
Correct |
75 ms |
22848 KB |
Output is correct |
28 |
Correct |
80 ms |
24384 KB |
Output is correct |
29 |
Correct |
74 ms |
21344 KB |
Output is correct |
30 |
Correct |
52 ms |
16452 KB |
Output is correct |
31 |
Correct |
50 ms |
17104 KB |
Output is correct |
32 |
Correct |
56 ms |
16444 KB |
Output is correct |
33 |
Correct |
48 ms |
16964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
600 KB |
Output is correct |
3 |
Correct |
0 ms |
544 KB |
Output is correct |
4 |
Correct |
9 ms |
344 KB |
Output is correct |
5 |
Correct |
20 ms |
344 KB |
Output is correct |
6 |
Correct |
2 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
2 ms |
344 KB |
Output is correct |
9 |
Correct |
46 ms |
2808 KB |
Output is correct |
10 |
Correct |
171 ms |
22420 KB |
Output is correct |
11 |
Correct |
5 ms |
344 KB |
Output is correct |
12 |
Correct |
28 ms |
344 KB |
Output is correct |
13 |
Correct |
116 ms |
52580 KB |
Output is correct |
14 |
Correct |
57 ms |
24264 KB |
Output is correct |
15 |
Correct |
1884 ms |
23528 KB |
Output is correct |
16 |
Correct |
4496 ms |
26580 KB |
Output is correct |
17 |
Runtime error |
2297 ms |
1048576 KB |
Execution killed with signal 9 |
18 |
Halted |
0 ms |
0 KB |
- |