Submission #995121

# Submission time Handle Problem Language Result Execution time Memory
995121 2024-06-08T13:26:09 Z Szil Jail (JOI22_jail) C++17
100 / 100
828 ms 179652 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int MAXN = 200'100;

vector<int> g[MAXN], g2[MAXN*21];
int pos[MAXN], tin[MAXN], tout[MAXN], head[MAXN], heavy[MAXN], sz[MAXN], par[MAXN], 
    s[MAXN], e[MAXN], depth[MAXN], col[MAXN*21], timer, pos_timer, n, m;
bool ok;

void init() {
    for (int i = 1; i <= n; i++) {
        g[i].clear();
    }
    for (int i = 1; i <= 20*n; i++) {
        g2[i].clear();
    }
    fill(heavy, heavy+n+1, 0);
    fill(s, s+n+1, 0);
    fill(e, e+n+1, 0);
    fill(col, col+20*n+1, 0);
    timer = pos_timer = 1;
    ok = true;
}

int get_segtree1_index(int u) {
    return 10*n + u;
}

int get_segtree2_index(int u) {
    return n + u;
}

int get_path_index(int u) {
    return u;
}

void dfs(int u, int p = -1, int d = 0) {
    tin[u] = timer++;
    sz[u] = 1;
    depth[u] = d;
    for (int v : g[u]) {
        if (v == p) continue;
        par[v] = u;
        dfs(v, u, d+1);
        sz[u] += sz[v];
        if (sz[heavy[u]] < sz[v]) {
            heavy[u] = v;
        }
    }
    tout[u] = timer++;
}

bool is_ancestor(int u, int v) {
    return tin[u] <= tin[v] && tout[v] <= tout[u];
}

int nxt(int u, int v) {
    if (u == v) return -1;
    if (is_ancestor(u, v)) {
        for (int x : g[u]) {
            if (x != par[u] && is_ancestor(x, v)) return x;
        }
    } else {
        return par[u];
    }
}

void dfs2(int u, int h, int p) {
    head[u] = h;
    pos[u] = pos_timer++;
    if (heavy[u]) {
        dfs2(heavy[u], h, u);
    }
    for (int v : g[u]) {
        if (v == p || v == heavy[u]) continue;
        dfs2(v, v, u);
    }
}

void dfs3(int u) {
    col[u] = 1;
    for (int v : g2[u]) {
        if (col[v] == 0) dfs3(v);
        else if (col[v] == 1) ok = false;
    }
    col[u] = 2;
}

void build_segtree1(int v, int tl, int tr) {
    if (tl == tr) {
        if (s[tl]) {
            g2[get_segtree1_index(v)].emplace_back(get_path_index(s[tl]));
        }
    } else {
        int tm = (tl + tr) / 2;
        build_segtree1(2*v, tl, tm);
        build_segtree1(2*v+1, tm+1, tr);
        g2[get_segtree1_index(v)].emplace_back(get_segtree1_index(2*v));
        g2[get_segtree1_index(v)].emplace_back(get_segtree1_index(2*v+1));
    }
}

void build_segtree2(int v, int tl, int tr) {
    if (tl == tr) {
        if (e[tl]) {
            g2[get_path_index(e[tl])].emplace_back(get_segtree2_index(v));
        }
    } else {
        int tm = (tl + tr) / 2;
        build_segtree2(2*v, tl, tm);
        build_segtree2(2*v+1, tm+1, tr);
        g2[get_segtree2_index(2*v)].emplace_back(get_segtree2_index(v));
        g2[get_segtree2_index(2*v+1)].emplace_back(get_segtree2_index(v));
    }
}

void add_segtree1_edge(int v, int tl, int tr, int l, int r, int node) {
    if (l > r) return;
    if (l == tl && r == tr) {
        g2[node].emplace_back(get_segtree1_index(v));
    } else {
        int tm = (tl + tr) / 2;
        add_segtree1_edge(2*v, tl, tm, l, min(tm, r), node);
        add_segtree1_edge(2*v+1, tm+1, tr, max(tm+1, l), r, node);
    }
}

void add_segtree2_edge(int v, int tl, int tr, int l, int r, int node) {
    if (l > r) return;
    if (l == tl && r == tr) {
        g2[get_segtree2_index(v)].emplace_back(node);
    } else {
        int tm = (tl + tr) / 2;
        add_segtree2_edge(2*v, tl, tm, l, min(tm, r), node);
        add_segtree2_edge(2*v+1, tm+1, tr, max(tm+1, l), r, node);
    }
}

void qry1(int u, int v, int node) {
    while (head[u] != head[v]) {
        if (depth[head[u]] < depth[head[v]]) swap(u, v);
        add_segtree1_edge(1, 1, n, pos[head[u]], pos[u], node);
        u = par[head[u]];
    }
    if (depth[u] > depth[v]) swap(u, v);
    add_segtree1_edge(1, 1, n, pos[u], pos[v], node);
}

void qry2(int u, int v, int node) {
    while (head[u] != head[v]) {
        if (depth[head[u]] < depth[head[v]]) swap(u, v);
        add_segtree2_edge(1, 1, n, pos[head[u]], pos[u], node);
        u = par[head[u]];
    }
    if (depth[u] > depth[v]) swap(u, v);
    add_segtree2_edge(1, 1, n, pos[u], pos[v], node);
}

void solve() {
    init();
    cin >> n;
    for (int i = 0; i < n-1; i++) {
        int u, v; cin >> u >> v;
        g[u].emplace_back(v);
        g[v].emplace_back(u);
    } 
    cin >> m;
    vector<pair<int, int>> edges(m+1);
    dfs(1);
    dfs2(1, 1, -1);
    for (int i = 1; i <= m; i++) {
        cin >> edges[i].first >> edges[i].second;
        auto [u, v] = edges[i];
        s[pos[u]] = i;
        e[pos[v]] = i;
    }
    build_segtree1(1, 1, n);
    build_segtree2(1, 1, n);
    for (int i = 1; i <= m; i++) {
        auto [u, v] = edges[i];
        qry1(nxt(u, v), v, get_path_index(i));
        qry2(u, nxt(v, u), get_path_index(i));
    }
    for (int i = 1; i <= m; i++) {
        int u = get_path_index(i);
        if (col[u] == 0) dfs3(u);
    }
    cout << (ok?"Yes\n":"No\n");
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int t; cin >> t;
    while (t--) {
        solve();
    }
}

Compilation message

jail.cpp: In function 'int nxt(int, int)':
jail.cpp:69:1: warning: control reaches end of non-void function [-Wreturn-type]
   69 | }
      | ^
# Verdict Execution time Memory Grader output
1 Correct 19 ms 113496 KB Output is correct
2 Correct 21 ms 113452 KB Output is correct
3 Correct 17 ms 113500 KB Output is correct
4 Correct 33 ms 113432 KB Output is correct
5 Correct 37 ms 113496 KB Output is correct
6 Correct 19 ms 113500 KB Output is correct
7 Correct 20 ms 113500 KB Output is correct
8 Correct 21 ms 113488 KB Output is correct
9 Correct 48 ms 115352 KB Output is correct
10 Correct 68 ms 139028 KB Output is correct
11 Correct 23 ms 113496 KB Output is correct
12 Correct 49 ms 113500 KB Output is correct
13 Correct 124 ms 147028 KB Output is correct
14 Correct 116 ms 147108 KB Output is correct
15 Correct 225 ms 152132 KB Output is correct
16 Correct 380 ms 175252 KB Output is correct
17 Correct 143 ms 154948 KB Output is correct
18 Correct 127 ms 150352 KB Output is correct
19 Correct 134 ms 153072 KB Output is correct
20 Correct 149 ms 153048 KB Output is correct
21 Correct 149 ms 154452 KB Output is correct
22 Correct 90 ms 144352 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 113496 KB Output is correct
2 Correct 18 ms 113512 KB Output is correct
3 Correct 20 ms 113500 KB Output is correct
4 Correct 21 ms 113516 KB Output is correct
5 Correct 20 ms 113500 KB Output is correct
6 Correct 19 ms 113500 KB Output is correct
7 Correct 19 ms 113668 KB Output is correct
8 Correct 19 ms 113500 KB Output is correct
9 Correct 19 ms 113420 KB Output is correct
10 Correct 20 ms 113664 KB Output is correct
11 Correct 20 ms 113496 KB Output is correct
12 Correct 21 ms 113500 KB Output is correct
13 Correct 22 ms 113500 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 113496 KB Output is correct
2 Correct 18 ms 113512 KB Output is correct
3 Correct 20 ms 113500 KB Output is correct
4 Correct 21 ms 113516 KB Output is correct
5 Correct 20 ms 113500 KB Output is correct
6 Correct 19 ms 113500 KB Output is correct
7 Correct 19 ms 113668 KB Output is correct
8 Correct 19 ms 113500 KB Output is correct
9 Correct 19 ms 113420 KB Output is correct
10 Correct 20 ms 113664 KB Output is correct
11 Correct 20 ms 113496 KB Output is correct
12 Correct 21 ms 113500 KB Output is correct
13 Correct 22 ms 113500 KB Output is correct
14 Correct 19 ms 113496 KB Output is correct
15 Correct 19 ms 113500 KB Output is correct
16 Correct 21 ms 113684 KB Output is correct
17 Correct 20 ms 113496 KB Output is correct
18 Correct 19 ms 113500 KB Output is correct
19 Correct 19 ms 113500 KB Output is correct
20 Correct 21 ms 113584 KB Output is correct
21 Correct 22 ms 113500 KB Output is correct
22 Correct 20 ms 113500 KB Output is correct
23 Correct 19 ms 113380 KB Output is correct
24 Correct 20 ms 113500 KB Output is correct
25 Correct 20 ms 113576 KB Output is correct
26 Correct 19 ms 113500 KB Output is correct
27 Correct 22 ms 113500 KB Output is correct
28 Correct 19 ms 113364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 113496 KB Output is correct
2 Correct 18 ms 113512 KB Output is correct
3 Correct 20 ms 113500 KB Output is correct
4 Correct 21 ms 113516 KB Output is correct
5 Correct 20 ms 113500 KB Output is correct
6 Correct 19 ms 113500 KB Output is correct
7 Correct 19 ms 113668 KB Output is correct
8 Correct 19 ms 113500 KB Output is correct
9 Correct 19 ms 113420 KB Output is correct
10 Correct 20 ms 113664 KB Output is correct
11 Correct 20 ms 113496 KB Output is correct
12 Correct 21 ms 113500 KB Output is correct
13 Correct 22 ms 113500 KB Output is correct
14 Correct 19 ms 113496 KB Output is correct
15 Correct 19 ms 113500 KB Output is correct
16 Correct 21 ms 113684 KB Output is correct
17 Correct 20 ms 113496 KB Output is correct
18 Correct 19 ms 113500 KB Output is correct
19 Correct 19 ms 113500 KB Output is correct
20 Correct 21 ms 113584 KB Output is correct
21 Correct 22 ms 113500 KB Output is correct
22 Correct 20 ms 113500 KB Output is correct
23 Correct 19 ms 113380 KB Output is correct
24 Correct 20 ms 113500 KB Output is correct
25 Correct 20 ms 113576 KB Output is correct
26 Correct 19 ms 113500 KB Output is correct
27 Correct 22 ms 113500 KB Output is correct
28 Correct 19 ms 113364 KB Output is correct
29 Correct 21 ms 113496 KB Output is correct
30 Correct 26 ms 113500 KB Output is correct
31 Correct 20 ms 113500 KB Output is correct
32 Correct 20 ms 113692 KB Output is correct
33 Correct 20 ms 113496 KB Output is correct
34 Correct 21 ms 113500 KB Output is correct
35 Correct 21 ms 113500 KB Output is correct
36 Correct 26 ms 113496 KB Output is correct
37 Correct 19 ms 113532 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 113496 KB Output is correct
2 Correct 18 ms 113512 KB Output is correct
3 Correct 20 ms 113500 KB Output is correct
4 Correct 21 ms 113516 KB Output is correct
5 Correct 20 ms 113500 KB Output is correct
6 Correct 19 ms 113500 KB Output is correct
7 Correct 19 ms 113668 KB Output is correct
8 Correct 19 ms 113500 KB Output is correct
9 Correct 19 ms 113420 KB Output is correct
10 Correct 20 ms 113664 KB Output is correct
11 Correct 20 ms 113496 KB Output is correct
12 Correct 21 ms 113500 KB Output is correct
13 Correct 22 ms 113500 KB Output is correct
14 Correct 19 ms 113496 KB Output is correct
15 Correct 19 ms 113500 KB Output is correct
16 Correct 21 ms 113684 KB Output is correct
17 Correct 20 ms 113496 KB Output is correct
18 Correct 19 ms 113500 KB Output is correct
19 Correct 19 ms 113500 KB Output is correct
20 Correct 21 ms 113584 KB Output is correct
21 Correct 22 ms 113500 KB Output is correct
22 Correct 20 ms 113500 KB Output is correct
23 Correct 19 ms 113380 KB Output is correct
24 Correct 20 ms 113500 KB Output is correct
25 Correct 20 ms 113576 KB Output is correct
26 Correct 19 ms 113500 KB Output is correct
27 Correct 22 ms 113500 KB Output is correct
28 Correct 19 ms 113364 KB Output is correct
29 Correct 21 ms 113496 KB Output is correct
30 Correct 26 ms 113500 KB Output is correct
31 Correct 20 ms 113500 KB Output is correct
32 Correct 20 ms 113692 KB Output is correct
33 Correct 20 ms 113496 KB Output is correct
34 Correct 21 ms 113500 KB Output is correct
35 Correct 21 ms 113500 KB Output is correct
36 Correct 26 ms 113496 KB Output is correct
37 Correct 19 ms 113532 KB Output is correct
38 Correct 52 ms 116504 KB Output is correct
39 Correct 74 ms 140496 KB Output is correct
40 Correct 60 ms 116560 KB Output is correct
41 Correct 62 ms 116444 KB Output is correct
42 Correct 52 ms 116560 KB Output is correct
43 Correct 41 ms 116052 KB Output is correct
44 Correct 32 ms 114008 KB Output is correct
45 Correct 74 ms 132140 KB Output is correct
46 Correct 77 ms 132032 KB Output is correct
47 Correct 72 ms 136272 KB Output is correct
48 Correct 72 ms 136248 KB Output is correct
49 Correct 71 ms 132436 KB Output is correct
50 Correct 75 ms 132432 KB Output is correct
51 Correct 79 ms 132948 KB Output is correct
52 Correct 60 ms 132944 KB Output is correct
53 Correct 31 ms 115536 KB Output is correct
54 Correct 78 ms 131924 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 113496 KB Output is correct
2 Correct 24 ms 113492 KB Output is correct
3 Correct 20 ms 113500 KB Output is correct
4 Correct 19 ms 113536 KB Output is correct
5 Correct 22 ms 113500 KB Output is correct
6 Correct 19 ms 113500 KB Output is correct
7 Correct 19 ms 113500 KB Output is correct
8 Correct 19 ms 113816 KB Output is correct
9 Correct 19 ms 113500 KB Output is correct
10 Correct 22 ms 113500 KB Output is correct
11 Correct 18 ms 113500 KB Output is correct
12 Correct 24 ms 113500 KB Output is correct
13 Correct 42 ms 114164 KB Output is correct
14 Correct 55 ms 114256 KB Output is correct
15 Correct 47 ms 114324 KB Output is correct
16 Correct 112 ms 134200 KB Output is correct
17 Correct 345 ms 147688 KB Output is correct
18 Correct 609 ms 168136 KB Output is correct
19 Correct 155 ms 136532 KB Output is correct
20 Correct 147 ms 136528 KB Output is correct
21 Correct 169 ms 136444 KB Output is correct
22 Correct 302 ms 145104 KB Output is correct
23 Correct 233 ms 144200 KB Output is correct
24 Correct 231 ms 144588 KB Output is correct
25 Correct 231 ms 144592 KB Output is correct
26 Correct 251 ms 144580 KB Output is correct
27 Correct 221 ms 151560 KB Output is correct
28 Correct 215 ms 154432 KB Output is correct
29 Correct 240 ms 148520 KB Output is correct
30 Correct 152 ms 143308 KB Output is correct
31 Correct 178 ms 144392 KB Output is correct
32 Correct 190 ms 142796 KB Output is correct
33 Correct 174 ms 144384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 113496 KB Output is correct
2 Correct 21 ms 113452 KB Output is correct
3 Correct 17 ms 113500 KB Output is correct
4 Correct 33 ms 113432 KB Output is correct
5 Correct 37 ms 113496 KB Output is correct
6 Correct 19 ms 113500 KB Output is correct
7 Correct 20 ms 113500 KB Output is correct
8 Correct 21 ms 113488 KB Output is correct
9 Correct 48 ms 115352 KB Output is correct
10 Correct 68 ms 139028 KB Output is correct
11 Correct 23 ms 113496 KB Output is correct
12 Correct 49 ms 113500 KB Output is correct
13 Correct 124 ms 147028 KB Output is correct
14 Correct 116 ms 147108 KB Output is correct
15 Correct 225 ms 152132 KB Output is correct
16 Correct 380 ms 175252 KB Output is correct
17 Correct 143 ms 154948 KB Output is correct
18 Correct 127 ms 150352 KB Output is correct
19 Correct 134 ms 153072 KB Output is correct
20 Correct 149 ms 153048 KB Output is correct
21 Correct 149 ms 154452 KB Output is correct
22 Correct 90 ms 144352 KB Output is correct
23 Correct 17 ms 113496 KB Output is correct
24 Correct 18 ms 113512 KB Output is correct
25 Correct 20 ms 113500 KB Output is correct
26 Correct 21 ms 113516 KB Output is correct
27 Correct 20 ms 113500 KB Output is correct
28 Correct 19 ms 113500 KB Output is correct
29 Correct 19 ms 113668 KB Output is correct
30 Correct 19 ms 113500 KB Output is correct
31 Correct 19 ms 113420 KB Output is correct
32 Correct 20 ms 113664 KB Output is correct
33 Correct 20 ms 113496 KB Output is correct
34 Correct 21 ms 113500 KB Output is correct
35 Correct 22 ms 113500 KB Output is correct
36 Correct 19 ms 113496 KB Output is correct
37 Correct 19 ms 113500 KB Output is correct
38 Correct 21 ms 113684 KB Output is correct
39 Correct 20 ms 113496 KB Output is correct
40 Correct 19 ms 113500 KB Output is correct
41 Correct 19 ms 113500 KB Output is correct
42 Correct 21 ms 113584 KB Output is correct
43 Correct 22 ms 113500 KB Output is correct
44 Correct 20 ms 113500 KB Output is correct
45 Correct 19 ms 113380 KB Output is correct
46 Correct 20 ms 113500 KB Output is correct
47 Correct 20 ms 113576 KB Output is correct
48 Correct 19 ms 113500 KB Output is correct
49 Correct 22 ms 113500 KB Output is correct
50 Correct 19 ms 113364 KB Output is correct
51 Correct 21 ms 113496 KB Output is correct
52 Correct 26 ms 113500 KB Output is correct
53 Correct 20 ms 113500 KB Output is correct
54 Correct 20 ms 113692 KB Output is correct
55 Correct 20 ms 113496 KB Output is correct
56 Correct 21 ms 113500 KB Output is correct
57 Correct 21 ms 113500 KB Output is correct
58 Correct 26 ms 113496 KB Output is correct
59 Correct 19 ms 113532 KB Output is correct
60 Correct 52 ms 116504 KB Output is correct
61 Correct 74 ms 140496 KB Output is correct
62 Correct 60 ms 116560 KB Output is correct
63 Correct 62 ms 116444 KB Output is correct
64 Correct 52 ms 116560 KB Output is correct
65 Correct 41 ms 116052 KB Output is correct
66 Correct 32 ms 114008 KB Output is correct
67 Correct 74 ms 132140 KB Output is correct
68 Correct 77 ms 132032 KB Output is correct
69 Correct 72 ms 136272 KB Output is correct
70 Correct 72 ms 136248 KB Output is correct
71 Correct 71 ms 132436 KB Output is correct
72 Correct 75 ms 132432 KB Output is correct
73 Correct 79 ms 132948 KB Output is correct
74 Correct 60 ms 132944 KB Output is correct
75 Correct 31 ms 115536 KB Output is correct
76 Correct 78 ms 131924 KB Output is correct
77 Correct 19 ms 113496 KB Output is correct
78 Correct 24 ms 113492 KB Output is correct
79 Correct 20 ms 113500 KB Output is correct
80 Correct 19 ms 113536 KB Output is correct
81 Correct 22 ms 113500 KB Output is correct
82 Correct 19 ms 113500 KB Output is correct
83 Correct 19 ms 113500 KB Output is correct
84 Correct 19 ms 113816 KB Output is correct
85 Correct 19 ms 113500 KB Output is correct
86 Correct 22 ms 113500 KB Output is correct
87 Correct 18 ms 113500 KB Output is correct
88 Correct 24 ms 113500 KB Output is correct
89 Correct 42 ms 114164 KB Output is correct
90 Correct 55 ms 114256 KB Output is correct
91 Correct 47 ms 114324 KB Output is correct
92 Correct 112 ms 134200 KB Output is correct
93 Correct 345 ms 147688 KB Output is correct
94 Correct 609 ms 168136 KB Output is correct
95 Correct 155 ms 136532 KB Output is correct
96 Correct 147 ms 136528 KB Output is correct
97 Correct 169 ms 136444 KB Output is correct
98 Correct 302 ms 145104 KB Output is correct
99 Correct 233 ms 144200 KB Output is correct
100 Correct 231 ms 144588 KB Output is correct
101 Correct 231 ms 144592 KB Output is correct
102 Correct 251 ms 144580 KB Output is correct
103 Correct 221 ms 151560 KB Output is correct
104 Correct 215 ms 154432 KB Output is correct
105 Correct 240 ms 148520 KB Output is correct
106 Correct 152 ms 143308 KB Output is correct
107 Correct 178 ms 144392 KB Output is correct
108 Correct 190 ms 142796 KB Output is correct
109 Correct 174 ms 144384 KB Output is correct
110 Correct 62 ms 114516 KB Output is correct
111 Correct 37 ms 114224 KB Output is correct
112 Correct 257 ms 154704 KB Output is correct
113 Correct 126 ms 141396 KB Output is correct
114 Correct 191 ms 146892 KB Output is correct
115 Correct 65 ms 132208 KB Output is correct
116 Correct 153 ms 137448 KB Output is correct
117 Correct 828 ms 179652 KB Output is correct
118 Correct 77 ms 132180 KB Output is correct
119 Correct 84 ms 132068 KB Output is correct
120 Correct 30 ms 115804 KB Output is correct
121 Correct 206 ms 139856 KB Output is correct
122 Correct 185 ms 139944 KB Output is correct
123 Correct 139 ms 143656 KB Output is correct
124 Correct 122 ms 144208 KB Output is correct
125 Correct 137 ms 144420 KB Output is correct
126 Correct 416 ms 176464 KB Output is correct
127 Correct 213 ms 155556 KB Output is correct
128 Correct 177 ms 156328 KB Output is correct
129 Correct 160 ms 155052 KB Output is correct
130 Correct 185 ms 156072 KB Output is correct