Submission #546348

# Submission time Handle Problem Language Result Execution time Memory
546348 2022-04-07T10:42:29 Z Jarif_Rahman Jail (JOI22_jail) C++17
100 / 100
1794 ms 117776 KB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

const int K = 19;
 
int n, m;
vector<vector<int>> v;
vector<int> s, t;
vector<vector<int>> sth;
vector<int> state;
vector<int> anc[K];
vector<int> d;
bool cycle;
 
void dfs1(int nd, int ss, int dis){
    for(int x: v[nd]) if(x != ss) dfs1(x, nd, dis+1);
    if(ss != -1) anc[0][nd] = ss;
    d[nd] = dis;
}

int get_anc(int nd, int h){
    for(int i = 0; i < K; i++){
        if(h%2 == 1) nd = anc[i][nd];
        h/=2;
    }
    return nd;
}
 
int lca_jump(int a, int b){
    if(a == b) return a;
    for(int i = K-1; i >= 0; i--) if(anc[i][a] != anc[i][b])
        return lca_jump(anc[i][a], anc[i][b]);
    return anc[0][a];
}
 
int lca(int a, int b){
    if(d[b] < d[a]) swap(a, b);
    b = get_anc(b, d[b]-d[a]);
    return lca_jump(a, b);
}

int fix_path(int a, int b){
    if(lca(a, b) == a) return get_anc(b, d[b]-d[a]-1);
    return anc[0][a];
}

struct segtree{
    int k;
    bool dir;
    vector<int> v;
    segtree(){}
    segtree(int n, bool _dir){
        k = 1, dir = _dir;
        while(k < n) k*=2;
        k*=2;
        v.resize(k, -1);
    }
    void graph_init(){
        for(int i = k/2-1; i > 0; i--){
            v[i] = sth.size();
            sth.pb({});
            state.pb(0);
            if(dir) sth[v[i]].pb(v[2*i]), sth[v[i]].pb(v[2*i+1]);
            else sth[v[2*i]].pb(v[i]), sth[v[2*i+1]].pb(v[i]);
        }
    }
    void add_edge(int l, int r, int nd, int a, int b, int x){
        if(a > r || b < l) return;
        if(a >= l && b <= r){
            if(dir) sth[x].pb(v[nd]);
            else sth[v[nd]].pb(x);
            return;
        }
        int md = (a+b)/2;
        add_edge(l, r, 2*nd, a, md, x);
        add_edge(l, r, 2*nd+1, md+1, b, x);
    }
    void add_edge(int l, int r, int x){
        add_edge(l, r, 1, 0, k/2-1, x);
    }
};

struct HLD{
    int n;
    bool dir;
    static const int k = 21;
    vector<vector<int>> v;
    vector<int> sz;
    vector<int> depth;
    vector<int> p;
    segtree seg;
    vector<int> id;
    vector<int> top;
    int cnt = 0;

    HLD(int _n, bool _dir){
        dir = _dir;
        n = _n;
        v.assign(n, {});
    }

    void pre_dfs(int nd, int ss, int d){
        for(int x: v[nd]) if(x != ss) pre_dfs(x, nd, d+1);
        for(int x: v[nd]) if(x != ss) sz[nd]+=sz[x];
        p[nd] = ss;
        depth[nd] = d;
    }

    void dfs(int nd, int ss, int tp){
        id[nd] = cnt;
        top[nd] = tp;
        cnt++;
        int mx = 0, in = -1;
        for(int x: v[nd]) if(x != ss) if(sz[x] > mx) mx = sz[x], in = x;
        if(in != -1) dfs(in, nd, tp);
        for(int x: v[nd]) if(x != ss  && x != in) dfs(x, nd, x);
    }

    void add_edge(int a, int b, int x){
        while(top[a] != top[b]){
            if(depth[top[a]] < depth[top[b]]) swap(a, b);
            seg.add_edge(id[top[a]], id[a], x);
            a = p[top[a]];
        }
        if(depth[a] > depth[b]) swap(a, b);
        seg.add_edge(id[a], id[b], x);
    }

    void init(){
        sz.assign(n, 1);
        depth.assign(n, -1);
        p.assign(n, -1);

        pre_dfs(0, -1, 0);

        id.assign(n, -1);
        top.assign(n, -1);
        dfs(0, -1, 0);

        seg = segtree(n, dir);

        for(int i = seg.k/2; i < seg.k; i++) seg.v[i] = 2*n;
        for(int i = 0; i < n; i++){
            seg.v[id[i]+seg.k/2] = i+(dir?0:n);
        }
        seg.graph_init();
    }

};

void dfs2(int nd){
    if(state[nd] == 1){
        cycle = 1;
        return;
    }
    if(state[nd] == 2) return;
    state[nd] = 1;
    for(int x: sth[nd]) dfs2(x);
    state[nd] = 2;
}
 
void solve(){
    cin >> n;
    v.assign(n, {});
    fill(anc, anc+K, vector<int>(n, 0));

    d.assign(n, 0);
    sth.assign(2*n+1, {});
    state.assign(2*n+1, 0);

    HLD hld1(n, 1), hld2(n, 0);

    for(int i = 0; i < n-1; i++){
        int a, b; cin >> a >> b; a--, b--;
        v[a].pb(b);
        v[b].pb(a);

        hld1.v[a].pb(b);
        hld1.v[b].pb(a);

        hld2.v[a].pb(b);
        hld2.v[b].pb(a);
    }

    hld1.init();
    hld2.init();
 
    dfs1(0, -1, 0);
    for(int p = 1; p < K; p++) for(int i = 0; i < n; i++)
        anc[p][i] = anc[p-1][anc[p-1][i]];
 
    cin >> m;
    s.assign(m, 0);
    t.assign(m, 0);
    for(int i = 0; i < m; i++) cin >> s[i] >> t[i], s[i]--, t[i]--;
    for(int i = 0; i < m; i++){
        sth[s[i]].pb(t[i]+n);
    }

    for(int i = 0; i < m; i++){
        hld1.add_edge(fix_path(s[i], t[i]), t[i], t[i]+n);
        hld2.add_edge(s[i], fix_path(t[i], s[i]), s[i]);
    }
    
    cycle = 0;
 
    for(int i = 0; i < sth.size(); i++) if(state[i] == 0) dfs2(i);
 
    cout << (cycle?"No\n":"Yes\n");
}
 
int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int T; cin >> T; while(T--) solve();
}

Compilation message

jail.cpp: In function 'void solve()':
jail.cpp:212:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::vector<int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  212 |     for(int i = 0; i < sth.size(); i++) if(state[i] == 0) dfs2(i);
      |                    ~~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 40 ms 340 KB Output is correct
5 Correct 83 ms 404 KB Output is correct
6 Correct 3 ms 468 KB Output is correct
7 Correct 4 ms 468 KB Output is correct
8 Correct 5 ms 468 KB Output is correct
9 Correct 107 ms 5488 KB Output is correct
10 Correct 153 ms 103168 KB Output is correct
11 Correct 16 ms 340 KB Output is correct
12 Correct 108 ms 404 KB Output is correct
13 Correct 237 ms 103212 KB Output is correct
14 Correct 227 ms 103236 KB Output is correct
15 Correct 452 ms 104136 KB Output is correct
16 Correct 802 ms 117776 KB Output is correct
17 Correct 259 ms 107808 KB Output is correct
18 Correct 223 ms 103296 KB Output is correct
19 Correct 256 ms 106500 KB Output is correct
20 Correct 255 ms 106468 KB Output is correct
21 Correct 299 ms 107132 KB Output is correct
22 Correct 183 ms 103228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 4 ms 468 KB Output is correct
4 Correct 4 ms 468 KB Output is correct
5 Correct 4 ms 468 KB Output is correct
6 Correct 4 ms 452 KB Output is correct
7 Correct 4 ms 468 KB Output is correct
8 Correct 4 ms 468 KB Output is correct
9 Correct 4 ms 468 KB Output is correct
10 Correct 4 ms 468 KB Output is correct
11 Correct 4 ms 484 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 3 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 4 ms 468 KB Output is correct
4 Correct 4 ms 468 KB Output is correct
5 Correct 4 ms 468 KB Output is correct
6 Correct 4 ms 452 KB Output is correct
7 Correct 4 ms 468 KB Output is correct
8 Correct 4 ms 468 KB Output is correct
9 Correct 4 ms 468 KB Output is correct
10 Correct 4 ms 468 KB Output is correct
11 Correct 4 ms 484 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 3 ms 468 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 468 KB Output is correct
17 Correct 5 ms 468 KB Output is correct
18 Correct 4 ms 496 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 4 ms 468 KB Output is correct
21 Correct 4 ms 468 KB Output is correct
22 Correct 4 ms 468 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 4 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 5 ms 468 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 4 ms 468 KB Output is correct
4 Correct 4 ms 468 KB Output is correct
5 Correct 4 ms 468 KB Output is correct
6 Correct 4 ms 452 KB Output is correct
7 Correct 4 ms 468 KB Output is correct
8 Correct 4 ms 468 KB Output is correct
9 Correct 4 ms 468 KB Output is correct
10 Correct 4 ms 468 KB Output is correct
11 Correct 4 ms 484 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 3 ms 468 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 468 KB Output is correct
17 Correct 5 ms 468 KB Output is correct
18 Correct 4 ms 496 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 4 ms 468 KB Output is correct
21 Correct 4 ms 468 KB Output is correct
22 Correct 4 ms 468 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 4 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 5 ms 468 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 6 ms 468 KB Output is correct
30 Correct 6 ms 468 KB Output is correct
31 Correct 6 ms 500 KB Output is correct
32 Correct 5 ms 468 KB Output is correct
33 Correct 4 ms 468 KB Output is correct
34 Correct 6 ms 340 KB Output is correct
35 Correct 6 ms 468 KB Output is correct
36 Correct 4 ms 468 KB Output is correct
37 Correct 3 ms 468 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 4 ms 468 KB Output is correct
4 Correct 4 ms 468 KB Output is correct
5 Correct 4 ms 468 KB Output is correct
6 Correct 4 ms 452 KB Output is correct
7 Correct 4 ms 468 KB Output is correct
8 Correct 4 ms 468 KB Output is correct
9 Correct 4 ms 468 KB Output is correct
10 Correct 4 ms 468 KB Output is correct
11 Correct 4 ms 484 KB Output is correct
12 Correct 2 ms 468 KB Output is correct
13 Correct 3 ms 468 KB Output is correct
14 Correct 0 ms 212 KB Output is correct
15 Correct 0 ms 212 KB Output is correct
16 Correct 4 ms 468 KB Output is correct
17 Correct 5 ms 468 KB Output is correct
18 Correct 4 ms 496 KB Output is correct
19 Correct 1 ms 212 KB Output is correct
20 Correct 4 ms 468 KB Output is correct
21 Correct 4 ms 468 KB Output is correct
22 Correct 4 ms 468 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 4 ms 468 KB Output is correct
26 Correct 1 ms 468 KB Output is correct
27 Correct 5 ms 468 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 6 ms 468 KB Output is correct
30 Correct 6 ms 468 KB Output is correct
31 Correct 6 ms 500 KB Output is correct
32 Correct 5 ms 468 KB Output is correct
33 Correct 4 ms 468 KB Output is correct
34 Correct 6 ms 340 KB Output is correct
35 Correct 6 ms 468 KB Output is correct
36 Correct 4 ms 468 KB Output is correct
37 Correct 3 ms 468 KB Output is correct
38 Correct 107 ms 5520 KB Output is correct
39 Correct 153 ms 103192 KB Output is correct
40 Correct 136 ms 4732 KB Output is correct
41 Correct 134 ms 4108 KB Output is correct
42 Correct 101 ms 5084 KB Output is correct
43 Correct 100 ms 4408 KB Output is correct
44 Correct 32 ms 876 KB Output is correct
45 Correct 224 ms 71464 KB Output is correct
46 Correct 231 ms 71416 KB Output is correct
47 Correct 149 ms 87156 KB Output is correct
48 Correct 159 ms 87208 KB Output is correct
49 Correct 202 ms 72004 KB Output is correct
50 Correct 204 ms 72076 KB Output is correct
51 Correct 161 ms 76760 KB Output is correct
52 Correct 155 ms 76624 KB Output is correct
53 Correct 32 ms 4444 KB Output is correct
54 Correct 307 ms 71244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 18 ms 340 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 4 ms 468 KB Output is correct
13 Correct 73 ms 396 KB Output is correct
14 Correct 121 ms 512 KB Output is correct
15 Correct 98 ms 468 KB Output is correct
16 Correct 316 ms 71876 KB Output is correct
17 Correct 715 ms 74104 KB Output is correct
18 Correct 1127 ms 88492 KB Output is correct
19 Correct 414 ms 71932 KB Output is correct
20 Correct 406 ms 71812 KB Output is correct
21 Correct 421 ms 71864 KB Output is correct
22 Correct 622 ms 72456 KB Output is correct
23 Correct 481 ms 71892 KB Output is correct
24 Correct 517 ms 72248 KB Output is correct
25 Correct 507 ms 72296 KB Output is correct
26 Correct 509 ms 72480 KB Output is correct
27 Correct 524 ms 76076 KB Output is correct
28 Correct 491 ms 76556 KB Output is correct
29 Correct 501 ms 72828 KB Output is correct
30 Correct 443 ms 72028 KB Output is correct
31 Correct 437 ms 72076 KB Output is correct
32 Correct 469 ms 72000 KB Output is correct
33 Correct 446 ms 72004 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 40 ms 340 KB Output is correct
5 Correct 83 ms 404 KB Output is correct
6 Correct 3 ms 468 KB Output is correct
7 Correct 4 ms 468 KB Output is correct
8 Correct 5 ms 468 KB Output is correct
9 Correct 107 ms 5488 KB Output is correct
10 Correct 153 ms 103168 KB Output is correct
11 Correct 16 ms 340 KB Output is correct
12 Correct 108 ms 404 KB Output is correct
13 Correct 237 ms 103212 KB Output is correct
14 Correct 227 ms 103236 KB Output is correct
15 Correct 452 ms 104136 KB Output is correct
16 Correct 802 ms 117776 KB Output is correct
17 Correct 259 ms 107808 KB Output is correct
18 Correct 223 ms 103296 KB Output is correct
19 Correct 256 ms 106500 KB Output is correct
20 Correct 255 ms 106468 KB Output is correct
21 Correct 299 ms 107132 KB Output is correct
22 Correct 183 ms 103228 KB Output is correct
23 Correct 0 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 4 ms 468 KB Output is correct
26 Correct 4 ms 468 KB Output is correct
27 Correct 4 ms 468 KB Output is correct
28 Correct 4 ms 452 KB Output is correct
29 Correct 4 ms 468 KB Output is correct
30 Correct 4 ms 468 KB Output is correct
31 Correct 4 ms 468 KB Output is correct
32 Correct 4 ms 468 KB Output is correct
33 Correct 4 ms 484 KB Output is correct
34 Correct 2 ms 468 KB Output is correct
35 Correct 3 ms 468 KB Output is correct
36 Correct 0 ms 212 KB Output is correct
37 Correct 0 ms 212 KB Output is correct
38 Correct 4 ms 468 KB Output is correct
39 Correct 5 ms 468 KB Output is correct
40 Correct 4 ms 496 KB Output is correct
41 Correct 1 ms 212 KB Output is correct
42 Correct 4 ms 468 KB Output is correct
43 Correct 4 ms 468 KB Output is correct
44 Correct 4 ms 468 KB Output is correct
45 Correct 1 ms 212 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 4 ms 468 KB Output is correct
48 Correct 1 ms 468 KB Output is correct
49 Correct 5 ms 468 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 6 ms 468 KB Output is correct
52 Correct 6 ms 468 KB Output is correct
53 Correct 6 ms 500 KB Output is correct
54 Correct 5 ms 468 KB Output is correct
55 Correct 4 ms 468 KB Output is correct
56 Correct 6 ms 340 KB Output is correct
57 Correct 6 ms 468 KB Output is correct
58 Correct 4 ms 468 KB Output is correct
59 Correct 3 ms 468 KB Output is correct
60 Correct 107 ms 5520 KB Output is correct
61 Correct 153 ms 103192 KB Output is correct
62 Correct 136 ms 4732 KB Output is correct
63 Correct 134 ms 4108 KB Output is correct
64 Correct 101 ms 5084 KB Output is correct
65 Correct 100 ms 4408 KB Output is correct
66 Correct 32 ms 876 KB Output is correct
67 Correct 224 ms 71464 KB Output is correct
68 Correct 231 ms 71416 KB Output is correct
69 Correct 149 ms 87156 KB Output is correct
70 Correct 159 ms 87208 KB Output is correct
71 Correct 202 ms 72004 KB Output is correct
72 Correct 204 ms 72076 KB Output is correct
73 Correct 161 ms 76760 KB Output is correct
74 Correct 155 ms 76624 KB Output is correct
75 Correct 32 ms 4444 KB Output is correct
76 Correct 307 ms 71244 KB Output is correct
77 Correct 0 ms 212 KB Output is correct
78 Correct 0 ms 212 KB Output is correct
79 Correct 1 ms 212 KB Output is correct
80 Correct 0 ms 212 KB Output is correct
81 Correct 18 ms 340 KB Output is correct
82 Correct 2 ms 468 KB Output is correct
83 Correct 2 ms 468 KB Output is correct
84 Correct 1 ms 212 KB Output is correct
85 Correct 1 ms 212 KB Output is correct
86 Correct 1 ms 340 KB Output is correct
87 Correct 1 ms 212 KB Output is correct
88 Correct 4 ms 468 KB Output is correct
89 Correct 73 ms 396 KB Output is correct
90 Correct 121 ms 512 KB Output is correct
91 Correct 98 ms 468 KB Output is correct
92 Correct 316 ms 71876 KB Output is correct
93 Correct 715 ms 74104 KB Output is correct
94 Correct 1127 ms 88492 KB Output is correct
95 Correct 414 ms 71932 KB Output is correct
96 Correct 406 ms 71812 KB Output is correct
97 Correct 421 ms 71864 KB Output is correct
98 Correct 622 ms 72456 KB Output is correct
99 Correct 481 ms 71892 KB Output is correct
100 Correct 517 ms 72248 KB Output is correct
101 Correct 507 ms 72296 KB Output is correct
102 Correct 509 ms 72480 KB Output is correct
103 Correct 524 ms 76076 KB Output is correct
104 Correct 491 ms 76556 KB Output is correct
105 Correct 501 ms 72828 KB Output is correct
106 Correct 443 ms 72028 KB Output is correct
107 Correct 437 ms 72076 KB Output is correct
108 Correct 469 ms 72000 KB Output is correct
109 Correct 446 ms 72004 KB Output is correct
110 Correct 124 ms 460 KB Output is correct
111 Correct 81 ms 524 KB Output is correct
112 Correct 529 ms 90368 KB Output is correct
113 Correct 314 ms 87364 KB Output is correct
114 Correct 390 ms 87928 KB Output is correct
115 Correct 120 ms 72768 KB Output is correct
116 Correct 438 ms 72344 KB Output is correct
117 Correct 1794 ms 99008 KB Output is correct
118 Correct 326 ms 71236 KB Output is correct
119 Correct 338 ms 71260 KB Output is correct
120 Correct 25 ms 6748 KB Output is correct
121 Correct 612 ms 71832 KB Output is correct
122 Correct 609 ms 71872 KB Output is correct
123 Correct 313 ms 92724 KB Output is correct
124 Correct 308 ms 92544 KB Output is correct
125 Correct 350 ms 92632 KB Output is correct
126 Correct 850 ms 107692 KB Output is correct
127 Correct 457 ms 88776 KB Output is correct
128 Correct 329 ms 88280 KB Output is correct
129 Correct 323 ms 88444 KB Output is correct
130 Correct 356 ms 88648 KB Output is correct