답안 #1000637

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1000637 2024-06-18T05:13:03 Z onbert Jail (JOI22_jail) C++17
100 / 100
1117 ms 160592 KB
#include <bits/stdc++.h>
using namespace std;
const int maxn = 120005, maxN = 480005, lgn = 17, INF = 1e6;
int n, m;
vector<int> ADJ[maxn], adj[maxn];
pair<int,int> a[maxn];
int sz[maxn], fa[maxn], d[maxn], newid[maxn], lim[maxn], cnter = 0;

bool cmp(int &x, int &y) {
    return sz[x] > sz[y];
}

void dfs1(int u) {
    sz[u] = 1;
    for (int v:ADJ[u]) {
        ADJ[v].erase(find(ADJ[v].begin(), ADJ[v].end(), u));
        dfs1(v);
        sz[u] += sz[v];
    }
    sort(ADJ[u].begin(), ADJ[u].end(), cmp);
}
void dfs2(int u) {
    cnter++;
    newid[u] = cnter;
    for (int v:ADJ[u]) {
        d[cnter+1] = d[newid[u]] + 1;
        fa[cnter+1] = newid[u];
        dfs2(v);
    }
    lim[newid[u]] = cnter;
}

int skip[maxn];
void dfs3(int u) {
    for (int v:adj[u]) {
        if (v == adj[u][0]) skip[v] = skip[u];
        else skip[v] = v;
        dfs3(v);
    }
}

vector<pair<int,int>> PATH(int u, int v) {
    vector<pair<int,int>> path;
    if (d[u] > d[v]) swap(u, v);
    path = {{skip[u], u}};
    u = skip[u];
    while (u>v || v>lim[u]) {
        u = fa[u];
        path.push_back({skip[u], u});
        u = skip[u];
    }
    auto [l, r] = path.back();
    while (l<r) {
        int mid = (l+r+1)/2;
        if (mid<=v && v<=lim[mid]) l = mid;
        else r = mid-1;
    }
    path.back().first = u = l;
    path.push_back({skip[v], v});
    v = skip[v];
    while (d[fa[v]] > d[u]) {
        v = fa[v];
        path.push_back({skip[v], v});
        v = skip[v];
    }
    path.back().first += d[u] - d[v] + 1;
    return path;
}

set<pair<int,int>> chain[maxn];

set<int> seg[maxN];
void build(int id, int l, int r) {
    seg[id].clear();
    if (l==r) return;
    int mid = (l+r)/2;
    build(id*2, l, mid); build(id*2+1, mid+1, r);
}
void update(int id, int l, int r, int &findl, int &findr, int &val, bool psh) {
    if (r<findl || findr<l) return;
    if (findl<=l && r<=findr) {
        if (psh) seg[id].insert(val);
        else seg[id].erase(val);
        // if (val == 3) cout << l << " " << r << " " << psh << endl;
        return;
    }
    int mid = (l+r)/2;
    update(id*2, l, mid, findl, findr, val, psh);
    update(id*2+1, mid+1, r, findl, findr, val, psh);
}
void qry(int id, int l, int r, int &target, vector<int> &nxt) {
    if (r<target || target<l) return;
    for (int i:seg[id]) nxt.push_back(i);
    if (l==r) return;
    int mid = (l+r)/2;
    qry(id*2, l, mid, target, nxt); qry(id*2+1, mid+1, r, target, nxt);
}

void UPDATE(int l, int r, int val, int psh) {
    // if (val == 3) cout << "test " << l << " " << r << " " << psh << endl;
    if (l==skip[l]) {
        if (psh) chain[l].insert({r, val});
        else chain[l].erase({r, val});
        // cout << l << " " << r << " " << val << " " << psh << endl;
    } else update(1, 1, n, l, r, val, psh);
}

int S[maxn], vis[maxn];
bool die = false;
set<int> exist;

void dfs4(int u) {
    auto [s, t] = a[u];
    // cout << "start " << u << " " << s << " " << t << endl;
    vis[u] = 1;
    vector<pair<int,int>> path = PATH(s, t);
    for (auto [L, R]:path) {
        int cur = L-1;
        while (true) {
            cur = *exist.upper_bound(cur);
            if (cur > R) break;
            if (cur==s) continue;
            int v = S[cur];
            // cout << "1 " << u << " " << v << " " << die << endl;
            if (!vis[v]) dfs4(v);
            else die = true;
            if (die) return;
        }
    }
    vector<int> nxt;
    qry(1, 1, n, t, nxt);
    for (int v:nxt) if (v!=u) {
        // cout << "2 " << u << " " << v << " " << die << endl;
        if (!vis[v]) dfs4(v);
        else if (vis[v]==1) die = true;
        if (die) return;
    }
    int head = skip[t];
    pair<int,int> cur = {t, 0};
    while (true) {
        cur = *chain[head].upper_bound(cur);
        if (cur.first == INF) break;
        if (cur.second == u) continue;
        int v = cur.second;
        // cout << "3 " << u << " " << v << " " << die << endl;
        if (!vis[v]) dfs4(v);
        else if (vis[v]==1) die = true;
        if (die) return;
    }
    for (auto [L, R]:path) UPDATE(L, R, u, 0);
    exist.erase(s);
    vis[u] = 2;
}

void solve() {
    cin >> n;
    for (int i=1;i<=n;i++) sz[i] = 0, adj[i].clear(), ADJ[i].clear();
    cnter = 0;
    for (int i=1;i<=n-1;i++) {
        int u, v;
        cin >> u >> v;
        ADJ[u].push_back(v);
        ADJ[v].push_back(u);
    }
    fa[1] = 0, d[1] = 0;
    dfs1(1); dfs2(1);
    for (int i=1;i<=n;i++) for (int j:ADJ[i]) adj[newid[i]].push_back(newid[j]);
    skip[1] = 1;
    dfs3(1);
    exist = {INF};
    build(1, 1, n);
    for (int i=1;i<=n;i++) chain[i] = {{INF, INF}}, S[i] = -1;
    cin >> m;
    for (int i=1;i<=m;i++) {
        int s, t;
        cin >> s >> t;
        s = newid[s], t = newid[t];
        a[i] = {s, t};
        S[s] = i;
        exist.insert(s);
        vector<pair<int,int>> path = PATH(s, t);
        for (auto [L, R]:path) UPDATE(L, R, i, 1);
    }
    for (int i=1;i<=m;i++) vis[i] = 0;
    die = false;
    for (int i=1;i<=m;i++) if (!vis[i]) {
        dfs4(i);
        if (die) break;
    }
    if (die) cout << "No\n";
    else cout << "Yes\n";
}

signed main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int t;
    cin >> t;
    while (t--) solve();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 38744 KB Output is correct
2 Correct 6 ms 38744 KB Output is correct
3 Correct 6 ms 38748 KB Output is correct
4 Correct 14 ms 38748 KB Output is correct
5 Correct 24 ms 38836 KB Output is correct
6 Correct 9 ms 38748 KB Output is correct
7 Correct 7 ms 38748 KB Output is correct
8 Correct 8 ms 38900 KB Output is correct
9 Correct 37 ms 40284 KB Output is correct
10 Correct 49 ms 65624 KB Output is correct
11 Correct 12 ms 38748 KB Output is correct
12 Correct 46 ms 38856 KB Output is correct
13 Correct 157 ms 85844 KB Output is correct
14 Correct 117 ms 85808 KB Output is correct
15 Correct 328 ms 104836 KB Output is correct
16 Correct 1010 ms 160592 KB Output is correct
17 Correct 298 ms 117584 KB Output is correct
18 Correct 233 ms 95056 KB Output is correct
19 Correct 266 ms 111440 KB Output is correct
20 Correct 274 ms 111640 KB Output is correct
21 Correct 282 ms 116948 KB Output is correct
22 Correct 87 ms 73552 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 38748 KB Output is correct
2 Correct 6 ms 38748 KB Output is correct
3 Correct 6 ms 38748 KB Output is correct
4 Correct 7 ms 38704 KB Output is correct
5 Correct 7 ms 38748 KB Output is correct
6 Correct 6 ms 38748 KB Output is correct
7 Correct 7 ms 38844 KB Output is correct
8 Correct 7 ms 38748 KB Output is correct
9 Correct 9 ms 38744 KB Output is correct
10 Correct 7 ms 38820 KB Output is correct
11 Correct 7 ms 38748 KB Output is correct
12 Correct 6 ms 38636 KB Output is correct
13 Correct 6 ms 38748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 38748 KB Output is correct
2 Correct 6 ms 38748 KB Output is correct
3 Correct 6 ms 38748 KB Output is correct
4 Correct 7 ms 38704 KB Output is correct
5 Correct 7 ms 38748 KB Output is correct
6 Correct 6 ms 38748 KB Output is correct
7 Correct 7 ms 38844 KB Output is correct
8 Correct 7 ms 38748 KB Output is correct
9 Correct 9 ms 38744 KB Output is correct
10 Correct 7 ms 38820 KB Output is correct
11 Correct 7 ms 38748 KB Output is correct
12 Correct 6 ms 38636 KB Output is correct
13 Correct 6 ms 38748 KB Output is correct
14 Correct 6 ms 38748 KB Output is correct
15 Correct 5 ms 38720 KB Output is correct
16 Correct 6 ms 38748 KB Output is correct
17 Correct 6 ms 38748 KB Output is correct
18 Correct 7 ms 38748 KB Output is correct
19 Correct 6 ms 38748 KB Output is correct
20 Correct 8 ms 38748 KB Output is correct
21 Correct 7 ms 38748 KB Output is correct
22 Correct 6 ms 38756 KB Output is correct
23 Correct 6 ms 38748 KB Output is correct
24 Correct 6 ms 38748 KB Output is correct
25 Correct 6 ms 38708 KB Output is correct
26 Correct 6 ms 38748 KB Output is correct
27 Correct 7 ms 38748 KB Output is correct
28 Correct 6 ms 38748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 38748 KB Output is correct
2 Correct 6 ms 38748 KB Output is correct
3 Correct 6 ms 38748 KB Output is correct
4 Correct 7 ms 38704 KB Output is correct
5 Correct 7 ms 38748 KB Output is correct
6 Correct 6 ms 38748 KB Output is correct
7 Correct 7 ms 38844 KB Output is correct
8 Correct 7 ms 38748 KB Output is correct
9 Correct 9 ms 38744 KB Output is correct
10 Correct 7 ms 38820 KB Output is correct
11 Correct 7 ms 38748 KB Output is correct
12 Correct 6 ms 38636 KB Output is correct
13 Correct 6 ms 38748 KB Output is correct
14 Correct 6 ms 38748 KB Output is correct
15 Correct 5 ms 38720 KB Output is correct
16 Correct 6 ms 38748 KB Output is correct
17 Correct 6 ms 38748 KB Output is correct
18 Correct 7 ms 38748 KB Output is correct
19 Correct 6 ms 38748 KB Output is correct
20 Correct 8 ms 38748 KB Output is correct
21 Correct 7 ms 38748 KB Output is correct
22 Correct 6 ms 38756 KB Output is correct
23 Correct 6 ms 38748 KB Output is correct
24 Correct 6 ms 38748 KB Output is correct
25 Correct 6 ms 38708 KB Output is correct
26 Correct 6 ms 38748 KB Output is correct
27 Correct 7 ms 38748 KB Output is correct
28 Correct 6 ms 38748 KB Output is correct
29 Correct 8 ms 38748 KB Output is correct
30 Correct 7 ms 38748 KB Output is correct
31 Correct 8 ms 38748 KB Output is correct
32 Correct 8 ms 38748 KB Output is correct
33 Correct 7 ms 38748 KB Output is correct
34 Correct 8 ms 38748 KB Output is correct
35 Correct 8 ms 38748 KB Output is correct
36 Correct 7 ms 38736 KB Output is correct
37 Correct 7 ms 38748 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 38748 KB Output is correct
2 Correct 6 ms 38748 KB Output is correct
3 Correct 6 ms 38748 KB Output is correct
4 Correct 7 ms 38704 KB Output is correct
5 Correct 7 ms 38748 KB Output is correct
6 Correct 6 ms 38748 KB Output is correct
7 Correct 7 ms 38844 KB Output is correct
8 Correct 7 ms 38748 KB Output is correct
9 Correct 9 ms 38744 KB Output is correct
10 Correct 7 ms 38820 KB Output is correct
11 Correct 7 ms 38748 KB Output is correct
12 Correct 6 ms 38636 KB Output is correct
13 Correct 6 ms 38748 KB Output is correct
14 Correct 6 ms 38748 KB Output is correct
15 Correct 5 ms 38720 KB Output is correct
16 Correct 6 ms 38748 KB Output is correct
17 Correct 6 ms 38748 KB Output is correct
18 Correct 7 ms 38748 KB Output is correct
19 Correct 6 ms 38748 KB Output is correct
20 Correct 8 ms 38748 KB Output is correct
21 Correct 7 ms 38748 KB Output is correct
22 Correct 6 ms 38756 KB Output is correct
23 Correct 6 ms 38748 KB Output is correct
24 Correct 6 ms 38748 KB Output is correct
25 Correct 6 ms 38708 KB Output is correct
26 Correct 6 ms 38748 KB Output is correct
27 Correct 7 ms 38748 KB Output is correct
28 Correct 6 ms 38748 KB Output is correct
29 Correct 8 ms 38748 KB Output is correct
30 Correct 7 ms 38748 KB Output is correct
31 Correct 8 ms 38748 KB Output is correct
32 Correct 8 ms 38748 KB Output is correct
33 Correct 7 ms 38748 KB Output is correct
34 Correct 8 ms 38748 KB Output is correct
35 Correct 8 ms 38748 KB Output is correct
36 Correct 7 ms 38736 KB Output is correct
37 Correct 7 ms 38748 KB Output is correct
38 Correct 43 ms 40284 KB Output is correct
39 Correct 49 ms 65616 KB Output is correct
40 Correct 45 ms 39972 KB Output is correct
41 Correct 67 ms 39760 KB Output is correct
42 Correct 38 ms 40276 KB Output is correct
43 Correct 28 ms 39516 KB Output is correct
44 Correct 22 ms 39136 KB Output is correct
45 Correct 62 ms 50768 KB Output is correct
46 Correct 55 ms 50852 KB Output is correct
47 Correct 50 ms 57680 KB Output is correct
48 Correct 46 ms 57680 KB Output is correct
49 Correct 56 ms 51292 KB Output is correct
50 Correct 51 ms 51284 KB Output is correct
51 Correct 43 ms 52820 KB Output is correct
52 Correct 45 ms 52820 KB Output is correct
53 Correct 20 ms 39772 KB Output is correct
54 Correct 60 ms 50440 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 38744 KB Output is correct
2 Correct 6 ms 38744 KB Output is correct
3 Correct 7 ms 38748 KB Output is correct
4 Correct 7 ms 38748 KB Output is correct
5 Correct 13 ms 38788 KB Output is correct
6 Correct 6 ms 38744 KB Output is correct
7 Correct 6 ms 38748 KB Output is correct
8 Correct 6 ms 38748 KB Output is correct
9 Correct 6 ms 38748 KB Output is correct
10 Correct 6 ms 38744 KB Output is correct
11 Correct 5 ms 38748 KB Output is correct
12 Correct 8 ms 38748 KB Output is correct
13 Correct 33 ms 38852 KB Output is correct
14 Correct 53 ms 39708 KB Output is correct
15 Correct 44 ms 39468 KB Output is correct
16 Correct 92 ms 56656 KB Output is correct
17 Correct 293 ms 83800 KB Output is correct
18 Correct 671 ms 114768 KB Output is correct
19 Correct 157 ms 62292 KB Output is correct
20 Correct 107 ms 62032 KB Output is correct
21 Correct 164 ms 62288 KB Output is correct
22 Correct 315 ms 84484 KB Output is correct
23 Correct 208 ms 86100 KB Output is correct
24 Correct 285 ms 84880 KB Output is correct
25 Correct 208 ms 85932 KB Output is correct
26 Correct 333 ms 86096 KB Output is correct
27 Correct 526 ms 95644 KB Output is correct
28 Correct 316 ms 102976 KB Output is correct
29 Correct 276 ms 87964 KB Output is correct
30 Correct 235 ms 75596 KB Output is correct
31 Correct 204 ms 78288 KB Output is correct
32 Correct 238 ms 74708 KB Output is correct
33 Correct 162 ms 78028 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 38744 KB Output is correct
2 Correct 6 ms 38744 KB Output is correct
3 Correct 6 ms 38748 KB Output is correct
4 Correct 14 ms 38748 KB Output is correct
5 Correct 24 ms 38836 KB Output is correct
6 Correct 9 ms 38748 KB Output is correct
7 Correct 7 ms 38748 KB Output is correct
8 Correct 8 ms 38900 KB Output is correct
9 Correct 37 ms 40284 KB Output is correct
10 Correct 49 ms 65624 KB Output is correct
11 Correct 12 ms 38748 KB Output is correct
12 Correct 46 ms 38856 KB Output is correct
13 Correct 157 ms 85844 KB Output is correct
14 Correct 117 ms 85808 KB Output is correct
15 Correct 328 ms 104836 KB Output is correct
16 Correct 1010 ms 160592 KB Output is correct
17 Correct 298 ms 117584 KB Output is correct
18 Correct 233 ms 95056 KB Output is correct
19 Correct 266 ms 111440 KB Output is correct
20 Correct 274 ms 111640 KB Output is correct
21 Correct 282 ms 116948 KB Output is correct
22 Correct 87 ms 73552 KB Output is correct
23 Correct 6 ms 38748 KB Output is correct
24 Correct 6 ms 38748 KB Output is correct
25 Correct 6 ms 38748 KB Output is correct
26 Correct 7 ms 38704 KB Output is correct
27 Correct 7 ms 38748 KB Output is correct
28 Correct 6 ms 38748 KB Output is correct
29 Correct 7 ms 38844 KB Output is correct
30 Correct 7 ms 38748 KB Output is correct
31 Correct 9 ms 38744 KB Output is correct
32 Correct 7 ms 38820 KB Output is correct
33 Correct 7 ms 38748 KB Output is correct
34 Correct 6 ms 38636 KB Output is correct
35 Correct 6 ms 38748 KB Output is correct
36 Correct 6 ms 38748 KB Output is correct
37 Correct 5 ms 38720 KB Output is correct
38 Correct 6 ms 38748 KB Output is correct
39 Correct 6 ms 38748 KB Output is correct
40 Correct 7 ms 38748 KB Output is correct
41 Correct 6 ms 38748 KB Output is correct
42 Correct 8 ms 38748 KB Output is correct
43 Correct 7 ms 38748 KB Output is correct
44 Correct 6 ms 38756 KB Output is correct
45 Correct 6 ms 38748 KB Output is correct
46 Correct 6 ms 38748 KB Output is correct
47 Correct 6 ms 38708 KB Output is correct
48 Correct 6 ms 38748 KB Output is correct
49 Correct 7 ms 38748 KB Output is correct
50 Correct 6 ms 38748 KB Output is correct
51 Correct 8 ms 38748 KB Output is correct
52 Correct 7 ms 38748 KB Output is correct
53 Correct 8 ms 38748 KB Output is correct
54 Correct 8 ms 38748 KB Output is correct
55 Correct 7 ms 38748 KB Output is correct
56 Correct 8 ms 38748 KB Output is correct
57 Correct 8 ms 38748 KB Output is correct
58 Correct 7 ms 38736 KB Output is correct
59 Correct 7 ms 38748 KB Output is correct
60 Correct 43 ms 40284 KB Output is correct
61 Correct 49 ms 65616 KB Output is correct
62 Correct 45 ms 39972 KB Output is correct
63 Correct 67 ms 39760 KB Output is correct
64 Correct 38 ms 40276 KB Output is correct
65 Correct 28 ms 39516 KB Output is correct
66 Correct 22 ms 39136 KB Output is correct
67 Correct 62 ms 50768 KB Output is correct
68 Correct 55 ms 50852 KB Output is correct
69 Correct 50 ms 57680 KB Output is correct
70 Correct 46 ms 57680 KB Output is correct
71 Correct 56 ms 51292 KB Output is correct
72 Correct 51 ms 51284 KB Output is correct
73 Correct 43 ms 52820 KB Output is correct
74 Correct 45 ms 52820 KB Output is correct
75 Correct 20 ms 39772 KB Output is correct
76 Correct 60 ms 50440 KB Output is correct
77 Correct 6 ms 38744 KB Output is correct
78 Correct 6 ms 38744 KB Output is correct
79 Correct 7 ms 38748 KB Output is correct
80 Correct 7 ms 38748 KB Output is correct
81 Correct 13 ms 38788 KB Output is correct
82 Correct 6 ms 38744 KB Output is correct
83 Correct 6 ms 38748 KB Output is correct
84 Correct 6 ms 38748 KB Output is correct
85 Correct 6 ms 38748 KB Output is correct
86 Correct 6 ms 38744 KB Output is correct
87 Correct 5 ms 38748 KB Output is correct
88 Correct 8 ms 38748 KB Output is correct
89 Correct 33 ms 38852 KB Output is correct
90 Correct 53 ms 39708 KB Output is correct
91 Correct 44 ms 39468 KB Output is correct
92 Correct 92 ms 56656 KB Output is correct
93 Correct 293 ms 83800 KB Output is correct
94 Correct 671 ms 114768 KB Output is correct
95 Correct 157 ms 62292 KB Output is correct
96 Correct 107 ms 62032 KB Output is correct
97 Correct 164 ms 62288 KB Output is correct
98 Correct 315 ms 84484 KB Output is correct
99 Correct 208 ms 86100 KB Output is correct
100 Correct 285 ms 84880 KB Output is correct
101 Correct 208 ms 85932 KB Output is correct
102 Correct 333 ms 86096 KB Output is correct
103 Correct 526 ms 95644 KB Output is correct
104 Correct 316 ms 102976 KB Output is correct
105 Correct 276 ms 87964 KB Output is correct
106 Correct 235 ms 75596 KB Output is correct
107 Correct 204 ms 78288 KB Output is correct
108 Correct 238 ms 74708 KB Output is correct
109 Correct 162 ms 78028 KB Output is correct
110 Correct 52 ms 39720 KB Output is correct
111 Correct 29 ms 39512 KB Output is correct
112 Correct 427 ms 107348 KB Output is correct
113 Correct 214 ms 76116 KB Output is correct
114 Correct 368 ms 107032 KB Output is correct
115 Correct 42 ms 51084 KB Output is correct
116 Correct 168 ms 63056 KB Output is correct
117 Correct 1117 ms 147024 KB Output is correct
118 Correct 62 ms 52304 KB Output is correct
119 Correct 64 ms 52304 KB Output is correct
120 Correct 20 ms 41308 KB Output is correct
121 Correct 241 ms 69000 KB Output is correct
122 Correct 244 ms 68948 KB Output is correct
123 Correct 273 ms 82040 KB Output is correct
124 Correct 179 ms 82040 KB Output is correct
125 Correct 267 ms 84564 KB Output is correct
126 Correct 933 ms 157520 KB Output is correct
127 Correct 276 ms 97364 KB Output is correct
128 Correct 431 ms 124240 KB Output is correct
129 Correct 254 ms 98104 KB Output is correct
130 Correct 240 ms 98140 KB Output is correct