답안 #702979

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
702979 2023-02-25T11:51:44 Z piOOE Jail (JOI22_jail) C++17
100 / 100
1118 ms 101756 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

class HLD {
public:
    vector<int> par, siz, head, tin, tout, ord, depth;

    int dfs1(int i, vector<vector<int>> &g) {
        for (int &t: g[i]) {
            if (t != par[i]) {
                depth[t] = depth[i] + 1;
                par[t] = i;
                siz[i] += dfs1(t, g);
                if (siz[t] > siz[g[i][0]] || g[i][0] == par[i]) swap(t, g[i][0]);
            }
        }
        return siz[i];
    }

    void dfs2(int i, int &T, const vector<vector<int>> &g) {
        tin[i] = T++;
        for (int t: g[i]) {
            if (t != par[i]) {
                head[t] = (T == tin[i] + 1 ? head[i] : t);
                dfs2(t, T, g);
            }
        }
        tout[i] = T;
    }

    HLD(vector<vector<int>> g, int r = 0)
            : par(g.size(), -1), siz(g.size(), 1), head(g.size(), r), tin(g.size()), ord(g.size()), tout(g.size()),
              depth(g.size()) {
        dfs1(r, g);
        int x = 0;
        dfs2(r, x, g);
        for (int i = 0; i < g.size(); ++i) ord[tin[i]] = i;
    }

    vector<pair<int, int>> path(int a, int b) {
        vector<pair<int, int>> res;
        for (;; b = par[head[b]]) {
            if (tin[b] < tin[a]) swap(a, b);
            if (tin[head[b]] <= tin[a]) {
                res.emplace_back(tin[a], tin[b] + 1);
                return res;
            }
            res.emplace_back(tin[head[b]], tin[b] + 1);
        }
    }

    pair<int, int> subtree(int i) {
        return {tin[i], tin[i] + siz[i]};
    }

    int dist(int a, int b) {
        return depth[a] + depth[b] - 2 * depth[lca(a, b)];
    }

    int lca(int a, int b) {
        for (;; b = par[head[b]]) {
            if (tin[b] < tin[a]) swap(a, b);
            if (tin[head[b]] <= tin[a]) return a;
        }
    }

    bool isp(int a, int b) {
        return tin[a] <= tin[b] && tout[a] >= tout[b];
    }
};

constexpr int N = 1.2e5, D = 256 * 1024 * 2;
constexpr int N_ = D + N;

vector<int> g[N_];
int top = 0;
int used[N_], in[D], out[D];

int sz = 1;

void init(int n) {
    sz = 1 << __lg(n) + !!(n & (n - 1));

    for (int i = 1; i < sz + n; ++i) {
        in[i] = top++;
        out[i] = top++;
    }
}

void solve() {
    int n;
    cin >> n;

    vector<vector<int>> adj(n);

    for (int i = 1; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        a -= 1, b -= 1;

        adj[a].push_back(b);
        adj[b].push_back(a);
    }

    HLD tree(adj);

    int m;
    cin >> m;

    top = m;

    vector<array<int, 2>> prisoner(m);

    for (auto &[s, t]: prisoner) {
        cin >> s >> t;
        s -= 1, t -= 1;
    }


    init(n);

    vector<vector<pair<int, int>>> same(n);

    for (int i = 0; i < m; ++i) {
        auto [S, T] = prisoner[i];

        if (S != T) {
            same[S].push_back({i, 0});
            same[T].push_back({i, 1});
        }

        auto path = tree.path(S, T);

        auto dfs = [&](auto dfs, int l, int r, int x, int lx, int rx) {
            if (l >= rx || lx >= r) return;
            if (l <= lx && rx <= r) {
                g[i].push_back(in[x]);
                g[out[x]].push_back(i);
                return;
            }

            int mid = lx + rx >> 1;
            dfs(dfs, l, r, x << 1, lx, mid), dfs(dfs, l, r, x << 1 | 1, mid, rx);
        };

        for (auto [lx, rx]: path) {
            if (lx == tree.tin[S]) lx++;
            if (rx == tree.tin[S] + 1) --rx;
            if (lx == tree.tin[T]) lx++;
            if (rx == tree.tin[T] + 1) --rx;

            assert(!(lx <= tree.tin[S] && tree.tin[S] < rx));
            assert(!(lx <= tree.tin[T] && tree.tin[T] < rx));

            if (lx < rx) {
                dfs(dfs, lx, rx, 1, 0, sz);
            }
        }
    }

    for (int i = 0; i < n; ++i) {
        if (same[i].size() == 2) {
            if (same[i][0].second == 0) {
                swap(same[i][0], same[i][1]);
            }

            g[same[i][0].first].push_back(same[i][1].first);
        }
    }

    for (int i = 0; i < m; ++i) {
        auto [S, T] = prisoner[i];

        S = tree.tin[S];
        T = tree.tin[T];

        for (int x = S + sz; x > 0; x >>= 1) {
            g[in[x]].push_back(i);
        }

        for (int x = T + sz; x > 0; x >>= 1) {
            g[i].push_back(out[x]);
        }
    }

    bool yay = true;

    memset(used, 0, sizeof(used[0]) * top);

    auto dfs = [&](auto self, int v) -> void {
        used[v] = 1;

        for (int to: g[v]) {
            if (!used[to]) {
                self(self, to);
            } else if (used[to] == 1) {
                yay = false;
            }
        }

        used[v] = 2;
    };

    for (int i = 0; i < top; ++i) {
        if (!used[i]) {
            dfs(dfs, i);
        }
    }

    for (int i = 0; i < top; ++i) {
        g[i].clear();
    }

    cout << (yay ? "Yes\n" : "No\n");
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int test = 1;
    cin >> test;

    while (test--) {
        solve();
    }

    return 0;
}

Compilation message

jail.cpp: In constructor 'HLD::HLD(std::vector<std::vector<int> >, int)':
jail.cpp:8:44: warning: 'HLD::ord' will be initialized after [-Wreorder]
    8 |     vector<int> par, siz, head, tin, tout, ord, depth;
      |                                            ^~~
jail.cpp:8:38: warning:   'std::vector<int> HLD::tout' [-Wreorder]
    8 |     vector<int> par, siz, head, tin, tout, ord, depth;
      |                                      ^~~~
jail.cpp:33:5: warning:   when initialized here [-Wreorder]
   33 |     HLD(vector<vector<int>> g, int r = 0)
      |     ^~~
jail.cpp:39:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   39 |         for (int i = 0; i < g.size(); ++i) ord[tin[i]] = i;
      |                         ~~^~~~~~~~~~
jail.cpp: In function 'void init(int)':
jail.cpp:84:23: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
   84 |     sz = 1 << __lg(n) + !!(n & (n - 1));
      |               ~~~~~~~~^~~~~~~~~~~~~~~~~
jail.cpp: In instantiation of 'solve()::<lambda(auto:23, int, int, int, int, int)> [with auto:23 = solve()::<lambda(auto:23, int, int, int, int, int)>]':
jail.cpp:158:42:   required from here
jail.cpp:144:26: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
  144 |             int mid = lx + rx >> 1;
      |                       ~~~^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 15444 KB Output is correct
2 Correct 8 ms 15444 KB Output is correct
3 Correct 8 ms 15468 KB Output is correct
4 Correct 24 ms 15468 KB Output is correct
5 Correct 47 ms 15492 KB Output is correct
6 Correct 10 ms 15488 KB Output is correct
7 Correct 10 ms 15528 KB Output is correct
8 Correct 12 ms 15536 KB Output is correct
9 Correct 66 ms 17588 KB Output is correct
10 Correct 56 ms 38860 KB Output is correct
11 Correct 20 ms 15444 KB Output is correct
12 Correct 75 ms 15516 KB Output is correct
13 Correct 205 ms 63384 KB Output is correct
14 Correct 175 ms 60876 KB Output is correct
15 Correct 388 ms 66484 KB Output is correct
16 Correct 824 ms 99124 KB Output is correct
17 Correct 242 ms 70480 KB Output is correct
18 Correct 198 ms 74112 KB Output is correct
19 Correct 214 ms 68380 KB Output is correct
20 Correct 222 ms 68480 KB Output is correct
21 Correct 256 ms 72100 KB Output is correct
22 Correct 144 ms 59536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 15444 KB Output is correct
2 Correct 10 ms 15444 KB Output is correct
3 Correct 11 ms 15444 KB Output is correct
4 Correct 12 ms 15460 KB Output is correct
5 Correct 12 ms 15444 KB Output is correct
6 Correct 10 ms 15396 KB Output is correct
7 Correct 10 ms 15444 KB Output is correct
8 Correct 10 ms 15476 KB Output is correct
9 Correct 10 ms 15444 KB Output is correct
10 Correct 10 ms 15444 KB Output is correct
11 Correct 10 ms 15444 KB Output is correct
12 Correct 9 ms 15444 KB Output is correct
13 Correct 9 ms 15444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 15444 KB Output is correct
2 Correct 10 ms 15444 KB Output is correct
3 Correct 11 ms 15444 KB Output is correct
4 Correct 12 ms 15460 KB Output is correct
5 Correct 12 ms 15444 KB Output is correct
6 Correct 10 ms 15396 KB Output is correct
7 Correct 10 ms 15444 KB Output is correct
8 Correct 10 ms 15476 KB Output is correct
9 Correct 10 ms 15444 KB Output is correct
10 Correct 10 ms 15444 KB Output is correct
11 Correct 10 ms 15444 KB Output is correct
12 Correct 9 ms 15444 KB Output is correct
13 Correct 9 ms 15444 KB Output is correct
14 Correct 9 ms 15444 KB Output is correct
15 Correct 9 ms 15480 KB Output is correct
16 Correct 10 ms 15500 KB Output is correct
17 Correct 11 ms 15444 KB Output is correct
18 Correct 11 ms 15536 KB Output is correct
19 Correct 9 ms 15364 KB Output is correct
20 Correct 15 ms 15444 KB Output is correct
21 Correct 10 ms 15428 KB Output is correct
22 Correct 10 ms 15444 KB Output is correct
23 Correct 9 ms 15444 KB Output is correct
24 Correct 8 ms 15500 KB Output is correct
25 Correct 10 ms 15444 KB Output is correct
26 Correct 9 ms 15444 KB Output is correct
27 Correct 10 ms 15444 KB Output is correct
28 Correct 9 ms 15484 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 15444 KB Output is correct
2 Correct 10 ms 15444 KB Output is correct
3 Correct 11 ms 15444 KB Output is correct
4 Correct 12 ms 15460 KB Output is correct
5 Correct 12 ms 15444 KB Output is correct
6 Correct 10 ms 15396 KB Output is correct
7 Correct 10 ms 15444 KB Output is correct
8 Correct 10 ms 15476 KB Output is correct
9 Correct 10 ms 15444 KB Output is correct
10 Correct 10 ms 15444 KB Output is correct
11 Correct 10 ms 15444 KB Output is correct
12 Correct 9 ms 15444 KB Output is correct
13 Correct 9 ms 15444 KB Output is correct
14 Correct 9 ms 15444 KB Output is correct
15 Correct 9 ms 15480 KB Output is correct
16 Correct 10 ms 15500 KB Output is correct
17 Correct 11 ms 15444 KB Output is correct
18 Correct 11 ms 15536 KB Output is correct
19 Correct 9 ms 15364 KB Output is correct
20 Correct 15 ms 15444 KB Output is correct
21 Correct 10 ms 15428 KB Output is correct
22 Correct 10 ms 15444 KB Output is correct
23 Correct 9 ms 15444 KB Output is correct
24 Correct 8 ms 15500 KB Output is correct
25 Correct 10 ms 15444 KB Output is correct
26 Correct 9 ms 15444 KB Output is correct
27 Correct 10 ms 15444 KB Output is correct
28 Correct 9 ms 15484 KB Output is correct
29 Correct 12 ms 15572 KB Output is correct
30 Correct 13 ms 15572 KB Output is correct
31 Correct 11 ms 15572 KB Output is correct
32 Correct 11 ms 15444 KB Output is correct
33 Correct 10 ms 15444 KB Output is correct
34 Correct 14 ms 15444 KB Output is correct
35 Correct 14 ms 15444 KB Output is correct
36 Correct 14 ms 15544 KB Output is correct
37 Correct 13 ms 15444 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 10 ms 15444 KB Output is correct
2 Correct 10 ms 15444 KB Output is correct
3 Correct 11 ms 15444 KB Output is correct
4 Correct 12 ms 15460 KB Output is correct
5 Correct 12 ms 15444 KB Output is correct
6 Correct 10 ms 15396 KB Output is correct
7 Correct 10 ms 15444 KB Output is correct
8 Correct 10 ms 15476 KB Output is correct
9 Correct 10 ms 15444 KB Output is correct
10 Correct 10 ms 15444 KB Output is correct
11 Correct 10 ms 15444 KB Output is correct
12 Correct 9 ms 15444 KB Output is correct
13 Correct 9 ms 15444 KB Output is correct
14 Correct 9 ms 15444 KB Output is correct
15 Correct 9 ms 15480 KB Output is correct
16 Correct 10 ms 15500 KB Output is correct
17 Correct 11 ms 15444 KB Output is correct
18 Correct 11 ms 15536 KB Output is correct
19 Correct 9 ms 15364 KB Output is correct
20 Correct 15 ms 15444 KB Output is correct
21 Correct 10 ms 15428 KB Output is correct
22 Correct 10 ms 15444 KB Output is correct
23 Correct 9 ms 15444 KB Output is correct
24 Correct 8 ms 15500 KB Output is correct
25 Correct 10 ms 15444 KB Output is correct
26 Correct 9 ms 15444 KB Output is correct
27 Correct 10 ms 15444 KB Output is correct
28 Correct 9 ms 15484 KB Output is correct
29 Correct 12 ms 15572 KB Output is correct
30 Correct 13 ms 15572 KB Output is correct
31 Correct 11 ms 15572 KB Output is correct
32 Correct 11 ms 15444 KB Output is correct
33 Correct 10 ms 15444 KB Output is correct
34 Correct 14 ms 15444 KB Output is correct
35 Correct 14 ms 15444 KB Output is correct
36 Correct 14 ms 15544 KB Output is correct
37 Correct 13 ms 15444 KB Output is correct
38 Correct 60 ms 17472 KB Output is correct
39 Correct 60 ms 38848 KB Output is correct
40 Correct 76 ms 17440 KB Output is correct
41 Correct 87 ms 17584 KB Output is correct
42 Correct 62 ms 17412 KB Output is correct
43 Correct 49 ms 16756 KB Output is correct
44 Correct 27 ms 15956 KB Output is correct
45 Correct 91 ms 33000 KB Output is correct
46 Correct 85 ms 32972 KB Output is correct
47 Correct 75 ms 35892 KB Output is correct
48 Correct 66 ms 35964 KB Output is correct
49 Correct 74 ms 33180 KB Output is correct
50 Correct 74 ms 33204 KB Output is correct
51 Correct 58 ms 34436 KB Output is correct
52 Correct 61 ms 34076 KB Output is correct
53 Correct 31 ms 17088 KB Output is correct
54 Correct 119 ms 33020 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 15444 KB Output is correct
2 Correct 8 ms 15472 KB Output is correct
3 Correct 8 ms 15472 KB Output is correct
4 Correct 8 ms 15444 KB Output is correct
5 Correct 17 ms 15444 KB Output is correct
6 Correct 9 ms 15444 KB Output is correct
7 Correct 10 ms 15444 KB Output is correct
8 Correct 8 ms 15444 KB Output is correct
9 Correct 8 ms 15480 KB Output is correct
10 Correct 9 ms 15500 KB Output is correct
11 Correct 9 ms 15444 KB Output is correct
12 Correct 11 ms 15444 KB Output is correct
13 Correct 63 ms 15444 KB Output is correct
14 Correct 80 ms 15504 KB Output is correct
15 Correct 63 ms 15520 KB Output is correct
16 Correct 144 ms 37724 KB Output is correct
17 Correct 482 ms 59724 KB Output is correct
18 Correct 869 ms 82636 KB Output is correct
19 Correct 204 ms 42568 KB Output is correct
20 Correct 200 ms 42560 KB Output is correct
21 Correct 253 ms 42540 KB Output is correct
22 Correct 391 ms 58572 KB Output is correct
23 Correct 310 ms 58168 KB Output is correct
24 Correct 299 ms 58260 KB Output is correct
25 Correct 287 ms 57808 KB Output is correct
26 Correct 294 ms 58224 KB Output is correct
27 Correct 508 ms 71980 KB Output is correct
28 Correct 545 ms 72724 KB Output is correct
29 Correct 514 ms 70836 KB Output is correct
30 Correct 343 ms 58968 KB Output is correct
31 Correct 348 ms 59556 KB Output is correct
32 Correct 317 ms 58748 KB Output is correct
33 Correct 363 ms 59808 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 15444 KB Output is correct
2 Correct 8 ms 15444 KB Output is correct
3 Correct 8 ms 15468 KB Output is correct
4 Correct 24 ms 15468 KB Output is correct
5 Correct 47 ms 15492 KB Output is correct
6 Correct 10 ms 15488 KB Output is correct
7 Correct 10 ms 15528 KB Output is correct
8 Correct 12 ms 15536 KB Output is correct
9 Correct 66 ms 17588 KB Output is correct
10 Correct 56 ms 38860 KB Output is correct
11 Correct 20 ms 15444 KB Output is correct
12 Correct 75 ms 15516 KB Output is correct
13 Correct 205 ms 63384 KB Output is correct
14 Correct 175 ms 60876 KB Output is correct
15 Correct 388 ms 66484 KB Output is correct
16 Correct 824 ms 99124 KB Output is correct
17 Correct 242 ms 70480 KB Output is correct
18 Correct 198 ms 74112 KB Output is correct
19 Correct 214 ms 68380 KB Output is correct
20 Correct 222 ms 68480 KB Output is correct
21 Correct 256 ms 72100 KB Output is correct
22 Correct 144 ms 59536 KB Output is correct
23 Correct 10 ms 15444 KB Output is correct
24 Correct 10 ms 15444 KB Output is correct
25 Correct 11 ms 15444 KB Output is correct
26 Correct 12 ms 15460 KB Output is correct
27 Correct 12 ms 15444 KB Output is correct
28 Correct 10 ms 15396 KB Output is correct
29 Correct 10 ms 15444 KB Output is correct
30 Correct 10 ms 15476 KB Output is correct
31 Correct 10 ms 15444 KB Output is correct
32 Correct 10 ms 15444 KB Output is correct
33 Correct 10 ms 15444 KB Output is correct
34 Correct 9 ms 15444 KB Output is correct
35 Correct 9 ms 15444 KB Output is correct
36 Correct 9 ms 15444 KB Output is correct
37 Correct 9 ms 15480 KB Output is correct
38 Correct 10 ms 15500 KB Output is correct
39 Correct 11 ms 15444 KB Output is correct
40 Correct 11 ms 15536 KB Output is correct
41 Correct 9 ms 15364 KB Output is correct
42 Correct 15 ms 15444 KB Output is correct
43 Correct 10 ms 15428 KB Output is correct
44 Correct 10 ms 15444 KB Output is correct
45 Correct 9 ms 15444 KB Output is correct
46 Correct 8 ms 15500 KB Output is correct
47 Correct 10 ms 15444 KB Output is correct
48 Correct 9 ms 15444 KB Output is correct
49 Correct 10 ms 15444 KB Output is correct
50 Correct 9 ms 15484 KB Output is correct
51 Correct 12 ms 15572 KB Output is correct
52 Correct 13 ms 15572 KB Output is correct
53 Correct 11 ms 15572 KB Output is correct
54 Correct 11 ms 15444 KB Output is correct
55 Correct 10 ms 15444 KB Output is correct
56 Correct 14 ms 15444 KB Output is correct
57 Correct 14 ms 15444 KB Output is correct
58 Correct 14 ms 15544 KB Output is correct
59 Correct 13 ms 15444 KB Output is correct
60 Correct 60 ms 17472 KB Output is correct
61 Correct 60 ms 38848 KB Output is correct
62 Correct 76 ms 17440 KB Output is correct
63 Correct 87 ms 17584 KB Output is correct
64 Correct 62 ms 17412 KB Output is correct
65 Correct 49 ms 16756 KB Output is correct
66 Correct 27 ms 15956 KB Output is correct
67 Correct 91 ms 33000 KB Output is correct
68 Correct 85 ms 32972 KB Output is correct
69 Correct 75 ms 35892 KB Output is correct
70 Correct 66 ms 35964 KB Output is correct
71 Correct 74 ms 33180 KB Output is correct
72 Correct 74 ms 33204 KB Output is correct
73 Correct 58 ms 34436 KB Output is correct
74 Correct 61 ms 34076 KB Output is correct
75 Correct 31 ms 17088 KB Output is correct
76 Correct 119 ms 33020 KB Output is correct
77 Correct 8 ms 15444 KB Output is correct
78 Correct 8 ms 15472 KB Output is correct
79 Correct 8 ms 15472 KB Output is correct
80 Correct 8 ms 15444 KB Output is correct
81 Correct 17 ms 15444 KB Output is correct
82 Correct 9 ms 15444 KB Output is correct
83 Correct 10 ms 15444 KB Output is correct
84 Correct 8 ms 15444 KB Output is correct
85 Correct 8 ms 15480 KB Output is correct
86 Correct 9 ms 15500 KB Output is correct
87 Correct 9 ms 15444 KB Output is correct
88 Correct 11 ms 15444 KB Output is correct
89 Correct 63 ms 15444 KB Output is correct
90 Correct 80 ms 15504 KB Output is correct
91 Correct 63 ms 15520 KB Output is correct
92 Correct 144 ms 37724 KB Output is correct
93 Correct 482 ms 59724 KB Output is correct
94 Correct 869 ms 82636 KB Output is correct
95 Correct 204 ms 42568 KB Output is correct
96 Correct 200 ms 42560 KB Output is correct
97 Correct 253 ms 42540 KB Output is correct
98 Correct 391 ms 58572 KB Output is correct
99 Correct 310 ms 58168 KB Output is correct
100 Correct 299 ms 58260 KB Output is correct
101 Correct 287 ms 57808 KB Output is correct
102 Correct 294 ms 58224 KB Output is correct
103 Correct 508 ms 71980 KB Output is correct
104 Correct 545 ms 72724 KB Output is correct
105 Correct 514 ms 70836 KB Output is correct
106 Correct 343 ms 58968 KB Output is correct
107 Correct 348 ms 59556 KB Output is correct
108 Correct 317 ms 58748 KB Output is correct
109 Correct 363 ms 59808 KB Output is correct
110 Correct 74 ms 15596 KB Output is correct
111 Correct 46 ms 15508 KB Output is correct
112 Correct 533 ms 67580 KB Output is correct
113 Correct 177 ms 47052 KB Output is correct
114 Correct 343 ms 61884 KB Output is correct
115 Correct 63 ms 34856 KB Output is correct
116 Correct 297 ms 55600 KB Output is correct
117 Correct 1118 ms 101756 KB Output is correct
118 Correct 118 ms 32996 KB Output is correct
119 Correct 98 ms 33000 KB Output is correct
120 Correct 24 ms 18788 KB Output is correct
121 Correct 375 ms 56920 KB Output is correct
122 Correct 399 ms 56748 KB Output is correct
123 Correct 197 ms 49816 KB Output is correct
124 Correct 196 ms 49836 KB Output is correct
125 Correct 230 ms 51252 KB Output is correct
126 Correct 829 ms 96676 KB Output is correct
127 Correct 319 ms 73512 KB Output is correct
128 Correct 261 ms 73876 KB Output is correct
129 Correct 283 ms 73420 KB Output is correct
130 Correct 259 ms 73672 KB Output is correct