Submission #580379

# Submission time Handle Problem Language Result Execution time Memory
580379 2022-06-21T07:33:12 Z balbit Jail (JOI22_jail) C++14
100 / 100
1060 ms 287912 KB
#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 = 120000+5;


vector<int> tree[maxn];
int dep[maxn];
int fa[18][maxn];
int tos[18][maxn], toe[18][maxn];
int nxt[maxn];
int st[maxn], en[maxn]; // starting or ending???
vector<int> g[maxn * 37];

int IT = 0;

void dfs(int v, int p) {
    tos[0][v] = IT++; toe[0][v] = IT++;
    if (st[v] != -1) {
        g[tos[0][v]].pb(st[v]);
    }
    if (en[v] != -1) {
        g[en[v]].pb(toe[0][v]);
    }
    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;
}


int indeg[maxn*37];

int kth(int a, int k) {
    REP(j,18) {
        if (k & (1<<j)) a = fa[j][a];
    }
    return a;
}

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;
        }

        IT = q;
        REP(i,q + n * 36) g[i].clear();

        dfs(0,-1);

        REP1(j, 18-1) {
            REP(i,n) {
                fa[j][i] = fa[j-1][fa[j-1][i]];
//                int F = fa[j][i];
                if (dep[i] + 1 - (1<<j) >= 0) {
                    tos[j][i] = IT++;
                    g[tos[j][i]].pb(tos[j-1][i]);
                    g[tos[j][i]].pb(tos[j-1][fa[j-1][i]]);

                    toe[j][i] = IT++;
                    g[toe[j-1][i]].pb(toe[j][i]);
                    g[toe[j-1][fa[j-1][i]]].pb(toe[j][i]);
                }
            }
        }


        REP(i,q) {
//            if (i) continue;
            int a = snode[i], b = enode[i];
            int c = lca(a,b);
//            bool yo = 0;

            for (int go : {a,b}) {

                // do s
                {
                    int v = go;
                    if (st[v] == i && st[c] == i) goto bruh;
                    if (st[v] == i) v = fa[0][v];
                    if (dep[v] < dep[c]) goto bruh;
                    if (st[c] == i) {
                        int ho = dep[v] - dep[c] - 1;
                        bug(ho);
                        c = kth(v, ho);
                    }
                    if (st[v] != i && dep[v] >= dep[c]) {
                        int gap = dep[v] - dep[c] + 1;
                        int L = __lg(gap);
                        bug(v,c,gap,L);
                        g[i].pb(tos[L][v]);
                        int nh = -dep[c] + dep[v] + 1 - (1<<L);
                        bug(kth(v, nh), L);
                        g[i].pb(tos[L][kth(v,nh)]);
                    }
                }
                bruh:;
                {
                    int v = go;
                    if (en[v] == i && en[c] == i) goto bruhh;
                    if (en[v] == i) v = fa[0][v];
                    if (dep[v] < dep[c]) goto bruhh;
                    if (en[c] == i) {
                        int ho = dep[v] - dep[c] - 1;
                        bug(ho);
                        c = kth(v, ho);
                    }
                    if (en[v] != i && dep[v] >= dep[c]) {
                        int gap = dep[v] - dep[c] + 1;
                        int L = __lg(gap);
                        bug(v,c,gap,L);
                        g[toe[L][v]].pb(i);
                        int nh = -dep[c] + dep[v] + 1 - (1<<L);
                        bug(kth(v, nh), L);
                        g[toe[L][kth(v,nh)]].pb(i);
                    }
                }
                bruhh:;

            }
        }

        bug("out");

        REP(i,IT) {
            indeg[i] = 0;
        }
        REP(i,IT) {
            for (int u : g[i]) {
                ++indeg[u];
            }
        }
        queue<int> qq;
        REP(i,IT) {
            if (indeg[i] == 0) {
                qq.push(i);
            }
        }
        int done = 0;
        while (!qq.empty() ){
            int v = qq.front(); qq.pop();
            ++done;
            bug(v, done);
            for (int u : g[v]) {
                if (--indeg[u] == 0) {
                    qq.push(u);
                }
            }
        }

        bug(done, IT);
        cout<<(done==IT?"Yes":"No")<<endl;

    }


}
# Verdict Execution time Memory Grader output
1 Correct 52 ms 107532 KB Output is correct
2 Correct 54 ms 107468 KB Output is correct
3 Correct 55 ms 107420 KB Output is correct
4 Correct 83 ms 107664 KB Output is correct
5 Correct 130 ms 107668 KB Output is correct
6 Correct 55 ms 107700 KB Output is correct
7 Correct 62 ms 107748 KB Output is correct
8 Correct 56 ms 107708 KB Output is correct
9 Correct 243 ms 114288 KB Output is correct
10 Correct 546 ms 276008 KB Output is correct
11 Correct 67 ms 107596 KB Output is correct
12 Correct 143 ms 107680 KB Output is correct
13 Correct 696 ms 279228 KB Output is correct
14 Correct 538 ms 279164 KB Output is correct
15 Correct 669 ms 279720 KB Output is correct
16 Correct 1016 ms 287912 KB Output is correct
17 Correct 594 ms 282004 KB Output is correct
18 Correct 630 ms 287276 KB Output is correct
19 Correct 620 ms 282036 KB Output is correct
20 Correct 568 ms 282200 KB Output is correct
21 Correct 510 ms 283516 KB Output is correct
22 Correct 510 ms 281916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 107564 KB Output is correct
2 Correct 57 ms 107544 KB Output is correct
3 Correct 60 ms 107776 KB Output is correct
4 Correct 63 ms 107704 KB Output is correct
5 Correct 64 ms 107716 KB Output is correct
6 Correct 56 ms 107708 KB Output is correct
7 Correct 58 ms 107672 KB Output is correct
8 Correct 58 ms 107596 KB Output is correct
9 Correct 55 ms 107604 KB Output is correct
10 Correct 55 ms 107596 KB Output is correct
11 Correct 54 ms 107592 KB Output is correct
12 Correct 61 ms 107512 KB Output is correct
13 Correct 64 ms 107652 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 107564 KB Output is correct
2 Correct 57 ms 107544 KB Output is correct
3 Correct 60 ms 107776 KB Output is correct
4 Correct 63 ms 107704 KB Output is correct
5 Correct 64 ms 107716 KB Output is correct
6 Correct 56 ms 107708 KB Output is correct
7 Correct 58 ms 107672 KB Output is correct
8 Correct 58 ms 107596 KB Output is correct
9 Correct 55 ms 107604 KB Output is correct
10 Correct 55 ms 107596 KB Output is correct
11 Correct 54 ms 107592 KB Output is correct
12 Correct 61 ms 107512 KB Output is correct
13 Correct 64 ms 107652 KB Output is correct
14 Correct 53 ms 107540 KB Output is correct
15 Correct 51 ms 107536 KB Output is correct
16 Correct 66 ms 107780 KB Output is correct
17 Correct 56 ms 107676 KB Output is correct
18 Correct 58 ms 107648 KB Output is correct
19 Correct 53 ms 107472 KB Output is correct
20 Correct 61 ms 107724 KB Output is correct
21 Correct 62 ms 107724 KB Output is correct
22 Correct 56 ms 107720 KB Output is correct
23 Correct 55 ms 107468 KB Output is correct
24 Correct 66 ms 107596 KB Output is correct
25 Correct 60 ms 107740 KB Output is correct
26 Correct 54 ms 107692 KB Output is correct
27 Correct 66 ms 107776 KB Output is correct
28 Correct 57 ms 107572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 107564 KB Output is correct
2 Correct 57 ms 107544 KB Output is correct
3 Correct 60 ms 107776 KB Output is correct
4 Correct 63 ms 107704 KB Output is correct
5 Correct 64 ms 107716 KB Output is correct
6 Correct 56 ms 107708 KB Output is correct
7 Correct 58 ms 107672 KB Output is correct
8 Correct 58 ms 107596 KB Output is correct
9 Correct 55 ms 107604 KB Output is correct
10 Correct 55 ms 107596 KB Output is correct
11 Correct 54 ms 107592 KB Output is correct
12 Correct 61 ms 107512 KB Output is correct
13 Correct 64 ms 107652 KB Output is correct
14 Correct 53 ms 107540 KB Output is correct
15 Correct 51 ms 107536 KB Output is correct
16 Correct 66 ms 107780 KB Output is correct
17 Correct 56 ms 107676 KB Output is correct
18 Correct 58 ms 107648 KB Output is correct
19 Correct 53 ms 107472 KB Output is correct
20 Correct 61 ms 107724 KB Output is correct
21 Correct 62 ms 107724 KB Output is correct
22 Correct 56 ms 107720 KB Output is correct
23 Correct 55 ms 107468 KB Output is correct
24 Correct 66 ms 107596 KB Output is correct
25 Correct 60 ms 107740 KB Output is correct
26 Correct 54 ms 107692 KB Output is correct
27 Correct 66 ms 107776 KB Output is correct
28 Correct 57 ms 107572 KB Output is correct
29 Correct 68 ms 107788 KB Output is correct
30 Correct 66 ms 107640 KB Output is correct
31 Correct 65 ms 107796 KB Output is correct
32 Correct 66 ms 107764 KB Output is correct
33 Correct 60 ms 107716 KB Output is correct
34 Correct 67 ms 107656 KB Output is correct
35 Correct 71 ms 107708 KB Output is correct
36 Correct 67 ms 107668 KB Output is correct
37 Correct 70 ms 107724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 107564 KB Output is correct
2 Correct 57 ms 107544 KB Output is correct
3 Correct 60 ms 107776 KB Output is correct
4 Correct 63 ms 107704 KB Output is correct
5 Correct 64 ms 107716 KB Output is correct
6 Correct 56 ms 107708 KB Output is correct
7 Correct 58 ms 107672 KB Output is correct
8 Correct 58 ms 107596 KB Output is correct
9 Correct 55 ms 107604 KB Output is correct
10 Correct 55 ms 107596 KB Output is correct
11 Correct 54 ms 107592 KB Output is correct
12 Correct 61 ms 107512 KB Output is correct
13 Correct 64 ms 107652 KB Output is correct
14 Correct 53 ms 107540 KB Output is correct
15 Correct 51 ms 107536 KB Output is correct
16 Correct 66 ms 107780 KB Output is correct
17 Correct 56 ms 107676 KB Output is correct
18 Correct 58 ms 107648 KB Output is correct
19 Correct 53 ms 107472 KB Output is correct
20 Correct 61 ms 107724 KB Output is correct
21 Correct 62 ms 107724 KB Output is correct
22 Correct 56 ms 107720 KB Output is correct
23 Correct 55 ms 107468 KB Output is correct
24 Correct 66 ms 107596 KB Output is correct
25 Correct 60 ms 107740 KB Output is correct
26 Correct 54 ms 107692 KB Output is correct
27 Correct 66 ms 107776 KB Output is correct
28 Correct 57 ms 107572 KB Output is correct
29 Correct 68 ms 107788 KB Output is correct
30 Correct 66 ms 107640 KB Output is correct
31 Correct 65 ms 107796 KB Output is correct
32 Correct 66 ms 107764 KB Output is correct
33 Correct 60 ms 107716 KB Output is correct
34 Correct 67 ms 107656 KB Output is correct
35 Correct 71 ms 107708 KB Output is correct
36 Correct 67 ms 107668 KB Output is correct
37 Correct 70 ms 107724 KB Output is correct
38 Correct 254 ms 114184 KB Output is correct
39 Correct 624 ms 275928 KB Output is correct
40 Correct 247 ms 114276 KB Output is correct
41 Correct 185 ms 110488 KB Output is correct
42 Correct 150 ms 112572 KB Output is correct
43 Correct 306 ms 114440 KB Output is correct
44 Correct 79 ms 108128 KB Output is correct
45 Correct 473 ms 176764 KB Output is correct
46 Correct 432 ms 177060 KB Output is correct
47 Correct 729 ms 260996 KB Output is correct
48 Correct 806 ms 261028 KB Output is correct
49 Correct 569 ms 217608 KB Output is correct
50 Correct 584 ms 217648 KB Output is correct
51 Correct 695 ms 246608 KB Output is correct
52 Correct 692 ms 246708 KB Output is correct
53 Correct 95 ms 111880 KB Output is correct
54 Correct 487 ms 168116 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 61 ms 107448 KB Output is correct
2 Correct 67 ms 107484 KB Output is correct
3 Correct 58 ms 107572 KB Output is correct
4 Correct 69 ms 107504 KB Output is correct
5 Correct 70 ms 107592 KB Output is correct
6 Correct 59 ms 107484 KB Output is correct
7 Correct 62 ms 107604 KB Output is correct
8 Correct 55 ms 107552 KB Output is correct
9 Correct 58 ms 107436 KB Output is correct
10 Correct 58 ms 107616 KB Output is correct
11 Correct 65 ms 107548 KB Output is correct
12 Correct 71 ms 107628 KB Output is correct
13 Correct 107 ms 107652 KB Output is correct
14 Correct 119 ms 107620 KB Output is correct
15 Correct 114 ms 107652 KB Output is correct
16 Correct 401 ms 154572 KB Output is correct
17 Correct 532 ms 161480 KB Output is correct
18 Correct 715 ms 169012 KB Output is correct
19 Correct 438 ms 156232 KB Output is correct
20 Correct 468 ms 155816 KB Output is correct
21 Correct 449 ms 155932 KB Output is correct
22 Correct 444 ms 160844 KB Output is correct
23 Correct 408 ms 160844 KB Output is correct
24 Correct 491 ms 161040 KB Output is correct
25 Correct 500 ms 161356 KB Output is correct
26 Correct 538 ms 161236 KB Output is correct
27 Correct 651 ms 149156 KB Output is correct
28 Correct 583 ms 149188 KB Output is correct
29 Correct 580 ms 149160 KB Output is correct
30 Correct 475 ms 146828 KB Output is correct
31 Correct 441 ms 146880 KB Output is correct
32 Correct 490 ms 146860 KB Output is correct
33 Correct 407 ms 146892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 107532 KB Output is correct
2 Correct 54 ms 107468 KB Output is correct
3 Correct 55 ms 107420 KB Output is correct
4 Correct 83 ms 107664 KB Output is correct
5 Correct 130 ms 107668 KB Output is correct
6 Correct 55 ms 107700 KB Output is correct
7 Correct 62 ms 107748 KB Output is correct
8 Correct 56 ms 107708 KB Output is correct
9 Correct 243 ms 114288 KB Output is correct
10 Correct 546 ms 276008 KB Output is correct
11 Correct 67 ms 107596 KB Output is correct
12 Correct 143 ms 107680 KB Output is correct
13 Correct 696 ms 279228 KB Output is correct
14 Correct 538 ms 279164 KB Output is correct
15 Correct 669 ms 279720 KB Output is correct
16 Correct 1016 ms 287912 KB Output is correct
17 Correct 594 ms 282004 KB Output is correct
18 Correct 630 ms 287276 KB Output is correct
19 Correct 620 ms 282036 KB Output is correct
20 Correct 568 ms 282200 KB Output is correct
21 Correct 510 ms 283516 KB Output is correct
22 Correct 510 ms 281916 KB Output is correct
23 Correct 54 ms 107564 KB Output is correct
24 Correct 57 ms 107544 KB Output is correct
25 Correct 60 ms 107776 KB Output is correct
26 Correct 63 ms 107704 KB Output is correct
27 Correct 64 ms 107716 KB Output is correct
28 Correct 56 ms 107708 KB Output is correct
29 Correct 58 ms 107672 KB Output is correct
30 Correct 58 ms 107596 KB Output is correct
31 Correct 55 ms 107604 KB Output is correct
32 Correct 55 ms 107596 KB Output is correct
33 Correct 54 ms 107592 KB Output is correct
34 Correct 61 ms 107512 KB Output is correct
35 Correct 64 ms 107652 KB Output is correct
36 Correct 53 ms 107540 KB Output is correct
37 Correct 51 ms 107536 KB Output is correct
38 Correct 66 ms 107780 KB Output is correct
39 Correct 56 ms 107676 KB Output is correct
40 Correct 58 ms 107648 KB Output is correct
41 Correct 53 ms 107472 KB Output is correct
42 Correct 61 ms 107724 KB Output is correct
43 Correct 62 ms 107724 KB Output is correct
44 Correct 56 ms 107720 KB Output is correct
45 Correct 55 ms 107468 KB Output is correct
46 Correct 66 ms 107596 KB Output is correct
47 Correct 60 ms 107740 KB Output is correct
48 Correct 54 ms 107692 KB Output is correct
49 Correct 66 ms 107776 KB Output is correct
50 Correct 57 ms 107572 KB Output is correct
51 Correct 68 ms 107788 KB Output is correct
52 Correct 66 ms 107640 KB Output is correct
53 Correct 65 ms 107796 KB Output is correct
54 Correct 66 ms 107764 KB Output is correct
55 Correct 60 ms 107716 KB Output is correct
56 Correct 67 ms 107656 KB Output is correct
57 Correct 71 ms 107708 KB Output is correct
58 Correct 67 ms 107668 KB Output is correct
59 Correct 70 ms 107724 KB Output is correct
60 Correct 254 ms 114184 KB Output is correct
61 Correct 624 ms 275928 KB Output is correct
62 Correct 247 ms 114276 KB Output is correct
63 Correct 185 ms 110488 KB Output is correct
64 Correct 150 ms 112572 KB Output is correct
65 Correct 306 ms 114440 KB Output is correct
66 Correct 79 ms 108128 KB Output is correct
67 Correct 473 ms 176764 KB Output is correct
68 Correct 432 ms 177060 KB Output is correct
69 Correct 729 ms 260996 KB Output is correct
70 Correct 806 ms 261028 KB Output is correct
71 Correct 569 ms 217608 KB Output is correct
72 Correct 584 ms 217648 KB Output is correct
73 Correct 695 ms 246608 KB Output is correct
74 Correct 692 ms 246708 KB Output is correct
75 Correct 95 ms 111880 KB Output is correct
76 Correct 487 ms 168116 KB Output is correct
77 Correct 61 ms 107448 KB Output is correct
78 Correct 67 ms 107484 KB Output is correct
79 Correct 58 ms 107572 KB Output is correct
80 Correct 69 ms 107504 KB Output is correct
81 Correct 70 ms 107592 KB Output is correct
82 Correct 59 ms 107484 KB Output is correct
83 Correct 62 ms 107604 KB Output is correct
84 Correct 55 ms 107552 KB Output is correct
85 Correct 58 ms 107436 KB Output is correct
86 Correct 58 ms 107616 KB Output is correct
87 Correct 65 ms 107548 KB Output is correct
88 Correct 71 ms 107628 KB Output is correct
89 Correct 107 ms 107652 KB Output is correct
90 Correct 119 ms 107620 KB Output is correct
91 Correct 114 ms 107652 KB Output is correct
92 Correct 401 ms 154572 KB Output is correct
93 Correct 532 ms 161480 KB Output is correct
94 Correct 715 ms 169012 KB Output is correct
95 Correct 438 ms 156232 KB Output is correct
96 Correct 468 ms 155816 KB Output is correct
97 Correct 449 ms 155932 KB Output is correct
98 Correct 444 ms 160844 KB Output is correct
99 Correct 408 ms 160844 KB Output is correct
100 Correct 491 ms 161040 KB Output is correct
101 Correct 500 ms 161356 KB Output is correct
102 Correct 538 ms 161236 KB Output is correct
103 Correct 651 ms 149156 KB Output is correct
104 Correct 583 ms 149188 KB Output is correct
105 Correct 580 ms 149160 KB Output is correct
106 Correct 475 ms 146828 KB Output is correct
107 Correct 441 ms 146880 KB Output is correct
108 Correct 490 ms 146860 KB Output is correct
109 Correct 407 ms 146892 KB Output is correct
110 Correct 122 ms 108748 KB Output is correct
111 Correct 115 ms 108260 KB Output is correct
112 Correct 885 ms 269864 KB Output is correct
113 Correct 948 ms 265220 KB Output is correct
114 Correct 535 ms 209032 KB Output is correct
115 Correct 148 ms 136260 KB Output is correct
116 Correct 778 ms 174360 KB Output is correct
117 Correct 918 ms 181736 KB Output is correct
118 Correct 486 ms 165320 KB Output is correct
119 Correct 491 ms 165368 KB Output is correct
120 Correct 95 ms 113492 KB Output is correct
121 Correct 929 ms 185164 KB Output is correct
122 Correct 859 ms 181872 KB Output is correct
123 Correct 1016 ms 271812 KB Output is correct
124 Correct 638 ms 271724 KB Output is correct
125 Correct 1060 ms 272388 KB Output is correct
126 Correct 1010 ms 281660 KB Output is correct
127 Correct 605 ms 263796 KB Output is correct
128 Correct 536 ms 263852 KB Output is correct
129 Correct 830 ms 263752 KB Output is correct
130 Correct 565 ms 263952 KB Output is correct