#include <bits/stdc++.h>
// #define int long long
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()
#define kill(x) cout << x << "\n", exit(0);
#define pii pair<int, int>
#define endl "\n"
using namespace std;
typedef long long ll;
typedef __int128_t lll;
typedef long double ld;
const int MAXN = (int)1e6 + 7;
const int MOD = 998244353;
const ll INF = (ll)1e9 + 7;
const int LG = 18;
int n, m, k, tmp, t, tmp2, tmp3, tmp4, u, v, w, flag, q, ans, flag2;
pii arr[MAXN]; int par[MAXN], h[MAXN], pow2dad[MAXN][LG], fake_t[MAXN][LG], fake_s[MAXN][LG], ind_s[MAXN], ind_t[MAXN];
vector<int> adj[MAXN], G[MAXN*40];
int seen[MAXN*40];
inline void add_edge(int u, int v) { G[u].pb(v); }
inline int up(int v, int k) { for (int i=LG-1; i>=0; i--) if (k >= (1<<i)) k -= (1<<i), v = pow2dad[v][i]; return v; }
inline int LCA(int u, int v) { if (h[u] < h[v]) swap(u, v); u = up(u, h[u]-h[v]); if (u == v) return u; for (int i=LG-1; i>=0; i--) if (h[u] >= (1<<i) && pow2dad[u][i] != pow2dad[v][i]) u = pow2dad[u][i], v = pow2dad[v][i]; return pow2dad[u][0]; }
void DFS(int v) {
for (int u:adj[v]) {
if (u == par[v]) continue;
par[u] = v; h[u] = h[v]+1; DFS(u);
}
}
void DFS2(int v) {
seen[v] = 1;
for (int u:G[v]) {
if (seen[u] == 0) DFS2(u);
else if (seen[u] == 1) tmp = 0;
}
seen[v] = 2;
}
inline void up_s(int v, int k, int ind) {
// cout << "S : " << v << " " << k << " " << ind << endl;
for (int i=LG-1; i>=0; i--) {
if ((1<<i) <= k) {
k -= (1<<i); add_edge(fake_s[v][i], ind); v = pow2dad[v][i];
}
}
}
inline void up_t(int v, int k, int ind) {
// cout << "T : " << v << " " << k << " " << ind << endl;
for (int i=LG-1; i>=0; i--) {
if ((1<<i) <= k) {
k -= (1<<i); add_edge(ind, fake_t[v][i]); v = pow2dad[v][i];
}
}
}
void solve() {
cin >> n;
for (int i=1; i<n; i++) {
cin >> u >> v;
adj[u].pb(v); adj[v].pb(u);
}
cin >> m;
for (int i=1; i<=m; i++) {
cin >> u >> v;
ind_s[u] = i; ind_t[v] = i; arr[i] = {u, v};
}
DFS(1); flag = n+1;
for (int i=1; i<=n; i++) {
pow2dad[i][0] = par[i];
fake_s[i][0] = flag++; if (ind_s[i]) add_edge(ind_s[i], fake_s[i][0]);
fake_t[i][0] = flag++; if (ind_t[i]) add_edge(fake_t[i][0], ind_t[i]);
}
for (int j=1; j<LG; j++) {
for (int i=1; i<=n; i++) {
pow2dad[i][j] = pow2dad[pow2dad[i][j-1]][j-1];
fake_s[i][j] = flag++; add_edge(fake_s[i][j-1], fake_s[i][j]); add_edge(fake_s[pow2dad[i][j-1]][j-1], fake_s[i][j]);
fake_t[i][j] = flag++; add_edge(fake_t[i][j], fake_t[i][j-1]); add_edge(fake_t[i][j], fake_t[pow2dad[i][j-1]][j-1]);
}
}
for (int i=1; i<=m; i++) {
u = arr[i].F; v = arr[i].S;
int lca = LCA(u, v);
if (lca == u) {
up_s(v, h[v]-h[u], i);
up_t(par[v], h[v]-h[u], i);
} else if (lca == v) {
up_s(par[u], h[u]-h[v], i);
up_t(u, h[u]-h[v], i);
} else {
up_s(par[u], h[u]-h[lca], i); up_t(u, h[u]-h[lca]+1, i);
up_s(v, h[v]-h[lca]+1, i); up_t(par[v], h[v]-h[lca], i);
}
}
tmp = 1;
for (int i=1; i<=n; i++) {
if (seen[i]) continue;
DFS2(i);
if (!tmp) break;
}
cout << (tmp? "Yes\n" : "No\n");
fill_n(seen, flag+7, 0); fill_n(ind_s, flag+7, 0); fill_n(ind_t, flag+7, 0); fill_n(par, flag+7, 0);
for (int i=1; i<=flag+7; i++) adj[i].clear(), G[i].clear(); flag = 0;
}
int32_t main() {
#ifdef LOCAL
freopen("inp.in", "r", stdin);
freopen("res.out", "w", stdout);
#else
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif
cin >> t;
while (t--) solve();
return 0;
}
Compilation message
jail.cpp: In function 'void solve()':
jail.cpp:154:5: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
154 | for (int i=1; i<=flag+7; i++) adj[i].clear(), G[i].clear(); flag = 0;
| ^~~
jail.cpp:154:65: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
154 | for (int i=1; i<=flag+7; i++) adj[i].clear(), G[i].clear(); flag = 0;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
362 ms |
963156 KB |
Output is correct |
2 |
Correct |
372 ms |
963408 KB |
Output is correct |
3 |
Correct |
364 ms |
963152 KB |
Output is correct |
4 |
Correct |
420 ms |
968152 KB |
Output is correct |
5 |
Correct |
422 ms |
972532 KB |
Output is correct |
6 |
Correct |
372 ms |
964160 KB |
Output is correct |
7 |
Correct |
379 ms |
964164 KB |
Output is correct |
8 |
Correct |
376 ms |
964176 KB |
Output is correct |
9 |
Correct |
505 ms |
980416 KB |
Output is correct |
10 |
Runtime error |
465 ms |
1048576 KB |
Execution killed with signal 9 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
363 ms |
963160 KB |
Output is correct |
2 |
Correct |
388 ms |
963152 KB |
Output is correct |
3 |
Correct |
391 ms |
964208 KB |
Output is correct |
4 |
Correct |
386 ms |
964176 KB |
Output is correct |
5 |
Correct |
388 ms |
964104 KB |
Output is correct |
6 |
Correct |
400 ms |
964180 KB |
Output is correct |
7 |
Correct |
394 ms |
964212 KB |
Output is correct |
8 |
Correct |
370 ms |
964180 KB |
Output is correct |
9 |
Correct |
395 ms |
964180 KB |
Output is correct |
10 |
Correct |
376 ms |
964180 KB |
Output is correct |
11 |
Correct |
398 ms |
964180 KB |
Output is correct |
12 |
Correct |
380 ms |
964172 KB |
Output is correct |
13 |
Correct |
386 ms |
964176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
363 ms |
963160 KB |
Output is correct |
2 |
Correct |
388 ms |
963152 KB |
Output is correct |
3 |
Correct |
391 ms |
964208 KB |
Output is correct |
4 |
Correct |
386 ms |
964176 KB |
Output is correct |
5 |
Correct |
388 ms |
964104 KB |
Output is correct |
6 |
Correct |
400 ms |
964180 KB |
Output is correct |
7 |
Correct |
394 ms |
964212 KB |
Output is correct |
8 |
Correct |
370 ms |
964180 KB |
Output is correct |
9 |
Correct |
395 ms |
964180 KB |
Output is correct |
10 |
Correct |
376 ms |
964180 KB |
Output is correct |
11 |
Correct |
398 ms |
964180 KB |
Output is correct |
12 |
Correct |
380 ms |
964172 KB |
Output is correct |
13 |
Correct |
386 ms |
964176 KB |
Output is correct |
14 |
Correct |
366 ms |
963184 KB |
Output is correct |
15 |
Correct |
387 ms |
963328 KB |
Output is correct |
16 |
Correct |
381 ms |
964124 KB |
Output is correct |
17 |
Correct |
377 ms |
964412 KB |
Output is correct |
18 |
Correct |
385 ms |
964344 KB |
Output is correct |
19 |
Correct |
389 ms |
963412 KB |
Output is correct |
20 |
Correct |
392 ms |
964180 KB |
Output is correct |
21 |
Correct |
391 ms |
964144 KB |
Output is correct |
22 |
Correct |
381 ms |
964340 KB |
Output is correct |
23 |
Correct |
404 ms |
963152 KB |
Output is correct |
24 |
Correct |
391 ms |
963664 KB |
Output is correct |
25 |
Correct |
387 ms |
964196 KB |
Output is correct |
26 |
Correct |
381 ms |
963664 KB |
Output is correct |
27 |
Correct |
385 ms |
964484 KB |
Output is correct |
28 |
Correct |
394 ms |
963412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
363 ms |
963160 KB |
Output is correct |
2 |
Correct |
388 ms |
963152 KB |
Output is correct |
3 |
Correct |
391 ms |
964208 KB |
Output is correct |
4 |
Correct |
386 ms |
964176 KB |
Output is correct |
5 |
Correct |
388 ms |
964104 KB |
Output is correct |
6 |
Correct |
400 ms |
964180 KB |
Output is correct |
7 |
Correct |
394 ms |
964212 KB |
Output is correct |
8 |
Correct |
370 ms |
964180 KB |
Output is correct |
9 |
Correct |
395 ms |
964180 KB |
Output is correct |
10 |
Correct |
376 ms |
964180 KB |
Output is correct |
11 |
Correct |
398 ms |
964180 KB |
Output is correct |
12 |
Correct |
380 ms |
964172 KB |
Output is correct |
13 |
Correct |
386 ms |
964176 KB |
Output is correct |
14 |
Correct |
366 ms |
963184 KB |
Output is correct |
15 |
Correct |
387 ms |
963328 KB |
Output is correct |
16 |
Correct |
381 ms |
964124 KB |
Output is correct |
17 |
Correct |
377 ms |
964412 KB |
Output is correct |
18 |
Correct |
385 ms |
964344 KB |
Output is correct |
19 |
Correct |
389 ms |
963412 KB |
Output is correct |
20 |
Correct |
392 ms |
964180 KB |
Output is correct |
21 |
Correct |
391 ms |
964144 KB |
Output is correct |
22 |
Correct |
381 ms |
964340 KB |
Output is correct |
23 |
Correct |
404 ms |
963152 KB |
Output is correct |
24 |
Correct |
391 ms |
963664 KB |
Output is correct |
25 |
Correct |
387 ms |
964196 KB |
Output is correct |
26 |
Correct |
381 ms |
963664 KB |
Output is correct |
27 |
Correct |
385 ms |
964484 KB |
Output is correct |
28 |
Correct |
394 ms |
963412 KB |
Output is correct |
29 |
Correct |
398 ms |
964180 KB |
Output is correct |
30 |
Correct |
387 ms |
964592 KB |
Output is correct |
31 |
Correct |
395 ms |
964176 KB |
Output is correct |
32 |
Correct |
399 ms |
964164 KB |
Output is correct |
33 |
Correct |
393 ms |
964176 KB |
Output is correct |
34 |
Correct |
380 ms |
964040 KB |
Output is correct |
35 |
Correct |
386 ms |
964072 KB |
Output is correct |
36 |
Correct |
382 ms |
964048 KB |
Output is correct |
37 |
Correct |
411 ms |
963924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
363 ms |
963160 KB |
Output is correct |
2 |
Correct |
388 ms |
963152 KB |
Output is correct |
3 |
Correct |
391 ms |
964208 KB |
Output is correct |
4 |
Correct |
386 ms |
964176 KB |
Output is correct |
5 |
Correct |
388 ms |
964104 KB |
Output is correct |
6 |
Correct |
400 ms |
964180 KB |
Output is correct |
7 |
Correct |
394 ms |
964212 KB |
Output is correct |
8 |
Correct |
370 ms |
964180 KB |
Output is correct |
9 |
Correct |
395 ms |
964180 KB |
Output is correct |
10 |
Correct |
376 ms |
964180 KB |
Output is correct |
11 |
Correct |
398 ms |
964180 KB |
Output is correct |
12 |
Correct |
380 ms |
964172 KB |
Output is correct |
13 |
Correct |
386 ms |
964176 KB |
Output is correct |
14 |
Correct |
366 ms |
963184 KB |
Output is correct |
15 |
Correct |
387 ms |
963328 KB |
Output is correct |
16 |
Correct |
381 ms |
964124 KB |
Output is correct |
17 |
Correct |
377 ms |
964412 KB |
Output is correct |
18 |
Correct |
385 ms |
964344 KB |
Output is correct |
19 |
Correct |
389 ms |
963412 KB |
Output is correct |
20 |
Correct |
392 ms |
964180 KB |
Output is correct |
21 |
Correct |
391 ms |
964144 KB |
Output is correct |
22 |
Correct |
381 ms |
964340 KB |
Output is correct |
23 |
Correct |
404 ms |
963152 KB |
Output is correct |
24 |
Correct |
391 ms |
963664 KB |
Output is correct |
25 |
Correct |
387 ms |
964196 KB |
Output is correct |
26 |
Correct |
381 ms |
963664 KB |
Output is correct |
27 |
Correct |
385 ms |
964484 KB |
Output is correct |
28 |
Correct |
394 ms |
963412 KB |
Output is correct |
29 |
Correct |
398 ms |
964180 KB |
Output is correct |
30 |
Correct |
387 ms |
964592 KB |
Output is correct |
31 |
Correct |
395 ms |
964176 KB |
Output is correct |
32 |
Correct |
399 ms |
964164 KB |
Output is correct |
33 |
Correct |
393 ms |
964176 KB |
Output is correct |
34 |
Correct |
380 ms |
964040 KB |
Output is correct |
35 |
Correct |
386 ms |
964072 KB |
Output is correct |
36 |
Correct |
382 ms |
964048 KB |
Output is correct |
37 |
Correct |
411 ms |
963924 KB |
Output is correct |
38 |
Correct |
516 ms |
980416 KB |
Output is correct |
39 |
Runtime error |
484 ms |
1048576 KB |
Execution killed with signal 9 |
40 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
389 ms |
963156 KB |
Output is correct |
2 |
Correct |
393 ms |
963224 KB |
Output is correct |
3 |
Correct |
392 ms |
963292 KB |
Output is correct |
4 |
Correct |
386 ms |
963156 KB |
Output is correct |
5 |
Correct |
386 ms |
965580 KB |
Output is correct |
6 |
Correct |
387 ms |
964176 KB |
Output is correct |
7 |
Correct |
401 ms |
964180 KB |
Output is correct |
8 |
Correct |
392 ms |
963408 KB |
Output is correct |
9 |
Correct |
395 ms |
963228 KB |
Output is correct |
10 |
Correct |
392 ms |
963360 KB |
Output is correct |
11 |
Correct |
380 ms |
963412 KB |
Output is correct |
12 |
Correct |
404 ms |
964044 KB |
Output is correct |
13 |
Correct |
426 ms |
967880 KB |
Output is correct |
14 |
Correct |
446 ms |
972644 KB |
Output is correct |
15 |
Correct |
436 ms |
972472 KB |
Output is correct |
16 |
Runtime error |
492 ms |
1048576 KB |
Execution killed with signal 9 |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
362 ms |
963156 KB |
Output is correct |
2 |
Correct |
372 ms |
963408 KB |
Output is correct |
3 |
Correct |
364 ms |
963152 KB |
Output is correct |
4 |
Correct |
420 ms |
968152 KB |
Output is correct |
5 |
Correct |
422 ms |
972532 KB |
Output is correct |
6 |
Correct |
372 ms |
964160 KB |
Output is correct |
7 |
Correct |
379 ms |
964164 KB |
Output is correct |
8 |
Correct |
376 ms |
964176 KB |
Output is correct |
9 |
Correct |
505 ms |
980416 KB |
Output is correct |
10 |
Runtime error |
465 ms |
1048576 KB |
Execution killed with signal 9 |
11 |
Halted |
0 ms |
0 KB |
- |