#include <bits/stdc++.h>
bool home = 1;
using namespace std;
const int N = 120000 + 7; // change this!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const int K = 20;
int n;
int m;
int bad;
int vis[N];
int act[N];
vector<int> g[N];
vector<int> relations[N];
pair<int, int> paths[N];
int first[N];
int last[N];
int top;
int depth[N];
int lg[2 * N];
pair<int, int> rmq[K][N];
void dfs(int a, int p, int dep) {
depth[a] = dep;
{
vector<int> kids;
for (auto &b : g[a]) {
if (b == p) continue;
kids.push_back(b);
}
g[a] = kids;
}
rmq[0][++top] = {dep, a};
first[a] = last[a] = top;
for (auto &b : g[a]) {
dfs(b, a, dep + 1);
rmq[0][++top] = {dep, a};
last[a] = top;
}
}
int get_lca(int a, int b) {
assert(1 <= a && a <= n);
assert(1 <= b && b <= n);
if (first[a] > last[b]) swap(a, b);
a = first[a];
b = last[b];
assert(a <= b);
int k = lg[b - a + 1];
return min(rmq[k][a], rmq[k][b - (1 << k) + 1]).second;
}
bool is_on_path(int a, int query, int b) {
int c = get_lca(a, b);
if (get_lca(c, query) == c) {
return get_lca(a, query) == query || get_lca(b, query) == query;
}
return 0;
}
void check_for_cycle(int a) {
vis[a] = act[a] = 1;
for (auto &b : relations[a]) {
if (act[b]) {
bad = 1;
}
if (!vis[b]) {
check_for_cycle(b);
}
}
act[a] = 0;
}
signed main() {
#ifdef ONLINE_JUDGE
home = 0;
#endif
home = 0;
if (home) {
freopen("I_am_iron_man", "r", stdin);
}
else {
ios::sync_with_stdio(0); cin.tie(0);
}
int Tests;
cin >> Tests;
for (int tc = 1; tc <= Tests; tc++) {
bad = 0;
cin >> n;
for (int i = 1; i <= n; i++) g[i].clear(), relations[i].clear(), vis[i] = 0;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
g[a].push_back(b);
g[b].push_back(a);
}
cin >> m;
for (int i = 1; i <= m; i++) {
cin >> paths[i].first >> paths[i].second;
}
top = 0;
dfs(1, -1, 0);
for (int i = 2; i <= top; i++) {
lg[i] = 1 + lg[i / 2];
}
for (int k = 1; (1 << k) <= top; k++) {
for (int i = 1; i + (1 << k) - 1 <= top; i++) {
rmq[k][i] = min(rmq[k - 1][i], rmq[k - 1][i + (1 << (k - 1))]);
}
}
/**for (int i = 1; i <= n; i++) {
assert(get_lca(i, i) == i);
assert(get_lca(1, i) == 1);
for (int j = 1; j <= n; j++) {
assert(get_lca(i, j) == get_lca(j, i));
if (1 < i && i < j) {
cout << i << " " << j << " -> " << get_lca(i, j) << "\n";
}
}
}**/
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= m; j++) {
if (i == j) {
continue;
}
int a = paths[i].first, b = paths[i].second;
int x = paths[j].first, y = paths[j].second;
if (is_on_path(a, x, b)) {
relations[i].push_back(j);
}
if (is_on_path(a, y, b)) {
relations[j].push_back(i);
}
}
}
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
check_for_cycle(i);
}
}
cout << ((!bad) ? ("Yes") : ("No")) << "\n";
}
}
Compilation message
jail.cpp: In function 'int main()':
jail.cpp:83:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | freopen("I_am_iron_man", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
3 ms |
5972 KB |
Output is correct |
4 |
Correct |
13 ms |
6072 KB |
Output is correct |
5 |
Correct |
26 ms |
6112 KB |
Output is correct |
6 |
Correct |
4 ms |
6052 KB |
Output is correct |
7 |
Correct |
5 ms |
6100 KB |
Output is correct |
8 |
Correct |
13 ms |
6120 KB |
Output is correct |
9 |
Correct |
251 ms |
11224 KB |
Output is correct |
10 |
Correct |
69 ms |
45416 KB |
Output is correct |
11 |
Correct |
13 ms |
6180 KB |
Output is correct |
12 |
Correct |
150 ms |
7328 KB |
Output is correct |
13 |
Execution timed out |
5027 ms |
46324 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
4 ms |
6100 KB |
Output is correct |
4 |
Correct |
5 ms |
6116 KB |
Output is correct |
5 |
Correct |
5 ms |
6100 KB |
Output is correct |
6 |
Correct |
5 ms |
6104 KB |
Output is correct |
7 |
Correct |
5 ms |
6108 KB |
Output is correct |
8 |
Correct |
5 ms |
6108 KB |
Output is correct |
9 |
Correct |
6 ms |
6100 KB |
Output is correct |
10 |
Correct |
5 ms |
6100 KB |
Output is correct |
11 |
Correct |
5 ms |
6112 KB |
Output is correct |
12 |
Correct |
4 ms |
6108 KB |
Output is correct |
13 |
Correct |
4 ms |
6100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
4 ms |
6100 KB |
Output is correct |
4 |
Correct |
5 ms |
6116 KB |
Output is correct |
5 |
Correct |
5 ms |
6100 KB |
Output is correct |
6 |
Correct |
5 ms |
6104 KB |
Output is correct |
7 |
Correct |
5 ms |
6108 KB |
Output is correct |
8 |
Correct |
5 ms |
6108 KB |
Output is correct |
9 |
Correct |
6 ms |
6100 KB |
Output is correct |
10 |
Correct |
5 ms |
6100 KB |
Output is correct |
11 |
Correct |
5 ms |
6112 KB |
Output is correct |
12 |
Correct |
4 ms |
6108 KB |
Output is correct |
13 |
Correct |
4 ms |
6100 KB |
Output is correct |
14 |
Correct |
3 ms |
5972 KB |
Output is correct |
15 |
Correct |
3 ms |
5972 KB |
Output is correct |
16 |
Correct |
6 ms |
6116 KB |
Output is correct |
17 |
Correct |
5 ms |
6100 KB |
Output is correct |
18 |
Correct |
5 ms |
6104 KB |
Output is correct |
19 |
Correct |
3 ms |
5968 KB |
Output is correct |
20 |
Correct |
5 ms |
6104 KB |
Output is correct |
21 |
Correct |
5 ms |
6100 KB |
Output is correct |
22 |
Correct |
5 ms |
6100 KB |
Output is correct |
23 |
Correct |
4 ms |
5972 KB |
Output is correct |
24 |
Correct |
4 ms |
5972 KB |
Output is correct |
25 |
Correct |
5 ms |
6236 KB |
Output is correct |
26 |
Correct |
4 ms |
5972 KB |
Output is correct |
27 |
Correct |
5 ms |
6100 KB |
Output is correct |
28 |
Correct |
3 ms |
5972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
4 ms |
6100 KB |
Output is correct |
4 |
Correct |
5 ms |
6116 KB |
Output is correct |
5 |
Correct |
5 ms |
6100 KB |
Output is correct |
6 |
Correct |
5 ms |
6104 KB |
Output is correct |
7 |
Correct |
5 ms |
6108 KB |
Output is correct |
8 |
Correct |
5 ms |
6108 KB |
Output is correct |
9 |
Correct |
6 ms |
6100 KB |
Output is correct |
10 |
Correct |
5 ms |
6100 KB |
Output is correct |
11 |
Correct |
5 ms |
6112 KB |
Output is correct |
12 |
Correct |
4 ms |
6108 KB |
Output is correct |
13 |
Correct |
4 ms |
6100 KB |
Output is correct |
14 |
Correct |
3 ms |
5972 KB |
Output is correct |
15 |
Correct |
3 ms |
5972 KB |
Output is correct |
16 |
Correct |
6 ms |
6116 KB |
Output is correct |
17 |
Correct |
5 ms |
6100 KB |
Output is correct |
18 |
Correct |
5 ms |
6104 KB |
Output is correct |
19 |
Correct |
3 ms |
5968 KB |
Output is correct |
20 |
Correct |
5 ms |
6104 KB |
Output is correct |
21 |
Correct |
5 ms |
6100 KB |
Output is correct |
22 |
Correct |
5 ms |
6100 KB |
Output is correct |
23 |
Correct |
4 ms |
5972 KB |
Output is correct |
24 |
Correct |
4 ms |
5972 KB |
Output is correct |
25 |
Correct |
5 ms |
6236 KB |
Output is correct |
26 |
Correct |
4 ms |
5972 KB |
Output is correct |
27 |
Correct |
5 ms |
6100 KB |
Output is correct |
28 |
Correct |
3 ms |
5972 KB |
Output is correct |
29 |
Correct |
14 ms |
6232 KB |
Output is correct |
30 |
Correct |
11 ms |
6100 KB |
Output is correct |
31 |
Correct |
10 ms |
6100 KB |
Output is correct |
32 |
Correct |
7 ms |
6100 KB |
Output is correct |
33 |
Correct |
5 ms |
6100 KB |
Output is correct |
34 |
Correct |
17 ms |
6144 KB |
Output is correct |
35 |
Correct |
20 ms |
6100 KB |
Output is correct |
36 |
Correct |
17 ms |
6100 KB |
Output is correct |
37 |
Correct |
12 ms |
6100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
4 ms |
6100 KB |
Output is correct |
4 |
Correct |
5 ms |
6116 KB |
Output is correct |
5 |
Correct |
5 ms |
6100 KB |
Output is correct |
6 |
Correct |
5 ms |
6104 KB |
Output is correct |
7 |
Correct |
5 ms |
6108 KB |
Output is correct |
8 |
Correct |
5 ms |
6108 KB |
Output is correct |
9 |
Correct |
6 ms |
6100 KB |
Output is correct |
10 |
Correct |
5 ms |
6100 KB |
Output is correct |
11 |
Correct |
5 ms |
6112 KB |
Output is correct |
12 |
Correct |
4 ms |
6108 KB |
Output is correct |
13 |
Correct |
4 ms |
6100 KB |
Output is correct |
14 |
Correct |
3 ms |
5972 KB |
Output is correct |
15 |
Correct |
3 ms |
5972 KB |
Output is correct |
16 |
Correct |
6 ms |
6116 KB |
Output is correct |
17 |
Correct |
5 ms |
6100 KB |
Output is correct |
18 |
Correct |
5 ms |
6104 KB |
Output is correct |
19 |
Correct |
3 ms |
5968 KB |
Output is correct |
20 |
Correct |
5 ms |
6104 KB |
Output is correct |
21 |
Correct |
5 ms |
6100 KB |
Output is correct |
22 |
Correct |
5 ms |
6100 KB |
Output is correct |
23 |
Correct |
4 ms |
5972 KB |
Output is correct |
24 |
Correct |
4 ms |
5972 KB |
Output is correct |
25 |
Correct |
5 ms |
6236 KB |
Output is correct |
26 |
Correct |
4 ms |
5972 KB |
Output is correct |
27 |
Correct |
5 ms |
6100 KB |
Output is correct |
28 |
Correct |
3 ms |
5972 KB |
Output is correct |
29 |
Correct |
14 ms |
6232 KB |
Output is correct |
30 |
Correct |
11 ms |
6100 KB |
Output is correct |
31 |
Correct |
10 ms |
6100 KB |
Output is correct |
32 |
Correct |
7 ms |
6100 KB |
Output is correct |
33 |
Correct |
5 ms |
6100 KB |
Output is correct |
34 |
Correct |
17 ms |
6144 KB |
Output is correct |
35 |
Correct |
20 ms |
6100 KB |
Output is correct |
36 |
Correct |
17 ms |
6100 KB |
Output is correct |
37 |
Correct |
12 ms |
6100 KB |
Output is correct |
38 |
Correct |
256 ms |
10800 KB |
Output is correct |
39 |
Correct |
73 ms |
45740 KB |
Output is correct |
40 |
Correct |
442 ms |
10204 KB |
Output is correct |
41 |
Correct |
535 ms |
8600 KB |
Output is correct |
42 |
Correct |
455 ms |
10300 KB |
Output is correct |
43 |
Correct |
37 ms |
8996 KB |
Output is correct |
44 |
Correct |
313 ms |
6672 KB |
Output is correct |
45 |
Incorrect |
106 ms |
31180 KB |
Output isn't correct |
46 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
4 ms |
5916 KB |
Output is correct |
3 |
Correct |
3 ms |
5972 KB |
Output is correct |
4 |
Correct |
3 ms |
5956 KB |
Output is correct |
5 |
Correct |
12 ms |
6032 KB |
Output is correct |
6 |
Correct |
4 ms |
5972 KB |
Output is correct |
7 |
Correct |
4 ms |
5972 KB |
Output is correct |
8 |
Correct |
4 ms |
5972 KB |
Output is correct |
9 |
Correct |
4 ms |
5968 KB |
Output is correct |
10 |
Correct |
4 ms |
5972 KB |
Output is correct |
11 |
Correct |
3 ms |
5972 KB |
Output is correct |
12 |
Correct |
15 ms |
6100 KB |
Output is correct |
13 |
Correct |
172 ms |
6476 KB |
Output is correct |
14 |
Correct |
106 ms |
6816 KB |
Output is correct |
15 |
Correct |
129 ms |
6584 KB |
Output is correct |
16 |
Execution timed out |
5060 ms |
30964 KB |
Time limit exceeded |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5972 KB |
Output is correct |
2 |
Correct |
3 ms |
5972 KB |
Output is correct |
3 |
Correct |
3 ms |
5972 KB |
Output is correct |
4 |
Correct |
13 ms |
6072 KB |
Output is correct |
5 |
Correct |
26 ms |
6112 KB |
Output is correct |
6 |
Correct |
4 ms |
6052 KB |
Output is correct |
7 |
Correct |
5 ms |
6100 KB |
Output is correct |
8 |
Correct |
13 ms |
6120 KB |
Output is correct |
9 |
Correct |
251 ms |
11224 KB |
Output is correct |
10 |
Correct |
69 ms |
45416 KB |
Output is correct |
11 |
Correct |
13 ms |
6180 KB |
Output is correct |
12 |
Correct |
150 ms |
7328 KB |
Output is correct |
13 |
Execution timed out |
5027 ms |
46324 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |