답안 #863649

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
863649 2023-10-20T16:32:02 Z lolismek Jail (JOI22_jail) C++14
100 / 100
1728 ms 355708 KB
#include <iostream>
#include <vector>

#define pii pair <int, int>

using namespace std;

const int NMAX = 120000;
const int LG = 17;

vector <int> adj[NMAX + 1];
pii ch[NMAX + 1];

int lvl[NMAX + 1];
int par[NMAX + 1][LG + 1];

int id1[NMAX + 1][LG + 1];
int id2[NMAX + 1][LG + 1];

void dfs(int node, int parent){
    lvl[node] = lvl[parent] + 1;
    par[node][0] = parent;

    for(int child : adj[node]){
        if(child != parent){
            dfs(child, node);
        }
    }
}

vector <int> G[2 * LG * NMAX + NMAX + 1];

int LCA(int a, int b){
    if(lvl[a] < lvl[b]){
        swap(a, b);
    }

    int diff = lvl[a] - lvl[b];
    for(int i = LG; i >= 0; i--){
        if((1 << i) & diff){
            a = par[a][i];
        }
    }

    if(a == b){
        return a;
    }

    for(int i = LG; i >= 0; i--){
        if(par[a][i] != par[b][i]){
            a = par[a][i];
            b = par[b][i];
        }
    }

    return par[a][0];
}

int stat[2 * LG * NMAX + NMAX + 1];

bool cyc = false;
void dfsg(int node){
    stat[node] = 1;

    for(int vec : G[node]){
        if(stat[vec] == 0){
            dfsg(vec);
        }else if(stat[vec] == 1){
            cyc = true;
        }
    }

    stat[node] = 2;
}

void solve(){
    int n;
    cin >> n;

    for(int i = 1; i <= n; i++){
        adj[i].clear();
    }

    for(int i = 1; i <= n - 1; i++){
        int a, b;
        cin >> a >> b;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    int m;
    cin >> m;

    for(int i = 1; i <= m; i++){
        cin >> ch[i].first >> ch[i].second;
    }

    dfs(1, 0);

    for(int j = 1; (1 << j) <= n; j++){
        for(int i = 1; i <= n; i++){
            par[i][j] = par[par[i][j - 1]][j - 1];
        }
    }

    int lbl = m;
    for(int i = 1; i <= n; i++){
        for(int j = 0; (1 << j) <= lvl[i]; j++){
            id1[i][j] = ++lbl;
        }
    }

    for(int i = 1; i <= n; i++){
        for(int j = 0; (1 << j) <= lvl[i]; j++){
            id2[i][j] = ++lbl;
        }
    }

    for(int i = 1; i <= lbl; i++){
        G[i].clear();
    }

    // type 1
    for(int i = 1; i <= n; i++){
        for(int j = 1; (1 << j) <= lvl[i]; j++){
            G[id1[i][j - 1]].push_back(id1[i][j]);
            G[id1[par[i][j - 1]][j - 1]].push_back(id1[i][j]);
        }
    }

    //type 2
    for(int i = 1; i <= n; i++){
        for(int j = 1; (1 << j) <= lvl[i]; j++){
            G[id2[i][j]].push_back(id2[i][j - 1]);
            G[id2[i][j]].push_back(id2[par[i][j - 1]][j - 1]);
        }
    }

    for(int i = 1; i <= m; i++){
        int lca = LCA(ch[i].first, ch[i].second);

        // type 1
        G[i].push_back(id1[ch[i].first][0]);

        int node = par[ch[i].first][0];
        if(node != 0){
            for(int j = LG; j >= 0; j--){
                if((1 << j ) <= lvl[node] && lvl[par[node][j]] + 1 >= lvl[lca]){
                    G[id1[node][j]].push_back(i);
                    node = par[node][j];
                }
            }
        }
        node = ch[i].second;
        for(int j = LG; j >= 0; j--){
            if((1 << j ) <= lvl[node] && lvl[par[node][j]] + 1 > lvl[lca]){
                G[id1[node][j]].push_back(i);
                node = par[node][j];
            }
        }

        // type 2
        G[id2[ch[i].second][0]].push_back(i);

        node = ch[i].first;
        for(int j = LG; j >= 0; j--){
            if((1 << j ) <= lvl[node] && lvl[par[node][j]] + 1 > lvl[lca]){
                G[i].push_back(id2[node][j]);
                node = par[node][j];
            }
        }
        node = par[ch[i].second][0];
        if(node != 0){
            for(int j = LG; j >= 0; j--){
                if((1 << j ) <= lvl[node] && lvl[par[node][j]] + 1 >= lvl[lca]){
                    G[i].push_back(id2[node][j]);
                    node = par[node][j];
                }
            }
        }
    }

    for(int i = 1; i <= lbl; i++){
        stat[i] = 0;
    }

    cyc = false;
    for(int i = 1; i <= lbl && !cyc; i++){
        if(stat[i] == 0){
            dfsg(i);
        }
    }

    if(cyc){
        cout << "No\n";
    }else{
        cout << "Yes\n";
    }
}

int main(){

    int T;
    cin >> T;

    while(T--){
        solve();
    }

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 110036 KB Output is correct
2 Correct 24 ms 109892 KB Output is correct
3 Correct 24 ms 109916 KB Output is correct
4 Correct 47 ms 109916 KB Output is correct
5 Correct 81 ms 110012 KB Output is correct
6 Correct 26 ms 109916 KB Output is correct
7 Correct 29 ms 110068 KB Output is correct
8 Correct 31 ms 110172 KB Output is correct
9 Correct 154 ms 119068 KB Output is correct
10 Correct 949 ms 268600 KB Output is correct
11 Correct 35 ms 109916 KB Output is correct
12 Correct 101 ms 110104 KB Output is correct
13 Correct 929 ms 272048 KB Output is correct
14 Correct 782 ms 272156 KB Output is correct
15 Correct 1318 ms 303164 KB Output is correct
16 Correct 1728 ms 355708 KB Output is correct
17 Correct 1062 ms 273280 KB Output is correct
18 Correct 846 ms 276056 KB Output is correct
19 Correct 944 ms 273236 KB Output is correct
20 Correct 688 ms 273240 KB Output is correct
21 Correct 1037 ms 308700 KB Output is correct
22 Correct 685 ms 273580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 109916 KB Output is correct
2 Correct 23 ms 109916 KB Output is correct
3 Correct 30 ms 110176 KB Output is correct
4 Correct 25 ms 110172 KB Output is correct
5 Correct 25 ms 109948 KB Output is correct
6 Correct 26 ms 110172 KB Output is correct
7 Correct 25 ms 110424 KB Output is correct
8 Correct 27 ms 109916 KB Output is correct
9 Correct 25 ms 110172 KB Output is correct
10 Correct 26 ms 110168 KB Output is correct
11 Correct 25 ms 110172 KB Output is correct
12 Correct 24 ms 109916 KB Output is correct
13 Correct 26 ms 109916 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 109916 KB Output is correct
2 Correct 23 ms 109916 KB Output is correct
3 Correct 30 ms 110176 KB Output is correct
4 Correct 25 ms 110172 KB Output is correct
5 Correct 25 ms 109948 KB Output is correct
6 Correct 26 ms 110172 KB Output is correct
7 Correct 25 ms 110424 KB Output is correct
8 Correct 27 ms 109916 KB Output is correct
9 Correct 25 ms 110172 KB Output is correct
10 Correct 26 ms 110168 KB Output is correct
11 Correct 25 ms 110172 KB Output is correct
12 Correct 24 ms 109916 KB Output is correct
13 Correct 26 ms 109916 KB Output is correct
14 Correct 22 ms 110168 KB Output is correct
15 Correct 23 ms 109952 KB Output is correct
16 Correct 27 ms 110172 KB Output is correct
17 Correct 25 ms 109952 KB Output is correct
18 Correct 30 ms 110172 KB Output is correct
19 Correct 23 ms 109916 KB Output is correct
20 Correct 26 ms 110172 KB Output is correct
21 Correct 25 ms 110172 KB Output is correct
22 Correct 26 ms 110172 KB Output is correct
23 Correct 23 ms 109940 KB Output is correct
24 Correct 23 ms 109916 KB Output is correct
25 Correct 30 ms 110420 KB Output is correct
26 Correct 23 ms 109916 KB Output is correct
27 Correct 27 ms 110148 KB Output is correct
28 Correct 23 ms 109912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 109916 KB Output is correct
2 Correct 23 ms 109916 KB Output is correct
3 Correct 30 ms 110176 KB Output is correct
4 Correct 25 ms 110172 KB Output is correct
5 Correct 25 ms 109948 KB Output is correct
6 Correct 26 ms 110172 KB Output is correct
7 Correct 25 ms 110424 KB Output is correct
8 Correct 27 ms 109916 KB Output is correct
9 Correct 25 ms 110172 KB Output is correct
10 Correct 26 ms 110168 KB Output is correct
11 Correct 25 ms 110172 KB Output is correct
12 Correct 24 ms 109916 KB Output is correct
13 Correct 26 ms 109916 KB Output is correct
14 Correct 22 ms 110168 KB Output is correct
15 Correct 23 ms 109952 KB Output is correct
16 Correct 27 ms 110172 KB Output is correct
17 Correct 25 ms 109952 KB Output is correct
18 Correct 30 ms 110172 KB Output is correct
19 Correct 23 ms 109916 KB Output is correct
20 Correct 26 ms 110172 KB Output is correct
21 Correct 25 ms 110172 KB Output is correct
22 Correct 26 ms 110172 KB Output is correct
23 Correct 23 ms 109940 KB Output is correct
24 Correct 23 ms 109916 KB Output is correct
25 Correct 30 ms 110420 KB Output is correct
26 Correct 23 ms 109916 KB Output is correct
27 Correct 27 ms 110148 KB Output is correct
28 Correct 23 ms 109912 KB Output is correct
29 Correct 26 ms 110068 KB Output is correct
30 Correct 26 ms 110168 KB Output is correct
31 Correct 26 ms 110212 KB Output is correct
32 Correct 28 ms 110216 KB Output is correct
33 Correct 29 ms 110120 KB Output is correct
34 Correct 25 ms 109916 KB Output is correct
35 Correct 26 ms 110172 KB Output is correct
36 Correct 26 ms 110172 KB Output is correct
37 Correct 29 ms 110172 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 109916 KB Output is correct
2 Correct 23 ms 109916 KB Output is correct
3 Correct 30 ms 110176 KB Output is correct
4 Correct 25 ms 110172 KB Output is correct
5 Correct 25 ms 109948 KB Output is correct
6 Correct 26 ms 110172 KB Output is correct
7 Correct 25 ms 110424 KB Output is correct
8 Correct 27 ms 109916 KB Output is correct
9 Correct 25 ms 110172 KB Output is correct
10 Correct 26 ms 110168 KB Output is correct
11 Correct 25 ms 110172 KB Output is correct
12 Correct 24 ms 109916 KB Output is correct
13 Correct 26 ms 109916 KB Output is correct
14 Correct 22 ms 110168 KB Output is correct
15 Correct 23 ms 109952 KB Output is correct
16 Correct 27 ms 110172 KB Output is correct
17 Correct 25 ms 109952 KB Output is correct
18 Correct 30 ms 110172 KB Output is correct
19 Correct 23 ms 109916 KB Output is correct
20 Correct 26 ms 110172 KB Output is correct
21 Correct 25 ms 110172 KB Output is correct
22 Correct 26 ms 110172 KB Output is correct
23 Correct 23 ms 109940 KB Output is correct
24 Correct 23 ms 109916 KB Output is correct
25 Correct 30 ms 110420 KB Output is correct
26 Correct 23 ms 109916 KB Output is correct
27 Correct 27 ms 110148 KB Output is correct
28 Correct 23 ms 109912 KB Output is correct
29 Correct 26 ms 110068 KB Output is correct
30 Correct 26 ms 110168 KB Output is correct
31 Correct 26 ms 110212 KB Output is correct
32 Correct 28 ms 110216 KB Output is correct
33 Correct 29 ms 110120 KB Output is correct
34 Correct 25 ms 109916 KB Output is correct
35 Correct 26 ms 110172 KB Output is correct
36 Correct 26 ms 110172 KB Output is correct
37 Correct 29 ms 110172 KB Output is correct
38 Correct 152 ms 119848 KB Output is correct
39 Correct 819 ms 269628 KB Output is correct
40 Correct 132 ms 120144 KB Output is correct
41 Correct 108 ms 117048 KB Output is correct
42 Correct 111 ms 118456 KB Output is correct
43 Correct 128 ms 120148 KB Output is correct
44 Correct 48 ms 112728 KB Output is correct
45 Correct 232 ms 183632 KB Output is correct
46 Correct 276 ms 183888 KB Output is correct
47 Correct 593 ms 258488 KB Output is correct
48 Correct 621 ms 258440 KB Output is correct
49 Correct 283 ms 221088 KB Output is correct
50 Correct 361 ms 221176 KB Output is correct
51 Correct 427 ms 247948 KB Output is correct
52 Correct 414 ms 248308 KB Output is correct
53 Correct 51 ms 117460 KB Output is correct
54 Correct 396 ms 176208 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 110200 KB Output is correct
2 Correct 23 ms 109940 KB Output is correct
3 Correct 23 ms 109916 KB Output is correct
4 Correct 23 ms 109916 KB Output is correct
5 Correct 41 ms 110160 KB Output is correct
6 Correct 23 ms 110076 KB Output is correct
7 Correct 27 ms 110040 KB Output is correct
8 Correct 23 ms 109912 KB Output is correct
9 Correct 23 ms 109948 KB Output is correct
10 Correct 26 ms 109916 KB Output is correct
11 Correct 23 ms 109916 KB Output is correct
12 Correct 26 ms 110000 KB Output is correct
13 Correct 71 ms 110456 KB Output is correct
14 Correct 99 ms 110684 KB Output is correct
15 Correct 76 ms 110416 KB Output is correct
16 Correct 205 ms 163888 KB Output is correct
17 Correct 471 ms 175504 KB Output is correct
18 Correct 748 ms 199256 KB Output is correct
19 Correct 352 ms 165456 KB Output is correct
20 Correct 240 ms 164948 KB Output is correct
21 Correct 302 ms 165308 KB Output is correct
22 Correct 379 ms 171676 KB Output is correct
23 Correct 348 ms 170008 KB Output is correct
24 Correct 352 ms 170008 KB Output is correct
25 Correct 317 ms 170524 KB Output is correct
26 Correct 365 ms 170192 KB Output is correct
27 Correct 353 ms 159668 KB Output is correct
28 Correct 330 ms 167880 KB Output is correct
29 Correct 322 ms 162208 KB Output is correct
30 Correct 326 ms 164144 KB Output is correct
31 Correct 361 ms 165972 KB Output is correct
32 Correct 315 ms 159944 KB Output is correct
33 Correct 358 ms 164048 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 25 ms 110036 KB Output is correct
2 Correct 24 ms 109892 KB Output is correct
3 Correct 24 ms 109916 KB Output is correct
4 Correct 47 ms 109916 KB Output is correct
5 Correct 81 ms 110012 KB Output is correct
6 Correct 26 ms 109916 KB Output is correct
7 Correct 29 ms 110068 KB Output is correct
8 Correct 31 ms 110172 KB Output is correct
9 Correct 154 ms 119068 KB Output is correct
10 Correct 949 ms 268600 KB Output is correct
11 Correct 35 ms 109916 KB Output is correct
12 Correct 101 ms 110104 KB Output is correct
13 Correct 929 ms 272048 KB Output is correct
14 Correct 782 ms 272156 KB Output is correct
15 Correct 1318 ms 303164 KB Output is correct
16 Correct 1728 ms 355708 KB Output is correct
17 Correct 1062 ms 273280 KB Output is correct
18 Correct 846 ms 276056 KB Output is correct
19 Correct 944 ms 273236 KB Output is correct
20 Correct 688 ms 273240 KB Output is correct
21 Correct 1037 ms 308700 KB Output is correct
22 Correct 685 ms 273580 KB Output is correct
23 Correct 25 ms 109916 KB Output is correct
24 Correct 23 ms 109916 KB Output is correct
25 Correct 30 ms 110176 KB Output is correct
26 Correct 25 ms 110172 KB Output is correct
27 Correct 25 ms 109948 KB Output is correct
28 Correct 26 ms 110172 KB Output is correct
29 Correct 25 ms 110424 KB Output is correct
30 Correct 27 ms 109916 KB Output is correct
31 Correct 25 ms 110172 KB Output is correct
32 Correct 26 ms 110168 KB Output is correct
33 Correct 25 ms 110172 KB Output is correct
34 Correct 24 ms 109916 KB Output is correct
35 Correct 26 ms 109916 KB Output is correct
36 Correct 22 ms 110168 KB Output is correct
37 Correct 23 ms 109952 KB Output is correct
38 Correct 27 ms 110172 KB Output is correct
39 Correct 25 ms 109952 KB Output is correct
40 Correct 30 ms 110172 KB Output is correct
41 Correct 23 ms 109916 KB Output is correct
42 Correct 26 ms 110172 KB Output is correct
43 Correct 25 ms 110172 KB Output is correct
44 Correct 26 ms 110172 KB Output is correct
45 Correct 23 ms 109940 KB Output is correct
46 Correct 23 ms 109916 KB Output is correct
47 Correct 30 ms 110420 KB Output is correct
48 Correct 23 ms 109916 KB Output is correct
49 Correct 27 ms 110148 KB Output is correct
50 Correct 23 ms 109912 KB Output is correct
51 Correct 26 ms 110068 KB Output is correct
52 Correct 26 ms 110168 KB Output is correct
53 Correct 26 ms 110212 KB Output is correct
54 Correct 28 ms 110216 KB Output is correct
55 Correct 29 ms 110120 KB Output is correct
56 Correct 25 ms 109916 KB Output is correct
57 Correct 26 ms 110172 KB Output is correct
58 Correct 26 ms 110172 KB Output is correct
59 Correct 29 ms 110172 KB Output is correct
60 Correct 152 ms 119848 KB Output is correct
61 Correct 819 ms 269628 KB Output is correct
62 Correct 132 ms 120144 KB Output is correct
63 Correct 108 ms 117048 KB Output is correct
64 Correct 111 ms 118456 KB Output is correct
65 Correct 128 ms 120148 KB Output is correct
66 Correct 48 ms 112728 KB Output is correct
67 Correct 232 ms 183632 KB Output is correct
68 Correct 276 ms 183888 KB Output is correct
69 Correct 593 ms 258488 KB Output is correct
70 Correct 621 ms 258440 KB Output is correct
71 Correct 283 ms 221088 KB Output is correct
72 Correct 361 ms 221176 KB Output is correct
73 Correct 427 ms 247948 KB Output is correct
74 Correct 414 ms 248308 KB Output is correct
75 Correct 51 ms 117460 KB Output is correct
76 Correct 396 ms 176208 KB Output is correct
77 Correct 24 ms 110200 KB Output is correct
78 Correct 23 ms 109940 KB Output is correct
79 Correct 23 ms 109916 KB Output is correct
80 Correct 23 ms 109916 KB Output is correct
81 Correct 41 ms 110160 KB Output is correct
82 Correct 23 ms 110076 KB Output is correct
83 Correct 27 ms 110040 KB Output is correct
84 Correct 23 ms 109912 KB Output is correct
85 Correct 23 ms 109948 KB Output is correct
86 Correct 26 ms 109916 KB Output is correct
87 Correct 23 ms 109916 KB Output is correct
88 Correct 26 ms 110000 KB Output is correct
89 Correct 71 ms 110456 KB Output is correct
90 Correct 99 ms 110684 KB Output is correct
91 Correct 76 ms 110416 KB Output is correct
92 Correct 205 ms 163888 KB Output is correct
93 Correct 471 ms 175504 KB Output is correct
94 Correct 748 ms 199256 KB Output is correct
95 Correct 352 ms 165456 KB Output is correct
96 Correct 240 ms 164948 KB Output is correct
97 Correct 302 ms 165308 KB Output is correct
98 Correct 379 ms 171676 KB Output is correct
99 Correct 348 ms 170008 KB Output is correct
100 Correct 352 ms 170008 KB Output is correct
101 Correct 317 ms 170524 KB Output is correct
102 Correct 365 ms 170192 KB Output is correct
103 Correct 353 ms 159668 KB Output is correct
104 Correct 330 ms 167880 KB Output is correct
105 Correct 322 ms 162208 KB Output is correct
106 Correct 326 ms 164144 KB Output is correct
107 Correct 361 ms 165972 KB Output is correct
108 Correct 315 ms 159944 KB Output is correct
109 Correct 358 ms 164048 KB Output is correct
110 Correct 85 ms 110672 KB Output is correct
111 Correct 64 ms 110420 KB Output is correct
112 Correct 1095 ms 301484 KB Output is correct
113 Correct 785 ms 261204 KB Output is correct
114 Correct 710 ms 238280 KB Output is correct
115 Correct 99 ms 146108 KB Output is correct
116 Correct 411 ms 178000 KB Output is correct
117 Correct 834 ms 214660 KB Output is correct
118 Correct 365 ms 173456 KB Output is correct
119 Correct 370 ms 173396 KB Output is correct
120 Correct 47 ms 118024 KB Output is correct
121 Correct 640 ms 187472 KB Output is correct
122 Correct 533 ms 184840 KB Output is correct
123 Correct 912 ms 265800 KB Output is correct
124 Correct 761 ms 265812 KB Output is correct
125 Correct 682 ms 266324 KB Output is correct
126 Correct 1459 ms 347816 KB Output is correct
127 Correct 927 ms 303448 KB Output is correct
128 Correct 819 ms 304596 KB Output is correct
129 Correct 869 ms 279132 KB Output is correct
130 Correct 843 ms 299444 KB Output is correct