/**
____ ____ ____ ____ ____ ____
||l |||e |||i |||n |||a |||d ||
||__|||__|||__|||__|||__|||__||
|/__\|/__\|/__\|/__\|/__\|/__\|
**/
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 2e5 + 10;
int n, m, s[maxn], t[maxn];
vector < int > adj[maxn];
void input()
{
cin >> n;
for (int i = 1; i < n; i ++)
{
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
cin >> m;
for (int i = 1; i <= m; i ++)
{
cin >> s[i] >> t[i];
}
}
int tin[maxn], tout[maxn], occ[2 * maxn], depth[maxn], timer;
void dfs(int v = 1, int p = -1)
{
tin[v] = ++ timer;
occ[timer] = v;
for (int u : adj[v])
{
if (u == p)
continue;
depth[u] = depth[v] + 1;
dfs(u, v);
occ[++ timer] = v;
}
tout[v] = timer;
}
const int maxlog = 20;
int dp[maxlog][maxn * 2], lg[2 * maxn];
void build_sparse_table()
{
for (int i = 1; i <= timer; i ++)
{
dp[0][i] = occ[i];
lg[i] = lg[i / 2] + 1;
}
for (int j = 1; j < lg[timer]; j ++)
{
for (int i = 1; i <= timer - (1 << j) + 1; i ++)
{
dp[j][i] = dp[j - 1][i + (1 << (j - 1))];
if (depth[dp[j - 1][i]] < depth[dp[j][i]])
dp[j][i] = dp[j - 1][i];
}
}
}
int get_lca(int v, int u)
{
int l = tin[v], r = tin[u];
if (l > r)
swap(l, r);
int len = lg[r - l + 1] - 1;
int lca = dp[len][r - (1 << len) + 1];
if (depth[dp[len][l]] < depth[lca])
lca = dp[len][l];
return lca;
}
vector < int > graph[maxn];
bool is_cycle;
bool in_subtree(int v, int u)
{
return (tin[v] <= tin[u] && tout[v] >= tout[u]);
}
bool on_path(int v, int u, int w)
{
int lca = get_lca(v, u);
if (in_subtree(lca, w) && in_subtree(w, v))
return true;
if (in_subtree(lca, w) && in_subtree(w, u))
return true;
return false;
}
void check_prisoners(int i, int j)
{
if (on_path(s[i], t[i], s[j]) && on_path(s[i], t[i], t[j]))
{
is_cycle = true;
return;
}
if (on_path(s[i], t[i], s[j]))
{
graph[i].push_back(j);
return;
}
if (on_path(s[i], t[i], t[j]))
{
graph[j].push_back(i);
return;
}
}
void build_graph()
{
for (int i = 1; i <= m; i ++)
{
for (int j = 1; j <= m; j ++)
{
if (i != j)
check_prisoners(i, j);
}
}
}
int used[maxn];
void check_dag(int v)
{
used[v] = 1;
for (int u : graph[v])
{
if (used[u] == 2)
continue;
///cout << v << " : " << u << endl;
if (used[u] == 1)
is_cycle = 1;
else
{
check_dag(u);
}
}
used[v] = 2;
}
void check_graph()
{
for (int i = 1; i <= m; i ++)
{
if (!used[i])
check_dag(i);
}
if (is_cycle)
cout << "No" << endl;
else
cout << "Yes" << endl;
}
void clear_data()
{
is_cycle = false;
for (int i = 1; i <= m; i ++)
graph[i].clear(), used[i] = 0;
for (int i = 1; i <= n; i ++)
{
adj[i].clear();
}
timer = 0;
}
void solve()
{
input();
dfs();
build_sparse_table();
build_graph();
check_graph();
clear_data();
}
int main()
{
speed();
int q;
cin >> q;
while(q --)
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20824 KB |
Output is correct |
2 |
Correct |
3 ms |
20824 KB |
Output is correct |
3 |
Correct |
3 ms |
18936 KB |
Output is correct |
4 |
Correct |
11 ms |
27936 KB |
Output is correct |
5 |
Correct |
18 ms |
27736 KB |
Output is correct |
6 |
Correct |
5 ms |
29016 KB |
Output is correct |
7 |
Correct |
5 ms |
29020 KB |
Output is correct |
8 |
Correct |
9 ms |
29272 KB |
Output is correct |
9 |
Correct |
115 ms |
40784 KB |
Output is correct |
10 |
Correct |
42 ms |
59728 KB |
Output is correct |
11 |
Correct |
9 ms |
25176 KB |
Output is correct |
12 |
Correct |
75 ms |
28496 KB |
Output is correct |
13 |
Execution timed out |
5033 ms |
61944 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20824 KB |
Output is correct |
2 |
Correct |
4 ms |
18776 KB |
Output is correct |
3 |
Correct |
5 ms |
29016 KB |
Output is correct |
4 |
Correct |
5 ms |
29172 KB |
Output is correct |
5 |
Correct |
6 ms |
29016 KB |
Output is correct |
6 |
Correct |
5 ms |
29020 KB |
Output is correct |
7 |
Correct |
5 ms |
29016 KB |
Output is correct |
8 |
Correct |
5 ms |
29020 KB |
Output is correct |
9 |
Correct |
5 ms |
29016 KB |
Output is correct |
10 |
Correct |
5 ms |
29016 KB |
Output is correct |
11 |
Correct |
5 ms |
29016 KB |
Output is correct |
12 |
Correct |
4 ms |
29016 KB |
Output is correct |
13 |
Correct |
5 ms |
29220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20824 KB |
Output is correct |
2 |
Correct |
4 ms |
18776 KB |
Output is correct |
3 |
Correct |
5 ms |
29016 KB |
Output is correct |
4 |
Correct |
5 ms |
29172 KB |
Output is correct |
5 |
Correct |
6 ms |
29016 KB |
Output is correct |
6 |
Correct |
5 ms |
29020 KB |
Output is correct |
7 |
Correct |
5 ms |
29016 KB |
Output is correct |
8 |
Correct |
5 ms |
29020 KB |
Output is correct |
9 |
Correct |
5 ms |
29016 KB |
Output is correct |
10 |
Correct |
5 ms |
29016 KB |
Output is correct |
11 |
Correct |
5 ms |
29016 KB |
Output is correct |
12 |
Correct |
4 ms |
29016 KB |
Output is correct |
13 |
Correct |
5 ms |
29220 KB |
Output is correct |
14 |
Correct |
4 ms |
20824 KB |
Output is correct |
15 |
Correct |
3 ms |
20824 KB |
Output is correct |
16 |
Correct |
5 ms |
29016 KB |
Output is correct |
17 |
Correct |
6 ms |
29016 KB |
Output is correct |
18 |
Correct |
5 ms |
29016 KB |
Output is correct |
19 |
Correct |
4 ms |
22872 KB |
Output is correct |
20 |
Correct |
6 ms |
29016 KB |
Output is correct |
21 |
Correct |
5 ms |
29020 KB |
Output is correct |
22 |
Correct |
5 ms |
29016 KB |
Output is correct |
23 |
Correct |
4 ms |
22872 KB |
Output is correct |
24 |
Correct |
4 ms |
26968 KB |
Output is correct |
25 |
Correct |
5 ms |
29020 KB |
Output is correct |
26 |
Correct |
4 ms |
29020 KB |
Output is correct |
27 |
Correct |
6 ms |
29016 KB |
Output is correct |
28 |
Correct |
4 ms |
24924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20824 KB |
Output is correct |
2 |
Correct |
4 ms |
18776 KB |
Output is correct |
3 |
Correct |
5 ms |
29016 KB |
Output is correct |
4 |
Correct |
5 ms |
29172 KB |
Output is correct |
5 |
Correct |
6 ms |
29016 KB |
Output is correct |
6 |
Correct |
5 ms |
29020 KB |
Output is correct |
7 |
Correct |
5 ms |
29016 KB |
Output is correct |
8 |
Correct |
5 ms |
29020 KB |
Output is correct |
9 |
Correct |
5 ms |
29016 KB |
Output is correct |
10 |
Correct |
5 ms |
29016 KB |
Output is correct |
11 |
Correct |
5 ms |
29016 KB |
Output is correct |
12 |
Correct |
4 ms |
29016 KB |
Output is correct |
13 |
Correct |
5 ms |
29220 KB |
Output is correct |
14 |
Correct |
4 ms |
20824 KB |
Output is correct |
15 |
Correct |
3 ms |
20824 KB |
Output is correct |
16 |
Correct |
5 ms |
29016 KB |
Output is correct |
17 |
Correct |
6 ms |
29016 KB |
Output is correct |
18 |
Correct |
5 ms |
29016 KB |
Output is correct |
19 |
Correct |
4 ms |
22872 KB |
Output is correct |
20 |
Correct |
6 ms |
29016 KB |
Output is correct |
21 |
Correct |
5 ms |
29020 KB |
Output is correct |
22 |
Correct |
5 ms |
29016 KB |
Output is correct |
23 |
Correct |
4 ms |
22872 KB |
Output is correct |
24 |
Correct |
4 ms |
26968 KB |
Output is correct |
25 |
Correct |
5 ms |
29020 KB |
Output is correct |
26 |
Correct |
4 ms |
29020 KB |
Output is correct |
27 |
Correct |
6 ms |
29016 KB |
Output is correct |
28 |
Correct |
4 ms |
24924 KB |
Output is correct |
29 |
Correct |
9 ms |
29272 KB |
Output is correct |
30 |
Correct |
7 ms |
29272 KB |
Output is correct |
31 |
Correct |
8 ms |
29272 KB |
Output is correct |
32 |
Correct |
6 ms |
29016 KB |
Output is correct |
33 |
Correct |
6 ms |
29016 KB |
Output is correct |
34 |
Correct |
9 ms |
29272 KB |
Output is correct |
35 |
Correct |
8 ms |
29272 KB |
Output is correct |
36 |
Correct |
9 ms |
29272 KB |
Output is correct |
37 |
Correct |
7 ms |
29016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20824 KB |
Output is correct |
2 |
Correct |
4 ms |
18776 KB |
Output is correct |
3 |
Correct |
5 ms |
29016 KB |
Output is correct |
4 |
Correct |
5 ms |
29172 KB |
Output is correct |
5 |
Correct |
6 ms |
29016 KB |
Output is correct |
6 |
Correct |
5 ms |
29020 KB |
Output is correct |
7 |
Correct |
5 ms |
29016 KB |
Output is correct |
8 |
Correct |
5 ms |
29020 KB |
Output is correct |
9 |
Correct |
5 ms |
29016 KB |
Output is correct |
10 |
Correct |
5 ms |
29016 KB |
Output is correct |
11 |
Correct |
5 ms |
29016 KB |
Output is correct |
12 |
Correct |
4 ms |
29016 KB |
Output is correct |
13 |
Correct |
5 ms |
29220 KB |
Output is correct |
14 |
Correct |
4 ms |
20824 KB |
Output is correct |
15 |
Correct |
3 ms |
20824 KB |
Output is correct |
16 |
Correct |
5 ms |
29016 KB |
Output is correct |
17 |
Correct |
6 ms |
29016 KB |
Output is correct |
18 |
Correct |
5 ms |
29016 KB |
Output is correct |
19 |
Correct |
4 ms |
22872 KB |
Output is correct |
20 |
Correct |
6 ms |
29016 KB |
Output is correct |
21 |
Correct |
5 ms |
29020 KB |
Output is correct |
22 |
Correct |
5 ms |
29016 KB |
Output is correct |
23 |
Correct |
4 ms |
22872 KB |
Output is correct |
24 |
Correct |
4 ms |
26968 KB |
Output is correct |
25 |
Correct |
5 ms |
29020 KB |
Output is correct |
26 |
Correct |
4 ms |
29020 KB |
Output is correct |
27 |
Correct |
6 ms |
29016 KB |
Output is correct |
28 |
Correct |
4 ms |
24924 KB |
Output is correct |
29 |
Correct |
9 ms |
29272 KB |
Output is correct |
30 |
Correct |
7 ms |
29272 KB |
Output is correct |
31 |
Correct |
8 ms |
29272 KB |
Output is correct |
32 |
Correct |
6 ms |
29016 KB |
Output is correct |
33 |
Correct |
6 ms |
29016 KB |
Output is correct |
34 |
Correct |
9 ms |
29272 KB |
Output is correct |
35 |
Correct |
8 ms |
29272 KB |
Output is correct |
36 |
Correct |
9 ms |
29272 KB |
Output is correct |
37 |
Correct |
7 ms |
29016 KB |
Output is correct |
38 |
Correct |
115 ms |
40788 KB |
Output is correct |
39 |
Correct |
42 ms |
59884 KB |
Output is correct |
40 |
Correct |
167 ms |
39988 KB |
Output is correct |
41 |
Correct |
154 ms |
38736 KB |
Output is correct |
42 |
Correct |
148 ms |
40020 KB |
Output is correct |
43 |
Correct |
27 ms |
38736 KB |
Output is correct |
44 |
Correct |
89 ms |
31568 KB |
Output is correct |
45 |
Correct |
47 ms |
49108 KB |
Output is correct |
46 |
Correct |
49 ms |
49232 KB |
Output is correct |
47 |
Correct |
45 ms |
53924 KB |
Output is correct |
48 |
Correct |
44 ms |
53840 KB |
Output is correct |
49 |
Correct |
48 ms |
49232 KB |
Output is correct |
50 |
Correct |
47 ms |
49472 KB |
Output is correct |
51 |
Correct |
38 ms |
50768 KB |
Output is correct |
52 |
Correct |
40 ms |
50768 KB |
Output is correct |
53 |
Correct |
105 ms |
37972 KB |
Output is correct |
54 |
Correct |
48 ms |
49468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20964 KB |
Output is correct |
2 |
Correct |
3 ms |
20828 KB |
Output is correct |
3 |
Correct |
3 ms |
20824 KB |
Output is correct |
4 |
Correct |
3 ms |
18776 KB |
Output is correct |
5 |
Correct |
11 ms |
25176 KB |
Output is correct |
6 |
Correct |
5 ms |
29016 KB |
Output is correct |
7 |
Correct |
5 ms |
29020 KB |
Output is correct |
8 |
Correct |
4 ms |
22872 KB |
Output is correct |
9 |
Correct |
4 ms |
22872 KB |
Output is correct |
10 |
Correct |
4 ms |
26968 KB |
Output is correct |
11 |
Correct |
4 ms |
24920 KB |
Output is correct |
12 |
Correct |
10 ms |
29272 KB |
Output is correct |
13 |
Correct |
67 ms |
27728 KB |
Output is correct |
14 |
Correct |
45 ms |
27984 KB |
Output is correct |
15 |
Correct |
46 ms |
27792 KB |
Output is correct |
16 |
Correct |
2767 ms |
49796 KB |
Output is correct |
17 |
Execution timed out |
5026 ms |
50856 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
20824 KB |
Output is correct |
2 |
Correct |
3 ms |
20824 KB |
Output is correct |
3 |
Correct |
3 ms |
18936 KB |
Output is correct |
4 |
Correct |
11 ms |
27936 KB |
Output is correct |
5 |
Correct |
18 ms |
27736 KB |
Output is correct |
6 |
Correct |
5 ms |
29016 KB |
Output is correct |
7 |
Correct |
5 ms |
29020 KB |
Output is correct |
8 |
Correct |
9 ms |
29272 KB |
Output is correct |
9 |
Correct |
115 ms |
40784 KB |
Output is correct |
10 |
Correct |
42 ms |
59728 KB |
Output is correct |
11 |
Correct |
9 ms |
25176 KB |
Output is correct |
12 |
Correct |
75 ms |
28496 KB |
Output is correct |
13 |
Execution timed out |
5033 ms |
61944 KB |
Time limit exceeded |
14 |
Halted |
0 ms |
0 KB |
- |