Submission #667246

# Submission time Handle Problem Language Result Execution time Memory
667246 2022-11-30T21:51:59 Z Kahou Jail (JOI22_jail) C++14
100 / 100
1497 ms 386112 KB
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = 1.2e5 + 50;
const int M = 5e6 + 50, LG = 20;
int n, om, m, par[LG][N], h[N], id[LG][N][2], mark[N][2];
int s[M], t[M];
int d[M];
vector<int> adj[N];
vector<int> adj2[M];
queue<int> q;

void dfs(int u, int p) {
        h[u] = h[p]+1;
        par[0][u] = p;

        if (mark[u][0]) {
                adj2[id[0][u][0]].push_back(mark[u][0]);
                d[mark[u][0]]++;
        }
        if (mark[u][1]) {
                adj2[mark[u][1]].push_back(id[0][u][1]);
                d[id[0][u][1]]++;
        }

        for (int v:adj[u]) {
                if (v == p) continue;
                dfs(v, u);
        }
}
int Par(int u, int x) {
        for (int k = 0; k < LG; k++) {
                if (x>>k&1) u = par[k][u];
        }
        return u;
}
int LCA(int u, int v) {
        if (h[u] > h[v]) swap(u, v);
        v = Par(v, h[v]-h[u]);
        for (int k = LG-1; k >= 0; k--){ 
                if (par[k][u] != par[k][v]) {
                        u = par[k][u];
                        v = par[k][v];
                }
        }
        if (u == v) return u;
        return par[0][u];
}

void solve() {
        cin >> n;
        for (int i = 1; i <= n-1; i++) {
                int u, v;
                cin >> u >> v;
                adj[u].push_back(v);
                adj[v].push_back(u);
        }
        cin >> m;
        om = m;
        for (int i = 1; i <= m; i++) {
                cin >> s[i] >> t[i];
                mark[s[i]][0] = mark[t[i]][1] = i;
        }
        for (int k = 0; k < LG; k++) {
                for (int u = 1; u <= n; u++) {
                        id[k][u][0] = ++m;
                }
        }
        for (int k = 0; k < LG; k++) {
                for (int u = 1; u <= n; u++) {
                        id[k][u][1] = ++m;
                }
        }

        dfs(1, 0);

        for (int k = 1; k < LG; k++) {
                for (int u = 1; u <= n; u++) {
                        par[k][u] = par[k-1][par[k-1][u]];

                        adj2[id[k][u][0]].push_back(id[k-1][u][0]);
                        d[id[k-1][u][0]]++;
                        adj2[id[k][u][0]].push_back(id[k-1][par[k-1][u]][0]);
                        d[id[k-1][par[k-1][u]][0]]++;

                        adj2[id[k-1][u][1]].push_back(id[k][u][1]);
                        d[id[k][u][1]]++;
                        adj2[id[k-1][par[k-1][u]][1]].push_back(id[k][u][1]);
                        d[id[k][u][1]]++;
                }
        }


        for (int i = 1; i <= om; i++) {
                int r = LCA(s[i], t[i]);
                int u, x;
                u = par[0][s[i]];
                x = h[u]-h[r]+1;
                for (int k = 0; k < LG; k++) {
                        if (x>>k&1) {
                                adj2[i].push_back(id[k][u][0]);
                                d[id[k][u][0]]++;
                                u = par[k][u];
                        }
                }
                u = s[i];
                x = h[u]-h[r] + (r != t[i]);
                for (int k = 0; k < LG; k++) {
                        if (x>>k&1) {
                                adj2[id[k][u][1]].push_back(i);
                                d[i]++;
                                u = par[k][u];
                        }
                }
                u = t[i];
                x = h[u]-h[r] + (r != s[i]);
                for (int k = 0; k < LG; k++) {
                        if (x>>k&1) {
                                adj2[i].push_back(id[k][u][0]);
                                d[id[k][u][0]]++;
                                u = par[k][u];
                        }
                }
                u = par[0][t[i]];
                x = h[u]-h[r]+1;
                for (int k = 0; k < LG; k++) {
                        if (x>>k&1) {
                                adj2[id[k][u][1]].push_back(i);
                                d[i]++;
                                u = par[k][u];
                        }
                }
        }
        for (int u = 1; u <= m; u++) {
                if (!d[u]) q.push(u);
        }
        while (q.size()) {
                int u = q.front();
                q.pop();
                for (int v:adj2[u]) {
                        d[v]--;
                        if (!d[v]) q.push(v);
                }
        }
        bool flg = 1;
        for (int u = 1; u <= m; u++) {
                flg = flg&&(!d[u]);
        }
        cout << (flg?"Yes":"No") << endl;

        for (int u = 0; u <= n; u++) {
                h[u] = 0;
                adj[u].clear();
                mark[u][0] = mark[u][1] = 0;
                for (int k = 0; k < LG; k++) {
                        par[k][u] = 0;
                        id[k][u][0] = 0;
                        id[k][u][1] = 0;
                }
        }
        for (int u = 0; u <= m; u++) {
                adj2[u].clear();
                s[u] = t[u] = 0;
                d[u] = 0;
        }
        m = om = 0;
}

int main() {
        ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
        int tc;
        cin >> tc;
        while(tc--)
                solve();
        return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 60 ms 120716 KB Output is correct
2 Correct 61 ms 120752 KB Output is correct
3 Correct 61 ms 120780 KB Output is correct
4 Correct 146 ms 121272 KB Output is correct
5 Correct 238 ms 121844 KB Output is correct
6 Correct 67 ms 121292 KB Output is correct
7 Correct 67 ms 121228 KB Output is correct
8 Correct 71 ms 121252 KB Output is correct
9 Correct 365 ms 134228 KB Output is correct
10 Correct 728 ms 362648 KB Output is correct
11 Correct 92 ms 120976 KB Output is correct
12 Correct 244 ms 121896 KB Output is correct
13 Correct 886 ms 368384 KB Output is correct
14 Correct 651 ms 368584 KB Output is correct
15 Correct 887 ms 369692 KB Output is correct
16 Correct 1301 ms 382072 KB Output is correct
17 Correct 711 ms 368400 KB Output is correct
18 Correct 739 ms 372420 KB Output is correct
19 Correct 736 ms 368364 KB Output is correct
20 Correct 656 ms 368428 KB Output is correct
21 Correct 686 ms 370688 KB Output is correct
22 Correct 633 ms 367484 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 120808 KB Output is correct
2 Correct 61 ms 120768 KB Output is correct
3 Correct 71 ms 121256 KB Output is correct
4 Correct 75 ms 121348 KB Output is correct
5 Correct 69 ms 121232 KB Output is correct
6 Correct 72 ms 121348 KB Output is correct
7 Correct 71 ms 121348 KB Output is correct
8 Correct 77 ms 121336 KB Output is correct
9 Correct 80 ms 121240 KB Output is correct
10 Correct 72 ms 121292 KB Output is correct
11 Correct 72 ms 121324 KB Output is correct
12 Correct 67 ms 121260 KB Output is correct
13 Correct 73 ms 121316 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 120808 KB Output is correct
2 Correct 61 ms 120768 KB Output is correct
3 Correct 71 ms 121256 KB Output is correct
4 Correct 75 ms 121348 KB Output is correct
5 Correct 69 ms 121232 KB Output is correct
6 Correct 72 ms 121348 KB Output is correct
7 Correct 71 ms 121348 KB Output is correct
8 Correct 77 ms 121336 KB Output is correct
9 Correct 80 ms 121240 KB Output is correct
10 Correct 72 ms 121292 KB Output is correct
11 Correct 72 ms 121324 KB Output is correct
12 Correct 67 ms 121260 KB Output is correct
13 Correct 73 ms 121316 KB Output is correct
14 Correct 59 ms 120700 KB Output is correct
15 Correct 66 ms 120856 KB Output is correct
16 Correct 69 ms 121268 KB Output is correct
17 Correct 69 ms 121336 KB Output is correct
18 Correct 69 ms 121336 KB Output is correct
19 Correct 59 ms 120796 KB Output is correct
20 Correct 76 ms 121312 KB Output is correct
21 Correct 68 ms 121216 KB Output is correct
22 Correct 73 ms 121288 KB Output is correct
23 Correct 59 ms 120732 KB Output is correct
24 Correct 64 ms 121048 KB Output is correct
25 Correct 69 ms 121328 KB Output is correct
26 Correct 63 ms 121184 KB Output is correct
27 Correct 70 ms 121252 KB Output is correct
28 Correct 59 ms 120832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 120808 KB Output is correct
2 Correct 61 ms 120768 KB Output is correct
3 Correct 71 ms 121256 KB Output is correct
4 Correct 75 ms 121348 KB Output is correct
5 Correct 69 ms 121232 KB Output is correct
6 Correct 72 ms 121348 KB Output is correct
7 Correct 71 ms 121348 KB Output is correct
8 Correct 77 ms 121336 KB Output is correct
9 Correct 80 ms 121240 KB Output is correct
10 Correct 72 ms 121292 KB Output is correct
11 Correct 72 ms 121324 KB Output is correct
12 Correct 67 ms 121260 KB Output is correct
13 Correct 73 ms 121316 KB Output is correct
14 Correct 59 ms 120700 KB Output is correct
15 Correct 66 ms 120856 KB Output is correct
16 Correct 69 ms 121268 KB Output is correct
17 Correct 69 ms 121336 KB Output is correct
18 Correct 69 ms 121336 KB Output is correct
19 Correct 59 ms 120796 KB Output is correct
20 Correct 76 ms 121312 KB Output is correct
21 Correct 68 ms 121216 KB Output is correct
22 Correct 73 ms 121288 KB Output is correct
23 Correct 59 ms 120732 KB Output is correct
24 Correct 64 ms 121048 KB Output is correct
25 Correct 69 ms 121328 KB Output is correct
26 Correct 63 ms 121184 KB Output is correct
27 Correct 70 ms 121252 KB Output is correct
28 Correct 59 ms 120832 KB Output is correct
29 Correct 70 ms 121292 KB Output is correct
30 Correct 69 ms 121308 KB Output is correct
31 Correct 74 ms 121336 KB Output is correct
32 Correct 68 ms 121360 KB Output is correct
33 Correct 71 ms 121332 KB Output is correct
34 Correct 67 ms 121236 KB Output is correct
35 Correct 66 ms 121204 KB Output is correct
36 Correct 71 ms 121248 KB Output is correct
37 Correct 66 ms 121168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 120808 KB Output is correct
2 Correct 61 ms 120768 KB Output is correct
3 Correct 71 ms 121256 KB Output is correct
4 Correct 75 ms 121348 KB Output is correct
5 Correct 69 ms 121232 KB Output is correct
6 Correct 72 ms 121348 KB Output is correct
7 Correct 71 ms 121348 KB Output is correct
8 Correct 77 ms 121336 KB Output is correct
9 Correct 80 ms 121240 KB Output is correct
10 Correct 72 ms 121292 KB Output is correct
11 Correct 72 ms 121324 KB Output is correct
12 Correct 67 ms 121260 KB Output is correct
13 Correct 73 ms 121316 KB Output is correct
14 Correct 59 ms 120700 KB Output is correct
15 Correct 66 ms 120856 KB Output is correct
16 Correct 69 ms 121268 KB Output is correct
17 Correct 69 ms 121336 KB Output is correct
18 Correct 69 ms 121336 KB Output is correct
19 Correct 59 ms 120796 KB Output is correct
20 Correct 76 ms 121312 KB Output is correct
21 Correct 68 ms 121216 KB Output is correct
22 Correct 73 ms 121288 KB Output is correct
23 Correct 59 ms 120732 KB Output is correct
24 Correct 64 ms 121048 KB Output is correct
25 Correct 69 ms 121328 KB Output is correct
26 Correct 63 ms 121184 KB Output is correct
27 Correct 70 ms 121252 KB Output is correct
28 Correct 59 ms 120832 KB Output is correct
29 Correct 70 ms 121292 KB Output is correct
30 Correct 69 ms 121308 KB Output is correct
31 Correct 74 ms 121336 KB Output is correct
32 Correct 68 ms 121360 KB Output is correct
33 Correct 71 ms 121332 KB Output is correct
34 Correct 67 ms 121236 KB Output is correct
35 Correct 66 ms 121204 KB Output is correct
36 Correct 71 ms 121248 KB Output is correct
37 Correct 66 ms 121168 KB Output is correct
38 Correct 359 ms 134404 KB Output is correct
39 Correct 698 ms 362744 KB Output is correct
40 Correct 401 ms 135744 KB Output is correct
41 Correct 450 ms 134660 KB Output is correct
42 Correct 319 ms 135232 KB Output is correct
43 Correct 401 ms 135416 KB Output is correct
44 Correct 104 ms 123216 KB Output is correct
45 Correct 867 ms 363792 KB Output is correct
46 Correct 897 ms 363764 KB Output is correct
47 Correct 799 ms 360464 KB Output is correct
48 Correct 792 ms 360392 KB Output is correct
49 Correct 687 ms 364428 KB Output is correct
50 Correct 745 ms 364336 KB Output is correct
51 Correct 731 ms 363632 KB Output is correct
52 Correct 754 ms 363632 KB Output is correct
53 Correct 151 ms 138192 KB Output is correct
54 Correct 884 ms 364576 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 120808 KB Output is correct
2 Correct 60 ms 120808 KB Output is correct
3 Correct 63 ms 120788 KB Output is correct
4 Correct 63 ms 120792 KB Output is correct
5 Correct 96 ms 120944 KB Output is correct
6 Correct 66 ms 121356 KB Output is correct
7 Correct 65 ms 121216 KB Output is correct
8 Correct 62 ms 120780 KB Output is correct
9 Correct 63 ms 120728 KB Output is correct
10 Correct 63 ms 120944 KB Output is correct
11 Correct 61 ms 120748 KB Output is correct
12 Correct 67 ms 121284 KB Output is correct
13 Correct 169 ms 121604 KB Output is correct
14 Correct 273 ms 121916 KB Output is correct
15 Correct 210 ms 121680 KB Output is correct
16 Correct 830 ms 365744 KB Output is correct
17 Correct 847 ms 372112 KB Output is correct
18 Correct 1056 ms 381240 KB Output is correct
19 Correct 912 ms 366992 KB Output is correct
20 Correct 917 ms 367060 KB Output is correct
21 Correct 948 ms 366896 KB Output is correct
22 Correct 788 ms 372144 KB Output is correct
23 Correct 789 ms 371696 KB Output is correct
24 Correct 1038 ms 371592 KB Output is correct
25 Correct 1000 ms 372060 KB Output is correct
26 Correct 1035 ms 371868 KB Output is correct
27 Correct 1497 ms 375128 KB Output is correct
28 Correct 888 ms 375328 KB Output is correct
29 Correct 859 ms 375228 KB Output is correct
30 Correct 1147 ms 369608 KB Output is correct
31 Correct 777 ms 369592 KB Output is correct
32 Correct 1079 ms 369356 KB Output is correct
33 Correct 767 ms 369364 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 60 ms 120716 KB Output is correct
2 Correct 61 ms 120752 KB Output is correct
3 Correct 61 ms 120780 KB Output is correct
4 Correct 146 ms 121272 KB Output is correct
5 Correct 238 ms 121844 KB Output is correct
6 Correct 67 ms 121292 KB Output is correct
7 Correct 67 ms 121228 KB Output is correct
8 Correct 71 ms 121252 KB Output is correct
9 Correct 365 ms 134228 KB Output is correct
10 Correct 728 ms 362648 KB Output is correct
11 Correct 92 ms 120976 KB Output is correct
12 Correct 244 ms 121896 KB Output is correct
13 Correct 886 ms 368384 KB Output is correct
14 Correct 651 ms 368584 KB Output is correct
15 Correct 887 ms 369692 KB Output is correct
16 Correct 1301 ms 382072 KB Output is correct
17 Correct 711 ms 368400 KB Output is correct
18 Correct 739 ms 372420 KB Output is correct
19 Correct 736 ms 368364 KB Output is correct
20 Correct 656 ms 368428 KB Output is correct
21 Correct 686 ms 370688 KB Output is correct
22 Correct 633 ms 367484 KB Output is correct
23 Correct 64 ms 120808 KB Output is correct
24 Correct 61 ms 120768 KB Output is correct
25 Correct 71 ms 121256 KB Output is correct
26 Correct 75 ms 121348 KB Output is correct
27 Correct 69 ms 121232 KB Output is correct
28 Correct 72 ms 121348 KB Output is correct
29 Correct 71 ms 121348 KB Output is correct
30 Correct 77 ms 121336 KB Output is correct
31 Correct 80 ms 121240 KB Output is correct
32 Correct 72 ms 121292 KB Output is correct
33 Correct 72 ms 121324 KB Output is correct
34 Correct 67 ms 121260 KB Output is correct
35 Correct 73 ms 121316 KB Output is correct
36 Correct 59 ms 120700 KB Output is correct
37 Correct 66 ms 120856 KB Output is correct
38 Correct 69 ms 121268 KB Output is correct
39 Correct 69 ms 121336 KB Output is correct
40 Correct 69 ms 121336 KB Output is correct
41 Correct 59 ms 120796 KB Output is correct
42 Correct 76 ms 121312 KB Output is correct
43 Correct 68 ms 121216 KB Output is correct
44 Correct 73 ms 121288 KB Output is correct
45 Correct 59 ms 120732 KB Output is correct
46 Correct 64 ms 121048 KB Output is correct
47 Correct 69 ms 121328 KB Output is correct
48 Correct 63 ms 121184 KB Output is correct
49 Correct 70 ms 121252 KB Output is correct
50 Correct 59 ms 120832 KB Output is correct
51 Correct 70 ms 121292 KB Output is correct
52 Correct 69 ms 121308 KB Output is correct
53 Correct 74 ms 121336 KB Output is correct
54 Correct 68 ms 121360 KB Output is correct
55 Correct 71 ms 121332 KB Output is correct
56 Correct 67 ms 121236 KB Output is correct
57 Correct 66 ms 121204 KB Output is correct
58 Correct 71 ms 121248 KB Output is correct
59 Correct 66 ms 121168 KB Output is correct
60 Correct 359 ms 134404 KB Output is correct
61 Correct 698 ms 362744 KB Output is correct
62 Correct 401 ms 135744 KB Output is correct
63 Correct 450 ms 134660 KB Output is correct
64 Correct 319 ms 135232 KB Output is correct
65 Correct 401 ms 135416 KB Output is correct
66 Correct 104 ms 123216 KB Output is correct
67 Correct 867 ms 363792 KB Output is correct
68 Correct 897 ms 363764 KB Output is correct
69 Correct 799 ms 360464 KB Output is correct
70 Correct 792 ms 360392 KB Output is correct
71 Correct 687 ms 364428 KB Output is correct
72 Correct 745 ms 364336 KB Output is correct
73 Correct 731 ms 363632 KB Output is correct
74 Correct 754 ms 363632 KB Output is correct
75 Correct 151 ms 138192 KB Output is correct
76 Correct 884 ms 364576 KB Output is correct
77 Correct 64 ms 120808 KB Output is correct
78 Correct 60 ms 120808 KB Output is correct
79 Correct 63 ms 120788 KB Output is correct
80 Correct 63 ms 120792 KB Output is correct
81 Correct 96 ms 120944 KB Output is correct
82 Correct 66 ms 121356 KB Output is correct
83 Correct 65 ms 121216 KB Output is correct
84 Correct 62 ms 120780 KB Output is correct
85 Correct 63 ms 120728 KB Output is correct
86 Correct 63 ms 120944 KB Output is correct
87 Correct 61 ms 120748 KB Output is correct
88 Correct 67 ms 121284 KB Output is correct
89 Correct 169 ms 121604 KB Output is correct
90 Correct 273 ms 121916 KB Output is correct
91 Correct 210 ms 121680 KB Output is correct
92 Correct 830 ms 365744 KB Output is correct
93 Correct 847 ms 372112 KB Output is correct
94 Correct 1056 ms 381240 KB Output is correct
95 Correct 912 ms 366992 KB Output is correct
96 Correct 917 ms 367060 KB Output is correct
97 Correct 948 ms 366896 KB Output is correct
98 Correct 788 ms 372144 KB Output is correct
99 Correct 789 ms 371696 KB Output is correct
100 Correct 1038 ms 371592 KB Output is correct
101 Correct 1000 ms 372060 KB Output is correct
102 Correct 1035 ms 371868 KB Output is correct
103 Correct 1497 ms 375128 KB Output is correct
104 Correct 888 ms 375328 KB Output is correct
105 Correct 859 ms 375228 KB Output is correct
106 Correct 1147 ms 369608 KB Output is correct
107 Correct 777 ms 369592 KB Output is correct
108 Correct 1079 ms 369356 KB Output is correct
109 Correct 767 ms 369364 KB Output is correct
110 Correct 256 ms 122192 KB Output is correct
111 Correct 213 ms 121768 KB Output is correct
112 Correct 1004 ms 374156 KB Output is correct
113 Correct 1103 ms 364736 KB Output is correct
114 Correct 820 ms 373124 KB Output is correct
115 Correct 554 ms 364980 KB Output is correct
116 Correct 1159 ms 370444 KB Output is correct
117 Correct 1237 ms 383672 KB Output is correct
118 Correct 866 ms 364624 KB Output is correct
119 Correct 843 ms 364684 KB Output is correct
120 Correct 138 ms 141580 KB Output is correct
121 Correct 1289 ms 371096 KB Output is correct
122 Correct 1445 ms 371024 KB Output is correct
123 Correct 1136 ms 364876 KB Output is correct
124 Correct 793 ms 364812 KB Output is correct
125 Correct 1269 ms 365500 KB Output is correct
126 Correct 1424 ms 386112 KB Output is correct
127 Correct 1014 ms 374676 KB Output is correct
128 Correct 898 ms 374048 KB Output is correct
129 Correct 1064 ms 374204 KB Output is correct
130 Correct 896 ms 374196 KB Output is correct