답안 #863646

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
863646 2023-10-20T16:29:40 Z lolismek Jail (JOI22_jail) C++14
100 / 100
1508 ms 358672 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++){
        //cout << "** " << i << endl;
        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);
                    //cout << 1 << ' ' << node << ' ' << j << ' ' << node << endl;
                    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);
                //cout << 2 << ' ' << node << ' ' << j << ' ' << node << endl;
                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]);
                //cout << 3 << ' ' << node << ' ' << j << ' ' << node << endl;
                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]);
                    //cout << 4 << ' ' << node << ' ' << j << ' ' << node << endl;
                    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;
}

/*
1
15
1 2
2 4
2 5
4 6
4 7
5 8
5 9
1 3
3 10
3 11
10 12
10 13
11 14
11 15
4
10 11
1 12
6 15
1 11

1
15
1 2
2 4
2 5
4 6
4 7
5 8
5 9
1 3
3 10
3 11
10 12
10 13
11 14
11 15
2
6 15
1 11

1
4
1 2
2 3
3 4
2
1 4
2 3
are ciclu!!!
*/
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 109912 KB Output is correct
2 Correct 22 ms 109916 KB Output is correct
3 Correct 23 ms 110036 KB Output is correct
4 Correct 45 ms 110428 KB Output is correct
5 Correct 71 ms 110796 KB Output is correct
6 Correct 25 ms 110168 KB Output is correct
7 Correct 25 ms 110172 KB Output is correct
8 Correct 26 ms 110172 KB Output is correct
9 Correct 137 ms 120148 KB Output is correct
10 Correct 767 ms 270040 KB Output is correct
11 Correct 42 ms 110160 KB Output is correct
12 Correct 97 ms 110928 KB Output is correct
13 Correct 901 ms 274284 KB Output is correct
14 Correct 680 ms 274068 KB Output is correct
15 Correct 1195 ms 304960 KB Output is correct
16 Correct 1508 ms 358672 KB Output is correct
17 Correct 950 ms 275404 KB Output is correct
18 Correct 767 ms 278912 KB Output is correct
19 Correct 863 ms 275404 KB Output is correct
20 Correct 625 ms 275588 KB Output is correct
21 Correct 936 ms 311092 KB Output is correct
22 Correct 630 ms 274440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 109916 KB Output is correct
2 Correct 22 ms 109852 KB Output is correct
3 Correct 26 ms 109992 KB Output is correct
4 Correct 25 ms 110172 KB Output is correct
5 Correct 25 ms 110000 KB Output is correct
6 Correct 25 ms 110172 KB Output is correct
7 Correct 25 ms 109908 KB Output is correct
8 Correct 27 ms 110116 KB Output is correct
9 Correct 26 ms 110168 KB Output is correct
10 Correct 26 ms 110172 KB Output is correct
11 Correct 26 ms 110168 KB Output is correct
12 Correct 24 ms 109916 KB Output is correct
13 Correct 26 ms 109912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 109916 KB Output is correct
2 Correct 22 ms 109852 KB Output is correct
3 Correct 26 ms 109992 KB Output is correct
4 Correct 25 ms 110172 KB Output is correct
5 Correct 25 ms 110000 KB Output is correct
6 Correct 25 ms 110172 KB Output is correct
7 Correct 25 ms 109908 KB Output is correct
8 Correct 27 ms 110116 KB Output is correct
9 Correct 26 ms 110168 KB Output is correct
10 Correct 26 ms 110172 KB Output is correct
11 Correct 26 ms 110168 KB Output is correct
12 Correct 24 ms 109916 KB Output is correct
13 Correct 26 ms 109912 KB Output is correct
14 Correct 23 ms 110024 KB Output is correct
15 Correct 23 ms 110024 KB Output is correct
16 Correct 27 ms 110168 KB Output is correct
17 Correct 25 ms 109916 KB Output is correct
18 Correct 25 ms 110172 KB Output is correct
19 Correct 23 ms 109916 KB Output is correct
20 Correct 25 ms 110172 KB Output is correct
21 Correct 25 ms 110172 KB Output is correct
22 Correct 25 ms 110136 KB Output is correct
23 Correct 25 ms 109916 KB Output is correct
24 Correct 26 ms 109916 KB Output is correct
25 Correct 28 ms 110164 KB Output is correct
26 Correct 23 ms 109916 KB Output is correct
27 Correct 24 ms 109972 KB Output is correct
28 Correct 22 ms 109968 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 109916 KB Output is correct
2 Correct 22 ms 109852 KB Output is correct
3 Correct 26 ms 109992 KB Output is correct
4 Correct 25 ms 110172 KB Output is correct
5 Correct 25 ms 110000 KB Output is correct
6 Correct 25 ms 110172 KB Output is correct
7 Correct 25 ms 109908 KB Output is correct
8 Correct 27 ms 110116 KB Output is correct
9 Correct 26 ms 110168 KB Output is correct
10 Correct 26 ms 110172 KB Output is correct
11 Correct 26 ms 110168 KB Output is correct
12 Correct 24 ms 109916 KB Output is correct
13 Correct 26 ms 109912 KB Output is correct
14 Correct 23 ms 110024 KB Output is correct
15 Correct 23 ms 110024 KB Output is correct
16 Correct 27 ms 110168 KB Output is correct
17 Correct 25 ms 109916 KB Output is correct
18 Correct 25 ms 110172 KB Output is correct
19 Correct 23 ms 109916 KB Output is correct
20 Correct 25 ms 110172 KB Output is correct
21 Correct 25 ms 110172 KB Output is correct
22 Correct 25 ms 110136 KB Output is correct
23 Correct 25 ms 109916 KB Output is correct
24 Correct 26 ms 109916 KB Output is correct
25 Correct 28 ms 110164 KB Output is correct
26 Correct 23 ms 109916 KB Output is correct
27 Correct 24 ms 109972 KB Output is correct
28 Correct 22 ms 109968 KB Output is correct
29 Correct 27 ms 110168 KB Output is correct
30 Correct 26 ms 109908 KB Output is correct
31 Correct 26 ms 110168 KB Output is correct
32 Correct 26 ms 110368 KB Output is correct
33 Correct 26 ms 110172 KB Output is correct
34 Correct 25 ms 109916 KB Output is correct
35 Correct 26 ms 110172 KB Output is correct
36 Correct 25 ms 110172 KB Output is correct
37 Correct 24 ms 109912 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 109916 KB Output is correct
2 Correct 22 ms 109852 KB Output is correct
3 Correct 26 ms 109992 KB Output is correct
4 Correct 25 ms 110172 KB Output is correct
5 Correct 25 ms 110000 KB Output is correct
6 Correct 25 ms 110172 KB Output is correct
7 Correct 25 ms 109908 KB Output is correct
8 Correct 27 ms 110116 KB Output is correct
9 Correct 26 ms 110168 KB Output is correct
10 Correct 26 ms 110172 KB Output is correct
11 Correct 26 ms 110168 KB Output is correct
12 Correct 24 ms 109916 KB Output is correct
13 Correct 26 ms 109912 KB Output is correct
14 Correct 23 ms 110024 KB Output is correct
15 Correct 23 ms 110024 KB Output is correct
16 Correct 27 ms 110168 KB Output is correct
17 Correct 25 ms 109916 KB Output is correct
18 Correct 25 ms 110172 KB Output is correct
19 Correct 23 ms 109916 KB Output is correct
20 Correct 25 ms 110172 KB Output is correct
21 Correct 25 ms 110172 KB Output is correct
22 Correct 25 ms 110136 KB Output is correct
23 Correct 25 ms 109916 KB Output is correct
24 Correct 26 ms 109916 KB Output is correct
25 Correct 28 ms 110164 KB Output is correct
26 Correct 23 ms 109916 KB Output is correct
27 Correct 24 ms 109972 KB Output is correct
28 Correct 22 ms 109968 KB Output is correct
29 Correct 27 ms 110168 KB Output is correct
30 Correct 26 ms 109908 KB Output is correct
31 Correct 26 ms 110168 KB Output is correct
32 Correct 26 ms 110368 KB Output is correct
33 Correct 26 ms 110172 KB Output is correct
34 Correct 25 ms 109916 KB Output is correct
35 Correct 26 ms 110172 KB Output is correct
36 Correct 25 ms 110172 KB Output is correct
37 Correct 24 ms 109912 KB Output is correct
38 Correct 146 ms 120100 KB Output is correct
39 Correct 787 ms 269944 KB Output is correct
40 Correct 136 ms 120660 KB Output is correct
41 Correct 99 ms 117460 KB Output is correct
42 Correct 93 ms 118868 KB Output is correct
43 Correct 155 ms 120540 KB Output is correct
44 Correct 38 ms 112724 KB Output is correct
45 Correct 225 ms 184016 KB Output is correct
46 Correct 263 ms 184144 KB Output is correct
47 Correct 611 ms 258784 KB Output is correct
48 Correct 619 ms 258844 KB Output is correct
49 Correct 294 ms 221508 KB Output is correct
50 Correct 401 ms 221424 KB Output is correct
51 Correct 489 ms 248564 KB Output is correct
52 Correct 340 ms 248724 KB Output is correct
53 Correct 50 ms 117332 KB Output is correct
54 Correct 384 ms 176828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 23 ms 109912 KB Output is correct
2 Correct 23 ms 109916 KB Output is correct
3 Correct 23 ms 109984 KB Output is correct
4 Correct 23 ms 109916 KB Output is correct
5 Correct 34 ms 110172 KB Output is correct
6 Correct 24 ms 110000 KB Output is correct
7 Correct 23 ms 109916 KB Output is correct
8 Correct 24 ms 109912 KB Output is correct
9 Correct 24 ms 109916 KB Output is correct
10 Correct 23 ms 109916 KB Output is correct
11 Correct 23 ms 109912 KB Output is correct
12 Correct 25 ms 110172 KB Output is correct
13 Correct 62 ms 110428 KB Output is correct
14 Correct 90 ms 110916 KB Output is correct
15 Correct 79 ms 110684 KB Output is correct
16 Correct 198 ms 164580 KB Output is correct
17 Correct 432 ms 176552 KB Output is correct
18 Correct 627 ms 200400 KB Output is correct
19 Correct 269 ms 166328 KB Output is correct
20 Correct 232 ms 165712 KB Output is correct
21 Correct 282 ms 165744 KB Output is correct
22 Correct 333 ms 172524 KB Output is correct
23 Correct 325 ms 170868 KB Output is correct
24 Correct 327 ms 170876 KB Output is correct
25 Correct 305 ms 171192 KB Output is correct
26 Correct 308 ms 171000 KB Output is correct
27 Correct 328 ms 160652 KB Output is correct
28 Correct 310 ms 169036 KB Output is correct
29 Correct 289 ms 163136 KB Output is correct
30 Correct 289 ms 165068 KB Output is correct
31 Correct 301 ms 166600 KB Output is correct
32 Correct 289 ms 160960 KB Output is correct
33 Correct 282 ms 165148 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 24 ms 109912 KB Output is correct
2 Correct 22 ms 109916 KB Output is correct
3 Correct 23 ms 110036 KB Output is correct
4 Correct 45 ms 110428 KB Output is correct
5 Correct 71 ms 110796 KB Output is correct
6 Correct 25 ms 110168 KB Output is correct
7 Correct 25 ms 110172 KB Output is correct
8 Correct 26 ms 110172 KB Output is correct
9 Correct 137 ms 120148 KB Output is correct
10 Correct 767 ms 270040 KB Output is correct
11 Correct 42 ms 110160 KB Output is correct
12 Correct 97 ms 110928 KB Output is correct
13 Correct 901 ms 274284 KB Output is correct
14 Correct 680 ms 274068 KB Output is correct
15 Correct 1195 ms 304960 KB Output is correct
16 Correct 1508 ms 358672 KB Output is correct
17 Correct 950 ms 275404 KB Output is correct
18 Correct 767 ms 278912 KB Output is correct
19 Correct 863 ms 275404 KB Output is correct
20 Correct 625 ms 275588 KB Output is correct
21 Correct 936 ms 311092 KB Output is correct
22 Correct 630 ms 274440 KB Output is correct
23 Correct 22 ms 109916 KB Output is correct
24 Correct 22 ms 109852 KB Output is correct
25 Correct 26 ms 109992 KB Output is correct
26 Correct 25 ms 110172 KB Output is correct
27 Correct 25 ms 110000 KB Output is correct
28 Correct 25 ms 110172 KB Output is correct
29 Correct 25 ms 109908 KB Output is correct
30 Correct 27 ms 110116 KB Output is correct
31 Correct 26 ms 110168 KB Output is correct
32 Correct 26 ms 110172 KB Output is correct
33 Correct 26 ms 110168 KB Output is correct
34 Correct 24 ms 109916 KB Output is correct
35 Correct 26 ms 109912 KB Output is correct
36 Correct 23 ms 110024 KB Output is correct
37 Correct 23 ms 110024 KB Output is correct
38 Correct 27 ms 110168 KB Output is correct
39 Correct 25 ms 109916 KB Output is correct
40 Correct 25 ms 110172 KB Output is correct
41 Correct 23 ms 109916 KB Output is correct
42 Correct 25 ms 110172 KB Output is correct
43 Correct 25 ms 110172 KB Output is correct
44 Correct 25 ms 110136 KB Output is correct
45 Correct 25 ms 109916 KB Output is correct
46 Correct 26 ms 109916 KB Output is correct
47 Correct 28 ms 110164 KB Output is correct
48 Correct 23 ms 109916 KB Output is correct
49 Correct 24 ms 109972 KB Output is correct
50 Correct 22 ms 109968 KB Output is correct
51 Correct 27 ms 110168 KB Output is correct
52 Correct 26 ms 109908 KB Output is correct
53 Correct 26 ms 110168 KB Output is correct
54 Correct 26 ms 110368 KB Output is correct
55 Correct 26 ms 110172 KB Output is correct
56 Correct 25 ms 109916 KB Output is correct
57 Correct 26 ms 110172 KB Output is correct
58 Correct 25 ms 110172 KB Output is correct
59 Correct 24 ms 109912 KB Output is correct
60 Correct 146 ms 120100 KB Output is correct
61 Correct 787 ms 269944 KB Output is correct
62 Correct 136 ms 120660 KB Output is correct
63 Correct 99 ms 117460 KB Output is correct
64 Correct 93 ms 118868 KB Output is correct
65 Correct 155 ms 120540 KB Output is correct
66 Correct 38 ms 112724 KB Output is correct
67 Correct 225 ms 184016 KB Output is correct
68 Correct 263 ms 184144 KB Output is correct
69 Correct 611 ms 258784 KB Output is correct
70 Correct 619 ms 258844 KB Output is correct
71 Correct 294 ms 221508 KB Output is correct
72 Correct 401 ms 221424 KB Output is correct
73 Correct 489 ms 248564 KB Output is correct
74 Correct 340 ms 248724 KB Output is correct
75 Correct 50 ms 117332 KB Output is correct
76 Correct 384 ms 176828 KB Output is correct
77 Correct 23 ms 109912 KB Output is correct
78 Correct 23 ms 109916 KB Output is correct
79 Correct 23 ms 109984 KB Output is correct
80 Correct 23 ms 109916 KB Output is correct
81 Correct 34 ms 110172 KB Output is correct
82 Correct 24 ms 110000 KB Output is correct
83 Correct 23 ms 109916 KB Output is correct
84 Correct 24 ms 109912 KB Output is correct
85 Correct 24 ms 109916 KB Output is correct
86 Correct 23 ms 109916 KB Output is correct
87 Correct 23 ms 109912 KB Output is correct
88 Correct 25 ms 110172 KB Output is correct
89 Correct 62 ms 110428 KB Output is correct
90 Correct 90 ms 110916 KB Output is correct
91 Correct 79 ms 110684 KB Output is correct
92 Correct 198 ms 164580 KB Output is correct
93 Correct 432 ms 176552 KB Output is correct
94 Correct 627 ms 200400 KB Output is correct
95 Correct 269 ms 166328 KB Output is correct
96 Correct 232 ms 165712 KB Output is correct
97 Correct 282 ms 165744 KB Output is correct
98 Correct 333 ms 172524 KB Output is correct
99 Correct 325 ms 170868 KB Output is correct
100 Correct 327 ms 170876 KB Output is correct
101 Correct 305 ms 171192 KB Output is correct
102 Correct 308 ms 171000 KB Output is correct
103 Correct 328 ms 160652 KB Output is correct
104 Correct 310 ms 169036 KB Output is correct
105 Correct 289 ms 163136 KB Output is correct
106 Correct 289 ms 165068 KB Output is correct
107 Correct 301 ms 166600 KB Output is correct
108 Correct 289 ms 160960 KB Output is correct
109 Correct 282 ms 165148 KB Output is correct
110 Correct 86 ms 111180 KB Output is correct
111 Correct 67 ms 110788 KB Output is correct
112 Correct 1029 ms 302376 KB Output is correct
113 Correct 748 ms 261968 KB Output is correct
114 Correct 681 ms 239048 KB Output is correct
115 Correct 118 ms 146116 KB Output is correct
116 Correct 373 ms 178920 KB Output is correct
117 Correct 781 ms 215848 KB Output is correct
118 Correct 327 ms 173904 KB Output is correct
119 Correct 263 ms 173908 KB Output is correct
120 Correct 43 ms 117932 KB Output is correct
121 Correct 510 ms 188556 KB Output is correct
122 Correct 512 ms 185528 KB Output is correct
123 Correct 819 ms 266452 KB Output is correct
124 Correct 724 ms 266456 KB Output is correct
125 Correct 656 ms 267068 KB Output is correct
126 Correct 1406 ms 349140 KB Output is correct
127 Correct 919 ms 304212 KB Output is correct
128 Correct 825 ms 305544 KB Output is correct
129 Correct 842 ms 280016 KB Output is correct
130 Correct 814 ms 300124 KB Output is correct