Submission #884015

# Submission time Handle Problem Language Result Execution time Memory
884015 2023-12-06T14:21:38 Z LucaIlie Jail (JOI22_jail) C++17
100 / 100
1164 ms 325320 KB
#include <bits/stdc++.h>

using namespace std;

struct prisoner {
    int b, w;
};

const int MAX_N = 1.2e5;
const int MAX_M = MAX_N;
const int MAX_LOG_N = 16;
const int MAX_V = MAX_M + 2 * MAX_N * (MAX_LOG_N + 1) + 1;
int parent[MAX_LOG_N + 1][MAX_N + 1], depth[MAX_N + 1], vertP[MAX_M], vertB[MAX_LOG_N + 1][MAX_N + 1], vertW[MAX_LOG_N + 1][MAX_N + 1], in[MAX_V];
vector <int> edges[MAX_N + 1], g[MAX_V];
prisoner prisoners[MAX_M + 1];

void dfs( int u, int p ) {
    depth[u] = depth[p] + 1;
    parent[0][u] = p;
    for ( int v: edges[u] ) {
        if ( v == p )
            continue;
        dfs( v, u );
    }
}

int findParent( int v, int d ) {
    if ( d < 0 )
        return 0;

    for ( int l = MAX_LOG_N; l >= 0; l-- ) {
        if ( d >= (1 << l) ) {
            d -= (1 << l);
            v = parent[l][v];
        }
    }

    return v;
}

int main() {
    int t;

    cin >> t;
    while ( t-- ) {
        int n, m;

        cin >> n;
        for ( int v = 1; v <= n; v++ )
            edges[v].clear();
        for ( int i = 0; i < n - 1; i++ ) {
            int u, v;
            cin >> u >> v;
            edges[u].push_back( v );
            edges[v].push_back( u );
        }
        cin >> m;
        for ( int i = 1; i <= m; i++ )
            cin >> prisoners[i].b >> prisoners[i].w;

        dfs( 1, 0 );

        int V = 1;
        for ( int i = 1; i <= m; i++ )
            vertP[i] = V++;
        for ( int l = 0; l <= log2( n ); l++ ) {
            for ( int v = 1; v <= n; v++ ) {
                vertB[l][v] = V++;
                vertW[l][v] = V++;
            }
        }
        for ( int v = 0; v < V; v++ )
            g[v].clear(), in[v] = 0;

        for ( int l = 1; l <= log2( n ); l++ ) {
            for ( int v = 1; v <= n; v++ ) {
                g[vertB[l - 1][v]].push_back( vertB[l][v] );
                g[vertB[l - 1][parent[l - 1][v]]].push_back( vertB[l][v] );
                g[vertW[l][v]].push_back( vertW[l - 1][v] );
                g[vertW[l][v]].push_back( vertW[l - 1][parent[l - 1][v]] );
                parent[l][v] = parent[l - 1][parent[l - 1][v]];
            }
        }

        for ( int i = 1; i <= m; i++ ) {
            g[vertP[i]].push_back( vertB[0][prisoners[i].b] );
            g[vertW[0][prisoners[i].w]].push_back( vertP[i] );
        }

        for ( int i = 1; i <= m; i++ ) {
            int u, v;

            u = prisoners[i].b, v = prisoners[i].w;
            if ( findParent( v, depth[v] - depth[u] ) == u )
                u = findParent( v, depth[v] - depth[u] - 1 );
            else
                u = parent[0][u];
            if ( depth[u] < depth[v] )
                swap( u, v );
            for ( int l = log2( n ); l >= 0; l-- ) {
                if ( depth[parent[l][u]] >= depth[v] ) {
                    g[vertB[l][u]].push_back( vertP[i] );
                    u = parent[l][u];
                }
            }
            for ( int l = log2( n ); l >= 0; l-- ) {
                if ( parent[l][u] != parent[l][v] ) {
                    g[vertB[l][u]].push_back( vertP[i] );
                    g[vertB[l][v]].push_back( vertP[i] );
                    u = parent[l][u];
                    v = parent[l][v];
                }
            }
            g[vertB[0][u]].push_back( vertP[i] );
            if ( u != v )
                g[vertB[1][v]].push_back( vertP[i] );

            u = prisoners[i].b, v = prisoners[i].w;
            if ( findParent( u, depth[u] - depth[v] ) == v )
                v = findParent( u, depth[u] - depth[v] - 1 );
            else
                v = parent[0][v];
            if ( depth[u] < depth[v] )
                swap( u, v );
            for ( int l = MAX_LOG_N; l >= 0; l-- ) {
                if ( depth[parent[l][u]] >= depth[v] ) {
                    g[vertP[i]].push_back( vertW[l][u] );
                    u = parent[l][u];
                }
            }
            for ( int l = MAX_LOG_N; l >= 0; l-- ) {
                if ( parent[l][u] != parent[l][v] ) {
                    g[vertP[i]].push_back( vertW[l][u] );
                    g[vertP[i]].push_back( vertW[l][v] );
                    u = parent[l][u];
                    v = parent[l][v];
                }
            }
            g[vertP[i]].push_back( vertW[0][u] );
            if ( u != v )
                g[vertP[i]].push_back( vertW[1][v] );
        }

        g[0].clear();
        for ( int i = 0; i < V; i++ ) {
            for ( int j: g[i] )
                in[j]++;
        }
        queue<int> q;
        for ( int i = 0; i < V; i++ ) {
            if ( in[i] == 0 )
                q.push( i );
        }
        int pasi = 0;
        while ( !q.empty() ) {
            int u = q.front();
            q.pop();
            pasi++;

            for ( int v: g[u] ) {
                in[v]--;
                if ( in[v] == 0 )
                    q.push( v );
            }
        }

        cout << (pasi == V ? "Yes\n" : "No\n");

        for ( int v = 0; v < V; v++ )
            g[v].clear(), in[v] = 0;
        for ( int i = 1; i <= m; i++ )
            vertP[i] = 0;
        for ( int l = 0; l <= log2( n ); l++ ) {
            for ( int v = 1; v <= n; v++ )
                parent[l][v] = vertB[l][v] = vertW[l][v] = 0;
        }
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 27 ms 113228 KB Output is correct
2 Correct 23 ms 113328 KB Output is correct
3 Correct 23 ms 111196 KB Output is correct
4 Correct 52 ms 119384 KB Output is correct
5 Correct 83 ms 119388 KB Output is correct
6 Correct 27 ms 119388 KB Output is correct
7 Correct 28 ms 119388 KB Output is correct
8 Correct 32 ms 119384 KB Output is correct
9 Correct 137 ms 132796 KB Output is correct
10 Correct 462 ms 274460 KB Output is correct
11 Correct 41 ms 113244 KB Output is correct
12 Correct 102 ms 119388 KB Output is correct
13 Correct 618 ms 279244 KB Output is correct
14 Correct 496 ms 279424 KB Output is correct
15 Correct 723 ms 281408 KB Output is correct
16 Correct 1164 ms 306372 KB Output is correct
17 Correct 549 ms 282828 KB Output is correct
18 Correct 575 ms 281884 KB Output is correct
19 Correct 532 ms 282832 KB Output is correct
20 Correct 505 ms 282832 KB Output is correct
21 Correct 526 ms 282504 KB Output is correct
22 Correct 453 ms 278064 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 113244 KB Output is correct
2 Correct 22 ms 111120 KB Output is correct
3 Correct 27 ms 119384 KB Output is correct
4 Correct 27 ms 119388 KB Output is correct
5 Correct 27 ms 119640 KB Output is correct
6 Correct 27 ms 119644 KB Output is correct
7 Correct 27 ms 119640 KB Output is correct
8 Correct 27 ms 119644 KB Output is correct
9 Correct 28 ms 119708 KB Output is correct
10 Correct 27 ms 119644 KB Output is correct
11 Correct 26 ms 119640 KB Output is correct
12 Correct 26 ms 117340 KB Output is correct
13 Correct 25 ms 117388 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 113244 KB Output is correct
2 Correct 22 ms 111120 KB Output is correct
3 Correct 27 ms 119384 KB Output is correct
4 Correct 27 ms 119388 KB Output is correct
5 Correct 27 ms 119640 KB Output is correct
6 Correct 27 ms 119644 KB Output is correct
7 Correct 27 ms 119640 KB Output is correct
8 Correct 27 ms 119644 KB Output is correct
9 Correct 28 ms 119708 KB Output is correct
10 Correct 27 ms 119644 KB Output is correct
11 Correct 26 ms 119640 KB Output is correct
12 Correct 26 ms 117340 KB Output is correct
13 Correct 25 ms 117388 KB Output is correct
14 Correct 23 ms 113244 KB Output is correct
15 Correct 23 ms 113244 KB Output is correct
16 Correct 27 ms 119640 KB Output is correct
17 Correct 27 ms 119644 KB Output is correct
18 Correct 27 ms 119640 KB Output is correct
19 Correct 24 ms 113244 KB Output is correct
20 Correct 30 ms 119640 KB Output is correct
21 Correct 27 ms 119640 KB Output is correct
22 Correct 26 ms 119644 KB Output is correct
23 Correct 23 ms 113244 KB Output is correct
24 Correct 25 ms 117340 KB Output is correct
25 Correct 27 ms 119640 KB Output is correct
26 Correct 25 ms 117340 KB Output is correct
27 Correct 27 ms 119644 KB Output is correct
28 Correct 23 ms 113244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 113244 KB Output is correct
2 Correct 22 ms 111120 KB Output is correct
3 Correct 27 ms 119384 KB Output is correct
4 Correct 27 ms 119388 KB Output is correct
5 Correct 27 ms 119640 KB Output is correct
6 Correct 27 ms 119644 KB Output is correct
7 Correct 27 ms 119640 KB Output is correct
8 Correct 27 ms 119644 KB Output is correct
9 Correct 28 ms 119708 KB Output is correct
10 Correct 27 ms 119644 KB Output is correct
11 Correct 26 ms 119640 KB Output is correct
12 Correct 26 ms 117340 KB Output is correct
13 Correct 25 ms 117388 KB Output is correct
14 Correct 23 ms 113244 KB Output is correct
15 Correct 23 ms 113244 KB Output is correct
16 Correct 27 ms 119640 KB Output is correct
17 Correct 27 ms 119644 KB Output is correct
18 Correct 27 ms 119640 KB Output is correct
19 Correct 24 ms 113244 KB Output is correct
20 Correct 30 ms 119640 KB Output is correct
21 Correct 27 ms 119640 KB Output is correct
22 Correct 26 ms 119644 KB Output is correct
23 Correct 23 ms 113244 KB Output is correct
24 Correct 25 ms 117340 KB Output is correct
25 Correct 27 ms 119640 KB Output is correct
26 Correct 25 ms 117340 KB Output is correct
27 Correct 27 ms 119644 KB Output is correct
28 Correct 23 ms 113244 KB Output is correct
29 Correct 28 ms 119640 KB Output is correct
30 Correct 27 ms 119644 KB Output is correct
31 Correct 28 ms 119684 KB Output is correct
32 Correct 29 ms 119664 KB Output is correct
33 Correct 27 ms 119640 KB Output is correct
34 Correct 28 ms 119644 KB Output is correct
35 Correct 28 ms 119644 KB Output is correct
36 Correct 26 ms 119436 KB Output is correct
37 Correct 26 ms 117596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 113244 KB Output is correct
2 Correct 22 ms 111120 KB Output is correct
3 Correct 27 ms 119384 KB Output is correct
4 Correct 27 ms 119388 KB Output is correct
5 Correct 27 ms 119640 KB Output is correct
6 Correct 27 ms 119644 KB Output is correct
7 Correct 27 ms 119640 KB Output is correct
8 Correct 27 ms 119644 KB Output is correct
9 Correct 28 ms 119708 KB Output is correct
10 Correct 27 ms 119644 KB Output is correct
11 Correct 26 ms 119640 KB Output is correct
12 Correct 26 ms 117340 KB Output is correct
13 Correct 25 ms 117388 KB Output is correct
14 Correct 23 ms 113244 KB Output is correct
15 Correct 23 ms 113244 KB Output is correct
16 Correct 27 ms 119640 KB Output is correct
17 Correct 27 ms 119644 KB Output is correct
18 Correct 27 ms 119640 KB Output is correct
19 Correct 24 ms 113244 KB Output is correct
20 Correct 30 ms 119640 KB Output is correct
21 Correct 27 ms 119640 KB Output is correct
22 Correct 26 ms 119644 KB Output is correct
23 Correct 23 ms 113244 KB Output is correct
24 Correct 25 ms 117340 KB Output is correct
25 Correct 27 ms 119640 KB Output is correct
26 Correct 25 ms 117340 KB Output is correct
27 Correct 27 ms 119644 KB Output is correct
28 Correct 23 ms 113244 KB Output is correct
29 Correct 28 ms 119640 KB Output is correct
30 Correct 27 ms 119644 KB Output is correct
31 Correct 28 ms 119684 KB Output is correct
32 Correct 29 ms 119664 KB Output is correct
33 Correct 27 ms 119640 KB Output is correct
34 Correct 28 ms 119644 KB Output is correct
35 Correct 28 ms 119644 KB Output is correct
36 Correct 26 ms 119436 KB Output is correct
37 Correct 26 ms 117596 KB Output is correct
38 Correct 150 ms 133908 KB Output is correct
39 Correct 470 ms 275916 KB Output is correct
40 Correct 151 ms 135376 KB Output is correct
41 Correct 142 ms 134348 KB Output is correct
42 Correct 139 ms 135284 KB Output is correct
43 Correct 155 ms 135096 KB Output is correct
44 Correct 44 ms 122420 KB Output is correct
45 Correct 494 ms 278524 KB Output is correct
46 Correct 482 ms 278192 KB Output is correct
47 Correct 513 ms 273372 KB Output is correct
48 Correct 511 ms 273664 KB Output is correct
49 Correct 466 ms 277512 KB Output is correct
50 Correct 486 ms 277688 KB Output is correct
51 Correct 520 ms 276464 KB Output is correct
52 Correct 512 ms 276668 KB Output is correct
53 Correct 72 ms 133452 KB Output is correct
54 Correct 525 ms 279352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 25 ms 113240 KB Output is correct
2 Correct 23 ms 113204 KB Output is correct
3 Correct 23 ms 113244 KB Output is correct
4 Correct 23 ms 111128 KB Output is correct
5 Correct 37 ms 113244 KB Output is correct
6 Correct 25 ms 117336 KB Output is correct
7 Correct 26 ms 117336 KB Output is correct
8 Correct 23 ms 113208 KB Output is correct
9 Correct 24 ms 113244 KB Output is correct
10 Correct 24 ms 117340 KB Output is correct
11 Correct 24 ms 113244 KB Output is correct
12 Correct 28 ms 119644 KB Output is correct
13 Correct 68 ms 117844 KB Output is correct
14 Correct 102 ms 120172 KB Output is correct
15 Correct 86 ms 120148 KB Output is correct
16 Correct 512 ms 280504 KB Output is correct
17 Correct 569 ms 288860 KB Output is correct
18 Correct 689 ms 313432 KB Output is correct
19 Correct 523 ms 282024 KB Output is correct
20 Correct 542 ms 282292 KB Output is correct
21 Correct 541 ms 281460 KB Output is correct
22 Correct 583 ms 287856 KB Output is correct
23 Correct 545 ms 288108 KB Output is correct
24 Correct 652 ms 288572 KB Output is correct
25 Correct 645 ms 289972 KB Output is correct
26 Correct 647 ms 288796 KB Output is correct
27 Correct 651 ms 287408 KB Output is correct
28 Correct 543 ms 288948 KB Output is correct
29 Correct 526 ms 287552 KB Output is correct
30 Correct 597 ms 282144 KB Output is correct
31 Correct 499 ms 283316 KB Output is correct
32 Correct 617 ms 282796 KB Output is correct
33 Correct 496 ms 282792 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 27 ms 113228 KB Output is correct
2 Correct 23 ms 113328 KB Output is correct
3 Correct 23 ms 111196 KB Output is correct
4 Correct 52 ms 119384 KB Output is correct
5 Correct 83 ms 119388 KB Output is correct
6 Correct 27 ms 119388 KB Output is correct
7 Correct 28 ms 119388 KB Output is correct
8 Correct 32 ms 119384 KB Output is correct
9 Correct 137 ms 132796 KB Output is correct
10 Correct 462 ms 274460 KB Output is correct
11 Correct 41 ms 113244 KB Output is correct
12 Correct 102 ms 119388 KB Output is correct
13 Correct 618 ms 279244 KB Output is correct
14 Correct 496 ms 279424 KB Output is correct
15 Correct 723 ms 281408 KB Output is correct
16 Correct 1164 ms 306372 KB Output is correct
17 Correct 549 ms 282828 KB Output is correct
18 Correct 575 ms 281884 KB Output is correct
19 Correct 532 ms 282832 KB Output is correct
20 Correct 505 ms 282832 KB Output is correct
21 Correct 526 ms 282504 KB Output is correct
22 Correct 453 ms 278064 KB Output is correct
23 Correct 25 ms 113244 KB Output is correct
24 Correct 22 ms 111120 KB Output is correct
25 Correct 27 ms 119384 KB Output is correct
26 Correct 27 ms 119388 KB Output is correct
27 Correct 27 ms 119640 KB Output is correct
28 Correct 27 ms 119644 KB Output is correct
29 Correct 27 ms 119640 KB Output is correct
30 Correct 27 ms 119644 KB Output is correct
31 Correct 28 ms 119708 KB Output is correct
32 Correct 27 ms 119644 KB Output is correct
33 Correct 26 ms 119640 KB Output is correct
34 Correct 26 ms 117340 KB Output is correct
35 Correct 25 ms 117388 KB Output is correct
36 Correct 23 ms 113244 KB Output is correct
37 Correct 23 ms 113244 KB Output is correct
38 Correct 27 ms 119640 KB Output is correct
39 Correct 27 ms 119644 KB Output is correct
40 Correct 27 ms 119640 KB Output is correct
41 Correct 24 ms 113244 KB Output is correct
42 Correct 30 ms 119640 KB Output is correct
43 Correct 27 ms 119640 KB Output is correct
44 Correct 26 ms 119644 KB Output is correct
45 Correct 23 ms 113244 KB Output is correct
46 Correct 25 ms 117340 KB Output is correct
47 Correct 27 ms 119640 KB Output is correct
48 Correct 25 ms 117340 KB Output is correct
49 Correct 27 ms 119644 KB Output is correct
50 Correct 23 ms 113244 KB Output is correct
51 Correct 28 ms 119640 KB Output is correct
52 Correct 27 ms 119644 KB Output is correct
53 Correct 28 ms 119684 KB Output is correct
54 Correct 29 ms 119664 KB Output is correct
55 Correct 27 ms 119640 KB Output is correct
56 Correct 28 ms 119644 KB Output is correct
57 Correct 28 ms 119644 KB Output is correct
58 Correct 26 ms 119436 KB Output is correct
59 Correct 26 ms 117596 KB Output is correct
60 Correct 150 ms 133908 KB Output is correct
61 Correct 470 ms 275916 KB Output is correct
62 Correct 151 ms 135376 KB Output is correct
63 Correct 142 ms 134348 KB Output is correct
64 Correct 139 ms 135284 KB Output is correct
65 Correct 155 ms 135096 KB Output is correct
66 Correct 44 ms 122420 KB Output is correct
67 Correct 494 ms 278524 KB Output is correct
68 Correct 482 ms 278192 KB Output is correct
69 Correct 513 ms 273372 KB Output is correct
70 Correct 511 ms 273664 KB Output is correct
71 Correct 466 ms 277512 KB Output is correct
72 Correct 486 ms 277688 KB Output is correct
73 Correct 520 ms 276464 KB Output is correct
74 Correct 512 ms 276668 KB Output is correct
75 Correct 72 ms 133452 KB Output is correct
76 Correct 525 ms 279352 KB Output is correct
77 Correct 25 ms 113240 KB Output is correct
78 Correct 23 ms 113204 KB Output is correct
79 Correct 23 ms 113244 KB Output is correct
80 Correct 23 ms 111128 KB Output is correct
81 Correct 37 ms 113244 KB Output is correct
82 Correct 25 ms 117336 KB Output is correct
83 Correct 26 ms 117336 KB Output is correct
84 Correct 23 ms 113208 KB Output is correct
85 Correct 24 ms 113244 KB Output is correct
86 Correct 24 ms 117340 KB Output is correct
87 Correct 24 ms 113244 KB Output is correct
88 Correct 28 ms 119644 KB Output is correct
89 Correct 68 ms 117844 KB Output is correct
90 Correct 102 ms 120172 KB Output is correct
91 Correct 86 ms 120148 KB Output is correct
92 Correct 512 ms 280504 KB Output is correct
93 Correct 569 ms 288860 KB Output is correct
94 Correct 689 ms 313432 KB Output is correct
95 Correct 523 ms 282024 KB Output is correct
96 Correct 542 ms 282292 KB Output is correct
97 Correct 541 ms 281460 KB Output is correct
98 Correct 583 ms 287856 KB Output is correct
99 Correct 545 ms 288108 KB Output is correct
100 Correct 652 ms 288572 KB Output is correct
101 Correct 645 ms 289972 KB Output is correct
102 Correct 647 ms 288796 KB Output is correct
103 Correct 651 ms 287408 KB Output is correct
104 Correct 543 ms 288948 KB Output is correct
105 Correct 526 ms 287552 KB Output is correct
106 Correct 597 ms 282144 KB Output is correct
107 Correct 499 ms 283316 KB Output is correct
108 Correct 617 ms 282796 KB Output is correct
109 Correct 496 ms 282792 KB Output is correct
110 Correct 104 ms 118344 KB Output is correct
111 Correct 83 ms 120148 KB Output is correct
112 Correct 793 ms 288208 KB Output is correct
113 Correct 716 ms 278148 KB Output is correct
114 Correct 643 ms 286288 KB Output is correct
115 Correct 375 ms 278784 KB Output is correct
116 Correct 664 ms 283804 KB Output is correct
117 Correct 715 ms 314008 KB Output is correct
118 Correct 573 ms 279160 KB Output is correct
119 Correct 572 ms 279588 KB Output is correct
120 Correct 65 ms 137420 KB Output is correct
121 Correct 779 ms 287340 KB Output is correct
122 Correct 757 ms 285540 KB Output is correct
123 Correct 755 ms 278272 KB Output is correct
124 Correct 615 ms 278220 KB Output is correct
125 Correct 778 ms 278668 KB Output is correct
126 Correct 1029 ms 325320 KB Output is correct
127 Correct 861 ms 292724 KB Output is correct
128 Correct 605 ms 289732 KB Output is correct
129 Correct 933 ms 293728 KB Output is correct
130 Correct 759 ms 293740 KB Output is correct