답안 #561295

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
561295 2022-05-12T15:24:30 Z wiwiho Jail (JOI22_jail) C++14
100 / 100
1742 ms 338600 KB
#include <bits/stdc++.h>

#define iter(a) a.begin(), a.end()
#define lsort(a) sort(iter(a))
#define gsort(a) sort(iter(a), greater<>())
#define eb emplace_back
#define pob pop_back()
#define mp make_pair
#define F first
#define S second
#define printv(a, b) { \
    for(auto pv : a) b << pv << " "; \
    b << "\n"; \
}

using namespace std;

typedef long long ll;

using pii = pair<int, int>;
using pll = pair<ll, ll>;

template<typename A, typename B>
ostream& operator<<(ostream& o, pair<A, B> p){
    return o << '(' << p.F << ',' << p.S << ')';
}

struct Node{
    int l = -1, r = -1;
};

int n, m;
vector<vector<int>> g, tg;
vector<int> from, to;
vector<int> in, out, pr, dpt;
vector<vector<int>> anc, up, down;
const int SZ = 18;
int node, ts;

#define initarr(a, b) a.clear(); a.resize(b);
#define initarr2(a, b, c) a.clear(); a.resize(b, c);

void init(){
    cin >> n;
    initarr(g, n + 1);
    for(int i = 0; i < n - 1; i++){
        int u, v;
        cin >> u >> v;
        g[u].eb(v);
        g[v].eb(u);
    }
    cin >> m;
    initarr(from, m + 1);
    initarr(to, m + 1);
    for(int i = 1; i <= m; i++) cin >> from[i] >> to[i];
    initarr(tg, 1 + m + 2 * n * SZ);

    initarr(in, n + 1);
    initarr(out, n + 1);
    initarr(pr, n + 1);
    initarr(dpt, n + 1);
    ts = 0;

    node = m;
    initarr2(anc, SZ, vector<int>(n + 1));
    initarr2(up, SZ, vector<int>(n + 1));
    initarr2(down, SZ, vector<int>(n + 1));
}

void dfs(int now, int p){
    pr[now] = p;
    anc[0][now] = p;
    in[now] = ts++;
    dpt[now] = now == p ? 0 : dpt[p] + 1;
    for(int i : g[now]){
        if(i == p) continue;
        dfs(i, now);
    }
    out[now] = ts++;
}

bool isAnc(int a, int b){
    return in[a] <= in[b] && out[b] <= out[a];
}

int jump(int a, int dis){
    for(int i = 0; i < SZ; i++){
        if(1 << i & dis) a = anc[i][a];
    }
    return a;
}

int getlca(int a, int b){
    if(isAnc(a, b)) return a;
    for(int i = SZ - 1; i >= 0; i--){
        if(!isAnc(anc[i][a], b)) a = anc[i][a];
    }
    return anc[0][a];
}

// 0: in, 1: out
void modify(int a, int b, int ty, vector<vector<int>>& st, int v){
    //cerr << "start " << a << " " << b << " " << ty << "\n";
    for(int i = SZ - 1; i >= 0; i--){
        if(isAnc(anc[i][a], b) && anc[i][a] != b) continue;
        //cerr << "modify " << ty << " " << v << " " << mp(i, a) << " " << st[i][a] << "\n";
        if(ty == 0){
            tg[v].eb(st[i][a]);
        }
        else{
            tg[st[i][a]].eb(v);
        }
        a = anc[i][a];
    }
    //cerr << "modify " << ty << " " << v << " " << mp(0, a) << " " << st[0][a] << "\n";
    if(ty == 0){
        tg[v].eb(st[0][a]);
    }
    else{
        tg[st[0][a]].eb(v);
    }
}

void solve(){
    
    init();
    dfs(1, 1);

    for(int i = 0; i < SZ; i++){
        for(int j = 1; j <= n; j++){
            down[i][j] = ++node;
            up[i][j] = ++node;
        }
    }
    //cerr << "node " << node << "\n";
    //cerr << "tg.size " << tg.size() << "\n";
    for(int i = 1; i < SZ; i++){
        for(int j = 1; j <= n; j++){
            anc[i][j] = anc[i - 1][anc[i - 1][j]];
            tg[down[i][j]].eb(down[i - 1][j]);
            tg[down[i][j]].eb(down[i - 1][anc[i - 1][j]]);
            tg[up[i - 1][j]].eb(up[i][j]);
            tg[up[i - 1][anc[i - 1][j]]].eb(up[i][j]);
        }
    }

    for(int i = 1; i <= m; i++){
        int s = from[i], t = to[i];
        modify(s, s, 0, up, i);
        modify(t, t, 1, down, i);

        {
            int tt = t;
            if(isAnc(tt, s)) tt = jump(s, dpt[s] - dpt[tt] - 1);
            else tt = pr[tt];
            int lca = getlca(s, tt);
            modify(s, lca, 0, down, i);
            modify(tt, lca, 0, down, i);
        }
        {
            int ss = s;
            if(isAnc(ss, t)) ss = jump(t, dpt[t] - dpt[ss] - 1);
            else ss = pr[ss];
            int lca = getlca(ss, t);
            modify(ss, lca, 1, up, i);
            modify(t, lca, 1, up, i);
        }
    }

    vector<int> deg(tg.size());
    for(int i = 1; i < tg.size(); i++){
        for(int j : tg[i]){
            //if(i <= m || j <= m) cerr << "edge " << i << "->" << j << "\n";
            deg[j]++;
        }
    }

    queue<int> q;
    for(int i = 1; i < tg.size(); i++){
        if(deg[i] == 0) q.push(i);
    }
    while(!q.empty()){
        int now = q.front();
        //cerr << "ok " << now << "\n";
        q.pop();
        for(int i : tg[now]){
            deg[i]--;
            if(deg[i] == 0){
                q.push(i);
            }
        }
    }
    
    bool ans = true;
    for(int i = 1; i <= m; i++){
        if(deg[i] != 0) ans = false;
    }
    if(ans) cout << "Yes\n";
    else cout << "No\n";

}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int T;
    cin >> T;
    while(T--) solve();

    return 0;
}

Compilation message

jail.cpp: In function 'void solve()':
jail.cpp:171:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  171 |     for(int i = 1; i < tg.size(); i++){
      |                    ~~^~~~~~~~~~~
jail.cpp:179:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  179 |     for(int i = 1; i < tg.size(); i++){
      |                    ~~^~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 185 ms 824 KB Output is correct
5 Correct 390 ms 652 KB Output is correct
6 Correct 18 ms 980 KB Output is correct
7 Correct 19 ms 852 KB Output is correct
8 Correct 20 ms 980 KB Output is correct
9 Correct 509 ms 15984 KB Output is correct
10 Correct 637 ms 288244 KB Output is correct
11 Correct 79 ms 556 KB Output is correct
12 Correct 423 ms 1772 KB Output is correct
13 Correct 900 ms 294328 KB Output is correct
14 Correct 669 ms 294772 KB Output is correct
15 Correct 1045 ms 295924 KB Output is correct
16 Correct 1533 ms 307708 KB Output is correct
17 Correct 724 ms 299052 KB Output is correct
18 Correct 872 ms 301660 KB Output is correct
19 Correct 743 ms 298876 KB Output is correct
20 Correct 666 ms 298996 KB Output is correct
21 Correct 734 ms 297912 KB Output is correct
22 Correct 621 ms 295304 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 19 ms 980 KB Output is correct
4 Correct 18 ms 980 KB Output is correct
5 Correct 19 ms 980 KB Output is correct
6 Correct 22 ms 980 KB Output is correct
7 Correct 20 ms 980 KB Output is correct
8 Correct 19 ms 980 KB Output is correct
9 Correct 20 ms 980 KB Output is correct
10 Correct 20 ms 964 KB Output is correct
11 Correct 16 ms 980 KB Output is correct
12 Correct 9 ms 980 KB Output is correct
13 Correct 11 ms 980 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 19 ms 980 KB Output is correct
4 Correct 18 ms 980 KB Output is correct
5 Correct 19 ms 980 KB Output is correct
6 Correct 22 ms 980 KB Output is correct
7 Correct 20 ms 980 KB Output is correct
8 Correct 19 ms 980 KB Output is correct
9 Correct 20 ms 980 KB Output is correct
10 Correct 20 ms 964 KB Output is correct
11 Correct 16 ms 980 KB Output is correct
12 Correct 9 ms 980 KB Output is correct
13 Correct 11 ms 980 KB Output is correct
14 Correct 1 ms 320 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 18 ms 980 KB Output is correct
17 Correct 21 ms 1152 KB Output is correct
18 Correct 19 ms 968 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 21 ms 912 KB Output is correct
21 Correct 19 ms 980 KB Output is correct
22 Correct 18 ms 980 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 596 KB Output is correct
25 Correct 18 ms 1120 KB Output is correct
26 Correct 4 ms 852 KB Output is correct
27 Correct 20 ms 976 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 19 ms 980 KB Output is correct
4 Correct 18 ms 980 KB Output is correct
5 Correct 19 ms 980 KB Output is correct
6 Correct 22 ms 980 KB Output is correct
7 Correct 20 ms 980 KB Output is correct
8 Correct 19 ms 980 KB Output is correct
9 Correct 20 ms 980 KB Output is correct
10 Correct 20 ms 964 KB Output is correct
11 Correct 16 ms 980 KB Output is correct
12 Correct 9 ms 980 KB Output is correct
13 Correct 11 ms 980 KB Output is correct
14 Correct 1 ms 320 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 18 ms 980 KB Output is correct
17 Correct 21 ms 1152 KB Output is correct
18 Correct 19 ms 968 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 21 ms 912 KB Output is correct
21 Correct 19 ms 980 KB Output is correct
22 Correct 18 ms 980 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 596 KB Output is correct
25 Correct 18 ms 1120 KB Output is correct
26 Correct 4 ms 852 KB Output is correct
27 Correct 20 ms 976 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 20 ms 980 KB Output is correct
30 Correct 20 ms 1228 KB Output is correct
31 Correct 20 ms 1188 KB Output is correct
32 Correct 23 ms 852 KB Output is correct
33 Correct 21 ms 976 KB Output is correct
34 Correct 19 ms 852 KB Output is correct
35 Correct 20 ms 852 KB Output is correct
36 Correct 14 ms 964 KB Output is correct
37 Correct 10 ms 852 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 19 ms 980 KB Output is correct
4 Correct 18 ms 980 KB Output is correct
5 Correct 19 ms 980 KB Output is correct
6 Correct 22 ms 980 KB Output is correct
7 Correct 20 ms 980 KB Output is correct
8 Correct 19 ms 980 KB Output is correct
9 Correct 20 ms 980 KB Output is correct
10 Correct 20 ms 964 KB Output is correct
11 Correct 16 ms 980 KB Output is correct
12 Correct 9 ms 980 KB Output is correct
13 Correct 11 ms 980 KB Output is correct
14 Correct 1 ms 320 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 18 ms 980 KB Output is correct
17 Correct 21 ms 1152 KB Output is correct
18 Correct 19 ms 968 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 21 ms 912 KB Output is correct
21 Correct 19 ms 980 KB Output is correct
22 Correct 18 ms 980 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 596 KB Output is correct
25 Correct 18 ms 1120 KB Output is correct
26 Correct 4 ms 852 KB Output is correct
27 Correct 20 ms 976 KB Output is correct
28 Correct 2 ms 340 KB Output is correct
29 Correct 20 ms 980 KB Output is correct
30 Correct 20 ms 1228 KB Output is correct
31 Correct 20 ms 1188 KB Output is correct
32 Correct 23 ms 852 KB Output is correct
33 Correct 21 ms 976 KB Output is correct
34 Correct 19 ms 852 KB Output is correct
35 Correct 20 ms 852 KB Output is correct
36 Correct 14 ms 964 KB Output is correct
37 Correct 10 ms 852 KB Output is correct
38 Correct 522 ms 15528 KB Output is correct
39 Correct 639 ms 288732 KB Output is correct
40 Correct 537 ms 16368 KB Output is correct
41 Correct 585 ms 16852 KB Output is correct
42 Correct 479 ms 17120 KB Output is correct
43 Correct 531 ms 15872 KB Output is correct
44 Correct 98 ms 3268 KB Output is correct
45 Correct 657 ms 290292 KB Output is correct
46 Correct 709 ms 290252 KB Output is correct
47 Correct 758 ms 286840 KB Output is correct
48 Correct 736 ms 286748 KB Output is correct
49 Correct 678 ms 290920 KB Output is correct
50 Correct 708 ms 290712 KB Output is correct
51 Correct 773 ms 289052 KB Output is correct
52 Correct 771 ms 289976 KB Output is correct
53 Correct 136 ms 20204 KB Output is correct
54 Correct 894 ms 291024 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 77 ms 516 KB Output is correct
6 Correct 10 ms 980 KB Output is correct
7 Correct 10 ms 980 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 2 ms 312 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 2 ms 340 KB Output is correct
12 Correct 15 ms 980 KB Output is correct
13 Correct 241 ms 1248 KB Output is correct
14 Correct 454 ms 1644 KB Output is correct
15 Correct 352 ms 1360 KB Output is correct
16 Correct 868 ms 294980 KB Output is correct
17 Correct 839 ms 303656 KB Output is correct
18 Correct 1127 ms 338600 KB Output is correct
19 Correct 793 ms 294208 KB Output is correct
20 Correct 783 ms 298048 KB Output is correct
21 Correct 886 ms 298356 KB Output is correct
22 Correct 867 ms 321664 KB Output is correct
23 Correct 793 ms 321808 KB Output is correct
24 Correct 1355 ms 320160 KB Output is correct
25 Correct 1353 ms 320144 KB Output is correct
26 Correct 1304 ms 321864 KB Output is correct
27 Correct 836 ms 302880 KB Output is correct
28 Correct 698 ms 303360 KB Output is correct
29 Correct 683 ms 304632 KB Output is correct
30 Correct 1016 ms 297572 KB Output is correct
31 Correct 750 ms 296812 KB Output is correct
32 Correct 1014 ms 298480 KB Output is correct
33 Correct 684 ms 297564 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 185 ms 824 KB Output is correct
5 Correct 390 ms 652 KB Output is correct
6 Correct 18 ms 980 KB Output is correct
7 Correct 19 ms 852 KB Output is correct
8 Correct 20 ms 980 KB Output is correct
9 Correct 509 ms 15984 KB Output is correct
10 Correct 637 ms 288244 KB Output is correct
11 Correct 79 ms 556 KB Output is correct
12 Correct 423 ms 1772 KB Output is correct
13 Correct 900 ms 294328 KB Output is correct
14 Correct 669 ms 294772 KB Output is correct
15 Correct 1045 ms 295924 KB Output is correct
16 Correct 1533 ms 307708 KB Output is correct
17 Correct 724 ms 299052 KB Output is correct
18 Correct 872 ms 301660 KB Output is correct
19 Correct 743 ms 298876 KB Output is correct
20 Correct 666 ms 298996 KB Output is correct
21 Correct 734 ms 297912 KB Output is correct
22 Correct 621 ms 295304 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 212 KB Output is correct
25 Correct 19 ms 980 KB Output is correct
26 Correct 18 ms 980 KB Output is correct
27 Correct 19 ms 980 KB Output is correct
28 Correct 22 ms 980 KB Output is correct
29 Correct 20 ms 980 KB Output is correct
30 Correct 19 ms 980 KB Output is correct
31 Correct 20 ms 980 KB Output is correct
32 Correct 20 ms 964 KB Output is correct
33 Correct 16 ms 980 KB Output is correct
34 Correct 9 ms 980 KB Output is correct
35 Correct 11 ms 980 KB Output is correct
36 Correct 1 ms 320 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 18 ms 980 KB Output is correct
39 Correct 21 ms 1152 KB Output is correct
40 Correct 19 ms 968 KB Output is correct
41 Correct 2 ms 340 KB Output is correct
42 Correct 21 ms 912 KB Output is correct
43 Correct 19 ms 980 KB Output is correct
44 Correct 18 ms 980 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 2 ms 596 KB Output is correct
47 Correct 18 ms 1120 KB Output is correct
48 Correct 4 ms 852 KB Output is correct
49 Correct 20 ms 976 KB Output is correct
50 Correct 2 ms 340 KB Output is correct
51 Correct 20 ms 980 KB Output is correct
52 Correct 20 ms 1228 KB Output is correct
53 Correct 20 ms 1188 KB Output is correct
54 Correct 23 ms 852 KB Output is correct
55 Correct 21 ms 976 KB Output is correct
56 Correct 19 ms 852 KB Output is correct
57 Correct 20 ms 852 KB Output is correct
58 Correct 14 ms 964 KB Output is correct
59 Correct 10 ms 852 KB Output is correct
60 Correct 522 ms 15528 KB Output is correct
61 Correct 639 ms 288732 KB Output is correct
62 Correct 537 ms 16368 KB Output is correct
63 Correct 585 ms 16852 KB Output is correct
64 Correct 479 ms 17120 KB Output is correct
65 Correct 531 ms 15872 KB Output is correct
66 Correct 98 ms 3268 KB Output is correct
67 Correct 657 ms 290292 KB Output is correct
68 Correct 709 ms 290252 KB Output is correct
69 Correct 758 ms 286840 KB Output is correct
70 Correct 736 ms 286748 KB Output is correct
71 Correct 678 ms 290920 KB Output is correct
72 Correct 708 ms 290712 KB Output is correct
73 Correct 773 ms 289052 KB Output is correct
74 Correct 771 ms 289976 KB Output is correct
75 Correct 136 ms 20204 KB Output is correct
76 Correct 894 ms 291024 KB Output is correct
77 Correct 1 ms 340 KB Output is correct
78 Correct 1 ms 340 KB Output is correct
79 Correct 1 ms 340 KB Output is correct
80 Correct 1 ms 212 KB Output is correct
81 Correct 77 ms 516 KB Output is correct
82 Correct 10 ms 980 KB Output is correct
83 Correct 10 ms 980 KB Output is correct
84 Correct 2 ms 340 KB Output is correct
85 Correct 2 ms 312 KB Output is correct
86 Correct 2 ms 596 KB Output is correct
87 Correct 2 ms 340 KB Output is correct
88 Correct 15 ms 980 KB Output is correct
89 Correct 241 ms 1248 KB Output is correct
90 Correct 454 ms 1644 KB Output is correct
91 Correct 352 ms 1360 KB Output is correct
92 Correct 868 ms 294980 KB Output is correct
93 Correct 839 ms 303656 KB Output is correct
94 Correct 1127 ms 338600 KB Output is correct
95 Correct 793 ms 294208 KB Output is correct
96 Correct 783 ms 298048 KB Output is correct
97 Correct 886 ms 298356 KB Output is correct
98 Correct 867 ms 321664 KB Output is correct
99 Correct 793 ms 321808 KB Output is correct
100 Correct 1355 ms 320160 KB Output is correct
101 Correct 1353 ms 320144 KB Output is correct
102 Correct 1304 ms 321864 KB Output is correct
103 Correct 836 ms 302880 KB Output is correct
104 Correct 698 ms 303360 KB Output is correct
105 Correct 683 ms 304632 KB Output is correct
106 Correct 1016 ms 297572 KB Output is correct
107 Correct 750 ms 296812 KB Output is correct
108 Correct 1014 ms 298480 KB Output is correct
109 Correct 684 ms 297564 KB Output is correct
110 Correct 491 ms 2052 KB Output is correct
111 Correct 334 ms 1096 KB Output is correct
112 Correct 1128 ms 300032 KB Output is correct
113 Correct 1117 ms 291420 KB Output is correct
114 Correct 952 ms 315668 KB Output is correct
115 Correct 514 ms 293224 KB Output is correct
116 Correct 1136 ms 298620 KB Output is correct
117 Correct 1174 ms 309560 KB Output is correct
118 Correct 935 ms 291188 KB Output is correct
119 Correct 918 ms 290932 KB Output is correct
120 Correct 85 ms 25232 KB Output is correct
121 Correct 1345 ms 299196 KB Output is correct
122 Correct 1401 ms 299220 KB Output is correct
123 Correct 1292 ms 291344 KB Output is correct
124 Correct 829 ms 291172 KB Output is correct
125 Correct 1253 ms 291796 KB Output is correct
126 Correct 1742 ms 310392 KB Output is correct
127 Correct 831 ms 317432 KB Output is correct
128 Correct 878 ms 317692 KB Output is correct
129 Correct 1185 ms 317708 KB Output is correct
130 Correct 938 ms 317592 KB Output is correct