#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
typedef long long llong;
const int MAXN = 300000 + 10;
const int INF = 2e9;
int n, q;
int parent[MAXN];
int in[MAXN], out[MAXN], timer;
int begin[MAXN], end[MAXN];
std::vector <int> g[MAXN];
std::vector <int> v[MAXN];
int from[MAXN], to[MAXN];
void initDFS(int node, int p)
{
parent[node] = p;
in[node] = ++timer;
for (const int &i : g[node])
{
if (i == p) continue;
initDFS(i, node);
}
out[node] = ++timer;
}
inline bool inSubtree(int x, int y)
{
return in[y] <= in[x] && out[x] <= out[y];
}
void cyclePath(int start, int finish, int num)
{
if (inSubtree(start, finish))
{
if (begin[start] != 0 && begin[start] != num)
{
v[num].push_back(begin[start]);
}
if (end[start] != 0 && end[start] != num)
{
v[end[start]].push_back(num);
}
if (start != finish) cyclePath(parent[start], finish, num);
} else
{
if (begin[finish] != 0 && begin[finish] != num)
{
v[num].push_back(begin[finish]);
}
if (end[finish] != 0 && end[finish] != num)
{
v[end[finish]].push_back(num);
}
cyclePath(start, parent[finish], num);
}
}
int vis[MAXN];
bool findCycle(int node)
{
vis[node] = 1;
for (const int &i : v[node])
{
if (vis[i] == 2) continue;
if (vis[i] == 1) return true;
if (findCycle(i)) return true;
}
vis[node] = 2;
return false;
}
void solve()
{
initDFS(1, 0);
for (int i = 1 ; i <= q ; ++i)
{
if (from[i] == to[i]) continue;
cyclePath(from[i], to[i], i);
}
for (int i = 1 ; i <= q ; ++i)
{
if (vis[i] == 2) continue;
if (findCycle(i))
{
std::cout << "No\n";
return;
}
}
std::cout << "Yes\n";
}
void read()
{
int x, y;
std::cin >> n;
for (int i = 2 ; i <= n ; ++i)
{
std::cin >> x >> y;
g[x].push_back(y);
g[y].push_back(x);
}
std::cin >> q;
for (int i = 1 ; i <= q ; ++i)
{
std::cin >> from[i] >> to[i];
begin[from[i]] = i;
end[to[i]] = i;
}
}
void reset()
{
for (int i = 1 ; i <= std::max(n, q) ; ++i)
{
g[i].clear();
v[i].clear();
begin[i] = 0;
end[i] = 0;
vis[i] = 0;
timer = 0;
}
}
void fastIO()
{
std::ios_base :: sync_with_stdio(0);
std::cout.tie(nullptr);
std::cin.tie(nullptr);
}
int tests;
int main()
{
fastIO();
std::cin >> tests;
while (tests--)
{
reset();
read();
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14416 KB |
Output is correct |
3 |
Correct |
8 ms |
14428 KB |
Output is correct |
4 |
Correct |
14 ms |
14704 KB |
Output is correct |
5 |
Correct |
22 ms |
15188 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14440 KB |
Output is correct |
8 |
Correct |
9 ms |
14576 KB |
Output is correct |
9 |
Correct |
97 ms |
18204 KB |
Output is correct |
10 |
Correct |
587 ms |
29848 KB |
Output is correct |
11 |
Correct |
11 ms |
14548 KB |
Output is correct |
12 |
Correct |
37 ms |
15520 KB |
Output is correct |
13 |
Correct |
139 ms |
58528 KB |
Output is correct |
14 |
Correct |
170 ms |
73396 KB |
Output is correct |
15 |
Execution timed out |
5034 ms |
586544 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
10 ms |
14420 KB |
Output is correct |
3 |
Correct |
9 ms |
14420 KB |
Output is correct |
4 |
Correct |
9 ms |
14444 KB |
Output is correct |
5 |
Correct |
9 ms |
14548 KB |
Output is correct |
6 |
Correct |
11 ms |
14420 KB |
Output is correct |
7 |
Correct |
11 ms |
14492 KB |
Output is correct |
8 |
Correct |
8 ms |
14444 KB |
Output is correct |
9 |
Correct |
8 ms |
14420 KB |
Output is correct |
10 |
Correct |
9 ms |
14516 KB |
Output is correct |
11 |
Correct |
8 ms |
14416 KB |
Output is correct |
12 |
Correct |
7 ms |
14440 KB |
Output is correct |
13 |
Correct |
8 ms |
14448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
10 ms |
14420 KB |
Output is correct |
3 |
Correct |
9 ms |
14420 KB |
Output is correct |
4 |
Correct |
9 ms |
14444 KB |
Output is correct |
5 |
Correct |
9 ms |
14548 KB |
Output is correct |
6 |
Correct |
11 ms |
14420 KB |
Output is correct |
7 |
Correct |
11 ms |
14492 KB |
Output is correct |
8 |
Correct |
8 ms |
14444 KB |
Output is correct |
9 |
Correct |
8 ms |
14420 KB |
Output is correct |
10 |
Correct |
9 ms |
14516 KB |
Output is correct |
11 |
Correct |
8 ms |
14416 KB |
Output is correct |
12 |
Correct |
7 ms |
14440 KB |
Output is correct |
13 |
Correct |
8 ms |
14448 KB |
Output is correct |
14 |
Correct |
8 ms |
14556 KB |
Output is correct |
15 |
Correct |
8 ms |
14388 KB |
Output is correct |
16 |
Correct |
8 ms |
14424 KB |
Output is correct |
17 |
Correct |
9 ms |
14448 KB |
Output is correct |
18 |
Correct |
9 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14432 KB |
Output is correct |
20 |
Correct |
8 ms |
14420 KB |
Output is correct |
21 |
Correct |
8 ms |
14444 KB |
Output is correct |
22 |
Correct |
8 ms |
14440 KB |
Output is correct |
23 |
Correct |
8 ms |
14460 KB |
Output is correct |
24 |
Correct |
10 ms |
14472 KB |
Output is correct |
25 |
Correct |
8 ms |
14420 KB |
Output is correct |
26 |
Correct |
8 ms |
14420 KB |
Output is correct |
27 |
Correct |
9 ms |
14420 KB |
Output is correct |
28 |
Correct |
8 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
10 ms |
14420 KB |
Output is correct |
3 |
Correct |
9 ms |
14420 KB |
Output is correct |
4 |
Correct |
9 ms |
14444 KB |
Output is correct |
5 |
Correct |
9 ms |
14548 KB |
Output is correct |
6 |
Correct |
11 ms |
14420 KB |
Output is correct |
7 |
Correct |
11 ms |
14492 KB |
Output is correct |
8 |
Correct |
8 ms |
14444 KB |
Output is correct |
9 |
Correct |
8 ms |
14420 KB |
Output is correct |
10 |
Correct |
9 ms |
14516 KB |
Output is correct |
11 |
Correct |
8 ms |
14416 KB |
Output is correct |
12 |
Correct |
7 ms |
14440 KB |
Output is correct |
13 |
Correct |
8 ms |
14448 KB |
Output is correct |
14 |
Correct |
8 ms |
14556 KB |
Output is correct |
15 |
Correct |
8 ms |
14388 KB |
Output is correct |
16 |
Correct |
8 ms |
14424 KB |
Output is correct |
17 |
Correct |
9 ms |
14448 KB |
Output is correct |
18 |
Correct |
9 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14432 KB |
Output is correct |
20 |
Correct |
8 ms |
14420 KB |
Output is correct |
21 |
Correct |
8 ms |
14444 KB |
Output is correct |
22 |
Correct |
8 ms |
14440 KB |
Output is correct |
23 |
Correct |
8 ms |
14460 KB |
Output is correct |
24 |
Correct |
10 ms |
14472 KB |
Output is correct |
25 |
Correct |
8 ms |
14420 KB |
Output is correct |
26 |
Correct |
8 ms |
14420 KB |
Output is correct |
27 |
Correct |
9 ms |
14420 KB |
Output is correct |
28 |
Correct |
8 ms |
14420 KB |
Output is correct |
29 |
Correct |
10 ms |
14548 KB |
Output is correct |
30 |
Correct |
9 ms |
14444 KB |
Output is correct |
31 |
Correct |
9 ms |
14548 KB |
Output is correct |
32 |
Correct |
9 ms |
14420 KB |
Output is correct |
33 |
Correct |
9 ms |
14440 KB |
Output is correct |
34 |
Correct |
9 ms |
14444 KB |
Output is correct |
35 |
Correct |
10 ms |
14548 KB |
Output is correct |
36 |
Correct |
10 ms |
14444 KB |
Output is correct |
37 |
Correct |
8 ms |
14420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
14420 KB |
Output is correct |
2 |
Correct |
10 ms |
14420 KB |
Output is correct |
3 |
Correct |
9 ms |
14420 KB |
Output is correct |
4 |
Correct |
9 ms |
14444 KB |
Output is correct |
5 |
Correct |
9 ms |
14548 KB |
Output is correct |
6 |
Correct |
11 ms |
14420 KB |
Output is correct |
7 |
Correct |
11 ms |
14492 KB |
Output is correct |
8 |
Correct |
8 ms |
14444 KB |
Output is correct |
9 |
Correct |
8 ms |
14420 KB |
Output is correct |
10 |
Correct |
9 ms |
14516 KB |
Output is correct |
11 |
Correct |
8 ms |
14416 KB |
Output is correct |
12 |
Correct |
7 ms |
14440 KB |
Output is correct |
13 |
Correct |
8 ms |
14448 KB |
Output is correct |
14 |
Correct |
8 ms |
14556 KB |
Output is correct |
15 |
Correct |
8 ms |
14388 KB |
Output is correct |
16 |
Correct |
8 ms |
14424 KB |
Output is correct |
17 |
Correct |
9 ms |
14448 KB |
Output is correct |
18 |
Correct |
9 ms |
14420 KB |
Output is correct |
19 |
Correct |
8 ms |
14432 KB |
Output is correct |
20 |
Correct |
8 ms |
14420 KB |
Output is correct |
21 |
Correct |
8 ms |
14444 KB |
Output is correct |
22 |
Correct |
8 ms |
14440 KB |
Output is correct |
23 |
Correct |
8 ms |
14460 KB |
Output is correct |
24 |
Correct |
10 ms |
14472 KB |
Output is correct |
25 |
Correct |
8 ms |
14420 KB |
Output is correct |
26 |
Correct |
8 ms |
14420 KB |
Output is correct |
27 |
Correct |
9 ms |
14420 KB |
Output is correct |
28 |
Correct |
8 ms |
14420 KB |
Output is correct |
29 |
Correct |
10 ms |
14548 KB |
Output is correct |
30 |
Correct |
9 ms |
14444 KB |
Output is correct |
31 |
Correct |
9 ms |
14548 KB |
Output is correct |
32 |
Correct |
9 ms |
14420 KB |
Output is correct |
33 |
Correct |
9 ms |
14440 KB |
Output is correct |
34 |
Correct |
9 ms |
14444 KB |
Output is correct |
35 |
Correct |
10 ms |
14548 KB |
Output is correct |
36 |
Correct |
10 ms |
14444 KB |
Output is correct |
37 |
Correct |
8 ms |
14420 KB |
Output is correct |
38 |
Correct |
102 ms |
18400 KB |
Output is correct |
39 |
Correct |
573 ms |
29792 KB |
Output is correct |
40 |
Correct |
59 ms |
17228 KB |
Output is correct |
41 |
Correct |
30 ms |
15988 KB |
Output is correct |
42 |
Correct |
49 ms |
17240 KB |
Output is correct |
43 |
Correct |
26 ms |
16012 KB |
Output is correct |
44 |
Correct |
14 ms |
14932 KB |
Output is correct |
45 |
Correct |
50 ms |
22012 KB |
Output is correct |
46 |
Correct |
47 ms |
21920 KB |
Output is correct |
47 |
Correct |
88 ms |
25556 KB |
Output is correct |
48 |
Correct |
89 ms |
25572 KB |
Output is correct |
49 |
Correct |
55 ms |
22168 KB |
Output is correct |
50 |
Correct |
52 ms |
22092 KB |
Output is correct |
51 |
Correct |
39 ms |
22744 KB |
Output is correct |
52 |
Correct |
44 ms |
22804 KB |
Output is correct |
53 |
Correct |
16 ms |
15224 KB |
Output is correct |
54 |
Correct |
55 ms |
21896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14432 KB |
Output is correct |
2 |
Correct |
7 ms |
14432 KB |
Output is correct |
3 |
Correct |
8 ms |
14432 KB |
Output is correct |
4 |
Correct |
8 ms |
14432 KB |
Output is correct |
5 |
Correct |
12 ms |
14584 KB |
Output is correct |
6 |
Correct |
8 ms |
14432 KB |
Output is correct |
7 |
Correct |
8 ms |
14420 KB |
Output is correct |
8 |
Correct |
8 ms |
14452 KB |
Output is correct |
9 |
Correct |
8 ms |
14420 KB |
Output is correct |
10 |
Correct |
9 ms |
14432 KB |
Output is correct |
11 |
Correct |
8 ms |
14432 KB |
Output is correct |
12 |
Correct |
8 ms |
14420 KB |
Output is correct |
13 |
Correct |
21 ms |
14972 KB |
Output is correct |
14 |
Correct |
28 ms |
15336 KB |
Output is correct |
15 |
Correct |
26 ms |
15180 KB |
Output is correct |
16 |
Correct |
51 ms |
22584 KB |
Output is correct |
17 |
Correct |
107 ms |
28680 KB |
Output is correct |
18 |
Correct |
249 ms |
49076 KB |
Output is correct |
19 |
Correct |
56 ms |
22776 KB |
Output is correct |
20 |
Correct |
63 ms |
22776 KB |
Output is correct |
21 |
Correct |
57 ms |
22756 KB |
Output is correct |
22 |
Correct |
91 ms |
28872 KB |
Output is correct |
23 |
Correct |
80 ms |
28996 KB |
Output is correct |
24 |
Correct |
81 ms |
29004 KB |
Output is correct |
25 |
Correct |
81 ms |
29148 KB |
Output is correct |
26 |
Correct |
83 ms |
29252 KB |
Output is correct |
27 |
Correct |
98 ms |
32996 KB |
Output is correct |
28 |
Correct |
101 ms |
34552 KB |
Output is correct |
29 |
Correct |
92 ms |
31620 KB |
Output is correct |
30 |
Correct |
79 ms |
27784 KB |
Output is correct |
31 |
Correct |
72 ms |
28276 KB |
Output is correct |
32 |
Correct |
69 ms |
27592 KB |
Output is correct |
33 |
Correct |
71 ms |
28244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
14420 KB |
Output is correct |
2 |
Correct |
7 ms |
14416 KB |
Output is correct |
3 |
Correct |
8 ms |
14428 KB |
Output is correct |
4 |
Correct |
14 ms |
14704 KB |
Output is correct |
5 |
Correct |
22 ms |
15188 KB |
Output is correct |
6 |
Correct |
8 ms |
14420 KB |
Output is correct |
7 |
Correct |
8 ms |
14440 KB |
Output is correct |
8 |
Correct |
9 ms |
14576 KB |
Output is correct |
9 |
Correct |
97 ms |
18204 KB |
Output is correct |
10 |
Correct |
587 ms |
29848 KB |
Output is correct |
11 |
Correct |
11 ms |
14548 KB |
Output is correct |
12 |
Correct |
37 ms |
15520 KB |
Output is correct |
13 |
Correct |
139 ms |
58528 KB |
Output is correct |
14 |
Correct |
170 ms |
73396 KB |
Output is correct |
15 |
Execution timed out |
5034 ms |
586544 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |