Submission #1046654

# Submission time Handle Problem Language Result Execution time Memory
1046654 2024-08-06T19:46:36 Z _8_8_ Jail (JOI22_jail) C++17
100 / 100
1542 ms 401228 KB
#include <bits/stdc++.h>

 
using namespace std;
    
typedef long long ll;
const int  N = 24e4 + 2, MOD = (int)1e9+7;

int n,x[N],y[N],m,it = 1,it1 = 1,s[N],f[N];
vector<int> g[N],G[N * 20];
int up[N][20],t[N][20],t1[N][20],tin[N],tout[N],timer = 0;
const int b = 18;
void dfs(int v,int pr) {
    tin[v] = ++timer;
    up[v][0] = pr;
    if(!x[v]) {
        t[v][0] = ++it;
    } else {
        t[v][0] = x[v];
    }
    if(!y[v]) {
        t1[v][0] = ++it;
    } else {
        t1[v][0] = y[v];
    }
    for(int i = 1;i <= b;i++) {
        up[v][i] = up[up[v][i-1]][i-1];
        t[v][i] = ++it;
        t1[v][i] = ++it;
        // cout << t[v][i] << ' ' << t[v][i - 1] << "\n" ;
        // cout << t[v][i] << ' ' << t[up[v][i-1]][i - 1] << '\n';
        G[t[v][i]].push_back(t[v][i-1]);G[t[v][i]].push_back(t[up[v][i-1]][i-1]);
        G[t1[v][i-1]].push_back(t1[v][i]);G[t1[up[v][i-1]][i-1]].push_back(t1[v][i]);
    }
    for(int to:g[v]) {
        if(to == pr) continue;
        dfs(to,v);  
    }
    tout[v] = ++timer;
}
int vis[N * 20];
bool ans = 1;
void go(int v) {
    vis[v] = 1;
    for(int to:G[v]) {
        if(!vis[to]) {
            // cout << v << ' ' << to << '\n';
            go(to);
        } else {
            if(vis[to] == 1) {
                ans = 0;
            }
        }
    }
    vis[v] = 2;
}
bool is(int v,int u) {
    return (tin[v] <= tin[u] && tout[v] >= tout[u]);
}
int lca(int v,int u) {
    if(is(v,u)) return v;
    if(is(u,v)) return u;
    for(int i = b;i >= 0;i--) {
        if(!is(up[v][i],u)) {
            v = up[v][i];
        }
    }
    return up[v][0];
}
void add(int v,int l,int id,bool ok) {
    if(v == l) {
        G[id].push_back(t[l][0]);
        return;
    }
    // if(!is(l,v) || (l == v && ok)) return;
    for(int i = b;i >= 0;i--) {
        if(!is(up[v][i],l)) {
            G[id].push_back(t[v][i]);
            v = up[v][i];
        }
    }
    if(ok)G[id].push_back(t[v][1]);
    else{
        G[id].push_back(t[v][0]);
    }
}
void add_(int v,int l,int id,bool ok) {
    if(v == l) {
        G[t1[v][0]].push_back(id);
        return;
    }
    // if(!is(l,v) || (l == v && ok)) return;
    // cout << v << ' ' << l << ' ' << id << ' ' << ok << '\n';
    // cout << v << ' ' << l << "x\n";
    for(int i = b;i >= 0;i--) {
        if(!is(up[v][i],l)) {
            // cout << t1[v][i] << ' ' << id << '\n';
            G[t1[v][i]].push_back(id);
            v = up[v][i];
        }
    }
    // cout << t1[v][1] << ' ' << id << '\n';
    // cout << "x\n";
    if(ok)G[t1[v][1]].push_back(id);
    else{
        G[t1[v][0]].push_back(id);
        // cout << t1[v][0] << ' ' << id << '\n';
    } 
}
void test() {
    ans = 1;
    timer = 0;
    cin >> n;
    for(int i = 1;i <= n;i++) {
        g[i].clear();
        x[i] = y[i] =0 ;
    }
    for(int i = 1;i <= n - 1;i++) {
        int a,b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    cin >> m;
    it = it1 = m;
    for(int i = 1;i <= m;i++) {
        cin >> s[i] >> f[i];
        y[f[i]] = x[s[i]] = i;
    }
    dfs(1,1);
    for(int i = 1;i <= m;i++) {
        int l = lca(s[i],f[i]);
        if(s[i] == l) {
            add(f[i],l,i,0);
        } else {
            add(up[s[i]][0],l,i,1);
            if(f[i] != l) add(f[i],l,i,1);
        }
        if(f[i] == l) {
            add_(s[i],l,i,0);
        } else {
            add_(up[f[i]][0],l,i,1);
            if(s[i] != l) add_(s[i],l,i,1);
        }
    }
    for(int i = 1;i <= it;i++) {
        if(!vis[i]) {
            go(i);
        }
    }
    cout << (ans ? "Yes\n":"No\n");
    for(int i = 1;i <= it;i++) {
        G[i].clear();
        vis[i] =0;
    }
}
int main() {
    ios_base::sync_with_stdio(false);cin.tie(0);
    int t = 1; 
    cin >> t;
    while(t--) {
        test();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 18 ms 127580 KB Output is correct
2 Correct 17 ms 127504 KB Output is correct
3 Correct 16 ms 127576 KB Output is correct
4 Correct 41 ms 128132 KB Output is correct
5 Correct 73 ms 128616 KB Output is correct
6 Correct 20 ms 128116 KB Output is correct
7 Correct 19 ms 128092 KB Output is correct
8 Correct 22 ms 128152 KB Output is correct
9 Correct 150 ms 140136 KB Output is correct
10 Correct 1031 ms 327724 KB Output is correct
11 Correct 27 ms 127832 KB Output is correct
12 Correct 83 ms 128848 KB Output is correct
13 Correct 893 ms 330256 KB Output is correct
14 Correct 861 ms 330672 KB Output is correct
15 Correct 1171 ms 351808 KB Output is correct
16 Correct 1435 ms 401228 KB Output is correct
17 Correct 1121 ms 364828 KB Output is correct
18 Correct 942 ms 328652 KB Output is correct
19 Correct 1077 ms 333692 KB Output is correct
20 Correct 1092 ms 347972 KB Output is correct
21 Correct 1131 ms 356164 KB Output is correct
22 Correct 794 ms 328008 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 127576 KB Output is correct
2 Correct 16 ms 127580 KB Output is correct
3 Correct 20 ms 128092 KB Output is correct
4 Correct 20 ms 128092 KB Output is correct
5 Correct 19 ms 128128 KB Output is correct
6 Correct 20 ms 128092 KB Output is correct
7 Correct 19 ms 128080 KB Output is correct
8 Correct 21 ms 128088 KB Output is correct
9 Correct 20 ms 128344 KB Output is correct
10 Correct 19 ms 128084 KB Output is correct
11 Correct 20 ms 128096 KB Output is correct
12 Correct 18 ms 128096 KB Output is correct
13 Correct 18 ms 128068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 127576 KB Output is correct
2 Correct 16 ms 127580 KB Output is correct
3 Correct 20 ms 128092 KB Output is correct
4 Correct 20 ms 128092 KB Output is correct
5 Correct 19 ms 128128 KB Output is correct
6 Correct 20 ms 128092 KB Output is correct
7 Correct 19 ms 128080 KB Output is correct
8 Correct 21 ms 128088 KB Output is correct
9 Correct 20 ms 128344 KB Output is correct
10 Correct 19 ms 128084 KB Output is correct
11 Correct 20 ms 128096 KB Output is correct
12 Correct 18 ms 128096 KB Output is correct
13 Correct 18 ms 128068 KB Output is correct
14 Correct 18 ms 127584 KB Output is correct
15 Correct 17 ms 127680 KB Output is correct
16 Correct 23 ms 128028 KB Output is correct
17 Correct 19 ms 128048 KB Output is correct
18 Correct 22 ms 128296 KB Output is correct
19 Correct 17 ms 127580 KB Output is correct
20 Correct 20 ms 128140 KB Output is correct
21 Correct 20 ms 128092 KB Output is correct
22 Correct 20 ms 128088 KB Output is correct
23 Correct 16 ms 127580 KB Output is correct
24 Correct 17 ms 127788 KB Output is correct
25 Correct 19 ms 128056 KB Output is correct
26 Correct 17 ms 127868 KB Output is correct
27 Correct 20 ms 128092 KB Output is correct
28 Correct 16 ms 127580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 127576 KB Output is correct
2 Correct 16 ms 127580 KB Output is correct
3 Correct 20 ms 128092 KB Output is correct
4 Correct 20 ms 128092 KB Output is correct
5 Correct 19 ms 128128 KB Output is correct
6 Correct 20 ms 128092 KB Output is correct
7 Correct 19 ms 128080 KB Output is correct
8 Correct 21 ms 128088 KB Output is correct
9 Correct 20 ms 128344 KB Output is correct
10 Correct 19 ms 128084 KB Output is correct
11 Correct 20 ms 128096 KB Output is correct
12 Correct 18 ms 128096 KB Output is correct
13 Correct 18 ms 128068 KB Output is correct
14 Correct 18 ms 127584 KB Output is correct
15 Correct 17 ms 127680 KB Output is correct
16 Correct 23 ms 128028 KB Output is correct
17 Correct 19 ms 128048 KB Output is correct
18 Correct 22 ms 128296 KB Output is correct
19 Correct 17 ms 127580 KB Output is correct
20 Correct 20 ms 128140 KB Output is correct
21 Correct 20 ms 128092 KB Output is correct
22 Correct 20 ms 128088 KB Output is correct
23 Correct 16 ms 127580 KB Output is correct
24 Correct 17 ms 127788 KB Output is correct
25 Correct 19 ms 128056 KB Output is correct
26 Correct 17 ms 127868 KB Output is correct
27 Correct 20 ms 128092 KB Output is correct
28 Correct 16 ms 127580 KB Output is correct
29 Correct 24 ms 128156 KB Output is correct
30 Correct 21 ms 128092 KB Output is correct
31 Correct 21 ms 128200 KB Output is correct
32 Correct 21 ms 128104 KB Output is correct
33 Correct 20 ms 128092 KB Output is correct
34 Correct 20 ms 127836 KB Output is correct
35 Correct 19 ms 127836 KB Output is correct
36 Correct 19 ms 128080 KB Output is correct
37 Correct 18 ms 127960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 127576 KB Output is correct
2 Correct 16 ms 127580 KB Output is correct
3 Correct 20 ms 128092 KB Output is correct
4 Correct 20 ms 128092 KB Output is correct
5 Correct 19 ms 128128 KB Output is correct
6 Correct 20 ms 128092 KB Output is correct
7 Correct 19 ms 128080 KB Output is correct
8 Correct 21 ms 128088 KB Output is correct
9 Correct 20 ms 128344 KB Output is correct
10 Correct 19 ms 128084 KB Output is correct
11 Correct 20 ms 128096 KB Output is correct
12 Correct 18 ms 128096 KB Output is correct
13 Correct 18 ms 128068 KB Output is correct
14 Correct 18 ms 127584 KB Output is correct
15 Correct 17 ms 127680 KB Output is correct
16 Correct 23 ms 128028 KB Output is correct
17 Correct 19 ms 128048 KB Output is correct
18 Correct 22 ms 128296 KB Output is correct
19 Correct 17 ms 127580 KB Output is correct
20 Correct 20 ms 128140 KB Output is correct
21 Correct 20 ms 128092 KB Output is correct
22 Correct 20 ms 128088 KB Output is correct
23 Correct 16 ms 127580 KB Output is correct
24 Correct 17 ms 127788 KB Output is correct
25 Correct 19 ms 128056 KB Output is correct
26 Correct 17 ms 127868 KB Output is correct
27 Correct 20 ms 128092 KB Output is correct
28 Correct 16 ms 127580 KB Output is correct
29 Correct 24 ms 128156 KB Output is correct
30 Correct 21 ms 128092 KB Output is correct
31 Correct 21 ms 128200 KB Output is correct
32 Correct 21 ms 128104 KB Output is correct
33 Correct 20 ms 128092 KB Output is correct
34 Correct 20 ms 127836 KB Output is correct
35 Correct 19 ms 127836 KB Output is correct
36 Correct 19 ms 128080 KB Output is correct
37 Correct 18 ms 127960 KB Output is correct
38 Correct 154 ms 140172 KB Output is correct
39 Correct 1054 ms 327744 KB Output is correct
40 Correct 136 ms 142308 KB Output is correct
41 Correct 118 ms 141296 KB Output is correct
42 Correct 104 ms 141652 KB Output is correct
43 Correct 131 ms 141396 KB Output is correct
44 Correct 32 ms 131604 KB Output is correct
45 Correct 431 ms 321280 KB Output is correct
46 Correct 500 ms 321344 KB Output is correct
47 Correct 847 ms 321720 KB Output is correct
48 Correct 824 ms 321856 KB Output is correct
49 Correct 468 ms 322336 KB Output is correct
50 Correct 475 ms 322328 KB Output is correct
51 Correct 580 ms 322512 KB Output is correct
52 Correct 611 ms 322620 KB Output is correct
53 Correct 52 ms 141908 KB Output is correct
54 Correct 412 ms 321912 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 19 ms 127580 KB Output is correct
2 Correct 16 ms 127580 KB Output is correct
3 Correct 16 ms 127664 KB Output is correct
4 Correct 17 ms 127696 KB Output is correct
5 Correct 26 ms 127876 KB Output is correct
6 Correct 18 ms 128088 KB Output is correct
7 Correct 19 ms 128092 KB Output is correct
8 Correct 16 ms 127580 KB Output is correct
9 Correct 19 ms 127576 KB Output is correct
10 Correct 17 ms 127716 KB Output is correct
11 Correct 16 ms 127580 KB Output is correct
12 Correct 20 ms 128092 KB Output is correct
13 Correct 58 ms 128596 KB Output is correct
14 Correct 79 ms 128848 KB Output is correct
15 Correct 67 ms 128596 KB Output is correct
16 Correct 447 ms 322804 KB Output is correct
17 Correct 576 ms 330768 KB Output is correct
18 Correct 719 ms 348252 KB Output is correct
19 Correct 489 ms 323564 KB Output is correct
20 Correct 497 ms 323852 KB Output is correct
21 Correct 466 ms 323568 KB Output is correct
22 Correct 538 ms 328044 KB Output is correct
23 Correct 508 ms 326804 KB Output is correct
24 Correct 537 ms 327132 KB Output is correct
25 Correct 555 ms 327200 KB Output is correct
26 Correct 541 ms 327040 KB Output is correct
27 Correct 595 ms 332684 KB Output is correct
28 Correct 582 ms 335480 KB Output is correct
29 Correct 593 ms 329364 KB Output is correct
30 Correct 522 ms 327816 KB Output is correct
31 Correct 547 ms 328664 KB Output is correct
32 Correct 530 ms 326960 KB Output is correct
33 Correct 541 ms 328080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 18 ms 127580 KB Output is correct
2 Correct 17 ms 127504 KB Output is correct
3 Correct 16 ms 127576 KB Output is correct
4 Correct 41 ms 128132 KB Output is correct
5 Correct 73 ms 128616 KB Output is correct
6 Correct 20 ms 128116 KB Output is correct
7 Correct 19 ms 128092 KB Output is correct
8 Correct 22 ms 128152 KB Output is correct
9 Correct 150 ms 140136 KB Output is correct
10 Correct 1031 ms 327724 KB Output is correct
11 Correct 27 ms 127832 KB Output is correct
12 Correct 83 ms 128848 KB Output is correct
13 Correct 893 ms 330256 KB Output is correct
14 Correct 861 ms 330672 KB Output is correct
15 Correct 1171 ms 351808 KB Output is correct
16 Correct 1435 ms 401228 KB Output is correct
17 Correct 1121 ms 364828 KB Output is correct
18 Correct 942 ms 328652 KB Output is correct
19 Correct 1077 ms 333692 KB Output is correct
20 Correct 1092 ms 347972 KB Output is correct
21 Correct 1131 ms 356164 KB Output is correct
22 Correct 794 ms 328008 KB Output is correct
23 Correct 17 ms 127576 KB Output is correct
24 Correct 16 ms 127580 KB Output is correct
25 Correct 20 ms 128092 KB Output is correct
26 Correct 20 ms 128092 KB Output is correct
27 Correct 19 ms 128128 KB Output is correct
28 Correct 20 ms 128092 KB Output is correct
29 Correct 19 ms 128080 KB Output is correct
30 Correct 21 ms 128088 KB Output is correct
31 Correct 20 ms 128344 KB Output is correct
32 Correct 19 ms 128084 KB Output is correct
33 Correct 20 ms 128096 KB Output is correct
34 Correct 18 ms 128096 KB Output is correct
35 Correct 18 ms 128068 KB Output is correct
36 Correct 18 ms 127584 KB Output is correct
37 Correct 17 ms 127680 KB Output is correct
38 Correct 23 ms 128028 KB Output is correct
39 Correct 19 ms 128048 KB Output is correct
40 Correct 22 ms 128296 KB Output is correct
41 Correct 17 ms 127580 KB Output is correct
42 Correct 20 ms 128140 KB Output is correct
43 Correct 20 ms 128092 KB Output is correct
44 Correct 20 ms 128088 KB Output is correct
45 Correct 16 ms 127580 KB Output is correct
46 Correct 17 ms 127788 KB Output is correct
47 Correct 19 ms 128056 KB Output is correct
48 Correct 17 ms 127868 KB Output is correct
49 Correct 20 ms 128092 KB Output is correct
50 Correct 16 ms 127580 KB Output is correct
51 Correct 24 ms 128156 KB Output is correct
52 Correct 21 ms 128092 KB Output is correct
53 Correct 21 ms 128200 KB Output is correct
54 Correct 21 ms 128104 KB Output is correct
55 Correct 20 ms 128092 KB Output is correct
56 Correct 20 ms 127836 KB Output is correct
57 Correct 19 ms 127836 KB Output is correct
58 Correct 19 ms 128080 KB Output is correct
59 Correct 18 ms 127960 KB Output is correct
60 Correct 154 ms 140172 KB Output is correct
61 Correct 1054 ms 327744 KB Output is correct
62 Correct 136 ms 142308 KB Output is correct
63 Correct 118 ms 141296 KB Output is correct
64 Correct 104 ms 141652 KB Output is correct
65 Correct 131 ms 141396 KB Output is correct
66 Correct 32 ms 131604 KB Output is correct
67 Correct 431 ms 321280 KB Output is correct
68 Correct 500 ms 321344 KB Output is correct
69 Correct 847 ms 321720 KB Output is correct
70 Correct 824 ms 321856 KB Output is correct
71 Correct 468 ms 322336 KB Output is correct
72 Correct 475 ms 322328 KB Output is correct
73 Correct 580 ms 322512 KB Output is correct
74 Correct 611 ms 322620 KB Output is correct
75 Correct 52 ms 141908 KB Output is correct
76 Correct 412 ms 321912 KB Output is correct
77 Correct 19 ms 127580 KB Output is correct
78 Correct 16 ms 127580 KB Output is correct
79 Correct 16 ms 127664 KB Output is correct
80 Correct 17 ms 127696 KB Output is correct
81 Correct 26 ms 127876 KB Output is correct
82 Correct 18 ms 128088 KB Output is correct
83 Correct 19 ms 128092 KB Output is correct
84 Correct 16 ms 127580 KB Output is correct
85 Correct 19 ms 127576 KB Output is correct
86 Correct 17 ms 127716 KB Output is correct
87 Correct 16 ms 127580 KB Output is correct
88 Correct 20 ms 128092 KB Output is correct
89 Correct 58 ms 128596 KB Output is correct
90 Correct 79 ms 128848 KB Output is correct
91 Correct 67 ms 128596 KB Output is correct
92 Correct 447 ms 322804 KB Output is correct
93 Correct 576 ms 330768 KB Output is correct
94 Correct 719 ms 348252 KB Output is correct
95 Correct 489 ms 323564 KB Output is correct
96 Correct 497 ms 323852 KB Output is correct
97 Correct 466 ms 323568 KB Output is correct
98 Correct 538 ms 328044 KB Output is correct
99 Correct 508 ms 326804 KB Output is correct
100 Correct 537 ms 327132 KB Output is correct
101 Correct 555 ms 327200 KB Output is correct
102 Correct 541 ms 327040 KB Output is correct
103 Correct 595 ms 332684 KB Output is correct
104 Correct 582 ms 335480 KB Output is correct
105 Correct 593 ms 329364 KB Output is correct
106 Correct 522 ms 327816 KB Output is correct
107 Correct 547 ms 328664 KB Output is correct
108 Correct 530 ms 326960 KB Output is correct
109 Correct 541 ms 328080 KB Output is correct
110 Correct 99 ms 129368 KB Output is correct
111 Correct 66 ms 128564 KB Output is correct
112 Correct 1240 ms 360388 KB Output is correct
113 Correct 1057 ms 325280 KB Output is correct
114 Correct 830 ms 352180 KB Output is correct
115 Correct 339 ms 322380 KB Output is correct
116 Correct 480 ms 324168 KB Output is correct
117 Correct 831 ms 353784 KB Output is correct
118 Correct 454 ms 322028 KB Output is correct
119 Correct 427 ms 321900 KB Output is correct
120 Correct 56 ms 143952 KB Output is correct
121 Correct 593 ms 326312 KB Output is correct
122 Correct 631 ms 326128 KB Output is correct
123 Correct 1211 ms 326216 KB Output is correct
124 Correct 1173 ms 326256 KB Output is correct
125 Correct 1216 ms 326468 KB Output is correct
126 Correct 1542 ms 400172 KB Output is correct
127 Correct 1177 ms 364488 KB Output is correct
128 Correct 1120 ms 365244 KB Output is correct
129 Correct 1004 ms 333224 KB Output is correct
130 Correct 1140 ms 359608 KB Output is correct