#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;
#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof... (u) ? ", " : ""), dout (u...);}
#else
#define debug(...) 7122
#endif
#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second
// s[i] lies on path(s[j], t[j]), build an edge(i -> j)
// t[i] lies on path(s[j], t[j]), build an edge(j -> i)
const int SIZE = 1.2e5 + 5;
const int H = __lg(SIZE) + 1;
int n, m;
int s[SIZE], t[SIZE];
vector<int> adj[SIZE];
int sz;
queue<int> q;
vector<int> edge[SIZE * H];
int h[SIZE], to[SIZE][H + 1];
int deg[SIZE * H], in[SIZE][H + 1], out[SIZE][H + 1]; // in : large to small, out small to large
int jump(int pos, int k) {
int cnt = 0;
while (k) {
if (k & 1) pos = to[pos][cnt];
cnt++;
k >>= 1;
}
return pos;
}
int lca(int a, int b) {
if (h[a] < h[b]) swap(a, b);
a = jump(a, h[a] - h[b]);
if (a == b) return a;
for (int i = H; i >= 0; i--) if (to[a][i] != to[b][i]) {
a = to[a][i];
b = to[b][i];
}
return to[a][0];
}
int walk(int a, int b) {
int c = lca(a, b);
if (h[a] > h[c]) return to[a][0];
return jump(b, h[b] - h[a] - 1);
}
int newnode() {
sz++;
edge[sz].clear();
deg[sz] = 0;
return sz;
}
void add(int pos, int k, int ty, int x) {
int cnt = 0;
while (k) {
if (k & 1) {
if (ty == 0) edge[out[pos][cnt]].pb(x);
if (ty == 1) edge[x].pb(in[pos][cnt]);
pos = to[pos][cnt];
}
cnt++;
k >>= 1;
}
}
void add_edge(int x, int a, int b, int ty) {
int c = lca(a, b);
add(a, h[a] - h[c] + 1, ty, x);
add(b, h[b] - h[c] + 1, ty, x);
}
void solve() {
cin >> n;
FOR (i, 1, n) {
adj[i].clear();
edge[i].clear();
deg[i] = 0;
}
FOR (i, 2, n) {
int a, b;
cin >> a >> b;
adj[a].pb(b);
adj[b].pb(a);
}
cin >> m;
FOR (i, 1, m) cin >> s[i] >> t[i];
auto dfs = [&](auto dfs, int pos)->void {
for (int np : adj[pos]) if (np != to[pos][0]) {
h[np] = h[pos] + 1;
to[np][0] = pos;
dfs(dfs, np);
}
};
h[1] = 1;
dfs(dfs, 1);
sz = m;
FOR (i, 1, n) {
in[i][0] = newnode();
out[i][0] = newnode();
}
FOR (j, 1, H) FOR (i, 1, n) {
to[i][j] = to[to[i][j - 1]][j - 1];
in[i][j] = newnode();
out[i][j] = newnode();
edge[in[i][j]].pb(in[i][j - 1]);
edge[out[i][j - 1]].pb(out[i][j]);
if (to[i][j - 1]) {
edge[in[i][j]].pb(in[to[i][j - 1]][j - 1]);
edge[out[to[i][j - 1]][j - 1]].pb(out[i][j]);
}
}
FOR (i, 1, m) {
edge[i].pb(out[s[i]][0]);
edge[in[t[i]][0]].pb(i);
}
FOR (i, 1, m) {
add_edge(i, walk(s[i], t[i]), t[i], 0);
add_edge(i, s[i], walk(t[i], s[i]), 1);
}
FOR (i, 1, sz) for (int j : edge[i]) deg[j]++;
FOR (i, 1, sz) if (!deg[i]) q.push(i);
int cnt = 0;
while (q.size()) {
int pos = q.front();
q.pop();
cnt++;
for (int np : edge[pos]) if (--deg[np] == 0) q.push(np);
}
cout << (cnt == sz ? "Yes" : "No") << '\n';
}
int main() {
Waimai;
int tt;
cin >> tt;
while (tt--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
51028 KB |
Output is correct |
2 |
Correct |
29 ms |
51100 KB |
Output is correct |
3 |
Correct |
29 ms |
51028 KB |
Output is correct |
4 |
Correct |
87 ms |
51572 KB |
Output is correct |
5 |
Correct |
151 ms |
52000 KB |
Output is correct |
6 |
Correct |
35 ms |
51404 KB |
Output is correct |
7 |
Correct |
34 ms |
51456 KB |
Output is correct |
8 |
Correct |
37 ms |
51432 KB |
Output is correct |
9 |
Correct |
275 ms |
61420 KB |
Output is correct |
10 |
Runtime error |
314 ms |
304792 KB |
Execution killed with signal 11 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
51052 KB |
Output is correct |
2 |
Correct |
31 ms |
51036 KB |
Output is correct |
3 |
Correct |
37 ms |
51508 KB |
Output is correct |
4 |
Correct |
36 ms |
51440 KB |
Output is correct |
5 |
Correct |
40 ms |
51508 KB |
Output is correct |
6 |
Correct |
37 ms |
51428 KB |
Output is correct |
7 |
Correct |
36 ms |
51496 KB |
Output is correct |
8 |
Correct |
35 ms |
51512 KB |
Output is correct |
9 |
Correct |
35 ms |
51460 KB |
Output is correct |
10 |
Correct |
38 ms |
51492 KB |
Output is correct |
11 |
Correct |
35 ms |
51424 KB |
Output is correct |
12 |
Correct |
33 ms |
51452 KB |
Output is correct |
13 |
Correct |
32 ms |
51420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
51052 KB |
Output is correct |
2 |
Correct |
31 ms |
51036 KB |
Output is correct |
3 |
Correct |
37 ms |
51508 KB |
Output is correct |
4 |
Correct |
36 ms |
51440 KB |
Output is correct |
5 |
Correct |
40 ms |
51508 KB |
Output is correct |
6 |
Correct |
37 ms |
51428 KB |
Output is correct |
7 |
Correct |
36 ms |
51496 KB |
Output is correct |
8 |
Correct |
35 ms |
51512 KB |
Output is correct |
9 |
Correct |
35 ms |
51460 KB |
Output is correct |
10 |
Correct |
38 ms |
51492 KB |
Output is correct |
11 |
Correct |
35 ms |
51424 KB |
Output is correct |
12 |
Correct |
33 ms |
51452 KB |
Output is correct |
13 |
Correct |
32 ms |
51420 KB |
Output is correct |
14 |
Correct |
29 ms |
51052 KB |
Output is correct |
15 |
Correct |
29 ms |
51036 KB |
Output is correct |
16 |
Correct |
36 ms |
51412 KB |
Output is correct |
17 |
Correct |
38 ms |
51448 KB |
Output is correct |
18 |
Correct |
36 ms |
51404 KB |
Output is correct |
19 |
Correct |
31 ms |
51116 KB |
Output is correct |
20 |
Correct |
37 ms |
51508 KB |
Output is correct |
21 |
Correct |
37 ms |
51412 KB |
Output is correct |
22 |
Correct |
37 ms |
51476 KB |
Output is correct |
23 |
Correct |
29 ms |
51092 KB |
Output is correct |
24 |
Correct |
30 ms |
51284 KB |
Output is correct |
25 |
Correct |
37 ms |
51428 KB |
Output is correct |
26 |
Correct |
31 ms |
51412 KB |
Output is correct |
27 |
Correct |
35 ms |
51396 KB |
Output is correct |
28 |
Correct |
31 ms |
51020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
51052 KB |
Output is correct |
2 |
Correct |
31 ms |
51036 KB |
Output is correct |
3 |
Correct |
37 ms |
51508 KB |
Output is correct |
4 |
Correct |
36 ms |
51440 KB |
Output is correct |
5 |
Correct |
40 ms |
51508 KB |
Output is correct |
6 |
Correct |
37 ms |
51428 KB |
Output is correct |
7 |
Correct |
36 ms |
51496 KB |
Output is correct |
8 |
Correct |
35 ms |
51512 KB |
Output is correct |
9 |
Correct |
35 ms |
51460 KB |
Output is correct |
10 |
Correct |
38 ms |
51492 KB |
Output is correct |
11 |
Correct |
35 ms |
51424 KB |
Output is correct |
12 |
Correct |
33 ms |
51452 KB |
Output is correct |
13 |
Correct |
32 ms |
51420 KB |
Output is correct |
14 |
Correct |
29 ms |
51052 KB |
Output is correct |
15 |
Correct |
29 ms |
51036 KB |
Output is correct |
16 |
Correct |
36 ms |
51412 KB |
Output is correct |
17 |
Correct |
38 ms |
51448 KB |
Output is correct |
18 |
Correct |
36 ms |
51404 KB |
Output is correct |
19 |
Correct |
31 ms |
51116 KB |
Output is correct |
20 |
Correct |
37 ms |
51508 KB |
Output is correct |
21 |
Correct |
37 ms |
51412 KB |
Output is correct |
22 |
Correct |
37 ms |
51476 KB |
Output is correct |
23 |
Correct |
29 ms |
51092 KB |
Output is correct |
24 |
Correct |
30 ms |
51284 KB |
Output is correct |
25 |
Correct |
37 ms |
51428 KB |
Output is correct |
26 |
Correct |
31 ms |
51412 KB |
Output is correct |
27 |
Correct |
35 ms |
51396 KB |
Output is correct |
28 |
Correct |
31 ms |
51020 KB |
Output is correct |
29 |
Correct |
39 ms |
51508 KB |
Output is correct |
30 |
Correct |
40 ms |
51532 KB |
Output is correct |
31 |
Correct |
36 ms |
51548 KB |
Output is correct |
32 |
Correct |
39 ms |
51464 KB |
Output is correct |
33 |
Correct |
37 ms |
51496 KB |
Output is correct |
34 |
Correct |
35 ms |
51448 KB |
Output is correct |
35 |
Correct |
39 ms |
51444 KB |
Output is correct |
36 |
Correct |
34 ms |
51448 KB |
Output is correct |
37 |
Correct |
33 ms |
51436 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
51052 KB |
Output is correct |
2 |
Correct |
31 ms |
51036 KB |
Output is correct |
3 |
Correct |
37 ms |
51508 KB |
Output is correct |
4 |
Correct |
36 ms |
51440 KB |
Output is correct |
5 |
Correct |
40 ms |
51508 KB |
Output is correct |
6 |
Correct |
37 ms |
51428 KB |
Output is correct |
7 |
Correct |
36 ms |
51496 KB |
Output is correct |
8 |
Correct |
35 ms |
51512 KB |
Output is correct |
9 |
Correct |
35 ms |
51460 KB |
Output is correct |
10 |
Correct |
38 ms |
51492 KB |
Output is correct |
11 |
Correct |
35 ms |
51424 KB |
Output is correct |
12 |
Correct |
33 ms |
51452 KB |
Output is correct |
13 |
Correct |
32 ms |
51420 KB |
Output is correct |
14 |
Correct |
29 ms |
51052 KB |
Output is correct |
15 |
Correct |
29 ms |
51036 KB |
Output is correct |
16 |
Correct |
36 ms |
51412 KB |
Output is correct |
17 |
Correct |
38 ms |
51448 KB |
Output is correct |
18 |
Correct |
36 ms |
51404 KB |
Output is correct |
19 |
Correct |
31 ms |
51116 KB |
Output is correct |
20 |
Correct |
37 ms |
51508 KB |
Output is correct |
21 |
Correct |
37 ms |
51412 KB |
Output is correct |
22 |
Correct |
37 ms |
51476 KB |
Output is correct |
23 |
Correct |
29 ms |
51092 KB |
Output is correct |
24 |
Correct |
30 ms |
51284 KB |
Output is correct |
25 |
Correct |
37 ms |
51428 KB |
Output is correct |
26 |
Correct |
31 ms |
51412 KB |
Output is correct |
27 |
Correct |
35 ms |
51396 KB |
Output is correct |
28 |
Correct |
31 ms |
51020 KB |
Output is correct |
29 |
Correct |
39 ms |
51508 KB |
Output is correct |
30 |
Correct |
40 ms |
51532 KB |
Output is correct |
31 |
Correct |
36 ms |
51548 KB |
Output is correct |
32 |
Correct |
39 ms |
51464 KB |
Output is correct |
33 |
Correct |
37 ms |
51496 KB |
Output is correct |
34 |
Correct |
35 ms |
51448 KB |
Output is correct |
35 |
Correct |
39 ms |
51444 KB |
Output is correct |
36 |
Correct |
34 ms |
51448 KB |
Output is correct |
37 |
Correct |
33 ms |
51436 KB |
Output is correct |
38 |
Correct |
276 ms |
61616 KB |
Output is correct |
39 |
Runtime error |
307 ms |
304884 KB |
Execution killed with signal 11 |
40 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
51020 KB |
Output is correct |
2 |
Correct |
30 ms |
51060 KB |
Output is correct |
3 |
Correct |
29 ms |
50992 KB |
Output is correct |
4 |
Correct |
31 ms |
51028 KB |
Output is correct |
5 |
Correct |
50 ms |
51220 KB |
Output is correct |
6 |
Correct |
32 ms |
51344 KB |
Output is correct |
7 |
Correct |
31 ms |
51372 KB |
Output is correct |
8 |
Correct |
29 ms |
51024 KB |
Output is correct |
9 |
Correct |
28 ms |
51112 KB |
Output is correct |
10 |
Correct |
29 ms |
51272 KB |
Output is correct |
11 |
Correct |
29 ms |
51028 KB |
Output is correct |
12 |
Correct |
34 ms |
51428 KB |
Output is correct |
13 |
Correct |
106 ms |
51944 KB |
Output is correct |
14 |
Correct |
175 ms |
52140 KB |
Output is correct |
15 |
Correct |
133 ms |
52020 KB |
Output is correct |
16 |
Runtime error |
338 ms |
298692 KB |
Execution killed with signal 11 |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
51028 KB |
Output is correct |
2 |
Correct |
29 ms |
51100 KB |
Output is correct |
3 |
Correct |
29 ms |
51028 KB |
Output is correct |
4 |
Correct |
87 ms |
51572 KB |
Output is correct |
5 |
Correct |
151 ms |
52000 KB |
Output is correct |
6 |
Correct |
35 ms |
51404 KB |
Output is correct |
7 |
Correct |
34 ms |
51456 KB |
Output is correct |
8 |
Correct |
37 ms |
51432 KB |
Output is correct |
9 |
Correct |
275 ms |
61420 KB |
Output is correct |
10 |
Runtime error |
314 ms |
304792 KB |
Execution killed with signal 11 |
11 |
Halted |
0 ms |
0 KB |
- |