Submission #879538

# Submission time Handle Problem Language Result Execution time Memory
879538 2023-11-27T15:50:41 Z Shayan86 Jail (JOI22_jail) C++17
100 / 100
2089 ms 454880 KB
#include <bits/stdc++.h>
using namespace std;

#pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// Ofast, O0, O1, O2, O3, unroll-loops, fast-math, trapv

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;

#define Mp          make_pair
#define sep         ' '
#define endl        '\n'
#define F	        first
#define S	        second
#define pb          push_back
#define all(x)      (x).begin(),(x).end()
#define kill(res)	cout << res << '\n', exit(0);
#define set_dec(x)	cout << fixed << setprecision(x);
#define fast_io     ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io     freopen("input.txt", "r", stdin) ; freopen("output.txt", "w", stdout);

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

const ll L = 20;
const ll K = 5e6;
const ll N = 12e4 + 50;
const ll Mod = 1e9 + 7;

ll n, m, k, s[N], t[N], qu[N][2], par[N][L], id[N][L][2], h[N], idx[K];

bool mark[K];
vector<int> adj[N], neigh[K], topol;

void dfs(int v, int p = 0){
    par[v][0] = p;
    id[v][0][0] = ++k;
    if(qu[v][0]) neigh[qu[v][0]].pb(k);
    id[v][0][1] = ++k;
    if(qu[v][1]) neigh[k].pb(qu[v][1]);

    for(int i = 1; i < L; i++){
        par[v][i] = par[par[v][i-1]][i-1];

        id[v][i][0] = ++k;
        neigh[id[v][i-1][0]].pb(k);
        if(par[v][i-1]) neigh[id[par[v][i-1]][i-1][0]].pb(k);
        id[v][i][1] = ++k;
        neigh[k].pb(id[v][i-1][1]);
        if(par[v][i-1]) neigh[k].pb(id[par[v][i-1]][i-1][1]);
    }

    for(int u: adj[v]){
        if(u == p) continue;
        h[u] = h[v] + 1; dfs(u, v);
    }
}

int getPar(int v, int x){
    for(int i = 0; i < L; i++){
        if(x >> i & 1) v = par[v][i];
    }
    return v;
}

int lca(int v, int u){
    if(h[v] < h[u]) swap(u, v);
    v = getPar(v, h[v] - h[u]);
    if(v == u) return v;

    for(int i = L-1; i >= 0; i--){
        if(par[v][i] != par[u][i]){
            v = par[v][i]; u = par[u][i];
        }
    }
    return par[v][0];
}

void add0(int x, int v, int p){
    int y = h[v] - h[p];
    if(y <= 0) return;

    for(int i = 0; i < L; i++){
        if(y >> i & 1){
            neigh[id[v][i][0]].pb(x);
            v = par[v][i];
        }
    }
}

void add1(int x, int v, int p){
    int y = h[v] - h[p];
    if(y <= 0) return;

    for(int i = 0; i < L; i++){
        if(y >> i & 1){
            neigh[x].pb(id[v][i][1]);
            v = par[v][i];
        }
    }
}

void tdfs(int v){
    mark[v] = true;
    for(int u: neigh[v]){
        if(!mark[u]) tdfs(u);
    }
    topol.pb(v);
}

int main(){
    fast_io;

    int T;
    cin >> T;

    while(T--){
        cin >> n;

        int u, v;
        for(int i = 1; i < n; i++){
            cin >> u >> v;
            adj[u].pb(v);
            adj[v].pb(u);
        }

        cin >> m;

        for(int i = 1; i <= m; i++){
            cin >> s[i] >> t[i]; 
            qu[s[i]][0] = i; qu[t[i]][1] = i;
        }

        k = m;
        h[1] = 1; dfs(1);

        for(int i = 1; i <= m; i++){
            int slt = lca(s[i], t[i]);

            if(s[i] == slt){
                add0(i, t[i], slt);
                add1(i, par[t[i]][0], par[slt][0]);
                continue;
            }
            if(t[i] == slt){
                add0(i, par[s[i]][0], par[slt][0]);
                add1(i, s[i], slt);
                continue;
            }

            add0(i, par[s[i]][0], slt);
            add0(i, t[i], par[slt][0]);
            add1(i, s[i], par[slt][0]);
            add1(i, par[t[i]][0], slt);
        }

        for(int i = 1; i <= k; i++){
            if(!mark[i]) tdfs(i);
        }
        for(int i = 0; i < k; i++) idx[topol[i]] = i+1;

        bool ch = true;
        for(int i = 1; i <= k; i++){
            for(int j: neigh[i]) if(idx[j] > idx[i]) ch = false;
        }

        cout << (ch ? "Yes" : "No") << endl;

        topol.clear();
        for(int i = 1; i <= n; i++) adj[i].clear();
        for(int i = 1; i <= n; i++) qu[i][0] = qu[i][1] = 0;

        for(int i = 1; i <= k; i++){
            neigh[i].clear(); idx[i] = 0; mark[i] = 0;
        }

    }

}
# Verdict Execution time Memory Grader output
1 Correct 47 ms 121752 KB Output is correct
2 Correct 45 ms 121684 KB Output is correct
3 Correct 44 ms 121684 KB Output is correct
4 Correct 77 ms 122392 KB Output is correct
5 Correct 121 ms 122796 KB Output is correct
6 Correct 48 ms 122192 KB Output is correct
7 Correct 50 ms 122200 KB Output is correct
8 Correct 51 ms 122204 KB Output is correct
9 Correct 344 ms 136900 KB Output is correct
10 Correct 1369 ms 402220 KB Output is correct
11 Correct 57 ms 121940 KB Output is correct
12 Correct 133 ms 123220 KB Output is correct
13 Correct 1254 ms 408900 KB Output is correct
14 Correct 1152 ms 409352 KB Output is correct
15 Correct 1635 ms 419408 KB Output is correct
16 Correct 2089 ms 454880 KB Output is correct
17 Correct 1470 ms 412264 KB Output is correct
18 Correct 1259 ms 415720 KB Output is correct
19 Correct 1306 ms 411232 KB Output is correct
20 Correct 1312 ms 411148 KB Output is correct
21 Correct 1458 ms 415120 KB Output is correct
22 Correct 1084 ms 408184 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 121692 KB Output is correct
2 Correct 41 ms 121680 KB Output is correct
3 Correct 47 ms 122200 KB Output is correct
4 Correct 43 ms 122192 KB Output is correct
5 Correct 59 ms 122192 KB Output is correct
6 Correct 42 ms 122220 KB Output is correct
7 Correct 43 ms 122196 KB Output is correct
8 Correct 43 ms 122252 KB Output is correct
9 Correct 43 ms 122196 KB Output is correct
10 Correct 44 ms 122360 KB Output is correct
11 Correct 43 ms 122204 KB Output is correct
12 Correct 41 ms 122200 KB Output is correct
13 Correct 41 ms 122204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 121692 KB Output is correct
2 Correct 41 ms 121680 KB Output is correct
3 Correct 47 ms 122200 KB Output is correct
4 Correct 43 ms 122192 KB Output is correct
5 Correct 59 ms 122192 KB Output is correct
6 Correct 42 ms 122220 KB Output is correct
7 Correct 43 ms 122196 KB Output is correct
8 Correct 43 ms 122252 KB Output is correct
9 Correct 43 ms 122196 KB Output is correct
10 Correct 44 ms 122360 KB Output is correct
11 Correct 43 ms 122204 KB Output is correct
12 Correct 41 ms 122200 KB Output is correct
13 Correct 41 ms 122204 KB Output is correct
14 Correct 39 ms 121500 KB Output is correct
15 Correct 45 ms 121760 KB Output is correct
16 Correct 42 ms 122204 KB Output is correct
17 Correct 44 ms 122200 KB Output is correct
18 Correct 42 ms 122192 KB Output is correct
19 Correct 43 ms 121692 KB Output is correct
20 Correct 43 ms 122192 KB Output is correct
21 Correct 44 ms 122392 KB Output is correct
22 Correct 43 ms 122204 KB Output is correct
23 Correct 38 ms 121684 KB Output is correct
24 Correct 41 ms 121940 KB Output is correct
25 Correct 43 ms 122384 KB Output is correct
26 Correct 40 ms 122332 KB Output is correct
27 Correct 43 ms 122192 KB Output is correct
28 Correct 38 ms 121680 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 121692 KB Output is correct
2 Correct 41 ms 121680 KB Output is correct
3 Correct 47 ms 122200 KB Output is correct
4 Correct 43 ms 122192 KB Output is correct
5 Correct 59 ms 122192 KB Output is correct
6 Correct 42 ms 122220 KB Output is correct
7 Correct 43 ms 122196 KB Output is correct
8 Correct 43 ms 122252 KB Output is correct
9 Correct 43 ms 122196 KB Output is correct
10 Correct 44 ms 122360 KB Output is correct
11 Correct 43 ms 122204 KB Output is correct
12 Correct 41 ms 122200 KB Output is correct
13 Correct 41 ms 122204 KB Output is correct
14 Correct 39 ms 121500 KB Output is correct
15 Correct 45 ms 121760 KB Output is correct
16 Correct 42 ms 122204 KB Output is correct
17 Correct 44 ms 122200 KB Output is correct
18 Correct 42 ms 122192 KB Output is correct
19 Correct 43 ms 121692 KB Output is correct
20 Correct 43 ms 122192 KB Output is correct
21 Correct 44 ms 122392 KB Output is correct
22 Correct 43 ms 122204 KB Output is correct
23 Correct 38 ms 121684 KB Output is correct
24 Correct 41 ms 121940 KB Output is correct
25 Correct 43 ms 122384 KB Output is correct
26 Correct 40 ms 122332 KB Output is correct
27 Correct 43 ms 122192 KB Output is correct
28 Correct 38 ms 121680 KB Output is correct
29 Correct 43 ms 122448 KB Output is correct
30 Correct 44 ms 122196 KB Output is correct
31 Correct 46 ms 122188 KB Output is correct
32 Correct 48 ms 122400 KB Output is correct
33 Correct 43 ms 122192 KB Output is correct
34 Correct 42 ms 122204 KB Output is correct
35 Correct 43 ms 122200 KB Output is correct
36 Correct 43 ms 122192 KB Output is correct
37 Correct 41 ms 122248 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 121692 KB Output is correct
2 Correct 41 ms 121680 KB Output is correct
3 Correct 47 ms 122200 KB Output is correct
4 Correct 43 ms 122192 KB Output is correct
5 Correct 59 ms 122192 KB Output is correct
6 Correct 42 ms 122220 KB Output is correct
7 Correct 43 ms 122196 KB Output is correct
8 Correct 43 ms 122252 KB Output is correct
9 Correct 43 ms 122196 KB Output is correct
10 Correct 44 ms 122360 KB Output is correct
11 Correct 43 ms 122204 KB Output is correct
12 Correct 41 ms 122200 KB Output is correct
13 Correct 41 ms 122204 KB Output is correct
14 Correct 39 ms 121500 KB Output is correct
15 Correct 45 ms 121760 KB Output is correct
16 Correct 42 ms 122204 KB Output is correct
17 Correct 44 ms 122200 KB Output is correct
18 Correct 42 ms 122192 KB Output is correct
19 Correct 43 ms 121692 KB Output is correct
20 Correct 43 ms 122192 KB Output is correct
21 Correct 44 ms 122392 KB Output is correct
22 Correct 43 ms 122204 KB Output is correct
23 Correct 38 ms 121684 KB Output is correct
24 Correct 41 ms 121940 KB Output is correct
25 Correct 43 ms 122384 KB Output is correct
26 Correct 40 ms 122332 KB Output is correct
27 Correct 43 ms 122192 KB Output is correct
28 Correct 38 ms 121680 KB Output is correct
29 Correct 43 ms 122448 KB Output is correct
30 Correct 44 ms 122196 KB Output is correct
31 Correct 46 ms 122188 KB Output is correct
32 Correct 48 ms 122400 KB Output is correct
33 Correct 43 ms 122192 KB Output is correct
34 Correct 42 ms 122204 KB Output is correct
35 Correct 43 ms 122200 KB Output is correct
36 Correct 43 ms 122192 KB Output is correct
37 Correct 41 ms 122248 KB Output is correct
38 Correct 319 ms 136904 KB Output is correct
39 Correct 1287 ms 402276 KB Output is correct
40 Correct 283 ms 138072 KB Output is correct
41 Correct 224 ms 137408 KB Output is correct
42 Correct 270 ms 137512 KB Output is correct
43 Correct 296 ms 137536 KB Output is correct
44 Correct 59 ms 124376 KB Output is correct
45 Correct 616 ms 391284 KB Output is correct
46 Correct 611 ms 391288 KB Output is correct
47 Correct 1104 ms 395896 KB Output is correct
48 Correct 1091 ms 395888 KB Output is correct
49 Correct 654 ms 394364 KB Output is correct
50 Correct 668 ms 394196 KB Output is correct
51 Correct 804 ms 395772 KB Output is correct
52 Correct 767 ms 395696 KB Output is correct
53 Correct 94 ms 140624 KB Output is correct
54 Correct 633 ms 391308 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 40 ms 121524 KB Output is correct
2 Correct 36 ms 121692 KB Output is correct
3 Correct 38 ms 121684 KB Output is correct
4 Correct 37 ms 121680 KB Output is correct
5 Correct 51 ms 121936 KB Output is correct
6 Correct 39 ms 122204 KB Output is correct
7 Correct 39 ms 122332 KB Output is correct
8 Correct 38 ms 121776 KB Output is correct
9 Correct 39 ms 121764 KB Output is correct
10 Correct 39 ms 121936 KB Output is correct
11 Correct 38 ms 121556 KB Output is correct
12 Correct 42 ms 122204 KB Output is correct
13 Correct 88 ms 122672 KB Output is correct
14 Correct 123 ms 122960 KB Output is correct
15 Correct 99 ms 122732 KB Output is correct
16 Correct 625 ms 392348 KB Output is correct
17 Correct 813 ms 402692 KB Output is correct
18 Correct 1036 ms 420336 KB Output is correct
19 Correct 661 ms 393844 KB Output is correct
20 Correct 673 ms 393844 KB Output is correct
21 Correct 647 ms 393792 KB Output is correct
22 Correct 697 ms 400764 KB Output is correct
23 Correct 701 ms 400084 KB Output is correct
24 Correct 714 ms 399840 KB Output is correct
25 Correct 643 ms 399864 KB Output is correct
26 Correct 651 ms 399732 KB Output is correct
27 Correct 680 ms 405136 KB Output is correct
28 Correct 732 ms 413292 KB Output is correct
29 Correct 722 ms 407688 KB Output is correct
30 Correct 693 ms 400404 KB Output is correct
31 Correct 694 ms 401748 KB Output is correct
32 Correct 676 ms 398760 KB Output is correct
33 Correct 695 ms 401436 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 121752 KB Output is correct
2 Correct 45 ms 121684 KB Output is correct
3 Correct 44 ms 121684 KB Output is correct
4 Correct 77 ms 122392 KB Output is correct
5 Correct 121 ms 122796 KB Output is correct
6 Correct 48 ms 122192 KB Output is correct
7 Correct 50 ms 122200 KB Output is correct
8 Correct 51 ms 122204 KB Output is correct
9 Correct 344 ms 136900 KB Output is correct
10 Correct 1369 ms 402220 KB Output is correct
11 Correct 57 ms 121940 KB Output is correct
12 Correct 133 ms 123220 KB Output is correct
13 Correct 1254 ms 408900 KB Output is correct
14 Correct 1152 ms 409352 KB Output is correct
15 Correct 1635 ms 419408 KB Output is correct
16 Correct 2089 ms 454880 KB Output is correct
17 Correct 1470 ms 412264 KB Output is correct
18 Correct 1259 ms 415720 KB Output is correct
19 Correct 1306 ms 411232 KB Output is correct
20 Correct 1312 ms 411148 KB Output is correct
21 Correct 1458 ms 415120 KB Output is correct
22 Correct 1084 ms 408184 KB Output is correct
23 Correct 41 ms 121692 KB Output is correct
24 Correct 41 ms 121680 KB Output is correct
25 Correct 47 ms 122200 KB Output is correct
26 Correct 43 ms 122192 KB Output is correct
27 Correct 59 ms 122192 KB Output is correct
28 Correct 42 ms 122220 KB Output is correct
29 Correct 43 ms 122196 KB Output is correct
30 Correct 43 ms 122252 KB Output is correct
31 Correct 43 ms 122196 KB Output is correct
32 Correct 44 ms 122360 KB Output is correct
33 Correct 43 ms 122204 KB Output is correct
34 Correct 41 ms 122200 KB Output is correct
35 Correct 41 ms 122204 KB Output is correct
36 Correct 39 ms 121500 KB Output is correct
37 Correct 45 ms 121760 KB Output is correct
38 Correct 42 ms 122204 KB Output is correct
39 Correct 44 ms 122200 KB Output is correct
40 Correct 42 ms 122192 KB Output is correct
41 Correct 43 ms 121692 KB Output is correct
42 Correct 43 ms 122192 KB Output is correct
43 Correct 44 ms 122392 KB Output is correct
44 Correct 43 ms 122204 KB Output is correct
45 Correct 38 ms 121684 KB Output is correct
46 Correct 41 ms 121940 KB Output is correct
47 Correct 43 ms 122384 KB Output is correct
48 Correct 40 ms 122332 KB Output is correct
49 Correct 43 ms 122192 KB Output is correct
50 Correct 38 ms 121680 KB Output is correct
51 Correct 43 ms 122448 KB Output is correct
52 Correct 44 ms 122196 KB Output is correct
53 Correct 46 ms 122188 KB Output is correct
54 Correct 48 ms 122400 KB Output is correct
55 Correct 43 ms 122192 KB Output is correct
56 Correct 42 ms 122204 KB Output is correct
57 Correct 43 ms 122200 KB Output is correct
58 Correct 43 ms 122192 KB Output is correct
59 Correct 41 ms 122248 KB Output is correct
60 Correct 319 ms 136904 KB Output is correct
61 Correct 1287 ms 402276 KB Output is correct
62 Correct 283 ms 138072 KB Output is correct
63 Correct 224 ms 137408 KB Output is correct
64 Correct 270 ms 137512 KB Output is correct
65 Correct 296 ms 137536 KB Output is correct
66 Correct 59 ms 124376 KB Output is correct
67 Correct 616 ms 391284 KB Output is correct
68 Correct 611 ms 391288 KB Output is correct
69 Correct 1104 ms 395896 KB Output is correct
70 Correct 1091 ms 395888 KB Output is correct
71 Correct 654 ms 394364 KB Output is correct
72 Correct 668 ms 394196 KB Output is correct
73 Correct 804 ms 395772 KB Output is correct
74 Correct 767 ms 395696 KB Output is correct
75 Correct 94 ms 140624 KB Output is correct
76 Correct 633 ms 391308 KB Output is correct
77 Correct 40 ms 121524 KB Output is correct
78 Correct 36 ms 121692 KB Output is correct
79 Correct 38 ms 121684 KB Output is correct
80 Correct 37 ms 121680 KB Output is correct
81 Correct 51 ms 121936 KB Output is correct
82 Correct 39 ms 122204 KB Output is correct
83 Correct 39 ms 122332 KB Output is correct
84 Correct 38 ms 121776 KB Output is correct
85 Correct 39 ms 121764 KB Output is correct
86 Correct 39 ms 121936 KB Output is correct
87 Correct 38 ms 121556 KB Output is correct
88 Correct 42 ms 122204 KB Output is correct
89 Correct 88 ms 122672 KB Output is correct
90 Correct 123 ms 122960 KB Output is correct
91 Correct 99 ms 122732 KB Output is correct
92 Correct 625 ms 392348 KB Output is correct
93 Correct 813 ms 402692 KB Output is correct
94 Correct 1036 ms 420336 KB Output is correct
95 Correct 661 ms 393844 KB Output is correct
96 Correct 673 ms 393844 KB Output is correct
97 Correct 647 ms 393792 KB Output is correct
98 Correct 697 ms 400764 KB Output is correct
99 Correct 701 ms 400084 KB Output is correct
100 Correct 714 ms 399840 KB Output is correct
101 Correct 643 ms 399864 KB Output is correct
102 Correct 651 ms 399732 KB Output is correct
103 Correct 680 ms 405136 KB Output is correct
104 Correct 732 ms 413292 KB Output is correct
105 Correct 722 ms 407688 KB Output is correct
106 Correct 693 ms 400404 KB Output is correct
107 Correct 694 ms 401748 KB Output is correct
108 Correct 676 ms 398760 KB Output is correct
109 Correct 695 ms 401436 KB Output is correct
110 Correct 132 ms 123476 KB Output is correct
111 Correct 98 ms 122792 KB Output is correct
112 Correct 1505 ms 421336 KB Output is correct
113 Correct 1257 ms 400708 KB Output is correct
114 Correct 1111 ms 408804 KB Output is correct
115 Correct 369 ms 390076 KB Output is correct
116 Correct 730 ms 398144 KB Output is correct
117 Correct 1142 ms 425360 KB Output is correct
118 Correct 665 ms 391308 KB Output is correct
119 Correct 657 ms 391416 KB Output is correct
120 Correct 76 ms 144576 KB Output is correct
121 Correct 837 ms 399868 KB Output is correct
122 Correct 761 ms 399648 KB Output is correct
123 Correct 1367 ms 402504 KB Output is correct
124 Correct 1304 ms 402716 KB Output is correct
125 Correct 1410 ms 403492 KB Output is correct
126 Correct 1919 ms 451748 KB Output is correct
127 Correct 1363 ms 412240 KB Output is correct
128 Correct 1252 ms 412412 KB Output is correct
129 Correct 1245 ms 412252 KB Output is correct
130 Correct 1290 ms 412280 KB Output is correct