#include <bits/stdc++.h>
using namespace std;
#define int long long
void add(vector<pair<int,int>> &v, pair<int,int> x) {
if (v.size() > 0 && x.second+1 == v.back().first) v.back().first = x.first;
else v.push_back(x);
}
const int maxn = 120005, maxN = 500005, lgn = 19, INF = 1e18;
int n, m;
vector<int> ADJ[maxn], adj[maxn];
pair<int,int> a[maxn];
int sz[maxn], fa[maxn], d[maxn], newid[maxn], lim[maxn], cnter = 0;
bool cmp(int x, int y) {
return sz[x] < sz[y];
}
void dfs1(int u) {
sz[u] = 1;
int f = -1;
for (int v:ADJ[u]) {
if (sz[v]) {
f = v;
continue;
}
dfs1(v);
sz[u] += sz[v];
}
if (f!=-1) ADJ[u].erase(find(ADJ[u].begin(), ADJ[u].end(), f));
sort(ADJ[u].begin(), ADJ[u].end(), cmp);
}
void dfs2(int u) {
cnter++;
newid[u] = cnter;
for (int v:ADJ[u]) {
d[cnter+1] = d[newid[u]] + 1;
dfs2(v);
fa[newid[v]] = newid[u];
}
lim[newid[u]] = cnter;
}
pair<int, vector<pair<int,int>>> binlift[maxn][lgn];
void dfs3(int u) {
for (int i=1;i<lgn;i++) {
if (binlift[u][i-1].first==-1 || binlift[binlift[u][i-1].first][i-1].first==-1) break;
binlift[u][i].first = binlift[binlift[u][i-1].first][i-1].first;
binlift[u][i].second = binlift[u][i-1].second;
for (auto [L, R]:binlift[binlift[u][i-1].first][i-1].second) add(binlift[u][i].second, {L, R});
}
for (int v:adj[u]) {
binlift[v][0] = {u, {{u, u}}};
dfs3(v);
}
}
vector<pair<int,int>> PATH(int u, int v) {
if (d[u] > d[v]) swap(u, v);
vector<pair<int,int>> U = {{u, u}}, V = {{v, v}};
for (int i=lgn-1;i>=0;i--) if (d[v] - (1<<i) >= d[u]) {
for (auto [L, R]:binlift[v][i].second) add(V, {L, R});
v = binlift[v][i].first;
}
if (u==v) return V;
for (int i=lgn-1;i>=0;i--) if (binlift[u][i].first != binlift[v][i].first) {
for (auto [L, R]:binlift[u][i].second) add(U, {L, R});
u = binlift[u][i].first;
for (auto [L, R]:binlift[v][i].second) add(V, {L, R});
v = binlift[v][i].first;
}
add(V, {fa[v], fa[v]});
for (auto [L, R]:U) V.push_back({L, R});
return V;
}
set<int> seg[maxN];
void build(int id, int l, int r) {
seg[id].clear();
if (l==r) return;
int mid = (l+r)/2;
build(id*2, l, mid); build(id*2+1, mid+1, r);
}
void update(int id, int l, int r, int findl, int findr, int val, bool add) {
if (r<findl || findr<l) return;
if (findl<=l && r<=findr) {
if (add) seg[id].insert(val);
else seg[id].erase(val);
return;
}
int mid = (l+r)/2;
update(id*2, l, mid, findl, findr, val, add);
update(id*2+1, mid+1, r, findl, findr, val, add);
}
vector<int> nxt;
void qry(int id, int l, int r, int target) {
if (r<target || target<l) return;
for (int i:seg[id]) nxt.push_back(i);
if (l==r) return;
int mid = (l+r)/2;
qry(id*2, l, mid, target); qry(id*2+1, mid+1, r, target);
}
int S[maxn], T[maxn], vis[maxn];
bool die = false;
set<int> exist;
void dfs4(int u) {
int s = a[u].first, t = a[u].second;
vis[u] = 1;
vector<pair<int,int>> path = PATH(s, t);
for (auto [L, R]:path) {
int cur = L-1;
while (exist.size() > 0) {
cur = *exist.upper_bound(cur);
if (cur > R) break;
if (cur==s) continue;
int v = S[cur];
// cout << u << " " << v << endl;
if (!vis[v]) dfs4(v);
else if (vis[v]==1) die = true;
if (die) return;
}
}
nxt.clear();
qry(1, 1, n, t);
for (int v:nxt) if (v!=u) {
// cout << u << " " << v << endl;
if (!vis[v]) dfs4(v);
else if (vis[v]==1) die = true;
if (die) return;
}
for (auto [L, R]:path) update(1, 1, n, L, R, u, 0);
exist.erase(s);
vis[u] = 2;
}
void solve() {
cin >> n;
for (int i=1;i<=n;i++) sz[i] = 0, adj[i].clear(), ADJ[i].clear();
cnter = 0;
for (int i=1;i<=n-1;i++) {
int u, v;
cin >> u >> v;
ADJ[u].push_back(v);
ADJ[v].push_back(u);
}
fa[1] = 0, d[1] = 0;
dfs1(1); dfs2(1);
for (int i=1;i<=n;i++) for (int j:ADJ[i]) adj[newid[i]].push_back(newid[j]);
for (int i=1;i<=n;i++) for (int j=0;j<lgn;j++) binlift[i][j] = {-1, {}};
dfs3(1);
exist = {INF};
build(1, 1, n);
for (int i=1;i<=n;i++) S[i] = -1, T[i] = -1;
cin >> m;
for (int i=1;i<=m;i++) {
int s, t;
cin >> s >> t;
s = newid[s], t = newid[t];
a[i] = {s, t};
S[s] = T[t] = i;
exist.insert(a[i].first);
vector<pair<int,int>> path = PATH(a[i].first, a[i].second);
for (auto [L, R]:path) update(1, 1, n, L, R, i, 1);
}
for (int i=1;i<=m;i++) vis[i] = 0;
die = false;
for (int i=1;i<=m;i++) if (!vis[i]) {
dfs4(i);
if (die) break;
}
if (die) cout << "No\n";
else cout << "Yes\n";
}
signed main() {
ios::sync_with_stdio(0); cin.tie(0);
int t;
cin >> t;
while (t--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
104028 KB |
Output is correct |
2 |
Correct |
20 ms |
103928 KB |
Output is correct |
3 |
Correct |
20 ms |
104028 KB |
Output is correct |
4 |
Correct |
35 ms |
104028 KB |
Output is correct |
5 |
Correct |
51 ms |
104024 KB |
Output is correct |
6 |
Correct |
21 ms |
104028 KB |
Output is correct |
7 |
Correct |
20 ms |
104028 KB |
Output is correct |
8 |
Correct |
21 ms |
104220 KB |
Output is correct |
9 |
Correct |
83 ms |
107856 KB |
Output is correct |
10 |
Correct |
162 ms |
191828 KB |
Output is correct |
11 |
Correct |
35 ms |
104028 KB |
Output is correct |
12 |
Correct |
69 ms |
104140 KB |
Output is correct |
13 |
Correct |
309 ms |
210368 KB |
Output is correct |
14 |
Correct |
274 ms |
210504 KB |
Output is correct |
15 |
Correct |
515 ms |
229280 KB |
Output is correct |
16 |
Correct |
1241 ms |
282344 KB |
Output is correct |
17 |
Correct |
478 ms |
241488 KB |
Output is correct |
18 |
Correct |
288 ms |
214612 KB |
Output is correct |
19 |
Correct |
422 ms |
235596 KB |
Output is correct |
20 |
Correct |
389 ms |
235600 KB |
Output is correct |
21 |
Correct |
471 ms |
241364 KB |
Output is correct |
22 |
Correct |
199 ms |
199336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
104024 KB |
Output is correct |
2 |
Correct |
18 ms |
104028 KB |
Output is correct |
3 |
Correct |
20 ms |
104024 KB |
Output is correct |
4 |
Correct |
21 ms |
104284 KB |
Output is correct |
5 |
Correct |
21 ms |
104024 KB |
Output is correct |
6 |
Correct |
22 ms |
104284 KB |
Output is correct |
7 |
Correct |
27 ms |
104024 KB |
Output is correct |
8 |
Correct |
22 ms |
104284 KB |
Output is correct |
9 |
Correct |
22 ms |
104028 KB |
Output is correct |
10 |
Correct |
21 ms |
104220 KB |
Output is correct |
11 |
Correct |
22 ms |
104284 KB |
Output is correct |
12 |
Correct |
20 ms |
104112 KB |
Output is correct |
13 |
Correct |
19 ms |
104028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
104024 KB |
Output is correct |
2 |
Correct |
18 ms |
104028 KB |
Output is correct |
3 |
Correct |
20 ms |
104024 KB |
Output is correct |
4 |
Correct |
21 ms |
104284 KB |
Output is correct |
5 |
Correct |
21 ms |
104024 KB |
Output is correct |
6 |
Correct |
22 ms |
104284 KB |
Output is correct |
7 |
Correct |
27 ms |
104024 KB |
Output is correct |
8 |
Correct |
22 ms |
104284 KB |
Output is correct |
9 |
Correct |
22 ms |
104028 KB |
Output is correct |
10 |
Correct |
21 ms |
104220 KB |
Output is correct |
11 |
Correct |
22 ms |
104284 KB |
Output is correct |
12 |
Correct |
20 ms |
104112 KB |
Output is correct |
13 |
Correct |
19 ms |
104028 KB |
Output is correct |
14 |
Correct |
19 ms |
104076 KB |
Output is correct |
15 |
Correct |
20 ms |
104024 KB |
Output is correct |
16 |
Correct |
20 ms |
104024 KB |
Output is correct |
17 |
Correct |
21 ms |
104100 KB |
Output is correct |
18 |
Correct |
23 ms |
104284 KB |
Output is correct |
19 |
Correct |
19 ms |
104028 KB |
Output is correct |
20 |
Correct |
23 ms |
104284 KB |
Output is correct |
21 |
Correct |
23 ms |
104280 KB |
Output is correct |
22 |
Correct |
21 ms |
104072 KB |
Output is correct |
23 |
Correct |
20 ms |
104028 KB |
Output is correct |
24 |
Correct |
21 ms |
104024 KB |
Output is correct |
25 |
Correct |
21 ms |
104172 KB |
Output is correct |
26 |
Correct |
19 ms |
104024 KB |
Output is correct |
27 |
Correct |
21 ms |
104108 KB |
Output is correct |
28 |
Correct |
18 ms |
104028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
104024 KB |
Output is correct |
2 |
Correct |
18 ms |
104028 KB |
Output is correct |
3 |
Correct |
20 ms |
104024 KB |
Output is correct |
4 |
Correct |
21 ms |
104284 KB |
Output is correct |
5 |
Correct |
21 ms |
104024 KB |
Output is correct |
6 |
Correct |
22 ms |
104284 KB |
Output is correct |
7 |
Correct |
27 ms |
104024 KB |
Output is correct |
8 |
Correct |
22 ms |
104284 KB |
Output is correct |
9 |
Correct |
22 ms |
104028 KB |
Output is correct |
10 |
Correct |
21 ms |
104220 KB |
Output is correct |
11 |
Correct |
22 ms |
104284 KB |
Output is correct |
12 |
Correct |
20 ms |
104112 KB |
Output is correct |
13 |
Correct |
19 ms |
104028 KB |
Output is correct |
14 |
Correct |
19 ms |
104076 KB |
Output is correct |
15 |
Correct |
20 ms |
104024 KB |
Output is correct |
16 |
Correct |
20 ms |
104024 KB |
Output is correct |
17 |
Correct |
21 ms |
104100 KB |
Output is correct |
18 |
Correct |
23 ms |
104284 KB |
Output is correct |
19 |
Correct |
19 ms |
104028 KB |
Output is correct |
20 |
Correct |
23 ms |
104284 KB |
Output is correct |
21 |
Correct |
23 ms |
104280 KB |
Output is correct |
22 |
Correct |
21 ms |
104072 KB |
Output is correct |
23 |
Correct |
20 ms |
104028 KB |
Output is correct |
24 |
Correct |
21 ms |
104024 KB |
Output is correct |
25 |
Correct |
21 ms |
104172 KB |
Output is correct |
26 |
Correct |
19 ms |
104024 KB |
Output is correct |
27 |
Correct |
21 ms |
104108 KB |
Output is correct |
28 |
Correct |
18 ms |
104028 KB |
Output is correct |
29 |
Correct |
22 ms |
104024 KB |
Output is correct |
30 |
Correct |
24 ms |
104028 KB |
Output is correct |
31 |
Correct |
27 ms |
104540 KB |
Output is correct |
32 |
Correct |
27 ms |
104540 KB |
Output is correct |
33 |
Correct |
22 ms |
104284 KB |
Output is correct |
34 |
Correct |
24 ms |
104024 KB |
Output is correct |
35 |
Correct |
35 ms |
104284 KB |
Output is correct |
36 |
Correct |
21 ms |
104028 KB |
Output is correct |
37 |
Correct |
23 ms |
104028 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
104024 KB |
Output is correct |
2 |
Correct |
18 ms |
104028 KB |
Output is correct |
3 |
Correct |
20 ms |
104024 KB |
Output is correct |
4 |
Correct |
21 ms |
104284 KB |
Output is correct |
5 |
Correct |
21 ms |
104024 KB |
Output is correct |
6 |
Correct |
22 ms |
104284 KB |
Output is correct |
7 |
Correct |
27 ms |
104024 KB |
Output is correct |
8 |
Correct |
22 ms |
104284 KB |
Output is correct |
9 |
Correct |
22 ms |
104028 KB |
Output is correct |
10 |
Correct |
21 ms |
104220 KB |
Output is correct |
11 |
Correct |
22 ms |
104284 KB |
Output is correct |
12 |
Correct |
20 ms |
104112 KB |
Output is correct |
13 |
Correct |
19 ms |
104028 KB |
Output is correct |
14 |
Correct |
19 ms |
104076 KB |
Output is correct |
15 |
Correct |
20 ms |
104024 KB |
Output is correct |
16 |
Correct |
20 ms |
104024 KB |
Output is correct |
17 |
Correct |
21 ms |
104100 KB |
Output is correct |
18 |
Correct |
23 ms |
104284 KB |
Output is correct |
19 |
Correct |
19 ms |
104028 KB |
Output is correct |
20 |
Correct |
23 ms |
104284 KB |
Output is correct |
21 |
Correct |
23 ms |
104280 KB |
Output is correct |
22 |
Correct |
21 ms |
104072 KB |
Output is correct |
23 |
Correct |
20 ms |
104028 KB |
Output is correct |
24 |
Correct |
21 ms |
104024 KB |
Output is correct |
25 |
Correct |
21 ms |
104172 KB |
Output is correct |
26 |
Correct |
19 ms |
104024 KB |
Output is correct |
27 |
Correct |
21 ms |
104108 KB |
Output is correct |
28 |
Correct |
18 ms |
104028 KB |
Output is correct |
29 |
Correct |
22 ms |
104024 KB |
Output is correct |
30 |
Correct |
24 ms |
104028 KB |
Output is correct |
31 |
Correct |
27 ms |
104540 KB |
Output is correct |
32 |
Correct |
27 ms |
104540 KB |
Output is correct |
33 |
Correct |
22 ms |
104284 KB |
Output is correct |
34 |
Correct |
24 ms |
104024 KB |
Output is correct |
35 |
Correct |
35 ms |
104284 KB |
Output is correct |
36 |
Correct |
21 ms |
104028 KB |
Output is correct |
37 |
Correct |
23 ms |
104028 KB |
Output is correct |
38 |
Correct |
101 ms |
107988 KB |
Output is correct |
39 |
Correct |
161 ms |
191824 KB |
Output is correct |
40 |
Correct |
1282 ms |
256620 KB |
Output is correct |
41 |
Correct |
126 ms |
107088 KB |
Output is correct |
42 |
Correct |
80 ms |
107348 KB |
Output is correct |
43 |
Correct |
876 ms |
250452 KB |
Output is correct |
44 |
Incorrect |
64 ms |
104912 KB |
Output isn't correct |
45 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
103968 KB |
Output is correct |
2 |
Correct |
19 ms |
104028 KB |
Output is correct |
3 |
Correct |
19 ms |
104028 KB |
Output is correct |
4 |
Correct |
18 ms |
104028 KB |
Output is correct |
5 |
Correct |
26 ms |
104028 KB |
Output is correct |
6 |
Correct |
19 ms |
103948 KB |
Output is correct |
7 |
Correct |
18 ms |
103984 KB |
Output is correct |
8 |
Correct |
19 ms |
103964 KB |
Output is correct |
9 |
Correct |
19 ms |
104024 KB |
Output is correct |
10 |
Correct |
19 ms |
104028 KB |
Output is correct |
11 |
Correct |
18 ms |
103984 KB |
Output is correct |
12 |
Correct |
22 ms |
104028 KB |
Output is correct |
13 |
Correct |
65 ms |
104088 KB |
Output is correct |
14 |
Correct |
103 ms |
104024 KB |
Output is correct |
15 |
Incorrect |
90 ms |
104024 KB |
Output isn't correct |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
104028 KB |
Output is correct |
2 |
Correct |
20 ms |
103928 KB |
Output is correct |
3 |
Correct |
20 ms |
104028 KB |
Output is correct |
4 |
Correct |
35 ms |
104028 KB |
Output is correct |
5 |
Correct |
51 ms |
104024 KB |
Output is correct |
6 |
Correct |
21 ms |
104028 KB |
Output is correct |
7 |
Correct |
20 ms |
104028 KB |
Output is correct |
8 |
Correct |
21 ms |
104220 KB |
Output is correct |
9 |
Correct |
83 ms |
107856 KB |
Output is correct |
10 |
Correct |
162 ms |
191828 KB |
Output is correct |
11 |
Correct |
35 ms |
104028 KB |
Output is correct |
12 |
Correct |
69 ms |
104140 KB |
Output is correct |
13 |
Correct |
309 ms |
210368 KB |
Output is correct |
14 |
Correct |
274 ms |
210504 KB |
Output is correct |
15 |
Correct |
515 ms |
229280 KB |
Output is correct |
16 |
Correct |
1241 ms |
282344 KB |
Output is correct |
17 |
Correct |
478 ms |
241488 KB |
Output is correct |
18 |
Correct |
288 ms |
214612 KB |
Output is correct |
19 |
Correct |
422 ms |
235596 KB |
Output is correct |
20 |
Correct |
389 ms |
235600 KB |
Output is correct |
21 |
Correct |
471 ms |
241364 KB |
Output is correct |
22 |
Correct |
199 ms |
199336 KB |
Output is correct |
23 |
Correct |
19 ms |
104024 KB |
Output is correct |
24 |
Correct |
18 ms |
104028 KB |
Output is correct |
25 |
Correct |
20 ms |
104024 KB |
Output is correct |
26 |
Correct |
21 ms |
104284 KB |
Output is correct |
27 |
Correct |
21 ms |
104024 KB |
Output is correct |
28 |
Correct |
22 ms |
104284 KB |
Output is correct |
29 |
Correct |
27 ms |
104024 KB |
Output is correct |
30 |
Correct |
22 ms |
104284 KB |
Output is correct |
31 |
Correct |
22 ms |
104028 KB |
Output is correct |
32 |
Correct |
21 ms |
104220 KB |
Output is correct |
33 |
Correct |
22 ms |
104284 KB |
Output is correct |
34 |
Correct |
20 ms |
104112 KB |
Output is correct |
35 |
Correct |
19 ms |
104028 KB |
Output is correct |
36 |
Correct |
19 ms |
104076 KB |
Output is correct |
37 |
Correct |
20 ms |
104024 KB |
Output is correct |
38 |
Correct |
20 ms |
104024 KB |
Output is correct |
39 |
Correct |
21 ms |
104100 KB |
Output is correct |
40 |
Correct |
23 ms |
104284 KB |
Output is correct |
41 |
Correct |
19 ms |
104028 KB |
Output is correct |
42 |
Correct |
23 ms |
104284 KB |
Output is correct |
43 |
Correct |
23 ms |
104280 KB |
Output is correct |
44 |
Correct |
21 ms |
104072 KB |
Output is correct |
45 |
Correct |
20 ms |
104028 KB |
Output is correct |
46 |
Correct |
21 ms |
104024 KB |
Output is correct |
47 |
Correct |
21 ms |
104172 KB |
Output is correct |
48 |
Correct |
19 ms |
104024 KB |
Output is correct |
49 |
Correct |
21 ms |
104108 KB |
Output is correct |
50 |
Correct |
18 ms |
104028 KB |
Output is correct |
51 |
Correct |
22 ms |
104024 KB |
Output is correct |
52 |
Correct |
24 ms |
104028 KB |
Output is correct |
53 |
Correct |
27 ms |
104540 KB |
Output is correct |
54 |
Correct |
27 ms |
104540 KB |
Output is correct |
55 |
Correct |
22 ms |
104284 KB |
Output is correct |
56 |
Correct |
24 ms |
104024 KB |
Output is correct |
57 |
Correct |
35 ms |
104284 KB |
Output is correct |
58 |
Correct |
21 ms |
104028 KB |
Output is correct |
59 |
Correct |
23 ms |
104028 KB |
Output is correct |
60 |
Correct |
101 ms |
107988 KB |
Output is correct |
61 |
Correct |
161 ms |
191824 KB |
Output is correct |
62 |
Correct |
1282 ms |
256620 KB |
Output is correct |
63 |
Correct |
126 ms |
107088 KB |
Output is correct |
64 |
Correct |
80 ms |
107348 KB |
Output is correct |
65 |
Correct |
876 ms |
250452 KB |
Output is correct |
66 |
Incorrect |
64 ms |
104912 KB |
Output isn't correct |
67 |
Halted |
0 ms |
0 KB |
- |