#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<ll, ll>
#define f first
#define s second
#define MX(a,b) a = max(a,b)
#define MN(a,b) a = min(a,b)
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(), (x).end()
#define pb push_back
#define FOR(i,a,b) for (int i = a; i<b; ++i)
#define REP(i,n) FOR(i,0,n)
#define REP1(i,n) FOR(i,1,n+1)
#ifdef BALBIT
#define bug(...) cerr<<"#"<<__LINE__<<": "<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do( T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do( T && x, S && ...y) {cerr<<x<<", "; _do(y...);}
#else
#define bug(...)
#define endl '\n'
#endif // BALBIT
const int maxn = 2e5+5;
vector<int> tree[maxn];
int dep[maxn];
int fa[18][maxn];
int nxt[maxn];
int st[maxn], en[maxn]; // starting or ending???
void dfs(int v, int p) {
if (st[v]!=-1 || en[v]!=-1) {
nxt[v] = v;
}else{
nxt[v] = p == -1? -1 : nxt[p];
}
bug(v, nxt[v]);
for (int u : tree[v]) {
if (u == p) continue;
dep[u] = dep[v] + 1;
fa[0][u] = v;
dfs(u,v);
}
}
int snode[maxn], enode[maxn];
void CLR(int n){
REP(i,n) {
tree[i].clear(); dep[i] = 0;
st[i] = en[i] = -1;
nxt[i] = -1;
}
}
int lca(int a, int b) {
if (dep[a] < dep[b]) swap(a,b);
int need = dep[a] - dep[b];
REP(k, 18) if (need & (1<<k)) a = fa[k][a];
assert(dep[a] == dep[b]);
for (int j = 17; j>=0; --j) {
if (fa[j][a] != fa[j][b]) {
a = fa[j][a]; b = fa[j][b];
}
}
if (a != b) return fa[0][a];
return a;
}
vector<int> g[maxn];
int indeg[maxn];
signed main(){
ios::sync_with_stdio(0), cin.tie(0);
bug(1,2);
int T; cin>>T;
while (T--) {
int n; cin>>n;
CLR(n);
REP(i,n-1) {
int a,b; cin>>a>>b; --a; --b;
tree[a].pb(b); tree[b].pb(a);
}
int q; cin>>q;
REP(i,q) {
cin>>snode[i]>>enode[i]; --snode[i]; --enode[i];
st[snode[i]] = i; en[enode[i]] = i;
}
dfs(0,-1);
REP1(j, 18-1) {
REP(i,n) {
fa[j][i] = fa[j-1][fa[j-1][i]];
}
}
REP(i,q) g[i].clear();
REP(i,q) {
int a = snode[i], b = enode[i];
int c = lca(a,b);
// bool yo = 0;
for (int v : {a,b}) {
v = nxt[v];
while (v != -1 && dep[v] >= dep[c]) {
if (~st[v] && st[v] != i) {
g[i].pb(st[v]);
bug(i, st[v]);
}
if (~en[v] && en[v] != i) {
g[en[v]].pb(i);
bug(en[v], i);
}
if (v == 0) break;
v = nxt[fa[0][v]];
bug(v, fa[0][v], nxt[fa[0][v]]);
}
}
}
REP(i,q) {
indeg[i] = 0;
}
REP(i,q) {
for (int u : g[i]) {
++indeg[u];
}
}
queue<int> qq;
REP(i,q) {
if (indeg[i] == 0) {
qq.push(i);
}
}
int done = 0;
while (!qq.empty() ){
int v = qq.front(); qq.pop();
++done;
for (int u : g[v]) {
if (--indeg[u] == 0) {
qq.push(u);
}
}
}
bug(done);
cout<<(done==q?"Yes":"No")<<endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
6 ms |
9812 KB |
Output is correct |
3 |
Correct |
6 ms |
9812 KB |
Output is correct |
4 |
Correct |
13 ms |
9872 KB |
Output is correct |
5 |
Correct |
22 ms |
9872 KB |
Output is correct |
6 |
Correct |
7 ms |
9896 KB |
Output is correct |
7 |
Correct |
7 ms |
9896 KB |
Output is correct |
8 |
Correct |
7 ms |
9940 KB |
Output is correct |
9 |
Correct |
54 ms |
12748 KB |
Output is correct |
10 |
Correct |
51 ms |
32740 KB |
Output is correct |
11 |
Correct |
11 ms |
9864 KB |
Output is correct |
12 |
Correct |
48 ms |
9952 KB |
Output is correct |
13 |
Correct |
184 ms |
61736 KB |
Output is correct |
14 |
Correct |
198 ms |
75776 KB |
Output is correct |
15 |
Execution timed out |
5103 ms |
1021760 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9812 KB |
Output is correct |
3 |
Correct |
6 ms |
9812 KB |
Output is correct |
4 |
Correct |
7 ms |
9812 KB |
Output is correct |
5 |
Correct |
7 ms |
9864 KB |
Output is correct |
6 |
Correct |
6 ms |
9812 KB |
Output is correct |
7 |
Correct |
8 ms |
9848 KB |
Output is correct |
8 |
Correct |
6 ms |
9812 KB |
Output is correct |
9 |
Correct |
6 ms |
9812 KB |
Output is correct |
10 |
Correct |
6 ms |
9812 KB |
Output is correct |
11 |
Correct |
6 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
9812 KB |
Output is correct |
13 |
Correct |
6 ms |
9884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9812 KB |
Output is correct |
3 |
Correct |
6 ms |
9812 KB |
Output is correct |
4 |
Correct |
7 ms |
9812 KB |
Output is correct |
5 |
Correct |
7 ms |
9864 KB |
Output is correct |
6 |
Correct |
6 ms |
9812 KB |
Output is correct |
7 |
Correct |
8 ms |
9848 KB |
Output is correct |
8 |
Correct |
6 ms |
9812 KB |
Output is correct |
9 |
Correct |
6 ms |
9812 KB |
Output is correct |
10 |
Correct |
6 ms |
9812 KB |
Output is correct |
11 |
Correct |
6 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
9812 KB |
Output is correct |
13 |
Correct |
6 ms |
9884 KB |
Output is correct |
14 |
Correct |
6 ms |
9812 KB |
Output is correct |
15 |
Correct |
5 ms |
9860 KB |
Output is correct |
16 |
Correct |
6 ms |
9812 KB |
Output is correct |
17 |
Correct |
8 ms |
9888 KB |
Output is correct |
18 |
Correct |
7 ms |
9828 KB |
Output is correct |
19 |
Correct |
5 ms |
9772 KB |
Output is correct |
20 |
Correct |
6 ms |
9812 KB |
Output is correct |
21 |
Correct |
6 ms |
9780 KB |
Output is correct |
22 |
Correct |
6 ms |
9812 KB |
Output is correct |
23 |
Correct |
6 ms |
9744 KB |
Output is correct |
24 |
Correct |
6 ms |
9812 KB |
Output is correct |
25 |
Correct |
6 ms |
9812 KB |
Output is correct |
26 |
Correct |
5 ms |
9812 KB |
Output is correct |
27 |
Correct |
8 ms |
9884 KB |
Output is correct |
28 |
Correct |
6 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9812 KB |
Output is correct |
3 |
Correct |
6 ms |
9812 KB |
Output is correct |
4 |
Correct |
7 ms |
9812 KB |
Output is correct |
5 |
Correct |
7 ms |
9864 KB |
Output is correct |
6 |
Correct |
6 ms |
9812 KB |
Output is correct |
7 |
Correct |
8 ms |
9848 KB |
Output is correct |
8 |
Correct |
6 ms |
9812 KB |
Output is correct |
9 |
Correct |
6 ms |
9812 KB |
Output is correct |
10 |
Correct |
6 ms |
9812 KB |
Output is correct |
11 |
Correct |
6 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
9812 KB |
Output is correct |
13 |
Correct |
6 ms |
9884 KB |
Output is correct |
14 |
Correct |
6 ms |
9812 KB |
Output is correct |
15 |
Correct |
5 ms |
9860 KB |
Output is correct |
16 |
Correct |
6 ms |
9812 KB |
Output is correct |
17 |
Correct |
8 ms |
9888 KB |
Output is correct |
18 |
Correct |
7 ms |
9828 KB |
Output is correct |
19 |
Correct |
5 ms |
9772 KB |
Output is correct |
20 |
Correct |
6 ms |
9812 KB |
Output is correct |
21 |
Correct |
6 ms |
9780 KB |
Output is correct |
22 |
Correct |
6 ms |
9812 KB |
Output is correct |
23 |
Correct |
6 ms |
9744 KB |
Output is correct |
24 |
Correct |
6 ms |
9812 KB |
Output is correct |
25 |
Correct |
6 ms |
9812 KB |
Output is correct |
26 |
Correct |
5 ms |
9812 KB |
Output is correct |
27 |
Correct |
8 ms |
9884 KB |
Output is correct |
28 |
Correct |
6 ms |
9812 KB |
Output is correct |
29 |
Correct |
7 ms |
9940 KB |
Output is correct |
30 |
Correct |
7 ms |
9812 KB |
Output is correct |
31 |
Correct |
7 ms |
9912 KB |
Output is correct |
32 |
Correct |
7 ms |
9812 KB |
Output is correct |
33 |
Correct |
6 ms |
9812 KB |
Output is correct |
34 |
Correct |
7 ms |
9912 KB |
Output is correct |
35 |
Correct |
8 ms |
9844 KB |
Output is correct |
36 |
Correct |
6 ms |
9812 KB |
Output is correct |
37 |
Correct |
6 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
9812 KB |
Output is correct |
2 |
Correct |
5 ms |
9812 KB |
Output is correct |
3 |
Correct |
6 ms |
9812 KB |
Output is correct |
4 |
Correct |
7 ms |
9812 KB |
Output is correct |
5 |
Correct |
7 ms |
9864 KB |
Output is correct |
6 |
Correct |
6 ms |
9812 KB |
Output is correct |
7 |
Correct |
8 ms |
9848 KB |
Output is correct |
8 |
Correct |
6 ms |
9812 KB |
Output is correct |
9 |
Correct |
6 ms |
9812 KB |
Output is correct |
10 |
Correct |
6 ms |
9812 KB |
Output is correct |
11 |
Correct |
6 ms |
9812 KB |
Output is correct |
12 |
Correct |
5 ms |
9812 KB |
Output is correct |
13 |
Correct |
6 ms |
9884 KB |
Output is correct |
14 |
Correct |
6 ms |
9812 KB |
Output is correct |
15 |
Correct |
5 ms |
9860 KB |
Output is correct |
16 |
Correct |
6 ms |
9812 KB |
Output is correct |
17 |
Correct |
8 ms |
9888 KB |
Output is correct |
18 |
Correct |
7 ms |
9828 KB |
Output is correct |
19 |
Correct |
5 ms |
9772 KB |
Output is correct |
20 |
Correct |
6 ms |
9812 KB |
Output is correct |
21 |
Correct |
6 ms |
9780 KB |
Output is correct |
22 |
Correct |
6 ms |
9812 KB |
Output is correct |
23 |
Correct |
6 ms |
9744 KB |
Output is correct |
24 |
Correct |
6 ms |
9812 KB |
Output is correct |
25 |
Correct |
6 ms |
9812 KB |
Output is correct |
26 |
Correct |
5 ms |
9812 KB |
Output is correct |
27 |
Correct |
8 ms |
9884 KB |
Output is correct |
28 |
Correct |
6 ms |
9812 KB |
Output is correct |
29 |
Correct |
7 ms |
9940 KB |
Output is correct |
30 |
Correct |
7 ms |
9812 KB |
Output is correct |
31 |
Correct |
7 ms |
9912 KB |
Output is correct |
32 |
Correct |
7 ms |
9812 KB |
Output is correct |
33 |
Correct |
6 ms |
9812 KB |
Output is correct |
34 |
Correct |
7 ms |
9912 KB |
Output is correct |
35 |
Correct |
8 ms |
9844 KB |
Output is correct |
36 |
Correct |
6 ms |
9812 KB |
Output is correct |
37 |
Correct |
6 ms |
9812 KB |
Output is correct |
38 |
Correct |
53 ms |
12852 KB |
Output is correct |
39 |
Correct |
52 ms |
32696 KB |
Output is correct |
40 |
Correct |
45 ms |
11780 KB |
Output is correct |
41 |
Correct |
31 ms |
10632 KB |
Output is correct |
42 |
Correct |
42 ms |
12016 KB |
Output is correct |
43 |
Correct |
25 ms |
10728 KB |
Output is correct |
44 |
Correct |
13 ms |
10124 KB |
Output is correct |
45 |
Correct |
57 ms |
23964 KB |
Output is correct |
46 |
Correct |
61 ms |
23964 KB |
Output is correct |
47 |
Correct |
45 ms |
27628 KB |
Output is correct |
48 |
Correct |
44 ms |
27724 KB |
Output is correct |
49 |
Correct |
51 ms |
24036 KB |
Output is correct |
50 |
Correct |
49 ms |
24108 KB |
Output is correct |
51 |
Correct |
45 ms |
25192 KB |
Output is correct |
52 |
Correct |
53 ms |
25128 KB |
Output is correct |
53 |
Correct |
14 ms |
10836 KB |
Output is correct |
54 |
Correct |
75 ms |
23936 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9860 KB |
Output is correct |
2 |
Correct |
5 ms |
9812 KB |
Output is correct |
3 |
Correct |
5 ms |
9812 KB |
Output is correct |
4 |
Correct |
5 ms |
9812 KB |
Output is correct |
5 |
Correct |
12 ms |
9812 KB |
Output is correct |
6 |
Correct |
6 ms |
9812 KB |
Output is correct |
7 |
Correct |
6 ms |
9812 KB |
Output is correct |
8 |
Correct |
5 ms |
9812 KB |
Output is correct |
9 |
Correct |
7 ms |
9852 KB |
Output is correct |
10 |
Correct |
7 ms |
9940 KB |
Output is correct |
11 |
Correct |
5 ms |
9812 KB |
Output is correct |
12 |
Correct |
7 ms |
9812 KB |
Output is correct |
13 |
Correct |
24 ms |
9884 KB |
Output is correct |
14 |
Correct |
30 ms |
9872 KB |
Output is correct |
15 |
Correct |
28 ms |
9904 KB |
Output is correct |
16 |
Correct |
70 ms |
24492 KB |
Output is correct |
17 |
Correct |
251 ms |
30200 KB |
Output is correct |
18 |
Correct |
539 ms |
50912 KB |
Output is correct |
19 |
Correct |
99 ms |
24588 KB |
Output is correct |
20 |
Correct |
95 ms |
24728 KB |
Output is correct |
21 |
Correct |
91 ms |
24496 KB |
Output is correct |
22 |
Correct |
220 ms |
31104 KB |
Output is correct |
23 |
Correct |
158 ms |
31196 KB |
Output is correct |
24 |
Correct |
185 ms |
31324 KB |
Output is correct |
25 |
Correct |
225 ms |
31604 KB |
Output is correct |
26 |
Correct |
172 ms |
31452 KB |
Output is correct |
27 |
Correct |
358 ms |
29476 KB |
Output is correct |
28 |
Correct |
334 ms |
29452 KB |
Output is correct |
29 |
Correct |
340 ms |
29652 KB |
Output is correct |
30 |
Correct |
205 ms |
26660 KB |
Output is correct |
31 |
Correct |
226 ms |
26724 KB |
Output is correct |
32 |
Correct |
213 ms |
26720 KB |
Output is correct |
33 |
Correct |
195 ms |
26808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
9812 KB |
Output is correct |
2 |
Correct |
6 ms |
9812 KB |
Output is correct |
3 |
Correct |
6 ms |
9812 KB |
Output is correct |
4 |
Correct |
13 ms |
9872 KB |
Output is correct |
5 |
Correct |
22 ms |
9872 KB |
Output is correct |
6 |
Correct |
7 ms |
9896 KB |
Output is correct |
7 |
Correct |
7 ms |
9896 KB |
Output is correct |
8 |
Correct |
7 ms |
9940 KB |
Output is correct |
9 |
Correct |
54 ms |
12748 KB |
Output is correct |
10 |
Correct |
51 ms |
32740 KB |
Output is correct |
11 |
Correct |
11 ms |
9864 KB |
Output is correct |
12 |
Correct |
48 ms |
9952 KB |
Output is correct |
13 |
Correct |
184 ms |
61736 KB |
Output is correct |
14 |
Correct |
198 ms |
75776 KB |
Output is correct |
15 |
Execution timed out |
5103 ms |
1021760 KB |
Time limit exceeded |
16 |
Halted |
0 ms |
0 KB |
- |