Submission #1116702

# Submission time Handle Problem Language Result Execution time Memory
1116702 2024-11-22T08:22:52 Z Zero_OP Jail (JOI22_jail) C++14
100 / 100
3107 ms 1006552 KB
#include <bits/stdc++.h>

using namespace std;

#define dbg(x) "[" #x " = " << (x) << "]"

bool cycle_detection(vector<vector<int>>& adj){
    int N = (int)adj.size();
    vector<int> vis(N);

    function<bool(int)> dfs = [&](int u){
        vis[u] = 1;
        for(auto v : adj[u]){
            if(!vis[v]){
                if(dfs(v)) return true;
            } else if(vis[v] == 1){
                return true;
            }
        }
        vis[u] = 2;
        return false;
    };

    for(int i = 0; i < N; ++i){
        if(!vis[i] && dfs(i)) return true;
    }
    return false;
}

struct path_decomposition_tree{
    int N, timerHLD, num;
    vector<int> par, depth, sz, tin, tout, head;
    vector<vector<int>> adj;
    vector<pair<int, int>> edges;

    path_decomposition_tree(int N) :
         timerHLD(0), num(0), N(N), depth(N), sz(N), tin(N, -1), tout(N, -1), par(N), head(N), adj(N), edges() {}

    void add_edge(int u, int v){
        adj[u].emplace_back(v);
        adj[v].emplace_back(u);
    }

    void dfs_sz(int u){
        sz[u] = 1;
        for(auto& v : adj[u]){
            adj[v].erase(find(adj[v].begin(), adj[v].end(), u));
            depth[v] = depth[u] + 1;
            par[v] = u;
            dfs_sz(v);
            if(sz[adj[u][0]] < sz[v]) swap(adj[u][0], v);
            sz[u] += sz[v];
        }
    }

    void dfs_hld(int u, int hd){
        head[u] = hd;
        tin[u] = timerHLD++;
        for(auto v : adj[u]){
            if(v == adj[u][0]) dfs_hld(v, hd);
            else dfs_hld(v, v);
        }
        tout[u] = timerHLD - 1;
    }

    void build(int N){
        num = 2 * N;
        for(int i = N - 1; i > 0; --i){
            edges.emplace_back(i, i << 1);
            edges.emplace_back(i, i << 1 | 1);
        }
    }

    void preprocess(int rt){
        dfs_sz(rt);
        dfs_hld(rt, rt);
        assert(timerHLD == N);
        build(N);
    }

    int auxiliary_nodes(){ return num; }
    bool in_subtree(int u, int v){ return tin[u] <= tin[v] && tout[v] <= tout[u]; }

    void query_auxiliary_nodes(int l, int r, vector<int>& ans){
        l += N;
        r += N + 1;
        for(; l < r; l >>= 1, r >>= 1){
            if(l & 1) ans.emplace_back(l++);
            if(r & 1) ans.emplace_back(--r);
        }
    }

    vector<int> query(int u, int v){ //ignore u, v
        if(u == v) assert(false);
        vector<int> cur;

        if(tin[u] > tin[v]) swap(u, v);

        if(in_subtree(u, v)){
            v = par[v];
            while(head[u] != head[v]){
                if(depth[head[u]] < depth[head[v]]) swap(u, v);
                query_auxiliary_nodes(tin[head[u]], tin[u], cur);
                u = par[head[u]];
            }

            if(tin[u] > tin[v]) swap(u, v);
            if(tin[u] < tin[v]) query_auxiliary_nodes(tin[u] + 1, tin[v], cur);
            return cur;
        } else{
            u = par[u];
            v = par[v];
        }

        while(head[u] != head[v]){
            if(depth[head[u]] < depth[head[v]]) swap(u, v);
            query_auxiliary_nodes(tin[head[u]], tin[u], cur);
            u = par[head[u]];
        }

        if(tin[u] > tin[v]) swap(u, v);
        query_auxiliary_nodes(tin[u], tin[v], cur);
        return cur;
    }

    int get(int u){
        return tin[u] + N;
    }
};

void testcase(){
    int N;
    cin >> N;

    path_decomposition_tree tr(N);
    for(int i = 1; i < N; ++i){
        int u, v;
        cin >> u >> v;
        --u, --v;
        tr.add_edge(u, v);
    }

    int M;
    cin >> M;

    tr.preprocess(0);
    int offset = tr.auxiliary_nodes();
    int nodes = 2 * offset + M;
    vector<vector<int>> G(nodes);

    for(auto [u, v] : tr.edges){
        G[u].emplace_back(v); //for T
        G[v + offset].emplace_back(u + offset); //for S
    }

    for(int i = 0; i < M; ++i){
        int s, t;
        cin >> s >> t;

        --s, --t;
        int loc = 2 * offset + i;
        vector<int> compress_nodes = tr.query(s, t); //auxiliary nodes without s, t
        for(auto it : compress_nodes){
            G[loc].emplace_back(it);
            G[it + offset].emplace_back(loc);
        }

        int pS = tr.get(s);
        G[loc].emplace_back(pS + offset);
        G[loc].emplace_back(pS);

        int pT = tr.get(t);
        G[pT].emplace_back(loc);
        G[pT + offset].emplace_back(loc);
    }

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

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

#ifdef LOCAL
    freopen("task.inp", "r", stdin);
    freopen("task.out", "w", stdout);
#endif // LOCAL

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

    return 0;
}

Compilation message

jail.cpp: In constructor 'path_decomposition_tree::path_decomposition_tree(int)':
jail.cpp:31:22: warning: 'path_decomposition_tree::num' will be initialized after [-Wreorder]
   31 |     int N, timerHLD, num;
      |                      ^~~
jail.cpp:31:9: warning:   'int path_decomposition_tree::N' [-Wreorder]
   31 |     int N, timerHLD, num;
      |         ^
jail.cpp:36:5: warning:   when initialized here [-Wreorder]
   36 |     path_decomposition_tree(int N) :
      |     ^~~~~~~~~~~~~~~~~~~~~~~
jail.cpp:32:38: warning: 'path_decomposition_tree::tout' will be initialized after [-Wreorder]
   32 |     vector<int> par, depth, sz, tin, tout, head;
      |                                      ^~~~
jail.cpp:32:17: warning:   'std::vector<int> path_decomposition_tree::par' [-Wreorder]
   32 |     vector<int> par, depth, sz, tin, tout, head;
      |                 ^~~
jail.cpp:36:5: warning:   when initialized here [-Wreorder]
   36 |     path_decomposition_tree(int N) :
      |     ^~~~~~~~~~~~~~~~~~~~~~~
jail.cpp: In function 'void testcase()':
jail.cpp:151:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
  151 |     for(auto [u, v] : tr.edges){
      |              ^
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 20 ms 884 KB Output is correct
5 Correct 38 ms 336 KB Output is correct
6 Correct 3 ms 764 KB Output is correct
7 Correct 3 ms 592 KB Output is correct
8 Correct 3 ms 592 KB Output is correct
9 Correct 51 ms 5644 KB Output is correct
10 Correct 119 ms 81080 KB Output is correct
11 Correct 10 ms 592 KB Output is correct
12 Correct 67 ms 1352 KB Output is correct
13 Correct 140 ms 86968 KB Output is correct
14 Correct 125 ms 85944 KB Output is correct
15 Correct 201 ms 92332 KB Output is correct
16 Correct 319 ms 109496 KB Output is correct
17 Correct 160 ms 95348 KB Output is correct
18 Correct 142 ms 92856 KB Output is correct
19 Correct 157 ms 94000 KB Output is correct
20 Correct 164 ms 94728 KB Output is correct
21 Correct 167 ms 96908 KB Output is correct
22 Correct 111 ms 86856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 504 KB Output is correct
3 Correct 3 ms 592 KB Output is correct
4 Correct 2 ms 336 KB Output is correct
5 Correct 3 ms 336 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 3 ms 592 KB Output is correct
8 Correct 3 ms 336 KB Output is correct
9 Correct 3 ms 504 KB Output is correct
10 Correct 3 ms 472 KB Output is correct
11 Correct 3 ms 336 KB Output is correct
12 Correct 2 ms 336 KB Output is correct
13 Correct 2 ms 472 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 504 KB Output is correct
3 Correct 3 ms 592 KB Output is correct
4 Correct 2 ms 336 KB Output is correct
5 Correct 3 ms 336 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 3 ms 592 KB Output is correct
8 Correct 3 ms 336 KB Output is correct
9 Correct 3 ms 504 KB Output is correct
10 Correct 3 ms 472 KB Output is correct
11 Correct 3 ms 336 KB Output is correct
12 Correct 2 ms 336 KB Output is correct
13 Correct 2 ms 472 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 3 ms 592 KB Output is correct
17 Correct 3 ms 476 KB Output is correct
18 Correct 3 ms 592 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 3 ms 592 KB Output is correct
21 Correct 3 ms 592 KB Output is correct
22 Correct 2 ms 616 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 3 ms 592 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 3 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 504 KB Output is correct
3 Correct 3 ms 592 KB Output is correct
4 Correct 2 ms 336 KB Output is correct
5 Correct 3 ms 336 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 3 ms 592 KB Output is correct
8 Correct 3 ms 336 KB Output is correct
9 Correct 3 ms 504 KB Output is correct
10 Correct 3 ms 472 KB Output is correct
11 Correct 3 ms 336 KB Output is correct
12 Correct 2 ms 336 KB Output is correct
13 Correct 2 ms 472 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 3 ms 592 KB Output is correct
17 Correct 3 ms 476 KB Output is correct
18 Correct 3 ms 592 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 3 ms 592 KB Output is correct
21 Correct 3 ms 592 KB Output is correct
22 Correct 2 ms 616 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 3 ms 592 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 3 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 4 ms 592 KB Output is correct
30 Correct 4 ms 592 KB Output is correct
31 Correct 4 ms 592 KB Output is correct
32 Correct 4 ms 760 KB Output is correct
33 Correct 3 ms 336 KB Output is correct
34 Correct 3 ms 336 KB Output is correct
35 Correct 4 ms 592 KB Output is correct
36 Correct 3 ms 336 KB Output is correct
37 Correct 2 ms 336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 504 KB Output is correct
3 Correct 3 ms 592 KB Output is correct
4 Correct 2 ms 336 KB Output is correct
5 Correct 3 ms 336 KB Output is correct
6 Correct 3 ms 504 KB Output is correct
7 Correct 3 ms 592 KB Output is correct
8 Correct 3 ms 336 KB Output is correct
9 Correct 3 ms 504 KB Output is correct
10 Correct 3 ms 472 KB Output is correct
11 Correct 3 ms 336 KB Output is correct
12 Correct 2 ms 336 KB Output is correct
13 Correct 2 ms 472 KB Output is correct
14 Correct 1 ms 336 KB Output is correct
15 Correct 1 ms 336 KB Output is correct
16 Correct 3 ms 592 KB Output is correct
17 Correct 3 ms 476 KB Output is correct
18 Correct 3 ms 592 KB Output is correct
19 Correct 1 ms 336 KB Output is correct
20 Correct 3 ms 592 KB Output is correct
21 Correct 3 ms 592 KB Output is correct
22 Correct 2 ms 616 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 336 KB Output is correct
25 Correct 3 ms 592 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 3 ms 336 KB Output is correct
28 Correct 1 ms 336 KB Output is correct
29 Correct 4 ms 592 KB Output is correct
30 Correct 4 ms 592 KB Output is correct
31 Correct 4 ms 592 KB Output is correct
32 Correct 4 ms 760 KB Output is correct
33 Correct 3 ms 336 KB Output is correct
34 Correct 3 ms 336 KB Output is correct
35 Correct 4 ms 592 KB Output is correct
36 Correct 3 ms 336 KB Output is correct
37 Correct 2 ms 336 KB Output is correct
38 Correct 55 ms 5672 KB Output is correct
39 Correct 108 ms 81080 KB Output is correct
40 Correct 62 ms 5204 KB Output is correct
41 Correct 57 ms 3360 KB Output is correct
42 Correct 47 ms 4376 KB Output is correct
43 Correct 46 ms 4244 KB Output is correct
44 Correct 15 ms 1020 KB Output is correct
45 Correct 68 ms 38080 KB Output is correct
46 Correct 84 ms 38072 KB Output is correct
47 Correct 103 ms 67436 KB Output is correct
48 Correct 128 ms 67500 KB Output is correct
49 Correct 73 ms 40484 KB Output is correct
50 Correct 70 ms 40592 KB Output is correct
51 Correct 68 ms 44216 KB Output is correct
52 Correct 65 ms 44224 KB Output is correct
53 Correct 17 ms 3276 KB Output is correct
54 Correct 77 ms 37816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 10 ms 764 KB Output is correct
6 Correct 1 ms 336 KB Output is correct
7 Correct 2 ms 336 KB Output is correct
8 Correct 1 ms 460 KB Output is correct
9 Correct 1 ms 336 KB Output is correct
10 Correct 1 ms 336 KB Output is correct
11 Correct 1 ms 336 KB Output is correct
12 Correct 3 ms 336 KB Output is correct
13 Correct 36 ms 848 KB Output is correct
14 Correct 62 ms 1356 KB Output is correct
15 Correct 46 ms 1104 KB Output is correct
16 Correct 81 ms 39612 KB Output is correct
17 Correct 159 ms 49860 KB Output is correct
18 Correct 234 ms 62240 KB Output is correct
19 Correct 116 ms 41920 KB Output is correct
20 Correct 94 ms 41752 KB Output is correct
21 Correct 138 ms 41660 KB Output is correct
22 Correct 151 ms 49852 KB Output is correct
23 Correct 123 ms 49732 KB Output is correct
24 Correct 147 ms 49784 KB Output is correct
25 Correct 131 ms 49852 KB Output is correct
26 Correct 167 ms 50120 KB Output is correct
27 Correct 224 ms 60984 KB Output is correct
28 Correct 205 ms 65848 KB Output is correct
29 Correct 234 ms 55228 KB Output is correct
30 Correct 172 ms 48184 KB Output is correct
31 Correct 128 ms 51768 KB Output is correct
32 Correct 151 ms 48952 KB Output is correct
33 Correct 149 ms 51648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 336 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 20 ms 884 KB Output is correct
5 Correct 38 ms 336 KB Output is correct
6 Correct 3 ms 764 KB Output is correct
7 Correct 3 ms 592 KB Output is correct
8 Correct 3 ms 592 KB Output is correct
9 Correct 51 ms 5644 KB Output is correct
10 Correct 119 ms 81080 KB Output is correct
11 Correct 10 ms 592 KB Output is correct
12 Correct 67 ms 1352 KB Output is correct
13 Correct 140 ms 86968 KB Output is correct
14 Correct 125 ms 85944 KB Output is correct
15 Correct 201 ms 92332 KB Output is correct
16 Correct 319 ms 109496 KB Output is correct
17 Correct 160 ms 95348 KB Output is correct
18 Correct 142 ms 92856 KB Output is correct
19 Correct 157 ms 94000 KB Output is correct
20 Correct 164 ms 94728 KB Output is correct
21 Correct 167 ms 96908 KB Output is correct
22 Correct 111 ms 86856 KB Output is correct
23 Correct 1 ms 336 KB Output is correct
24 Correct 1 ms 504 KB Output is correct
25 Correct 3 ms 592 KB Output is correct
26 Correct 2 ms 336 KB Output is correct
27 Correct 3 ms 336 KB Output is correct
28 Correct 3 ms 504 KB Output is correct
29 Correct 3 ms 592 KB Output is correct
30 Correct 3 ms 336 KB Output is correct
31 Correct 3 ms 504 KB Output is correct
32 Correct 3 ms 472 KB Output is correct
33 Correct 3 ms 336 KB Output is correct
34 Correct 2 ms 336 KB Output is correct
35 Correct 2 ms 472 KB Output is correct
36 Correct 1 ms 336 KB Output is correct
37 Correct 1 ms 336 KB Output is correct
38 Correct 3 ms 592 KB Output is correct
39 Correct 3 ms 476 KB Output is correct
40 Correct 3 ms 592 KB Output is correct
41 Correct 1 ms 336 KB Output is correct
42 Correct 3 ms 592 KB Output is correct
43 Correct 3 ms 592 KB Output is correct
44 Correct 2 ms 616 KB Output is correct
45 Correct 1 ms 336 KB Output is correct
46 Correct 1 ms 336 KB Output is correct
47 Correct 3 ms 592 KB Output is correct
48 Correct 1 ms 336 KB Output is correct
49 Correct 3 ms 336 KB Output is correct
50 Correct 1 ms 336 KB Output is correct
51 Correct 4 ms 592 KB Output is correct
52 Correct 4 ms 592 KB Output is correct
53 Correct 4 ms 592 KB Output is correct
54 Correct 4 ms 760 KB Output is correct
55 Correct 3 ms 336 KB Output is correct
56 Correct 3 ms 336 KB Output is correct
57 Correct 4 ms 592 KB Output is correct
58 Correct 3 ms 336 KB Output is correct
59 Correct 2 ms 336 KB Output is correct
60 Correct 55 ms 5672 KB Output is correct
61 Correct 108 ms 81080 KB Output is correct
62 Correct 62 ms 5204 KB Output is correct
63 Correct 57 ms 3360 KB Output is correct
64 Correct 47 ms 4376 KB Output is correct
65 Correct 46 ms 4244 KB Output is correct
66 Correct 15 ms 1020 KB Output is correct
67 Correct 68 ms 38080 KB Output is correct
68 Correct 84 ms 38072 KB Output is correct
69 Correct 103 ms 67436 KB Output is correct
70 Correct 128 ms 67500 KB Output is correct
71 Correct 73 ms 40484 KB Output is correct
72 Correct 70 ms 40592 KB Output is correct
73 Correct 68 ms 44216 KB Output is correct
74 Correct 65 ms 44224 KB Output is correct
75 Correct 17 ms 3276 KB Output is correct
76 Correct 77 ms 37816 KB Output is correct
77 Correct 1 ms 336 KB Output is correct
78 Correct 1 ms 336 KB Output is correct
79 Correct 1 ms 336 KB Output is correct
80 Correct 1 ms 336 KB Output is correct
81 Correct 10 ms 764 KB Output is correct
82 Correct 1 ms 336 KB Output is correct
83 Correct 2 ms 336 KB Output is correct
84 Correct 1 ms 460 KB Output is correct
85 Correct 1 ms 336 KB Output is correct
86 Correct 1 ms 336 KB Output is correct
87 Correct 1 ms 336 KB Output is correct
88 Correct 3 ms 336 KB Output is correct
89 Correct 36 ms 848 KB Output is correct
90 Correct 62 ms 1356 KB Output is correct
91 Correct 46 ms 1104 KB Output is correct
92 Correct 81 ms 39612 KB Output is correct
93 Correct 159 ms 49860 KB Output is correct
94 Correct 234 ms 62240 KB Output is correct
95 Correct 116 ms 41920 KB Output is correct
96 Correct 94 ms 41752 KB Output is correct
97 Correct 138 ms 41660 KB Output is correct
98 Correct 151 ms 49852 KB Output is correct
99 Correct 123 ms 49732 KB Output is correct
100 Correct 147 ms 49784 KB Output is correct
101 Correct 131 ms 49852 KB Output is correct
102 Correct 167 ms 50120 KB Output is correct
103 Correct 224 ms 60984 KB Output is correct
104 Correct 205 ms 65848 KB Output is correct
105 Correct 234 ms 55228 KB Output is correct
106 Correct 172 ms 48184 KB Output is correct
107 Correct 128 ms 51768 KB Output is correct
108 Correct 151 ms 48952 KB Output is correct
109 Correct 149 ms 51648 KB Output is correct
110 Correct 55 ms 1488 KB Output is correct
111 Correct 37 ms 1104 KB Output is correct
112 Correct 3107 ms 1006552 KB Output is correct
113 Correct 1163 ms 397608 KB Output is correct
114 Correct 201 ms 70536 KB Output is correct
115 Correct 63 ms 38204 KB Output is correct
116 Correct 140 ms 44416 KB Output is correct
117 Correct 333 ms 72376 KB Output is correct
118 Correct 89 ms 37816 KB Output is correct
119 Correct 74 ms 37972 KB Output is correct
120 Correct 16 ms 4072 KB Output is correct
121 Correct 201 ms 45764 KB Output is correct
122 Correct 165 ms 46012 KB Output is correct
123 Correct 153 ms 72120 KB Output is correct
124 Correct 122 ms 72116 KB Output is correct
125 Correct 145 ms 72888 KB Output is correct
126 Correct 295 ms 95672 KB Output is correct
127 Correct 177 ms 70328 KB Output is correct
128 Correct 137 ms 71876 KB Output is correct
129 Correct 162 ms 70168 KB Output is correct
130 Correct 147 ms 71428 KB Output is correct