Submission #551443

# Submission time Handle Problem Language Result Execution time Memory
551443 2022-04-20T17:33:41 Z doowey Jail (JOI22_jail) C++14
100 / 100
2231 ms 377044 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;

#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);

const int N = 123456;
const int LOG = 18;
vector<int> T[N];

int par[N];
int go[LOG][N];
int S[N], E[N];
int ss[N], ee[N];
int tin[N];
int tout[N];
int dep[N];
int ti;

void dfs(int u, int pp){
    ti ++ ;
    tin[u] = ti;
    par[u] = pp;
    go[0][u] = pp;
    for(int i = 1; i < LOG; i ++ ){
        go[i][u]=go[i-1][go[i-1][u]];
    }
    for(auto x : T[u]){
        if(x == pp) continue;
        dep[x] = dep[u] + 1;
        dfs(x, u);

    }
    tout[u] = ti;
}

bool is_par(int u, int v){
    if(tin[u] <= tin[v] && tout[u] >= tout[v]) return true;
    return false;
}

int lca(int u, int v){
    if(is_par(u, v)) return u;
    for(int lg = LOG - 1; lg >= 0 ; lg -- ){
        if(!is_par(go[lg][u], v)) u = go[lg][u];
    }
    return go[0][u];
}

const int M = 40 * N;
vector<int> G[M];
int vis[M];

int n, m;

int get_id(int node, int v){
    return m + n * v + node;
}

void add_edge(int u, int v){
    G[u].push_back(v);
}

bool cycle;

void dfs1(int u){
    if(vis[u] == 1){
        cycle = true;
        return;
    }
    if(vis[u] == 2){
        return;
    }
    //convert(u);
    //cout << u << "!!\n";
    vis[u] = 1;
    for(auto x : G[u]){
        dfs1(x);
    }
    vis[u] = 2;
}

void solve(){
    cin >> n;
    for(int i = 1; i <= n; i ++ ){
        T[i].clear();
        G[i].clear();
        ss[i] = ee[i] = -1;
    }
    for(int j = 1; j <= 40 * n; j ++){
        G[j].clear();
        vis[j] = 0;
    }
    int u, v;
    for(int i = 1; i < n; i ++ ){
        cin >> u >> v;
        T[u].push_back(v);
        T[v].push_back(u);
    }
    ti = 0;
    dep[1] = 0;
    dfs(1, 1);
    cin >> m;
    for(int i = 1; i <= n; i ++ ){
        for(int j = 1; j < LOG; j ++ ){
            add_edge(get_id(i, j - 1), get_id(i, j));
            add_edge(get_id(go[j - 1][i], j - 1), get_id(i, j));
            // ------------
            add_edge(get_id(i, LOG + j), get_id(i, LOG + j - 1));
            add_edge(get_id(i, LOG + j), get_id(go[j - 1][i], LOG + j - 1));
        }
    }
    int lc;
    int pa;
    int nex;
    int dd;
    for(int i = 1; i <= m ; i ++ ){
        cin >> S[i] >> E[i];

        ss[S[i]] = i;
        ee[E[i]] = i;

        lc = lca(S[i], E[i]);
        add_edge(i, get_id(S[i], 0));
        if(S[i] != 1){
            pa = go[0][S[i]];
            dd = dep[pa] - dep[lc] + 1;
            for(int lg = LOG - 1; lg >= 0 ; lg -- ){
                if((dd & (1 << lg))){
                    add_edge(get_id(pa, lg), i);
                    pa = go[lg][pa];
                }
            }
        }
        pa = E[i];
        dd = dep[pa] - dep[lc] + 1;
        if(S[i] == lc) dd -- ;

        for(int lg = LOG - 1; lg >= 0 ; lg -- ){
            if((dd & (1 << lg))){
                add_edge(get_id(pa, lg), i);
                pa = go[lg][pa];
            }
        }

        // --------------
        add_edge(get_id(E[i], LOG), i);

        pa = S[i];
        dd = dep[pa] - dep[lc] + 1;
        if(E[i] == lc) dd -- ;
        for(int lg = LOG - 1; lg >= 0 ; lg -- ){
            if((dd & (1 << lg))){
                add_edge(i, get_id(pa, LOG + lg));
                pa = go[lg][pa];
            }
        }

        if(E[i] != 1){
            pa = go[0][E[i]];
            dd = dep[pa] - dep[lc] + 1;
            for(int lg = LOG - 1; lg >= 0 ; lg -- ){
                if((dd & (1 << lg))){
                    add_edge(i, get_id(pa, LOG + lg));
                    pa = go[lg][pa];
                }
            }
        }



    }
    //return;
    cycle = false;
    for(int i = 1; i <= 40 * n; i ++ ){
        dfs1(i);
    }
    if(cycle){
        cout << "No\n";
    }
    else{
        cout << "Yes\n";
    }

}

int main(){
    fastIO;
    //freopen("in.txt","r",stdin);
    int tc;
    cin >> tc;
    for(int iq = 1; iq <= tc; iq ++ ){
        solve();
    }
    return 0;
}

Compilation message

jail.cpp: In function 'void solve()':
jail.cpp:121:9: warning: unused variable 'nex' [-Wunused-variable]
  121 |     int nex;
      |         ^~~
# Verdict Execution time Memory Grader output
1 Correct 57 ms 119188 KB Output is correct
2 Correct 55 ms 119316 KB Output is correct
3 Correct 54 ms 119304 KB Output is correct
4 Correct 112 ms 119620 KB Output is correct
5 Correct 194 ms 119520 KB Output is correct
6 Correct 64 ms 119764 KB Output is correct
7 Correct 62 ms 119672 KB Output is correct
8 Correct 63 ms 119696 KB Output is correct
9 Correct 466 ms 129164 KB Output is correct
10 Correct 1045 ms 289580 KB Output is correct
11 Correct 94 ms 119500 KB Output is correct
12 Correct 203 ms 121096 KB Output is correct
13 Correct 1051 ms 294412 KB Output is correct
14 Correct 1066 ms 294752 KB Output is correct
15 Correct 1635 ms 323140 KB Output is correct
16 Correct 2197 ms 377044 KB Output is correct
17 Correct 1100 ms 297968 KB Output is correct
18 Correct 1072 ms 300324 KB Output is correct
19 Correct 1126 ms 296048 KB Output is correct
20 Correct 1094 ms 296108 KB Output is correct
21 Correct 1261 ms 329384 KB Output is correct
22 Correct 1008 ms 295320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 119264 KB Output is correct
2 Correct 62 ms 119396 KB Output is correct
3 Correct 64 ms 119644 KB Output is correct
4 Correct 70 ms 119680 KB Output is correct
5 Correct 64 ms 119708 KB Output is correct
6 Correct 61 ms 119616 KB Output is correct
7 Correct 61 ms 119704 KB Output is correct
8 Correct 70 ms 119768 KB Output is correct
9 Correct 67 ms 119756 KB Output is correct
10 Correct 67 ms 119816 KB Output is correct
11 Correct 62 ms 119704 KB Output is correct
12 Correct 58 ms 119632 KB Output is correct
13 Correct 61 ms 119628 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 119264 KB Output is correct
2 Correct 62 ms 119396 KB Output is correct
3 Correct 64 ms 119644 KB Output is correct
4 Correct 70 ms 119680 KB Output is correct
5 Correct 64 ms 119708 KB Output is correct
6 Correct 61 ms 119616 KB Output is correct
7 Correct 61 ms 119704 KB Output is correct
8 Correct 70 ms 119768 KB Output is correct
9 Correct 67 ms 119756 KB Output is correct
10 Correct 67 ms 119816 KB Output is correct
11 Correct 62 ms 119704 KB Output is correct
12 Correct 58 ms 119632 KB Output is correct
13 Correct 61 ms 119628 KB Output is correct
14 Correct 56 ms 119276 KB Output is correct
15 Correct 58 ms 119244 KB Output is correct
16 Correct 64 ms 119704 KB Output is correct
17 Correct 63 ms 119652 KB Output is correct
18 Correct 63 ms 119724 KB Output is correct
19 Correct 62 ms 119232 KB Output is correct
20 Correct 62 ms 119732 KB Output is correct
21 Correct 73 ms 119712 KB Output is correct
22 Correct 62 ms 119724 KB Output is correct
23 Correct 65 ms 119300 KB Output is correct
24 Correct 61 ms 119440 KB Output is correct
25 Correct 67 ms 119660 KB Output is correct
26 Correct 57 ms 119664 KB Output is correct
27 Correct 64 ms 119704 KB Output is correct
28 Correct 59 ms 119336 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 119264 KB Output is correct
2 Correct 62 ms 119396 KB Output is correct
3 Correct 64 ms 119644 KB Output is correct
4 Correct 70 ms 119680 KB Output is correct
5 Correct 64 ms 119708 KB Output is correct
6 Correct 61 ms 119616 KB Output is correct
7 Correct 61 ms 119704 KB Output is correct
8 Correct 70 ms 119768 KB Output is correct
9 Correct 67 ms 119756 KB Output is correct
10 Correct 67 ms 119816 KB Output is correct
11 Correct 62 ms 119704 KB Output is correct
12 Correct 58 ms 119632 KB Output is correct
13 Correct 61 ms 119628 KB Output is correct
14 Correct 56 ms 119276 KB Output is correct
15 Correct 58 ms 119244 KB Output is correct
16 Correct 64 ms 119704 KB Output is correct
17 Correct 63 ms 119652 KB Output is correct
18 Correct 63 ms 119724 KB Output is correct
19 Correct 62 ms 119232 KB Output is correct
20 Correct 62 ms 119732 KB Output is correct
21 Correct 73 ms 119712 KB Output is correct
22 Correct 62 ms 119724 KB Output is correct
23 Correct 65 ms 119300 KB Output is correct
24 Correct 61 ms 119440 KB Output is correct
25 Correct 67 ms 119660 KB Output is correct
26 Correct 57 ms 119664 KB Output is correct
27 Correct 64 ms 119704 KB Output is correct
28 Correct 59 ms 119336 KB Output is correct
29 Correct 67 ms 119752 KB Output is correct
30 Correct 64 ms 119908 KB Output is correct
31 Correct 62 ms 119932 KB Output is correct
32 Correct 67 ms 119756 KB Output is correct
33 Correct 63 ms 119736 KB Output is correct
34 Correct 65 ms 119668 KB Output is correct
35 Correct 62 ms 119628 KB Output is correct
36 Correct 59 ms 119628 KB Output is correct
37 Correct 62 ms 119692 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 119264 KB Output is correct
2 Correct 62 ms 119396 KB Output is correct
3 Correct 64 ms 119644 KB Output is correct
4 Correct 70 ms 119680 KB Output is correct
5 Correct 64 ms 119708 KB Output is correct
6 Correct 61 ms 119616 KB Output is correct
7 Correct 61 ms 119704 KB Output is correct
8 Correct 70 ms 119768 KB Output is correct
9 Correct 67 ms 119756 KB Output is correct
10 Correct 67 ms 119816 KB Output is correct
11 Correct 62 ms 119704 KB Output is correct
12 Correct 58 ms 119632 KB Output is correct
13 Correct 61 ms 119628 KB Output is correct
14 Correct 56 ms 119276 KB Output is correct
15 Correct 58 ms 119244 KB Output is correct
16 Correct 64 ms 119704 KB Output is correct
17 Correct 63 ms 119652 KB Output is correct
18 Correct 63 ms 119724 KB Output is correct
19 Correct 62 ms 119232 KB Output is correct
20 Correct 62 ms 119732 KB Output is correct
21 Correct 73 ms 119712 KB Output is correct
22 Correct 62 ms 119724 KB Output is correct
23 Correct 65 ms 119300 KB Output is correct
24 Correct 61 ms 119440 KB Output is correct
25 Correct 67 ms 119660 KB Output is correct
26 Correct 57 ms 119664 KB Output is correct
27 Correct 64 ms 119704 KB Output is correct
28 Correct 59 ms 119336 KB Output is correct
29 Correct 67 ms 119752 KB Output is correct
30 Correct 64 ms 119908 KB Output is correct
31 Correct 62 ms 119932 KB Output is correct
32 Correct 67 ms 119756 KB Output is correct
33 Correct 63 ms 119736 KB Output is correct
34 Correct 65 ms 119668 KB Output is correct
35 Correct 62 ms 119628 KB Output is correct
36 Correct 59 ms 119628 KB Output is correct
37 Correct 62 ms 119692 KB Output is correct
38 Correct 478 ms 128712 KB Output is correct
39 Correct 1030 ms 289876 KB Output is correct
40 Correct 480 ms 130784 KB Output is correct
41 Correct 398 ms 129220 KB Output is correct
42 Correct 345 ms 130144 KB Output is correct
43 Correct 417 ms 130304 KB Output is correct
44 Correct 88 ms 121036 KB Output is correct
45 Correct 1239 ms 289644 KB Output is correct
46 Correct 1219 ms 289664 KB Output is correct
47 Correct 1124 ms 287332 KB Output is correct
48 Correct 973 ms 287260 KB Output is correct
49 Correct 1190 ms 290444 KB Output is correct
50 Correct 1173 ms 290228 KB Output is correct
51 Correct 1208 ms 288944 KB Output is correct
52 Correct 1224 ms 289684 KB Output is correct
53 Correct 153 ms 131960 KB Output is correct
54 Correct 1514 ms 290492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 54 ms 119396 KB Output is correct
2 Correct 59 ms 119232 KB Output is correct
3 Correct 61 ms 119288 KB Output is correct
4 Correct 57 ms 119228 KB Output is correct
5 Correct 86 ms 119652 KB Output is correct
6 Correct 60 ms 119568 KB Output is correct
7 Correct 60 ms 119628 KB Output is correct
8 Correct 58 ms 119316 KB Output is correct
9 Correct 59 ms 119244 KB Output is correct
10 Correct 58 ms 119492 KB Output is correct
11 Correct 57 ms 119244 KB Output is correct
12 Correct 66 ms 119716 KB Output is correct
13 Correct 141 ms 119960 KB Output is correct
14 Correct 196 ms 120280 KB Output is correct
15 Correct 174 ms 119992 KB Output is correct
16 Correct 1245 ms 290928 KB Output is correct
17 Correct 1694 ms 300428 KB Output is correct
18 Correct 1827 ms 325548 KB Output is correct
19 Correct 1292 ms 291700 KB Output is correct
20 Correct 1322 ms 290888 KB Output is correct
21 Correct 1303 ms 291008 KB Output is correct
22 Correct 1261 ms 298016 KB Output is correct
23 Correct 1291 ms 296368 KB Output is correct
24 Correct 1320 ms 294516 KB Output is correct
25 Correct 1316 ms 294744 KB Output is correct
26 Correct 1286 ms 296432 KB Output is correct
27 Correct 1192 ms 303616 KB Output is correct
28 Correct 1319 ms 316808 KB Output is correct
29 Correct 1288 ms 308972 KB Output is correct
30 Correct 1602 ms 302180 KB Output is correct
31 Correct 1583 ms 303108 KB Output is correct
32 Correct 1628 ms 300072 KB Output is correct
33 Correct 1611 ms 303368 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 119188 KB Output is correct
2 Correct 55 ms 119316 KB Output is correct
3 Correct 54 ms 119304 KB Output is correct
4 Correct 112 ms 119620 KB Output is correct
5 Correct 194 ms 119520 KB Output is correct
6 Correct 64 ms 119764 KB Output is correct
7 Correct 62 ms 119672 KB Output is correct
8 Correct 63 ms 119696 KB Output is correct
9 Correct 466 ms 129164 KB Output is correct
10 Correct 1045 ms 289580 KB Output is correct
11 Correct 94 ms 119500 KB Output is correct
12 Correct 203 ms 121096 KB Output is correct
13 Correct 1051 ms 294412 KB Output is correct
14 Correct 1066 ms 294752 KB Output is correct
15 Correct 1635 ms 323140 KB Output is correct
16 Correct 2197 ms 377044 KB Output is correct
17 Correct 1100 ms 297968 KB Output is correct
18 Correct 1072 ms 300324 KB Output is correct
19 Correct 1126 ms 296048 KB Output is correct
20 Correct 1094 ms 296108 KB Output is correct
21 Correct 1261 ms 329384 KB Output is correct
22 Correct 1008 ms 295320 KB Output is correct
23 Correct 56 ms 119264 KB Output is correct
24 Correct 62 ms 119396 KB Output is correct
25 Correct 64 ms 119644 KB Output is correct
26 Correct 70 ms 119680 KB Output is correct
27 Correct 64 ms 119708 KB Output is correct
28 Correct 61 ms 119616 KB Output is correct
29 Correct 61 ms 119704 KB Output is correct
30 Correct 70 ms 119768 KB Output is correct
31 Correct 67 ms 119756 KB Output is correct
32 Correct 67 ms 119816 KB Output is correct
33 Correct 62 ms 119704 KB Output is correct
34 Correct 58 ms 119632 KB Output is correct
35 Correct 61 ms 119628 KB Output is correct
36 Correct 56 ms 119276 KB Output is correct
37 Correct 58 ms 119244 KB Output is correct
38 Correct 64 ms 119704 KB Output is correct
39 Correct 63 ms 119652 KB Output is correct
40 Correct 63 ms 119724 KB Output is correct
41 Correct 62 ms 119232 KB Output is correct
42 Correct 62 ms 119732 KB Output is correct
43 Correct 73 ms 119712 KB Output is correct
44 Correct 62 ms 119724 KB Output is correct
45 Correct 65 ms 119300 KB Output is correct
46 Correct 61 ms 119440 KB Output is correct
47 Correct 67 ms 119660 KB Output is correct
48 Correct 57 ms 119664 KB Output is correct
49 Correct 64 ms 119704 KB Output is correct
50 Correct 59 ms 119336 KB Output is correct
51 Correct 67 ms 119752 KB Output is correct
52 Correct 64 ms 119908 KB Output is correct
53 Correct 62 ms 119932 KB Output is correct
54 Correct 67 ms 119756 KB Output is correct
55 Correct 63 ms 119736 KB Output is correct
56 Correct 65 ms 119668 KB Output is correct
57 Correct 62 ms 119628 KB Output is correct
58 Correct 59 ms 119628 KB Output is correct
59 Correct 62 ms 119692 KB Output is correct
60 Correct 478 ms 128712 KB Output is correct
61 Correct 1030 ms 289876 KB Output is correct
62 Correct 480 ms 130784 KB Output is correct
63 Correct 398 ms 129220 KB Output is correct
64 Correct 345 ms 130144 KB Output is correct
65 Correct 417 ms 130304 KB Output is correct
66 Correct 88 ms 121036 KB Output is correct
67 Correct 1239 ms 289644 KB Output is correct
68 Correct 1219 ms 289664 KB Output is correct
69 Correct 1124 ms 287332 KB Output is correct
70 Correct 973 ms 287260 KB Output is correct
71 Correct 1190 ms 290444 KB Output is correct
72 Correct 1173 ms 290228 KB Output is correct
73 Correct 1208 ms 288944 KB Output is correct
74 Correct 1224 ms 289684 KB Output is correct
75 Correct 153 ms 131960 KB Output is correct
76 Correct 1514 ms 290492 KB Output is correct
77 Correct 54 ms 119396 KB Output is correct
78 Correct 59 ms 119232 KB Output is correct
79 Correct 61 ms 119288 KB Output is correct
80 Correct 57 ms 119228 KB Output is correct
81 Correct 86 ms 119652 KB Output is correct
82 Correct 60 ms 119568 KB Output is correct
83 Correct 60 ms 119628 KB Output is correct
84 Correct 58 ms 119316 KB Output is correct
85 Correct 59 ms 119244 KB Output is correct
86 Correct 58 ms 119492 KB Output is correct
87 Correct 57 ms 119244 KB Output is correct
88 Correct 66 ms 119716 KB Output is correct
89 Correct 141 ms 119960 KB Output is correct
90 Correct 196 ms 120280 KB Output is correct
91 Correct 174 ms 119992 KB Output is correct
92 Correct 1245 ms 290928 KB Output is correct
93 Correct 1694 ms 300428 KB Output is correct
94 Correct 1827 ms 325548 KB Output is correct
95 Correct 1292 ms 291700 KB Output is correct
96 Correct 1322 ms 290888 KB Output is correct
97 Correct 1303 ms 291008 KB Output is correct
98 Correct 1261 ms 298016 KB Output is correct
99 Correct 1291 ms 296368 KB Output is correct
100 Correct 1320 ms 294516 KB Output is correct
101 Correct 1316 ms 294744 KB Output is correct
102 Correct 1286 ms 296432 KB Output is correct
103 Correct 1192 ms 303616 KB Output is correct
104 Correct 1319 ms 316808 KB Output is correct
105 Correct 1288 ms 308972 KB Output is correct
106 Correct 1602 ms 302180 KB Output is correct
107 Correct 1583 ms 303108 KB Output is correct
108 Correct 1628 ms 300072 KB Output is correct
109 Correct 1611 ms 303368 KB Output is correct
110 Correct 218 ms 120652 KB Output is correct
111 Correct 164 ms 119964 KB Output is correct
112 Correct 1802 ms 329912 KB Output is correct
113 Correct 1364 ms 290712 KB Output is correct
114 Correct 1439 ms 324868 KB Output is correct
115 Correct 787 ms 291020 KB Output is correct
116 Correct 1709 ms 295972 KB Output is correct
117 Correct 2231 ms 332248 KB Output is correct
118 Correct 1482 ms 290548 KB Output is correct
119 Correct 1614 ms 290296 KB Output is correct
120 Correct 151 ms 134136 KB Output is correct
121 Correct 1503 ms 296432 KB Output is correct
122 Correct 1885 ms 296084 KB Output is correct
123 Correct 1388 ms 291032 KB Output is correct
124 Correct 1396 ms 290920 KB Output is correct
125 Correct 1418 ms 291648 KB Output is correct
126 Correct 2127 ms 372648 KB Output is correct
127 Correct 1592 ms 334260 KB Output is correct
128 Correct 1517 ms 336092 KB Output is correct
129 Correct 1388 ms 310540 KB Output is correct
130 Correct 1474 ms 330476 KB Output is correct