Submission #1040165

# Submission time Handle Problem Language Result Execution time Memory
1040165 2024-07-31T17:23:47 Z Sharky Jail (JOI22_jail) C++17
100 / 100
1076 ms 133900 KB
#include <bits/stdc++.h>
using namespace std;

const int N = 120008;
const int LG = 17;

int n, m, sz[N], dep[N], node = 0, pr[N][LG], rt[N], tin[N], pa[N], timer = 0, leaf[N * 3], ls[N * 11], rs[N * 11], st[2], vis[N * 11], cyc = 0, col[N * 11];
vector<int> adj[N], g[N * 11];

void dfs(int u) {
    sz[u] = 1;
    for (int i = 1; i < LG; i++) pr[u][i] = pr[pr[u][i - 1]][i - 1];
    for (auto& v : adj[u]) {
        if (v == pr[u][0]) continue;
        pr[v][0] = u;
        dep[v] = dep[u] + 1;
        pa[v] = u;
        dfs(v);
        sz[u] += sz[v];
        if (sz[v] > sz[adj[u][0]]) swap(v, adj[u][0]);
    }
}

void dfs_hld(int u) {
    tin[u] = ++timer;
    for (int v : adj[u]) {
        if (v == pr[u][0]) continue;
        rt[v] = (v == adj[u][0] ? rt[u] : v);
        dfs_hld(v);
    }
}

int jump(int s, int di) {
    for (int i = LG - 1; i >= 0; i--) 
        if (di & (1 << i)) s = pr[s][i];
    return s;
}

int lca(int u, int v) {
    if (dep[u] > dep[v]) swap(u, v);
    v = jump(v, dep[v] - dep[u]);
    if (u == v) return u;
    for (int i = LG - 1; i >= 0; i--) {
        int ut = pr[u][i], vt = pr[v][i];
        if (ut != vt) u = ut, v = vt;
    }
    return pr[u][0];
}

void build(int l, int r, int id, bool up) {
    if (l == r) {
        leaf[l] = id;
        return;
    }
    int mid = (l + r) / 2;
    if (l != mid || !up) {
        ls[id] = ++node;
        build(l, mid, ls[id], up);
    } else ls[id] = leaf[l];
    if (mid + 1 != r || !up) {
        rs[id] = ++node;
        build(mid + 1, r, rs[id], up);
    } else rs[id] = leaf[r];
    if (!up) {
        g[id].push_back(ls[id]);
        g[id].push_back(rs[id]);
    } else {
        g[ls[id]].push_back(id);
        g[rs[id]].push_back(id);
    }
}

void update(int ql, int qr, int v, int l, int r, int id, bool up) {
    if (qr < l || r < ql) return;
    if (ql <= l && r <= qr) {
        if (!up) {
            // cout << "edge: " << v << "-->" << '[' << l << ", " << r << ']' << "\n";
            g[leaf[v]].push_back(id);
        } else {
            // cout << "edge: " << '[' << l << ", " << r << ']' << " --> " << v << "\n";
            g[id].push_back(leaf[v]);
        }
        return;
    }
    int mid = (l + r) / 2;
    update(ql, qr, v, l, mid, ls[id], up);
    update(ql, qr, v, mid + 1, r, rs[id], up);
}

void pti(int x, int l, int r) {
    update(l, r, x, 1, 2 * n + m, st[0], 0);
}

void itp(int x, int l, int r) {
    update(l, r, x, 1, 2 * n + m, st[1], 1);
}

void path(int x, int u, int v, bool b) {
    // cout << x << " " << u << " " << v << " " << b << "\n";
    for (; rt[u] != rt[v]; v = pa[rt[v]]) {
        if (dep[rt[u]] > dep[rt[v]]) swap(u, v);
        if (!b) pti(x + 2 * n, tin[rt[v]] + n, tin[v] + n);
        else itp(x + 2 * n, tin[rt[v]], tin[v]);
    }
    if (dep[u] > dep[v]) swap(u, v);
    if (!b) pti(x + 2 * n, tin[u] + n, tin[v] + n);
    else itp(x + 2 * n, tin[u], tin[v]);
}

bool DFS(int u) {
    col[u] = 1;
    for (int v : g[u]) {
        if (!col[v]) {
            if (DFS(v)) {
                cyc = 1;
                return true;
            }
        } else if (col[v] == 1) {
            cyc = 1;
            return true;
        }
    }
    col[u] = 2;
    return false;
}

void solve() {
    cin >> n;
    for (int i = 1; i < n; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    cin >> m;
    rt[1] = pr[1][0] = 1;
    dfs(1); dfs_hld(1);
    st[0] = ++node;
    build(1, 2 * n + m, node, 0);
    st[1] = ++node;
    build(1, 2 * n + m, node, 1);
    for (int i = 1; i <= m; i++) {
        int u, v;
        cin >> u >> v;
        int x = lca(u, v);
        g[leaf[n * 2 + i]].push_back(leaf[tin[u]]);
        g[leaf[tin[v] + n]].push_back(leaf[n * 2 + i]);
        // cout << "edge: " << n * 2 + i << " --> " << tin[u] << '\n';
        // cout << "edge: " << tin[v] + n << " --> " << n * 2 + i << '\n';
        int d = max(dep[u], dep[v]) - dep[x] - 1;
        if (u != x) {
            path(i, pa[u], v, 1);
        } else {
            path(i, jump(v, d), v, 1);
        }
        if (v != x) {
            path(i, u, pa[v], 0);
        } else {
            path(i, u, jump(u, d), 0);
        }
    }
    // for (int i = 1; i <= node; i++) for (int j : g[i]) cout << i << " --> " << j << '\n';
    for (int i = 1; i <= node; i++) if (!col[i] && DFS(i)) break;
    cout << (cyc ? "No" : "Yes") << '\n';
    for (int i = 1; i <= n; i++) {
        sz[i] = dep[i] = rt[i] = tin[i] = pa[i] = 0;
        adj[i].clear();
        for (int j = 0; j < LG; j++) pr[i][j] = 0;
    }
    for (int i = 1; i <= n * 3; i++) leaf[i] = 0;
    for (int i = 1; i <= node; i++) {
        g[i].clear();
        ls[i] = rs[i] = col[i] = vis[i] = 0;
    }
    timer = cyc = node = st[0] = st[1] = 0;
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int q;
    cin >> q;
    while (q--) solve();   
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 45660 KB Output is correct
2 Correct 6 ms 45660 KB Output is correct
3 Correct 7 ms 47708 KB Output is correct
4 Correct 17 ms 47868 KB Output is correct
5 Correct 27 ms 47748 KB Output is correct
6 Correct 8 ms 45656 KB Output is correct
7 Correct 8 ms 45872 KB Output is correct
8 Correct 9 ms 45772 KB Output is correct
9 Correct 40 ms 51980 KB Output is correct
10 Correct 92 ms 100180 KB Output is correct
11 Correct 14 ms 45808 KB Output is correct
12 Correct 45 ms 45860 KB Output is correct
13 Correct 144 ms 108960 KB Output is correct
14 Correct 133 ms 108920 KB Output is correct
15 Correct 265 ms 113920 KB Output is correct
16 Correct 474 ms 133900 KB Output is correct
17 Correct 181 ms 118872 KB Output is correct
18 Correct 161 ms 112576 KB Output is correct
19 Correct 179 ms 117828 KB Output is correct
20 Correct 155 ms 117984 KB Output is correct
21 Correct 186 ms 120276 KB Output is correct
22 Correct 114 ms 107856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 45656 KB Output is correct
2 Correct 6 ms 47708 KB Output is correct
3 Correct 7 ms 45660 KB Output is correct
4 Correct 7 ms 47844 KB Output is correct
5 Correct 8 ms 45660 KB Output is correct
6 Correct 7 ms 45784 KB Output is correct
7 Correct 8 ms 47708 KB Output is correct
8 Correct 7 ms 45660 KB Output is correct
9 Correct 7 ms 45656 KB Output is correct
10 Correct 6 ms 45660 KB Output is correct
11 Correct 8 ms 45660 KB Output is correct
12 Correct 7 ms 47768 KB Output is correct
13 Correct 7 ms 47708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 45656 KB Output is correct
2 Correct 6 ms 47708 KB Output is correct
3 Correct 7 ms 45660 KB Output is correct
4 Correct 7 ms 47844 KB Output is correct
5 Correct 8 ms 45660 KB Output is correct
6 Correct 7 ms 45784 KB Output is correct
7 Correct 8 ms 47708 KB Output is correct
8 Correct 7 ms 45660 KB Output is correct
9 Correct 7 ms 45656 KB Output is correct
10 Correct 6 ms 45660 KB Output is correct
11 Correct 8 ms 45660 KB Output is correct
12 Correct 7 ms 47768 KB Output is correct
13 Correct 7 ms 47708 KB Output is correct
14 Correct 6 ms 45660 KB Output is correct
15 Correct 8 ms 45768 KB Output is correct
16 Correct 7 ms 45660 KB Output is correct
17 Correct 7 ms 45744 KB Output is correct
18 Correct 7 ms 45724 KB Output is correct
19 Correct 8 ms 45660 KB Output is correct
20 Correct 7 ms 45660 KB Output is correct
21 Correct 7 ms 45856 KB Output is correct
22 Correct 8 ms 47764 KB Output is correct
23 Correct 6 ms 45724 KB Output is correct
24 Correct 6 ms 45656 KB Output is correct
25 Correct 7 ms 45660 KB Output is correct
26 Correct 7 ms 45660 KB Output is correct
27 Correct 7 ms 47704 KB Output is correct
28 Correct 6 ms 47708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 45656 KB Output is correct
2 Correct 6 ms 47708 KB Output is correct
3 Correct 7 ms 45660 KB Output is correct
4 Correct 7 ms 47844 KB Output is correct
5 Correct 8 ms 45660 KB Output is correct
6 Correct 7 ms 45784 KB Output is correct
7 Correct 8 ms 47708 KB Output is correct
8 Correct 7 ms 45660 KB Output is correct
9 Correct 7 ms 45656 KB Output is correct
10 Correct 6 ms 45660 KB Output is correct
11 Correct 8 ms 45660 KB Output is correct
12 Correct 7 ms 47768 KB Output is correct
13 Correct 7 ms 47708 KB Output is correct
14 Correct 6 ms 45660 KB Output is correct
15 Correct 8 ms 45768 KB Output is correct
16 Correct 7 ms 45660 KB Output is correct
17 Correct 7 ms 45744 KB Output is correct
18 Correct 7 ms 45724 KB Output is correct
19 Correct 8 ms 45660 KB Output is correct
20 Correct 7 ms 45660 KB Output is correct
21 Correct 7 ms 45856 KB Output is correct
22 Correct 8 ms 47764 KB Output is correct
23 Correct 6 ms 45724 KB Output is correct
24 Correct 6 ms 45656 KB Output is correct
25 Correct 7 ms 45660 KB Output is correct
26 Correct 7 ms 45660 KB Output is correct
27 Correct 7 ms 47704 KB Output is correct
28 Correct 6 ms 47708 KB Output is correct
29 Correct 8 ms 45660 KB Output is correct
30 Correct 9 ms 47708 KB Output is correct
31 Correct 8 ms 47716 KB Output is correct
32 Correct 9 ms 47708 KB Output is correct
33 Correct 7 ms 47708 KB Output is correct
34 Correct 9 ms 47760 KB Output is correct
35 Correct 8 ms 47900 KB Output is correct
36 Correct 8 ms 45660 KB Output is correct
37 Correct 8 ms 45660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 45656 KB Output is correct
2 Correct 6 ms 47708 KB Output is correct
3 Correct 7 ms 45660 KB Output is correct
4 Correct 7 ms 47844 KB Output is correct
5 Correct 8 ms 45660 KB Output is correct
6 Correct 7 ms 45784 KB Output is correct
7 Correct 8 ms 47708 KB Output is correct
8 Correct 7 ms 45660 KB Output is correct
9 Correct 7 ms 45656 KB Output is correct
10 Correct 6 ms 45660 KB Output is correct
11 Correct 8 ms 45660 KB Output is correct
12 Correct 7 ms 47768 KB Output is correct
13 Correct 7 ms 47708 KB Output is correct
14 Correct 6 ms 45660 KB Output is correct
15 Correct 8 ms 45768 KB Output is correct
16 Correct 7 ms 45660 KB Output is correct
17 Correct 7 ms 45744 KB Output is correct
18 Correct 7 ms 45724 KB Output is correct
19 Correct 8 ms 45660 KB Output is correct
20 Correct 7 ms 45660 KB Output is correct
21 Correct 7 ms 45856 KB Output is correct
22 Correct 8 ms 47764 KB Output is correct
23 Correct 6 ms 45724 KB Output is correct
24 Correct 6 ms 45656 KB Output is correct
25 Correct 7 ms 45660 KB Output is correct
26 Correct 7 ms 45660 KB Output is correct
27 Correct 7 ms 47704 KB Output is correct
28 Correct 6 ms 47708 KB Output is correct
29 Correct 8 ms 45660 KB Output is correct
30 Correct 9 ms 47708 KB Output is correct
31 Correct 8 ms 47716 KB Output is correct
32 Correct 9 ms 47708 KB Output is correct
33 Correct 7 ms 47708 KB Output is correct
34 Correct 9 ms 47760 KB Output is correct
35 Correct 8 ms 47900 KB Output is correct
36 Correct 8 ms 45660 KB Output is correct
37 Correct 8 ms 45660 KB Output is correct
38 Correct 38 ms 51804 KB Output is correct
39 Correct 84 ms 100176 KB Output is correct
40 Correct 55 ms 53580 KB Output is correct
41 Correct 65 ms 51796 KB Output is correct
42 Correct 44 ms 53844 KB Output is correct
43 Correct 37 ms 51548 KB Output is correct
44 Correct 26 ms 48220 KB Output is correct
45 Correct 77 ms 92660 KB Output is correct
46 Correct 87 ms 90816 KB Output is correct
47 Correct 83 ms 95388 KB Output is correct
48 Correct 87 ms 95300 KB Output is correct
49 Correct 82 ms 92824 KB Output is correct
50 Correct 85 ms 90960 KB Output is correct
51 Correct 74 ms 92240 KB Output is correct
52 Correct 66 ms 94032 KB Output is correct
53 Correct 22 ms 53840 KB Output is correct
54 Correct 101 ms 90560 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 45656 KB Output is correct
2 Correct 6 ms 45660 KB Output is correct
3 Correct 6 ms 45660 KB Output is correct
4 Correct 6 ms 47748 KB Output is correct
5 Correct 11 ms 45660 KB Output is correct
6 Correct 7 ms 47708 KB Output is correct
7 Correct 7 ms 47704 KB Output is correct
8 Correct 6 ms 45656 KB Output is correct
9 Correct 6 ms 45752 KB Output is correct
10 Correct 6 ms 45724 KB Output is correct
11 Correct 6 ms 47708 KB Output is correct
12 Correct 8 ms 45728 KB Output is correct
13 Correct 37 ms 45660 KB Output is correct
14 Correct 58 ms 45660 KB Output is correct
15 Correct 45 ms 47708 KB Output is correct
16 Correct 149 ms 95316 KB Output is correct
17 Correct 477 ms 106856 KB Output is correct
18 Correct 874 ms 123168 KB Output is correct
19 Correct 269 ms 96236 KB Output is correct
20 Correct 228 ms 98388 KB Output is correct
21 Correct 261 ms 96612 KB Output is correct
22 Correct 439 ms 106696 KB Output is correct
23 Correct 348 ms 106704 KB Output is correct
24 Correct 407 ms 105756 KB Output is correct
25 Correct 374 ms 106700 KB Output is correct
26 Correct 417 ms 106572 KB Output is correct
27 Correct 358 ms 107076 KB Output is correct
28 Correct 332 ms 115532 KB Output is correct
29 Correct 305 ms 106612 KB Output is correct
30 Correct 268 ms 102868 KB Output is correct
31 Correct 263 ms 105932 KB Output is correct
32 Correct 277 ms 102864 KB Output is correct
33 Correct 238 ms 105932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 45660 KB Output is correct
2 Correct 6 ms 45660 KB Output is correct
3 Correct 7 ms 47708 KB Output is correct
4 Correct 17 ms 47868 KB Output is correct
5 Correct 27 ms 47748 KB Output is correct
6 Correct 8 ms 45656 KB Output is correct
7 Correct 8 ms 45872 KB Output is correct
8 Correct 9 ms 45772 KB Output is correct
9 Correct 40 ms 51980 KB Output is correct
10 Correct 92 ms 100180 KB Output is correct
11 Correct 14 ms 45808 KB Output is correct
12 Correct 45 ms 45860 KB Output is correct
13 Correct 144 ms 108960 KB Output is correct
14 Correct 133 ms 108920 KB Output is correct
15 Correct 265 ms 113920 KB Output is correct
16 Correct 474 ms 133900 KB Output is correct
17 Correct 181 ms 118872 KB Output is correct
18 Correct 161 ms 112576 KB Output is correct
19 Correct 179 ms 117828 KB Output is correct
20 Correct 155 ms 117984 KB Output is correct
21 Correct 186 ms 120276 KB Output is correct
22 Correct 114 ms 107856 KB Output is correct
23 Correct 6 ms 45656 KB Output is correct
24 Correct 6 ms 47708 KB Output is correct
25 Correct 7 ms 45660 KB Output is correct
26 Correct 7 ms 47844 KB Output is correct
27 Correct 8 ms 45660 KB Output is correct
28 Correct 7 ms 45784 KB Output is correct
29 Correct 8 ms 47708 KB Output is correct
30 Correct 7 ms 45660 KB Output is correct
31 Correct 7 ms 45656 KB Output is correct
32 Correct 6 ms 45660 KB Output is correct
33 Correct 8 ms 45660 KB Output is correct
34 Correct 7 ms 47768 KB Output is correct
35 Correct 7 ms 47708 KB Output is correct
36 Correct 6 ms 45660 KB Output is correct
37 Correct 8 ms 45768 KB Output is correct
38 Correct 7 ms 45660 KB Output is correct
39 Correct 7 ms 45744 KB Output is correct
40 Correct 7 ms 45724 KB Output is correct
41 Correct 8 ms 45660 KB Output is correct
42 Correct 7 ms 45660 KB Output is correct
43 Correct 7 ms 45856 KB Output is correct
44 Correct 8 ms 47764 KB Output is correct
45 Correct 6 ms 45724 KB Output is correct
46 Correct 6 ms 45656 KB Output is correct
47 Correct 7 ms 45660 KB Output is correct
48 Correct 7 ms 45660 KB Output is correct
49 Correct 7 ms 47704 KB Output is correct
50 Correct 6 ms 47708 KB Output is correct
51 Correct 8 ms 45660 KB Output is correct
52 Correct 9 ms 47708 KB Output is correct
53 Correct 8 ms 47716 KB Output is correct
54 Correct 9 ms 47708 KB Output is correct
55 Correct 7 ms 47708 KB Output is correct
56 Correct 9 ms 47760 KB Output is correct
57 Correct 8 ms 47900 KB Output is correct
58 Correct 8 ms 45660 KB Output is correct
59 Correct 8 ms 45660 KB Output is correct
60 Correct 38 ms 51804 KB Output is correct
61 Correct 84 ms 100176 KB Output is correct
62 Correct 55 ms 53580 KB Output is correct
63 Correct 65 ms 51796 KB Output is correct
64 Correct 44 ms 53844 KB Output is correct
65 Correct 37 ms 51548 KB Output is correct
66 Correct 26 ms 48220 KB Output is correct
67 Correct 77 ms 92660 KB Output is correct
68 Correct 87 ms 90816 KB Output is correct
69 Correct 83 ms 95388 KB Output is correct
70 Correct 87 ms 95300 KB Output is correct
71 Correct 82 ms 92824 KB Output is correct
72 Correct 85 ms 90960 KB Output is correct
73 Correct 74 ms 92240 KB Output is correct
74 Correct 66 ms 94032 KB Output is correct
75 Correct 22 ms 53840 KB Output is correct
76 Correct 101 ms 90560 KB Output is correct
77 Correct 6 ms 45656 KB Output is correct
78 Correct 6 ms 45660 KB Output is correct
79 Correct 6 ms 45660 KB Output is correct
80 Correct 6 ms 47748 KB Output is correct
81 Correct 11 ms 45660 KB Output is correct
82 Correct 7 ms 47708 KB Output is correct
83 Correct 7 ms 47704 KB Output is correct
84 Correct 6 ms 45656 KB Output is correct
85 Correct 6 ms 45752 KB Output is correct
86 Correct 6 ms 45724 KB Output is correct
87 Correct 6 ms 47708 KB Output is correct
88 Correct 8 ms 45728 KB Output is correct
89 Correct 37 ms 45660 KB Output is correct
90 Correct 58 ms 45660 KB Output is correct
91 Correct 45 ms 47708 KB Output is correct
92 Correct 149 ms 95316 KB Output is correct
93 Correct 477 ms 106856 KB Output is correct
94 Correct 874 ms 123168 KB Output is correct
95 Correct 269 ms 96236 KB Output is correct
96 Correct 228 ms 98388 KB Output is correct
97 Correct 261 ms 96612 KB Output is correct
98 Correct 439 ms 106696 KB Output is correct
99 Correct 348 ms 106704 KB Output is correct
100 Correct 407 ms 105756 KB Output is correct
101 Correct 374 ms 106700 KB Output is correct
102 Correct 417 ms 106572 KB Output is correct
103 Correct 358 ms 107076 KB Output is correct
104 Correct 332 ms 115532 KB Output is correct
105 Correct 305 ms 106612 KB Output is correct
106 Correct 268 ms 102868 KB Output is correct
107 Correct 263 ms 105932 KB Output is correct
108 Correct 277 ms 102864 KB Output is correct
109 Correct 238 ms 105932 KB Output is correct
110 Correct 60 ms 47956 KB Output is correct
111 Correct 27 ms 47708 KB Output is correct
112 Correct 282 ms 111956 KB Output is correct
113 Correct 174 ms 100948 KB Output is correct
114 Correct 244 ms 111056 KB Output is correct
115 Correct 100 ms 91596 KB Output is correct
116 Correct 222 ms 97412 KB Output is correct
117 Correct 1076 ms 130296 KB Output is correct
118 Correct 98 ms 90720 KB Output is correct
119 Correct 86 ms 92672 KB Output is correct
120 Correct 20 ms 55132 KB Output is correct
121 Correct 273 ms 100904 KB Output is correct
122 Correct 273 ms 100932 KB Output is correct
123 Correct 179 ms 103608 KB Output is correct
124 Correct 155 ms 105552 KB Output is correct
125 Correct 185 ms 106316 KB Output is correct
126 Correct 434 ms 129876 KB Output is correct
127 Correct 259 ms 114620 KB Output is correct
128 Correct 196 ms 114748 KB Output is correct
129 Correct 233 ms 114512 KB Output is correct
130 Correct 200 ms 114136 KB Output is correct