Submission #1043504

# Submission time Handle Problem Language Result Execution time Memory
1043504 2024-08-04T10:16:22 Z vladilius Jail (JOI22_jail) C++17
100 / 100
482 ms 89428 KB
#include <bits/stdc++.h>
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define ins insert

struct STG{
    vector<vector<int>> g;
    vector<int> a, b;
    int n, m, k;
    STG(int& ns, int& ms, vector<int>& as, vector<int>& bs){
        n = ns;
        m = ms;
        a = as;
        b = bs;
        g.resize(m + 8 * n + 1);
        build1(1, 1, n);
        k = m + 4 * n;
        build2(1, 1, n);
    }
    void build1(int v, int tl, int tr){
        if (tl == tr){
            if (a[tl]) g[v + m].pb(a[tl]);
            return;
        }
        int tm = (tl + tr) / 2, vv = 2 * v;
        build1(vv, tl, tm);
        build1(vv + 1, tm + 1, tr);
        g[v + m].pb(vv + m);
        g[v + m].pb(vv + m + 1);
    }
    void build2(int v, int tl, int tr){
        if (tl == tr){
            if (b[tl]) g[b[tl]].pb(v + k);
            return;
        }
        int tm = (tl + tr) / 2, vv = 2 * v;
        build2(vv, tl, tm);
        build2(vv + 1, tm + 1, tr);
        g[vv + k].pb(v + k);
        g[vv + k + 1].pb(v + k);
    }
    void add1(int v, int tl, int tr, int& l, int& r, int& u){
        if (l > tr || r < tl) return;
        if (l <= tl && tr <= r){
            g[u].pb(v + m);
            return;
        }
        int tm = (tl + tr) / 2, vv = 2 * v;
        add1(vv, tl, tm, l, r, u);
        add1(vv + 1, tm + 1, tr, l, r, u);
    }
    void add1(int u, int l, int r){
        if (l > r) return;
        add1(1, 1, n, l, r, u);
    }
    void add2(int v, int tl, int tr, int& l, int& r, int& u){
        if (l > tr || r < tl) return;
        if (l <= tl && tr <= r){
            g[v + k].pb(u);
            return;
        }
        int tm = (tl + tr) / 2, vv = 2 * v;
        add2(vv, tl, tm, l, r, u);
        add2(vv + 1, tm + 1, tr, l, r, u);
    }
    void add2(int u, int l, int r){
        if (l > r) return;
        add2(1, 1, n, l, r, u);
    }
    vector<bool> used, seen;
    bool dfs(int v){
        used[v] = seen[v] = 1;
        for (int i: g[v]){
            if (used[i]){
                if (seen[i]){
                    return 0;
                }
                continue;
            }
            if (!dfs(i)) return 0;
        }
        seen[v] = 0;
        return 1;
    }
    bool check(){
        int nn = (int) g.size() - 1;
        used.resize(nn + 1);
        seen.resize(nn + 1);
        for (int i = 1; i <= nn; i++){
            if (!used[i]){
                if (!dfs(i)){
                    return 0;
                }
            }
        }
        return 1;
    }
};

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    
    int tt; cin>>tt;
    while (tt--){
        int n; cin>>n;
        vector<int> g[n + 1];
        for (int i = 1; i < n; i++){
            int x, y; cin>>x>>y;
            g[x].pb(y);
            g[y].pb(x);
        }
        int m; cin>>m;
        vector<int> s(m + 1), t(m + 1);
        for (int i = 1; i <= m; i++){
            cin>>s[i]>>t[i];
        }
        
        vector<int> sz(n + 1), h(n + 1), d(n + 1), p(n + 1);
        function<void(int, int)> fill = [&](int v, int pr){
            p[v] = pr;
            sz[v] = 1;
            for (int i: g[v]){
                if (sz[i]) continue;
                d[i] = d[v] + 1;
                fill(i, v);
                if (sz[i] > sz[h[v]]){
                    h[v] = i;
                }
                sz[v] += sz[i];
            }
        };
        fill(1, 0);
        
        vector<int> head(n + 1), pos(n + 1);
        int timer = 0;
        function<void(int, int)> fill_hld = [&](int v, int k){
            head[v] = k;
            pos[v] = ++timer;
            if (!h[v]) return;
            fill_hld(h[v], k);
            for (int i: g[v]){
                if (pos[i]) continue;
                fill_hld(i, i);
            }
        };
        fill_hld(1, 1);
        
        vector<int> a(n + 1), b(n + 1);
        for (int i = 1; i <= m; i++){
            a[pos[s[i]]] = i;
            b[pos[t[i]]] = i;
        }
        
        STG T(n, m, a, b);

        for (int i = 1; i <= m; i++){
            int x = s[i], y = t[i];
            while (head[x] != head[y]){
                if (d[head[x]] > d[head[y]]){
                    swap(x, y);
                }
                T.add1(i, pos[head[y]] + (head[y] == s[i]), pos[y] - (y == s[i]));
                T.add2(i, pos[head[y]] + (head[y] == t[i]), pos[y] - (y == t[i]));
                y = p[head[y]];
            }
            if (d[x] > d[y]) swap(x, y);
            T.add1(i, pos[x] + (x == s[i]), pos[y] - (y == s[i]));
            T.add2(i, pos[x] + (x == t[i]), pos[y] - (y == t[i]));
        }
        
        cout<<((T.check()) ? "Yes" : "No")<<"\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 14 ms 508 KB Output is correct
5 Correct 30 ms 512 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 3 ms 348 KB Output is correct
9 Correct 40 ms 3472 KB Output is correct
10 Correct 57 ms 58992 KB Output is correct
11 Correct 10 ms 348 KB Output is correct
12 Correct 47 ms 532 KB Output is correct
13 Correct 89 ms 66936 KB Output is correct
14 Correct 82 ms 67156 KB Output is correct
15 Correct 109 ms 71920 KB Output is correct
16 Correct 205 ms 89428 KB Output is correct
17 Correct 112 ms 75344 KB Output is correct
18 Correct 93 ms 70228 KB Output is correct
19 Correct 109 ms 73556 KB Output is correct
20 Correct 107 ms 73812 KB Output is correct
21 Correct 94 ms 74348 KB Output is correct
22 Correct 63 ms 64596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 576 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 576 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 2 ms 348 KB Output is correct
18 Correct 2 ms 560 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 2 ms 348 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 2 ms 604 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 2 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 576 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 2 ms 348 KB Output is correct
18 Correct 2 ms 560 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 2 ms 348 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 2 ms 604 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 2 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 3 ms 344 KB Output is correct
30 Correct 3 ms 604 KB Output is correct
31 Correct 2 ms 620 KB Output is correct
32 Correct 2 ms 348 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 3 ms 548 KB Output is correct
35 Correct 3 ms 348 KB Output is correct
36 Correct 2 ms 348 KB Output is correct
37 Correct 2 ms 604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 576 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 2 ms 348 KB Output is correct
18 Correct 2 ms 560 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 2 ms 348 KB Output is correct
21 Correct 2 ms 348 KB Output is correct
22 Correct 2 ms 604 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 0 ms 348 KB Output is correct
27 Correct 2 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 3 ms 344 KB Output is correct
30 Correct 3 ms 604 KB Output is correct
31 Correct 2 ms 620 KB Output is correct
32 Correct 2 ms 348 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 3 ms 548 KB Output is correct
35 Correct 3 ms 348 KB Output is correct
36 Correct 2 ms 348 KB Output is correct
37 Correct 2 ms 604 KB Output is correct
38 Correct 52 ms 3420 KB Output is correct
39 Correct 59 ms 59120 KB Output is correct
40 Correct 62 ms 3040 KB Output is correct
41 Correct 63 ms 2792 KB Output is correct
42 Correct 41 ms 3048 KB Output is correct
43 Correct 34 ms 2908 KB Output is correct
44 Correct 14 ms 916 KB Output is correct
45 Correct 62 ms 45820 KB Output is correct
46 Correct 58 ms 45868 KB Output is correct
47 Correct 51 ms 52480 KB Output is correct
48 Correct 48 ms 52308 KB Output is correct
49 Correct 49 ms 46164 KB Output is correct
50 Correct 58 ms 46280 KB Output is correct
51 Correct 50 ms 47952 KB Output is correct
52 Correct 43 ms 47956 KB Output is correct
53 Correct 14 ms 3472 KB Output is correct
54 Correct 63 ms 45940 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 8 ms 472 KB Output is correct
6 Correct 1 ms 356 KB Output is correct
7 Correct 1 ms 356 KB Output is correct
8 Correct 0 ms 352 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 1 ms 352 KB Output is correct
11 Correct 0 ms 356 KB Output is correct
12 Correct 2 ms 556 KB Output is correct
13 Correct 32 ms 524 KB Output is correct
14 Correct 59 ms 624 KB Output is correct
15 Correct 45 ms 352 KB Output is correct
16 Correct 80 ms 48236 KB Output is correct
17 Correct 193 ms 59480 KB Output is correct
18 Correct 354 ms 72524 KB Output is correct
19 Correct 118 ms 50508 KB Output is correct
20 Correct 108 ms 50524 KB Output is correct
21 Correct 117 ms 50520 KB Output is correct
22 Correct 195 ms 59396 KB Output is correct
23 Correct 139 ms 58964 KB Output is correct
24 Correct 154 ms 59292 KB Output is correct
25 Correct 142 ms 59344 KB Output is correct
26 Correct 162 ms 59592 KB Output is correct
27 Correct 149 ms 64856 KB Output is correct
28 Correct 136 ms 67156 KB Output is correct
29 Correct 131 ms 62200 KB Output is correct
30 Correct 110 ms 56528 KB Output is correct
31 Correct 110 ms 57304 KB Output is correct
32 Correct 132 ms 55996 KB Output is correct
33 Correct 115 ms 57304 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 14 ms 508 KB Output is correct
5 Correct 30 ms 512 KB Output is correct
6 Correct 2 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 3 ms 348 KB Output is correct
9 Correct 40 ms 3472 KB Output is correct
10 Correct 57 ms 58992 KB Output is correct
11 Correct 10 ms 348 KB Output is correct
12 Correct 47 ms 532 KB Output is correct
13 Correct 89 ms 66936 KB Output is correct
14 Correct 82 ms 67156 KB Output is correct
15 Correct 109 ms 71920 KB Output is correct
16 Correct 205 ms 89428 KB Output is correct
17 Correct 112 ms 75344 KB Output is correct
18 Correct 93 ms 70228 KB Output is correct
19 Correct 109 ms 73556 KB Output is correct
20 Correct 107 ms 73812 KB Output is correct
21 Correct 94 ms 74348 KB Output is correct
22 Correct 63 ms 64596 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 576 KB Output is correct
26 Correct 2 ms 348 KB Output is correct
27 Correct 2 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 2 ms 348 KB Output is correct
30 Correct 2 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 2 ms 348 KB Output is correct
40 Correct 2 ms 560 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 2 ms 348 KB Output is correct
43 Correct 2 ms 348 KB Output is correct
44 Correct 2 ms 604 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 2 ms 348 KB Output is correct
48 Correct 0 ms 348 KB Output is correct
49 Correct 2 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 3 ms 344 KB Output is correct
52 Correct 3 ms 604 KB Output is correct
53 Correct 2 ms 620 KB Output is correct
54 Correct 2 ms 348 KB Output is correct
55 Correct 2 ms 348 KB Output is correct
56 Correct 3 ms 548 KB Output is correct
57 Correct 3 ms 348 KB Output is correct
58 Correct 2 ms 348 KB Output is correct
59 Correct 2 ms 604 KB Output is correct
60 Correct 52 ms 3420 KB Output is correct
61 Correct 59 ms 59120 KB Output is correct
62 Correct 62 ms 3040 KB Output is correct
63 Correct 63 ms 2792 KB Output is correct
64 Correct 41 ms 3048 KB Output is correct
65 Correct 34 ms 2908 KB Output is correct
66 Correct 14 ms 916 KB Output is correct
67 Correct 62 ms 45820 KB Output is correct
68 Correct 58 ms 45868 KB Output is correct
69 Correct 51 ms 52480 KB Output is correct
70 Correct 48 ms 52308 KB Output is correct
71 Correct 49 ms 46164 KB Output is correct
72 Correct 58 ms 46280 KB Output is correct
73 Correct 50 ms 47952 KB Output is correct
74 Correct 43 ms 47956 KB Output is correct
75 Correct 14 ms 3472 KB Output is correct
76 Correct 63 ms 45940 KB Output is correct
77 Correct 0 ms 344 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 0 ms 348 KB Output is correct
81 Correct 8 ms 472 KB Output is correct
82 Correct 1 ms 356 KB Output is correct
83 Correct 1 ms 356 KB Output is correct
84 Correct 0 ms 352 KB Output is correct
85 Correct 0 ms 348 KB Output is correct
86 Correct 1 ms 352 KB Output is correct
87 Correct 0 ms 356 KB Output is correct
88 Correct 2 ms 556 KB Output is correct
89 Correct 32 ms 524 KB Output is correct
90 Correct 59 ms 624 KB Output is correct
91 Correct 45 ms 352 KB Output is correct
92 Correct 80 ms 48236 KB Output is correct
93 Correct 193 ms 59480 KB Output is correct
94 Correct 354 ms 72524 KB Output is correct
95 Correct 118 ms 50508 KB Output is correct
96 Correct 108 ms 50524 KB Output is correct
97 Correct 117 ms 50520 KB Output is correct
98 Correct 195 ms 59396 KB Output is correct
99 Correct 139 ms 58964 KB Output is correct
100 Correct 154 ms 59292 KB Output is correct
101 Correct 142 ms 59344 KB Output is correct
102 Correct 162 ms 59592 KB Output is correct
103 Correct 149 ms 64856 KB Output is correct
104 Correct 136 ms 67156 KB Output is correct
105 Correct 131 ms 62200 KB Output is correct
106 Correct 110 ms 56528 KB Output is correct
107 Correct 110 ms 57304 KB Output is correct
108 Correct 132 ms 55996 KB Output is correct
109 Correct 115 ms 57304 KB Output is correct
110 Correct 52 ms 344 KB Output is correct
111 Correct 31 ms 344 KB Output is correct
112 Correct 136 ms 67948 KB Output is correct
113 Correct 95 ms 57688 KB Output is correct
114 Correct 115 ms 63188 KB Output is correct
115 Correct 53 ms 46832 KB Output is correct
116 Correct 118 ms 51792 KB Output is correct
117 Correct 482 ms 82888 KB Output is correct
118 Correct 74 ms 45904 KB Output is correct
119 Correct 62 ms 46048 KB Output is correct
120 Correct 10 ms 4952 KB Output is correct
121 Correct 150 ms 54120 KB Output is correct
122 Correct 153 ms 54352 KB Output is correct
123 Correct 100 ms 60712 KB Output is correct
124 Correct 113 ms 61008 KB Output is correct
125 Correct 103 ms 61616 KB Output is correct
126 Correct 214 ms 85072 KB Output is correct
127 Correct 125 ms 71284 KB Output is correct
128 Correct 107 ms 72100 KB Output is correct
129 Correct 125 ms 71112 KB Output is correct
130 Correct 111 ms 71856 KB Output is correct