Submission #545055

# Submission time Handle Problem Language Result Execution time Memory
545055 2022-04-03T13:18:59 Z baluteshih Jail (JOI22_jail) C++14
100 / 100
712 ms 61816 KB
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define X first
#define Y second
#define SZ(a) ((int)a.size())
#define ALL(v) v.begin(), v.end()
#define pb push_back

const int MAXN = 120005;
vector<int> G[MAXN];
int pa[MAXN], head[MAXN], sz[MAXN], dep[MAXN], dfn[MAXN], seq[MAXN], best[MAXN], dft;
int n;
pii walk[MAXN];

pii mem[MAXN * 100];
int memtop;
int tG[MAXN * 10], top, indeg[MAXN * 10];

struct Node {
    int up, dwn;
} seg[MAXN << 2];

void add_edge(int u, int v) {
    //cerr << "add_edge " << u << " " << v << "\n";
    mem[++memtop] = pii(tG[u], v);
    tG[u] = memtop;
    ++indeg[v];
}

void build(int l, int r, int rt) {
    if (l == r) {
        seg[rt].up = l;
        seg[rt].dwn = l + n;
        return;
    }
    seg[rt].up = ++top;
    indeg[top] = 0, tG[top] = 0;
    seg[rt].dwn = ++top;
    indeg[top] = 0, tG[top] = 0;
    //cerr << "[" << l << " " << r << "]: " << seg[rt].up << " " << seg[rt].dwn << "\n";
    int mid = (l + r) >> 1;
    build(l, mid, rt << 1), build(mid + 1, r, rt << 1 | 1);
    add_edge(seg[rt << 1].up, seg[rt].up);
    add_edge(seg[rt << 1 | 1].up, seg[rt].up);
    add_edge(seg[rt].dwn, seg[rt << 1].dwn);
    add_edge(seg[rt].dwn, seg[rt << 1 | 1].dwn);
}

void modify(int L, int R, int l, int r, int rt, int up, int idx) {
    if (L <= l && R >= r) { 
        if (up)
            add_edge(seg[rt].up, idx);
        else
            add_edge(idx, seg[rt].dwn);
        return;
    }
    int mid = (l + r) >> 1;
    if (L <= mid)
        modify(L, R, l, mid, rt << 1, up, idx);
    if (R > mid)
        modify(L, R, mid + 1, r, rt << 1 | 1, up, idx);
}

void dfs(int u, int f) {
    pa[u] = f;
    sz[u] = 1;
    best[u] = 0;
    for (int i : G[u])
        if (i != f) {
            dep[i] = dep[u] + 1;
            dfs(i, u);
            sz[u] += sz[i];
            if (sz[best[u]] < sz[i])
                best[u] = i;
        }
}

void dfs2(int u, int h) {
    head[u] = h;
    dfn[u] = ++dft;
    seq[dft] = u;
    if (best[u])
        dfs2(best[u], h);
    for (int i : G[u])
        if (i != pa[u] && i != best[u])
            dfs2(i, i);
}

void path_add(int u, int v, int up, int idx) {
    //cerr << "path_add " << u << " " << v << " " << up << " " << idx << "\n";
    int ban = v;
    while (head[u] != head[v]) {
        if (dep[head[u]] < dep[head[v]])
            swap(u, v);
        if (u != ban)
            modify(dfn[head[u]], dfn[u], 1, n, 1, up, idx);
        else if (dfn[head[u]] < dfn[u])
            modify(dfn[head[u]], dfn[u] - 1, 1, n, 1, up, idx);
        u = pa[head[u]];
    }
    if (dfn[u] > dfn[v])
        swap(u, v);
    if (u != ban && v != ban)
        modify(dfn[u], dfn[v], 1, n, 1, up, idx);
    else if (u == ban && dfn[u] < dfn[v])
        modify(dfn[u] + 1, dfn[v], 1, n, 1, up, idx);
    else if (v == ban && dfn[u] < dfn[v])
        modify(dfn[u], dfn[v] - 1, 1, n, 1, up, idx);
}

bool solve() {
    memtop = 0;
    int m;
    cin >> n, top = n * 2;
    for (int i = 1; i <= n; ++i)
        G[i].clear(), indeg[i] = tG[i] = indeg[i + n] = tG[i + n] = 0;
    build(1, n, 1);
    for (int i = 1; i < n; ++i) {
        int a, b;
        cin >> a >> b;
        G[a].pb(b);
        G[b].pb(a);
    }
    dft = 0, dfs(1, 1), dfs2(1, 1);
    /*for (int i = 1; i <= n; ++i)
        cerr << seq[i] << " \n"[i == n];
    for (int i = 1; i <= n; ++i)
        cerr << head[i] << " \n"[i == n];*/
    cin >> m;
    for (int i = 1; i <= m; ++i) {
        indeg[top + i] = tG[top + i] = 0;
        cin >> walk[i].X >> walk[i].Y;
    }
    for (int i = 1; i <= m; ++i) {
        add_edge(top + i, dfn[walk[i].X]);
        add_edge(dfn[walk[i].Y] + n, top + i);
    }
    for (int i = 1; i <= m; ++i) {
        int u = walk[i].X, v = walk[i].Y;
        path_add(u, v, 0, top + i);
        path_add(v, u, 1, top + i);
    }
    top += m;
    queue<int> q;
    for (int i = 1; i <= top; ++i)
        if (!indeg[i])
            q.push(i);
    while (!q.empty()) {
        int u = q.front();
        //cerr << "fin " << u << "\n";
        q.pop();
        while (tG[u] != 0) {
            if (!--indeg[mem[tG[u]].Y])
                q.push(mem[tG[u]].Y);
            tG[u] = mem[tG[u]].X;
        }
    }
    for (int i = 1; i <= top; ++i)
        if (indeg[i])
            return 0;
    return 1;
}

int main() {
    ios::sync_with_stdio(0), cin.tie(0);
    int T;
    cin >> T;
    while (T--) {
        if (solve())
            cout << "Yes\n";
        else
            cout << "No\n";
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3280 KB Output is correct
2 Correct 2 ms 3156 KB Output is correct
3 Correct 2 ms 3156 KB Output is correct
4 Correct 13 ms 3156 KB Output is correct
5 Correct 24 ms 3236 KB Output is correct
6 Correct 3 ms 3156 KB Output is correct
7 Correct 3 ms 3156 KB Output is correct
8 Correct 4 ms 3156 KB Output is correct
9 Correct 33 ms 4608 KB Output is correct
10 Correct 55 ms 29864 KB Output is correct
11 Correct 9 ms 3224 KB Output is correct
12 Correct 44 ms 3236 KB Output is correct
13 Correct 95 ms 36108 KB Output is correct
14 Correct 70 ms 36300 KB Output is correct
15 Correct 111 ms 42560 KB Output is correct
16 Correct 209 ms 60880 KB Output is correct
17 Correct 151 ms 46228 KB Output is correct
18 Correct 92 ms 34916 KB Output is correct
19 Correct 132 ms 44676 KB Output is correct
20 Correct 138 ms 44620 KB Output is correct
21 Correct 107 ms 46236 KB Output is correct
22 Correct 70 ms 32120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3156 KB Output is correct
2 Correct 2 ms 3156 KB Output is correct
3 Correct 3 ms 3156 KB Output is correct
4 Correct 3 ms 3156 KB Output is correct
5 Correct 3 ms 3156 KB Output is correct
6 Correct 3 ms 3156 KB Output is correct
7 Correct 3 ms 3156 KB Output is correct
8 Correct 3 ms 3156 KB Output is correct
9 Correct 3 ms 3156 KB Output is correct
10 Correct 3 ms 3156 KB Output is correct
11 Correct 3 ms 3156 KB Output is correct
12 Correct 2 ms 3156 KB Output is correct
13 Correct 3 ms 3156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3156 KB Output is correct
2 Correct 2 ms 3156 KB Output is correct
3 Correct 3 ms 3156 KB Output is correct
4 Correct 3 ms 3156 KB Output is correct
5 Correct 3 ms 3156 KB Output is correct
6 Correct 3 ms 3156 KB Output is correct
7 Correct 3 ms 3156 KB Output is correct
8 Correct 3 ms 3156 KB Output is correct
9 Correct 3 ms 3156 KB Output is correct
10 Correct 3 ms 3156 KB Output is correct
11 Correct 3 ms 3156 KB Output is correct
12 Correct 2 ms 3156 KB Output is correct
13 Correct 3 ms 3156 KB Output is correct
14 Correct 2 ms 3156 KB Output is correct
15 Correct 2 ms 3156 KB Output is correct
16 Correct 3 ms 3156 KB Output is correct
17 Correct 4 ms 3156 KB Output is correct
18 Correct 3 ms 3156 KB Output is correct
19 Correct 2 ms 3156 KB Output is correct
20 Correct 4 ms 3156 KB Output is correct
21 Correct 3 ms 3252 KB Output is correct
22 Correct 3 ms 3156 KB Output is correct
23 Correct 2 ms 3156 KB Output is correct
24 Correct 2 ms 3156 KB Output is correct
25 Correct 3 ms 3156 KB Output is correct
26 Correct 2 ms 3156 KB Output is correct
27 Correct 3 ms 3156 KB Output is correct
28 Correct 2 ms 3156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3156 KB Output is correct
2 Correct 2 ms 3156 KB Output is correct
3 Correct 3 ms 3156 KB Output is correct
4 Correct 3 ms 3156 KB Output is correct
5 Correct 3 ms 3156 KB Output is correct
6 Correct 3 ms 3156 KB Output is correct
7 Correct 3 ms 3156 KB Output is correct
8 Correct 3 ms 3156 KB Output is correct
9 Correct 3 ms 3156 KB Output is correct
10 Correct 3 ms 3156 KB Output is correct
11 Correct 3 ms 3156 KB Output is correct
12 Correct 2 ms 3156 KB Output is correct
13 Correct 3 ms 3156 KB Output is correct
14 Correct 2 ms 3156 KB Output is correct
15 Correct 2 ms 3156 KB Output is correct
16 Correct 3 ms 3156 KB Output is correct
17 Correct 4 ms 3156 KB Output is correct
18 Correct 3 ms 3156 KB Output is correct
19 Correct 2 ms 3156 KB Output is correct
20 Correct 4 ms 3156 KB Output is correct
21 Correct 3 ms 3252 KB Output is correct
22 Correct 3 ms 3156 KB Output is correct
23 Correct 2 ms 3156 KB Output is correct
24 Correct 2 ms 3156 KB Output is correct
25 Correct 3 ms 3156 KB Output is correct
26 Correct 2 ms 3156 KB Output is correct
27 Correct 3 ms 3156 KB Output is correct
28 Correct 2 ms 3156 KB Output is correct
29 Correct 4 ms 3156 KB Output is correct
30 Correct 4 ms 3156 KB Output is correct
31 Correct 4 ms 3156 KB Output is correct
32 Correct 4 ms 3156 KB Output is correct
33 Correct 5 ms 3156 KB Output is correct
34 Correct 5 ms 3156 KB Output is correct
35 Correct 5 ms 3156 KB Output is correct
36 Correct 5 ms 3156 KB Output is correct
37 Correct 4 ms 3156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3156 KB Output is correct
2 Correct 2 ms 3156 KB Output is correct
3 Correct 3 ms 3156 KB Output is correct
4 Correct 3 ms 3156 KB Output is correct
5 Correct 3 ms 3156 KB Output is correct
6 Correct 3 ms 3156 KB Output is correct
7 Correct 3 ms 3156 KB Output is correct
8 Correct 3 ms 3156 KB Output is correct
9 Correct 3 ms 3156 KB Output is correct
10 Correct 3 ms 3156 KB Output is correct
11 Correct 3 ms 3156 KB Output is correct
12 Correct 2 ms 3156 KB Output is correct
13 Correct 3 ms 3156 KB Output is correct
14 Correct 2 ms 3156 KB Output is correct
15 Correct 2 ms 3156 KB Output is correct
16 Correct 3 ms 3156 KB Output is correct
17 Correct 4 ms 3156 KB Output is correct
18 Correct 3 ms 3156 KB Output is correct
19 Correct 2 ms 3156 KB Output is correct
20 Correct 4 ms 3156 KB Output is correct
21 Correct 3 ms 3252 KB Output is correct
22 Correct 3 ms 3156 KB Output is correct
23 Correct 2 ms 3156 KB Output is correct
24 Correct 2 ms 3156 KB Output is correct
25 Correct 3 ms 3156 KB Output is correct
26 Correct 2 ms 3156 KB Output is correct
27 Correct 3 ms 3156 KB Output is correct
28 Correct 2 ms 3156 KB Output is correct
29 Correct 4 ms 3156 KB Output is correct
30 Correct 4 ms 3156 KB Output is correct
31 Correct 4 ms 3156 KB Output is correct
32 Correct 4 ms 3156 KB Output is correct
33 Correct 5 ms 3156 KB Output is correct
34 Correct 5 ms 3156 KB Output is correct
35 Correct 5 ms 3156 KB Output is correct
36 Correct 5 ms 3156 KB Output is correct
37 Correct 4 ms 3156 KB Output is correct
38 Correct 36 ms 4628 KB Output is correct
39 Correct 60 ms 29792 KB Output is correct
40 Correct 48 ms 4360 KB Output is correct
41 Correct 59 ms 4228 KB Output is correct
42 Correct 43 ms 4652 KB Output is correct
43 Correct 33 ms 4180 KB Output is correct
44 Correct 17 ms 3456 KB Output is correct
45 Correct 71 ms 20428 KB Output is correct
46 Correct 65 ms 20468 KB Output is correct
47 Correct 52 ms 25080 KB Output is correct
48 Correct 53 ms 25036 KB Output is correct
49 Correct 58 ms 20660 KB Output is correct
50 Correct 72 ms 20676 KB Output is correct
51 Correct 55 ms 21768 KB Output is correct
52 Correct 49 ms 21836 KB Output is correct
53 Correct 15 ms 4344 KB Output is correct
54 Correct 78 ms 20344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3156 KB Output is correct
2 Correct 3 ms 3156 KB Output is correct
3 Correct 2 ms 3156 KB Output is correct
4 Correct 2 ms 3156 KB Output is correct
5 Correct 9 ms 3220 KB Output is correct
6 Correct 3 ms 3156 KB Output is correct
7 Correct 3 ms 3156 KB Output is correct
8 Correct 2 ms 3156 KB Output is correct
9 Correct 2 ms 3156 KB Output is correct
10 Correct 2 ms 3156 KB Output is correct
11 Correct 2 ms 3156 KB Output is correct
12 Correct 4 ms 3156 KB Output is correct
13 Correct 32 ms 3156 KB Output is correct
14 Correct 70 ms 3220 KB Output is correct
15 Correct 41 ms 3224 KB Output is correct
16 Correct 110 ms 22500 KB Output is correct
17 Correct 299 ms 33228 KB Output is correct
18 Correct 469 ms 46768 KB Output is correct
19 Correct 171 ms 24996 KB Output is correct
20 Correct 169 ms 24964 KB Output is correct
21 Correct 185 ms 25040 KB Output is correct
22 Correct 298 ms 32948 KB Output is correct
23 Correct 194 ms 33100 KB Output is correct
24 Correct 219 ms 33524 KB Output is correct
25 Correct 251 ms 33612 KB Output is correct
26 Correct 228 ms 33752 KB Output is correct
27 Correct 238 ms 27976 KB Output is correct
28 Correct 178 ms 27972 KB Output is correct
29 Correct 144 ms 27948 KB Output is correct
30 Correct 190 ms 24684 KB Output is correct
31 Correct 136 ms 24748 KB Output is correct
32 Correct 159 ms 24664 KB Output is correct
33 Correct 137 ms 24676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 3280 KB Output is correct
2 Correct 2 ms 3156 KB Output is correct
3 Correct 2 ms 3156 KB Output is correct
4 Correct 13 ms 3156 KB Output is correct
5 Correct 24 ms 3236 KB Output is correct
6 Correct 3 ms 3156 KB Output is correct
7 Correct 3 ms 3156 KB Output is correct
8 Correct 4 ms 3156 KB Output is correct
9 Correct 33 ms 4608 KB Output is correct
10 Correct 55 ms 29864 KB Output is correct
11 Correct 9 ms 3224 KB Output is correct
12 Correct 44 ms 3236 KB Output is correct
13 Correct 95 ms 36108 KB Output is correct
14 Correct 70 ms 36300 KB Output is correct
15 Correct 111 ms 42560 KB Output is correct
16 Correct 209 ms 60880 KB Output is correct
17 Correct 151 ms 46228 KB Output is correct
18 Correct 92 ms 34916 KB Output is correct
19 Correct 132 ms 44676 KB Output is correct
20 Correct 138 ms 44620 KB Output is correct
21 Correct 107 ms 46236 KB Output is correct
22 Correct 70 ms 32120 KB Output is correct
23 Correct 2 ms 3156 KB Output is correct
24 Correct 2 ms 3156 KB Output is correct
25 Correct 3 ms 3156 KB Output is correct
26 Correct 3 ms 3156 KB Output is correct
27 Correct 3 ms 3156 KB Output is correct
28 Correct 3 ms 3156 KB Output is correct
29 Correct 3 ms 3156 KB Output is correct
30 Correct 3 ms 3156 KB Output is correct
31 Correct 3 ms 3156 KB Output is correct
32 Correct 3 ms 3156 KB Output is correct
33 Correct 3 ms 3156 KB Output is correct
34 Correct 2 ms 3156 KB Output is correct
35 Correct 3 ms 3156 KB Output is correct
36 Correct 2 ms 3156 KB Output is correct
37 Correct 2 ms 3156 KB Output is correct
38 Correct 3 ms 3156 KB Output is correct
39 Correct 4 ms 3156 KB Output is correct
40 Correct 3 ms 3156 KB Output is correct
41 Correct 2 ms 3156 KB Output is correct
42 Correct 4 ms 3156 KB Output is correct
43 Correct 3 ms 3252 KB Output is correct
44 Correct 3 ms 3156 KB Output is correct
45 Correct 2 ms 3156 KB Output is correct
46 Correct 2 ms 3156 KB Output is correct
47 Correct 3 ms 3156 KB Output is correct
48 Correct 2 ms 3156 KB Output is correct
49 Correct 3 ms 3156 KB Output is correct
50 Correct 2 ms 3156 KB Output is correct
51 Correct 4 ms 3156 KB Output is correct
52 Correct 4 ms 3156 KB Output is correct
53 Correct 4 ms 3156 KB Output is correct
54 Correct 4 ms 3156 KB Output is correct
55 Correct 5 ms 3156 KB Output is correct
56 Correct 5 ms 3156 KB Output is correct
57 Correct 5 ms 3156 KB Output is correct
58 Correct 5 ms 3156 KB Output is correct
59 Correct 4 ms 3156 KB Output is correct
60 Correct 36 ms 4628 KB Output is correct
61 Correct 60 ms 29792 KB Output is correct
62 Correct 48 ms 4360 KB Output is correct
63 Correct 59 ms 4228 KB Output is correct
64 Correct 43 ms 4652 KB Output is correct
65 Correct 33 ms 4180 KB Output is correct
66 Correct 17 ms 3456 KB Output is correct
67 Correct 71 ms 20428 KB Output is correct
68 Correct 65 ms 20468 KB Output is correct
69 Correct 52 ms 25080 KB Output is correct
70 Correct 53 ms 25036 KB Output is correct
71 Correct 58 ms 20660 KB Output is correct
72 Correct 72 ms 20676 KB Output is correct
73 Correct 55 ms 21768 KB Output is correct
74 Correct 49 ms 21836 KB Output is correct
75 Correct 15 ms 4344 KB Output is correct
76 Correct 78 ms 20344 KB Output is correct
77 Correct 2 ms 3156 KB Output is correct
78 Correct 3 ms 3156 KB Output is correct
79 Correct 2 ms 3156 KB Output is correct
80 Correct 2 ms 3156 KB Output is correct
81 Correct 9 ms 3220 KB Output is correct
82 Correct 3 ms 3156 KB Output is correct
83 Correct 3 ms 3156 KB Output is correct
84 Correct 2 ms 3156 KB Output is correct
85 Correct 2 ms 3156 KB Output is correct
86 Correct 2 ms 3156 KB Output is correct
87 Correct 2 ms 3156 KB Output is correct
88 Correct 4 ms 3156 KB Output is correct
89 Correct 32 ms 3156 KB Output is correct
90 Correct 70 ms 3220 KB Output is correct
91 Correct 41 ms 3224 KB Output is correct
92 Correct 110 ms 22500 KB Output is correct
93 Correct 299 ms 33228 KB Output is correct
94 Correct 469 ms 46768 KB Output is correct
95 Correct 171 ms 24996 KB Output is correct
96 Correct 169 ms 24964 KB Output is correct
97 Correct 185 ms 25040 KB Output is correct
98 Correct 298 ms 32948 KB Output is correct
99 Correct 194 ms 33100 KB Output is correct
100 Correct 219 ms 33524 KB Output is correct
101 Correct 251 ms 33612 KB Output is correct
102 Correct 228 ms 33752 KB Output is correct
103 Correct 238 ms 27976 KB Output is correct
104 Correct 178 ms 27972 KB Output is correct
105 Correct 144 ms 27948 KB Output is correct
106 Correct 190 ms 24684 KB Output is correct
107 Correct 136 ms 24748 KB Output is correct
108 Correct 159 ms 24664 KB Output is correct
109 Correct 137 ms 24676 KB Output is correct
110 Correct 48 ms 3272 KB Output is correct
111 Correct 28 ms 3236 KB Output is correct
112 Correct 169 ms 40568 KB Output is correct
113 Correct 131 ms 30524 KB Output is correct
114 Correct 121 ms 34388 KB Output is correct
115 Correct 50 ms 20936 KB Output is correct
116 Correct 162 ms 24136 KB Output is correct
117 Correct 712 ms 61816 KB Output is correct
118 Correct 86 ms 20588 KB Output is correct
119 Correct 100 ms 20556 KB Output is correct
120 Correct 13 ms 5204 KB Output is correct
121 Correct 265 ms 26656 KB Output is correct
122 Correct 236 ms 26596 KB Output is correct
123 Correct 126 ms 33060 KB Output is correct
124 Correct 123 ms 33176 KB Output is correct
125 Correct 152 ms 33836 KB Output is correct
126 Correct 228 ms 57392 KB Output is correct
127 Correct 198 ms 49924 KB Output is correct
128 Correct 167 ms 50856 KB Output is correct
129 Correct 259 ms 50020 KB Output is correct
130 Correct 182 ms 51080 KB Output is correct