답안 #885285

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
885285 2023-12-09T12:06:09 Z Ahmed57 Jail (JOI22_jail) C++17
100 / 100
1404 ms 300128 KB
#include <bits/stdc++.h>
 
using namespace std;
int P[120001][17];
int lola[120001][17];
int lolb[120001][17];
int lolc[120001],dep[120001];
vector<int> adj[120001],tree[120005+120000*34];
    int deg[120005+120000*34] = {0};
void dfs(int i,int pr){
    P[i][0] = pr;
    dep[i] = dep[pr]+1;
    for(int j = 1;j<17;j++){
        P[i][j] = P[P[i][j-1]][j-1];
    }
    for(auto j:adj[i]){
        if(j==pr)continue;
        dfs(j,i);
    }
}
int go(int x,int len){
    for(int i = 16;i>=0;i--){
        if(len>=(1<<i)){
            x = P[x][i];
            len-=(1<<i);
        }
    }
    return x;
}
signed main(){
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    //freopen("input.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int t;cin>>t;while(t--){
    int n;
    cin>>n;
    for(int i = 1;i<=n;i++){
        adj[i].clear();
    }
    for(int i = 1;i<n;i++){
        int a,b;
        cin>>a>>b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    int q;cin>>q;
    vector<pair<int,int>> v;v.push_back({0,0});
    for(int i = 1;i<=q;i++){
        int a,b;cin>>a>>b;
        v.push_back({a,b});
    }
    dfs(1,0);
    int NODES = 0;
    for(int i = 1;i<=q;i++){
        lolc[i] = ++NODES;
        //cout<<lolc[i]<<endl;
    }
    for(int i = 1;i<=n;i++){
        for(int j = 0;j<=16;j++){
            lola[i][j] = ++NODES;
            lolb[i][j] = ++NODES;
        }
    }
    for(int i = 1;i<=n;i++){
        for(int j = 1;j<=16;j++){
            tree[lolb[i][j]].push_back(lolb[i][j-1]);
            tree[lolb[i][j]].push_back(lolb[P[i][j-1]][j-1]);
            tree[lola[i][j-1]].push_back(lola[i][j]);
            tree[lola[P[i][j-1]][j-1]].push_back(lola[i][j]);            
        }
    }
    //cout<<tree[0].size()<<endl;
    for(int i = 1;i<=q;i++){
        tree[lolc[i]].push_back(lola[v[i].first][0]);
        tree[lolb[v[i].second][0]].push_back(lolc[i]);
    }
    for(int i = 1;i<=q;i++){
        int x,y;
        x = v[i].first , y = v[i].second;
        if(dep[x]>dep[y]&&go(x,dep[x]-dep[y])==y){
            y=go(x,dep[x]-dep[y]-1);
        }else y = P[y][0];
        //cout<<x<<" "<<y<<endl;
        if(dep[x]<dep[y])swap(x,y);
        for(int j =16;j>=0;j--){
            if(dep[x]-(1<<j)>=dep[y]){
                tree[lolc[i]].push_back(lolb[x][j]);
                x = P[x][j];
            }
        }
        for(int j = 16;j>=0;j--){
            if(P[x][j]!=P[y][j]){
                tree[lolc[i]].push_back(lolb[x][j]);
                tree[lolc[i]].push_back(lolb[y][j]);
                x = P[x][j];y = P[y][j];
            }
        }
        tree[lolc[i]].push_back(lolb[x][0]);
        if(x!=y)tree[lolc[i]].push_back(lolb[y][1]);
        x = v[i].first , y = v[i].second;
        if(dep[y]>dep[x]&&go(y,dep[y]-dep[x])==x){
            x=go(y,dep[y]-dep[x]-1);
        }else x = P[x][0];
        if(dep[x]<dep[y])swap(x,y);
        for(int j = 16;j>=0;j--){
            if(dep[x]-(1<<j)>=dep[y]){
                tree[lola[x][j]].push_back(lolc[i]);
                x = P[x][j];
            }
        }
        for(int j =16;j>=0;j--){
            if(P[x][j]!=P[y][j]){
                tree[lola[x][j]].push_back(lolc[i]);
                tree[lola[y][j]].push_back(lolc[i]);
                x = P[x][j];y = P[y][j];
            }
        }
        tree[lola[x][0]].push_back(lolc[i]);
        if(x!=y){tree[lola[y][1]].push_back(lolc[i]);}
    }
    //cout<<"ggg"<<endl;
    tree[0].clear();
    for(int i = 0;i<=NODES;i++){
        for(auto j:tree[i]){
            //if(j>NODES||j==0)assert(0);
            deg[j]++;
        }
    }
    queue<int> qe;
    for(int i = 0;i<=NODES;i++){
        if(deg[i]==0){
            qe.push(i);
        }
    }
    int cnt = 0;
    while(!qe.empty()){
        int x = qe.front();qe.pop();
        cnt++;
        for(auto j:tree[x]){
            deg[j]--;
            if(deg[j]==0)qe.push(j);
        }
    }
    for(int i = 0;i<=NODES;i++){
        //cout<<vis[i]<<" "<<i<<endl;
    }
    if(cnt==NODES+1)cout<<"Yes\n";
    else cout<<"No\n";
    for(int i = 0;i<=NODES;i++){tree[i].clear();deg[i] = 0;}
}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 109144 KB Output is correct
2 Correct 23 ms 109148 KB Output is correct
3 Correct 23 ms 109128 KB Output is correct
4 Correct 56 ms 109144 KB Output is correct
5 Correct 93 ms 109352 KB Output is correct
6 Correct 26 ms 109404 KB Output is correct
7 Correct 28 ms 109404 KB Output is correct
8 Correct 28 ms 109372 KB Output is correct
9 Correct 188 ms 120132 KB Output is correct
10 Correct 820 ms 274036 KB Output is correct
11 Correct 38 ms 109148 KB Output is correct
12 Correct 111 ms 110164 KB Output is correct
13 Correct 1167 ms 281192 KB Output is correct
14 Correct 943 ms 281100 KB Output is correct
15 Correct 1067 ms 283168 KB Output is correct
16 Correct 1404 ms 296096 KB Output is correct
17 Correct 1249 ms 285124 KB Output is correct
18 Correct 1082 ms 285128 KB Output is correct
19 Correct 1237 ms 284796 KB Output is correct
20 Correct 1102 ms 284936 KB Output is correct
21 Correct 943 ms 284516 KB Output is correct
22 Correct 939 ms 280136 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 109148 KB Output is correct
2 Correct 23 ms 109068 KB Output is correct
3 Correct 26 ms 109404 KB Output is correct
4 Correct 27 ms 109360 KB Output is correct
5 Correct 26 ms 109384 KB Output is correct
6 Correct 27 ms 109480 KB Output is correct
7 Correct 27 ms 109392 KB Output is correct
8 Correct 27 ms 109404 KB Output is correct
9 Correct 27 ms 109404 KB Output is correct
10 Correct 28 ms 109400 KB Output is correct
11 Correct 27 ms 109404 KB Output is correct
12 Correct 27 ms 109400 KB Output is correct
13 Correct 25 ms 109400 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 109148 KB Output is correct
2 Correct 23 ms 109068 KB Output is correct
3 Correct 26 ms 109404 KB Output is correct
4 Correct 27 ms 109360 KB Output is correct
5 Correct 26 ms 109384 KB Output is correct
6 Correct 27 ms 109480 KB Output is correct
7 Correct 27 ms 109392 KB Output is correct
8 Correct 27 ms 109404 KB Output is correct
9 Correct 27 ms 109404 KB Output is correct
10 Correct 28 ms 109400 KB Output is correct
11 Correct 27 ms 109404 KB Output is correct
12 Correct 27 ms 109400 KB Output is correct
13 Correct 25 ms 109400 KB Output is correct
14 Correct 24 ms 109148 KB Output is correct
15 Correct 26 ms 109092 KB Output is correct
16 Correct 27 ms 109404 KB Output is correct
17 Correct 28 ms 109564 KB Output is correct
18 Correct 27 ms 109536 KB Output is correct
19 Correct 24 ms 109052 KB Output is correct
20 Correct 34 ms 109404 KB Output is correct
21 Correct 27 ms 109516 KB Output is correct
22 Correct 29 ms 109404 KB Output is correct
23 Correct 24 ms 109148 KB Output is correct
24 Correct 24 ms 109148 KB Output is correct
25 Correct 33 ms 109404 KB Output is correct
26 Correct 25 ms 109404 KB Output is correct
27 Correct 33 ms 109352 KB Output is correct
28 Correct 24 ms 109144 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 109148 KB Output is correct
2 Correct 23 ms 109068 KB Output is correct
3 Correct 26 ms 109404 KB Output is correct
4 Correct 27 ms 109360 KB Output is correct
5 Correct 26 ms 109384 KB Output is correct
6 Correct 27 ms 109480 KB Output is correct
7 Correct 27 ms 109392 KB Output is correct
8 Correct 27 ms 109404 KB Output is correct
9 Correct 27 ms 109404 KB Output is correct
10 Correct 28 ms 109400 KB Output is correct
11 Correct 27 ms 109404 KB Output is correct
12 Correct 27 ms 109400 KB Output is correct
13 Correct 25 ms 109400 KB Output is correct
14 Correct 24 ms 109148 KB Output is correct
15 Correct 26 ms 109092 KB Output is correct
16 Correct 27 ms 109404 KB Output is correct
17 Correct 28 ms 109564 KB Output is correct
18 Correct 27 ms 109536 KB Output is correct
19 Correct 24 ms 109052 KB Output is correct
20 Correct 34 ms 109404 KB Output is correct
21 Correct 27 ms 109516 KB Output is correct
22 Correct 29 ms 109404 KB Output is correct
23 Correct 24 ms 109148 KB Output is correct
24 Correct 24 ms 109148 KB Output is correct
25 Correct 33 ms 109404 KB Output is correct
26 Correct 25 ms 109404 KB Output is correct
27 Correct 33 ms 109352 KB Output is correct
28 Correct 24 ms 109144 KB Output is correct
29 Correct 28 ms 109404 KB Output is correct
30 Correct 31 ms 109404 KB Output is correct
31 Correct 28 ms 109416 KB Output is correct
32 Correct 31 ms 109400 KB Output is correct
33 Correct 27 ms 109404 KB Output is correct
34 Correct 27 ms 109260 KB Output is correct
35 Correct 27 ms 109404 KB Output is correct
36 Correct 27 ms 109404 KB Output is correct
37 Correct 26 ms 109528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 109148 KB Output is correct
2 Correct 23 ms 109068 KB Output is correct
3 Correct 26 ms 109404 KB Output is correct
4 Correct 27 ms 109360 KB Output is correct
5 Correct 26 ms 109384 KB Output is correct
6 Correct 27 ms 109480 KB Output is correct
7 Correct 27 ms 109392 KB Output is correct
8 Correct 27 ms 109404 KB Output is correct
9 Correct 27 ms 109404 KB Output is correct
10 Correct 28 ms 109400 KB Output is correct
11 Correct 27 ms 109404 KB Output is correct
12 Correct 27 ms 109400 KB Output is correct
13 Correct 25 ms 109400 KB Output is correct
14 Correct 24 ms 109148 KB Output is correct
15 Correct 26 ms 109092 KB Output is correct
16 Correct 27 ms 109404 KB Output is correct
17 Correct 28 ms 109564 KB Output is correct
18 Correct 27 ms 109536 KB Output is correct
19 Correct 24 ms 109052 KB Output is correct
20 Correct 34 ms 109404 KB Output is correct
21 Correct 27 ms 109516 KB Output is correct
22 Correct 29 ms 109404 KB Output is correct
23 Correct 24 ms 109148 KB Output is correct
24 Correct 24 ms 109148 KB Output is correct
25 Correct 33 ms 109404 KB Output is correct
26 Correct 25 ms 109404 KB Output is correct
27 Correct 33 ms 109352 KB Output is correct
28 Correct 24 ms 109144 KB Output is correct
29 Correct 28 ms 109404 KB Output is correct
30 Correct 31 ms 109404 KB Output is correct
31 Correct 28 ms 109416 KB Output is correct
32 Correct 31 ms 109400 KB Output is correct
33 Correct 27 ms 109404 KB Output is correct
34 Correct 27 ms 109260 KB Output is correct
35 Correct 27 ms 109404 KB Output is correct
36 Correct 27 ms 109404 KB Output is correct
37 Correct 26 ms 109528 KB Output is correct
38 Correct 290 ms 120428 KB Output is correct
39 Correct 875 ms 274180 KB Output is correct
40 Correct 232 ms 122824 KB Output is correct
41 Correct 237 ms 121800 KB Output is correct
42 Correct 213 ms 122364 KB Output is correct
43 Correct 311 ms 122560 KB Output is correct
44 Correct 45 ms 110684 KB Output is correct
45 Correct 750 ms 276672 KB Output is correct
46 Correct 751 ms 277420 KB Output is correct
47 Correct 881 ms 273024 KB Output is correct
48 Correct 849 ms 273116 KB Output is correct
49 Correct 641 ms 278540 KB Output is correct
50 Correct 665 ms 277072 KB Output is correct
51 Correct 732 ms 276308 KB Output is correct
52 Correct 738 ms 276412 KB Output is correct
53 Correct 75 ms 123080 KB Output is correct
54 Correct 944 ms 278580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 109144 KB Output is correct
2 Correct 22 ms 109144 KB Output is correct
3 Correct 23 ms 109112 KB Output is correct
4 Correct 23 ms 109148 KB Output is correct
5 Correct 38 ms 109120 KB Output is correct
6 Correct 24 ms 109404 KB Output is correct
7 Correct 26 ms 109624 KB Output is correct
8 Correct 23 ms 109144 KB Output is correct
9 Correct 23 ms 109148 KB Output is correct
10 Correct 23 ms 109148 KB Output is correct
11 Correct 22 ms 109140 KB Output is correct
12 Correct 26 ms 109404 KB Output is correct
13 Correct 72 ms 109368 KB Output is correct
14 Correct 118 ms 109648 KB Output is correct
15 Correct 89 ms 109396 KB Output is correct
16 Correct 816 ms 278192 KB Output is correct
17 Correct 762 ms 287416 KB Output is correct
18 Correct 898 ms 297580 KB Output is correct
19 Correct 813 ms 281652 KB Output is correct
20 Correct 855 ms 281488 KB Output is correct
21 Correct 843 ms 281528 KB Output is correct
22 Correct 813 ms 288380 KB Output is correct
23 Correct 693 ms 288180 KB Output is correct
24 Correct 853 ms 288768 KB Output is correct
25 Correct 846 ms 288652 KB Output is correct
26 Correct 889 ms 287668 KB Output is correct
27 Correct 771 ms 287436 KB Output is correct
28 Correct 603 ms 288292 KB Output is correct
29 Correct 619 ms 288048 KB Output is correct
30 Correct 786 ms 283444 KB Output is correct
31 Correct 734 ms 282088 KB Output is correct
32 Correct 782 ms 282176 KB Output is correct
33 Correct 628 ms 282664 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 28 ms 109144 KB Output is correct
2 Correct 23 ms 109148 KB Output is correct
3 Correct 23 ms 109128 KB Output is correct
4 Correct 56 ms 109144 KB Output is correct
5 Correct 93 ms 109352 KB Output is correct
6 Correct 26 ms 109404 KB Output is correct
7 Correct 28 ms 109404 KB Output is correct
8 Correct 28 ms 109372 KB Output is correct
9 Correct 188 ms 120132 KB Output is correct
10 Correct 820 ms 274036 KB Output is correct
11 Correct 38 ms 109148 KB Output is correct
12 Correct 111 ms 110164 KB Output is correct
13 Correct 1167 ms 281192 KB Output is correct
14 Correct 943 ms 281100 KB Output is correct
15 Correct 1067 ms 283168 KB Output is correct
16 Correct 1404 ms 296096 KB Output is correct
17 Correct 1249 ms 285124 KB Output is correct
18 Correct 1082 ms 285128 KB Output is correct
19 Correct 1237 ms 284796 KB Output is correct
20 Correct 1102 ms 284936 KB Output is correct
21 Correct 943 ms 284516 KB Output is correct
22 Correct 939 ms 280136 KB Output is correct
23 Correct 24 ms 109148 KB Output is correct
24 Correct 23 ms 109068 KB Output is correct
25 Correct 26 ms 109404 KB Output is correct
26 Correct 27 ms 109360 KB Output is correct
27 Correct 26 ms 109384 KB Output is correct
28 Correct 27 ms 109480 KB Output is correct
29 Correct 27 ms 109392 KB Output is correct
30 Correct 27 ms 109404 KB Output is correct
31 Correct 27 ms 109404 KB Output is correct
32 Correct 28 ms 109400 KB Output is correct
33 Correct 27 ms 109404 KB Output is correct
34 Correct 27 ms 109400 KB Output is correct
35 Correct 25 ms 109400 KB Output is correct
36 Correct 24 ms 109148 KB Output is correct
37 Correct 26 ms 109092 KB Output is correct
38 Correct 27 ms 109404 KB Output is correct
39 Correct 28 ms 109564 KB Output is correct
40 Correct 27 ms 109536 KB Output is correct
41 Correct 24 ms 109052 KB Output is correct
42 Correct 34 ms 109404 KB Output is correct
43 Correct 27 ms 109516 KB Output is correct
44 Correct 29 ms 109404 KB Output is correct
45 Correct 24 ms 109148 KB Output is correct
46 Correct 24 ms 109148 KB Output is correct
47 Correct 33 ms 109404 KB Output is correct
48 Correct 25 ms 109404 KB Output is correct
49 Correct 33 ms 109352 KB Output is correct
50 Correct 24 ms 109144 KB Output is correct
51 Correct 28 ms 109404 KB Output is correct
52 Correct 31 ms 109404 KB Output is correct
53 Correct 28 ms 109416 KB Output is correct
54 Correct 31 ms 109400 KB Output is correct
55 Correct 27 ms 109404 KB Output is correct
56 Correct 27 ms 109260 KB Output is correct
57 Correct 27 ms 109404 KB Output is correct
58 Correct 27 ms 109404 KB Output is correct
59 Correct 26 ms 109528 KB Output is correct
60 Correct 290 ms 120428 KB Output is correct
61 Correct 875 ms 274180 KB Output is correct
62 Correct 232 ms 122824 KB Output is correct
63 Correct 237 ms 121800 KB Output is correct
64 Correct 213 ms 122364 KB Output is correct
65 Correct 311 ms 122560 KB Output is correct
66 Correct 45 ms 110684 KB Output is correct
67 Correct 750 ms 276672 KB Output is correct
68 Correct 751 ms 277420 KB Output is correct
69 Correct 881 ms 273024 KB Output is correct
70 Correct 849 ms 273116 KB Output is correct
71 Correct 641 ms 278540 KB Output is correct
72 Correct 665 ms 277072 KB Output is correct
73 Correct 732 ms 276308 KB Output is correct
74 Correct 738 ms 276412 KB Output is correct
75 Correct 75 ms 123080 KB Output is correct
76 Correct 944 ms 278580 KB Output is correct
77 Correct 23 ms 109144 KB Output is correct
78 Correct 22 ms 109144 KB Output is correct
79 Correct 23 ms 109112 KB Output is correct
80 Correct 23 ms 109148 KB Output is correct
81 Correct 38 ms 109120 KB Output is correct
82 Correct 24 ms 109404 KB Output is correct
83 Correct 26 ms 109624 KB Output is correct
84 Correct 23 ms 109144 KB Output is correct
85 Correct 23 ms 109148 KB Output is correct
86 Correct 23 ms 109148 KB Output is correct
87 Correct 22 ms 109140 KB Output is correct
88 Correct 26 ms 109404 KB Output is correct
89 Correct 72 ms 109368 KB Output is correct
90 Correct 118 ms 109648 KB Output is correct
91 Correct 89 ms 109396 KB Output is correct
92 Correct 816 ms 278192 KB Output is correct
93 Correct 762 ms 287416 KB Output is correct
94 Correct 898 ms 297580 KB Output is correct
95 Correct 813 ms 281652 KB Output is correct
96 Correct 855 ms 281488 KB Output is correct
97 Correct 843 ms 281528 KB Output is correct
98 Correct 813 ms 288380 KB Output is correct
99 Correct 693 ms 288180 KB Output is correct
100 Correct 853 ms 288768 KB Output is correct
101 Correct 846 ms 288652 KB Output is correct
102 Correct 889 ms 287668 KB Output is correct
103 Correct 771 ms 287436 KB Output is correct
104 Correct 603 ms 288292 KB Output is correct
105 Correct 619 ms 288048 KB Output is correct
106 Correct 786 ms 283444 KB Output is correct
107 Correct 734 ms 282088 KB Output is correct
108 Correct 782 ms 282176 KB Output is correct
109 Correct 628 ms 282664 KB Output is correct
110 Correct 118 ms 110420 KB Output is correct
111 Correct 90 ms 110004 KB Output is correct
112 Correct 905 ms 287984 KB Output is correct
113 Correct 954 ms 277824 KB Output is correct
114 Correct 837 ms 286100 KB Output is correct
115 Correct 581 ms 278200 KB Output is correct
116 Correct 896 ms 284804 KB Output is correct
117 Correct 1026 ms 300128 KB Output is correct
118 Correct 896 ms 277648 KB Output is correct
119 Correct 872 ms 279104 KB Output is correct
120 Correct 75 ms 127312 KB Output is correct
121 Correct 1018 ms 287420 KB Output is correct
122 Correct 997 ms 285704 KB Output is correct
123 Correct 1000 ms 278004 KB Output is correct
124 Correct 821 ms 277964 KB Output is correct
125 Correct 1024 ms 278476 KB Output is correct
126 Correct 1382 ms 299588 KB Output is correct
127 Correct 1124 ms 292520 KB Output is correct
128 Correct 897 ms 289696 KB Output is correct
129 Correct 1145 ms 293600 KB Output is correct
130 Correct 989 ms 293832 KB Output is correct