#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
namespace x = __gnu_pbds;
template <typename T>
using ordered_set = x::tree<T, x::null_type, less<T>, x::rb_tree_tag, x::tree_order_statistics_node_update>;
template <typename T>
using normal_queue = priority_queue<T, vector<T>, greater<>>;
#define all(x) begin(x), end(x)
#define sz(x) ((int) (x).size())
#define x first
#define y second
using ll = long long;
using ld = long double;
const int N = 1.25e5, M = 5 * N;
int s[N], t[N];
vector<int> g[N], g2[M];
char used[N];
vector<int> path;
bool dfs_cycle(int u) {
if (used[u] == 1) {
return false;
}
used[u] = 1;
for (int v : g2[u]) {
if (used[v] != 2 && !dfs_cycle(v)) {
return false;
}
}
used[u] = 2;
return true;
}
bool dfs(int u, int w, int p = -1) {
path.push_back(u);
if (u == w) {
return true;
}
for (int v : g[u]) {
if (v != p && dfs(v, w, u)) {
return true;
}
}
path.pop_back();
return false;
}
signed main() {
cin.tie(0)->sync_with_stdio(0);
int testcases;
cin >> testcases;
while (testcases--) {
int n, m;
cin >> n;
for (int i = 0; i < 5 * n; ++i) {
g2[i].clear();
}
for (int i = 0; i < n; ++i) {
g[i].clear();
}
for (int i = 1; i < n; ++i) {
int u, v;
cin >> u >> v;
g[--u].push_back(--v);
g[v].push_back(u);
}
cin >> m;
for (int i = 0; i < m; ++i) {
cin >> s[i] >> t[i];
--s[i], --t[i];
path.clear();
dfs(s[i], t[i]);
for (int u : path) {
if (u != s[i]) {
g2[4 * n + i].push_back(n + u);
}
}
for (int u : path) {
if (u != t[i]) {
g2[3 * n + u].push_back(4 * n + i);
}
}
g2[n + s[i]].push_back(4 * n + i);
g2[4 * n + i].push_back(3 * n + t[i]);
}
// for (int i = 0; i < 5 * n; ++i) {
// for (int v : g[i]) {
// cout << i << " -> " << v << endl;
// }
// }
fill(used, used + 5 * n, 0);
bool flag = true;
for (int i = 0; i < 5 * n; ++i) {
if (!used[i]) {
flag &= dfs_cycle(i);
}
}
cout << (flag ? "Yes\n" : "No\n");
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
17876 KB |
Output is correct |
2 |
Correct |
10 ms |
17832 KB |
Output is correct |
3 |
Correct |
10 ms |
17928 KB |
Output is correct |
4 |
Correct |
21 ms |
17984 KB |
Output is correct |
5 |
Correct |
33 ms |
17992 KB |
Output is correct |
6 |
Correct |
11 ms |
18004 KB |
Output is correct |
7 |
Correct |
11 ms |
17876 KB |
Output is correct |
8 |
Correct |
13 ms |
18120 KB |
Output is correct |
9 |
Correct |
456 ms |
45980 KB |
Output is correct |
10 |
Runtime error |
3319 ms |
506972 KB |
Execution killed with signal 11 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
17880 KB |
Output is correct |
2 |
Correct |
11 ms |
17952 KB |
Output is correct |
3 |
Correct |
11 ms |
17984 KB |
Output is correct |
4 |
Correct |
12 ms |
17876 KB |
Output is correct |
5 |
Correct |
11 ms |
17964 KB |
Output is correct |
6 |
Correct |
11 ms |
17876 KB |
Output is correct |
7 |
Correct |
13 ms |
18004 KB |
Output is correct |
8 |
Correct |
13 ms |
17956 KB |
Output is correct |
9 |
Correct |
14 ms |
17876 KB |
Output is correct |
10 |
Correct |
14 ms |
17876 KB |
Output is correct |
11 |
Correct |
13 ms |
17964 KB |
Output is correct |
12 |
Correct |
10 ms |
17876 KB |
Output is correct |
13 |
Correct |
11 ms |
17876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
17880 KB |
Output is correct |
2 |
Correct |
11 ms |
17952 KB |
Output is correct |
3 |
Correct |
11 ms |
17984 KB |
Output is correct |
4 |
Correct |
12 ms |
17876 KB |
Output is correct |
5 |
Correct |
11 ms |
17964 KB |
Output is correct |
6 |
Correct |
11 ms |
17876 KB |
Output is correct |
7 |
Correct |
13 ms |
18004 KB |
Output is correct |
8 |
Correct |
13 ms |
17956 KB |
Output is correct |
9 |
Correct |
14 ms |
17876 KB |
Output is correct |
10 |
Correct |
14 ms |
17876 KB |
Output is correct |
11 |
Correct |
13 ms |
17964 KB |
Output is correct |
12 |
Correct |
10 ms |
17876 KB |
Output is correct |
13 |
Correct |
11 ms |
17876 KB |
Output is correct |
14 |
Correct |
12 ms |
17876 KB |
Output is correct |
15 |
Correct |
10 ms |
17876 KB |
Output is correct |
16 |
Correct |
11 ms |
18016 KB |
Output is correct |
17 |
Correct |
11 ms |
17992 KB |
Output is correct |
18 |
Correct |
13 ms |
17904 KB |
Output is correct |
19 |
Correct |
12 ms |
17876 KB |
Output is correct |
20 |
Correct |
13 ms |
17940 KB |
Output is correct |
21 |
Correct |
11 ms |
17912 KB |
Output is correct |
22 |
Correct |
12 ms |
18004 KB |
Output is correct |
23 |
Correct |
11 ms |
17864 KB |
Output is correct |
24 |
Correct |
11 ms |
17940 KB |
Output is correct |
25 |
Correct |
11 ms |
17940 KB |
Output is correct |
26 |
Correct |
10 ms |
17952 KB |
Output is correct |
27 |
Correct |
11 ms |
17976 KB |
Output is correct |
28 |
Correct |
10 ms |
17900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
17880 KB |
Output is correct |
2 |
Correct |
11 ms |
17952 KB |
Output is correct |
3 |
Correct |
11 ms |
17984 KB |
Output is correct |
4 |
Correct |
12 ms |
17876 KB |
Output is correct |
5 |
Correct |
11 ms |
17964 KB |
Output is correct |
6 |
Correct |
11 ms |
17876 KB |
Output is correct |
7 |
Correct |
13 ms |
18004 KB |
Output is correct |
8 |
Correct |
13 ms |
17956 KB |
Output is correct |
9 |
Correct |
14 ms |
17876 KB |
Output is correct |
10 |
Correct |
14 ms |
17876 KB |
Output is correct |
11 |
Correct |
13 ms |
17964 KB |
Output is correct |
12 |
Correct |
10 ms |
17876 KB |
Output is correct |
13 |
Correct |
11 ms |
17876 KB |
Output is correct |
14 |
Correct |
12 ms |
17876 KB |
Output is correct |
15 |
Correct |
10 ms |
17876 KB |
Output is correct |
16 |
Correct |
11 ms |
18016 KB |
Output is correct |
17 |
Correct |
11 ms |
17992 KB |
Output is correct |
18 |
Correct |
13 ms |
17904 KB |
Output is correct |
19 |
Correct |
12 ms |
17876 KB |
Output is correct |
20 |
Correct |
13 ms |
17940 KB |
Output is correct |
21 |
Correct |
11 ms |
17912 KB |
Output is correct |
22 |
Correct |
12 ms |
18004 KB |
Output is correct |
23 |
Correct |
11 ms |
17864 KB |
Output is correct |
24 |
Correct |
11 ms |
17940 KB |
Output is correct |
25 |
Correct |
11 ms |
17940 KB |
Output is correct |
26 |
Correct |
10 ms |
17952 KB |
Output is correct |
27 |
Correct |
11 ms |
17976 KB |
Output is correct |
28 |
Correct |
10 ms |
17900 KB |
Output is correct |
29 |
Correct |
15 ms |
18132 KB |
Output is correct |
30 |
Correct |
15 ms |
18004 KB |
Output is correct |
31 |
Correct |
18 ms |
18108 KB |
Output is correct |
32 |
Correct |
16 ms |
17948 KB |
Output is correct |
33 |
Correct |
12 ms |
17968 KB |
Output is correct |
34 |
Correct |
14 ms |
17960 KB |
Output is correct |
35 |
Correct |
17 ms |
18004 KB |
Output is correct |
36 |
Correct |
12 ms |
17952 KB |
Output is correct |
37 |
Correct |
13 ms |
18004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
17880 KB |
Output is correct |
2 |
Correct |
11 ms |
17952 KB |
Output is correct |
3 |
Correct |
11 ms |
17984 KB |
Output is correct |
4 |
Correct |
12 ms |
17876 KB |
Output is correct |
5 |
Correct |
11 ms |
17964 KB |
Output is correct |
6 |
Correct |
11 ms |
17876 KB |
Output is correct |
7 |
Correct |
13 ms |
18004 KB |
Output is correct |
8 |
Correct |
13 ms |
17956 KB |
Output is correct |
9 |
Correct |
14 ms |
17876 KB |
Output is correct |
10 |
Correct |
14 ms |
17876 KB |
Output is correct |
11 |
Correct |
13 ms |
17964 KB |
Output is correct |
12 |
Correct |
10 ms |
17876 KB |
Output is correct |
13 |
Correct |
11 ms |
17876 KB |
Output is correct |
14 |
Correct |
12 ms |
17876 KB |
Output is correct |
15 |
Correct |
10 ms |
17876 KB |
Output is correct |
16 |
Correct |
11 ms |
18016 KB |
Output is correct |
17 |
Correct |
11 ms |
17992 KB |
Output is correct |
18 |
Correct |
13 ms |
17904 KB |
Output is correct |
19 |
Correct |
12 ms |
17876 KB |
Output is correct |
20 |
Correct |
13 ms |
17940 KB |
Output is correct |
21 |
Correct |
11 ms |
17912 KB |
Output is correct |
22 |
Correct |
12 ms |
18004 KB |
Output is correct |
23 |
Correct |
11 ms |
17864 KB |
Output is correct |
24 |
Correct |
11 ms |
17940 KB |
Output is correct |
25 |
Correct |
11 ms |
17940 KB |
Output is correct |
26 |
Correct |
10 ms |
17952 KB |
Output is correct |
27 |
Correct |
11 ms |
17976 KB |
Output is correct |
28 |
Correct |
10 ms |
17900 KB |
Output is correct |
29 |
Correct |
15 ms |
18132 KB |
Output is correct |
30 |
Correct |
15 ms |
18004 KB |
Output is correct |
31 |
Correct |
18 ms |
18108 KB |
Output is correct |
32 |
Correct |
16 ms |
17948 KB |
Output is correct |
33 |
Correct |
12 ms |
17968 KB |
Output is correct |
34 |
Correct |
14 ms |
17960 KB |
Output is correct |
35 |
Correct |
17 ms |
18004 KB |
Output is correct |
36 |
Correct |
12 ms |
17952 KB |
Output is correct |
37 |
Correct |
13 ms |
18004 KB |
Output is correct |
38 |
Correct |
476 ms |
46148 KB |
Output is correct |
39 |
Runtime error |
3366 ms |
507236 KB |
Execution killed with signal 11 |
40 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
17876 KB |
Output is correct |
2 |
Correct |
11 ms |
17876 KB |
Output is correct |
3 |
Correct |
10 ms |
17876 KB |
Output is correct |
4 |
Correct |
11 ms |
17864 KB |
Output is correct |
5 |
Correct |
16 ms |
18100 KB |
Output is correct |
6 |
Correct |
11 ms |
17876 KB |
Output is correct |
7 |
Correct |
11 ms |
17876 KB |
Output is correct |
8 |
Correct |
10 ms |
17876 KB |
Output is correct |
9 |
Correct |
10 ms |
17916 KB |
Output is correct |
10 |
Correct |
10 ms |
17876 KB |
Output is correct |
11 |
Correct |
10 ms |
17876 KB |
Output is correct |
12 |
Correct |
12 ms |
17960 KB |
Output is correct |
13 |
Correct |
52 ms |
18400 KB |
Output is correct |
14 |
Correct |
59 ms |
18640 KB |
Output is correct |
15 |
Correct |
56 ms |
18456 KB |
Output is correct |
16 |
Execution timed out |
5047 ms |
23524 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
17876 KB |
Output is correct |
2 |
Correct |
10 ms |
17832 KB |
Output is correct |
3 |
Correct |
10 ms |
17928 KB |
Output is correct |
4 |
Correct |
21 ms |
17984 KB |
Output is correct |
5 |
Correct |
33 ms |
17992 KB |
Output is correct |
6 |
Correct |
11 ms |
18004 KB |
Output is correct |
7 |
Correct |
11 ms |
17876 KB |
Output is correct |
8 |
Correct |
13 ms |
18120 KB |
Output is correct |
9 |
Correct |
456 ms |
45980 KB |
Output is correct |
10 |
Runtime error |
3319 ms |
506972 KB |
Execution killed with signal 11 |
11 |
Halted |
0 ms |
0 KB |
- |