Submission #567495

# Submission time Handle Problem Language Result Execution time Memory
567495 2022-05-23T15:26:19 Z maomao90 Jail (JOI22_jail) C++17
100 / 100
2290 ms 641032 KB
// Hallelujah, praise the one who set me free
// Hallelujah, death has lost its grip on me
// You have broken every chain, There's salvation in your name
// Jesus Christ, my living hope
#include <bits/stdc++.h> 
using namespace std;

template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define SZ(_a) (int) _a.size()
#define pb push_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;
typedef vector<iii> viii;

#ifndef DEBUG
#define cerr if (0) cerr
#endif

const int INF = 1000000005;
const ll LINF = 1000000000000000005ll;
const int MAXN = 200005;
const int MAXL = 20;

int q;
int n, m;
vi adj[MAXN], radj[MAXN * 80];
int s[MAXN], t[MAXN];
int mps[MAXN], mpt[MAXN];

int p[MAXL][MAXN], id[MAXL][MAXN], ptr;
int lvl[MAXN];
void dfs(int u, int cp) {
    p[0][u] = cp;
    REP (k, 1, MAXL) {
        if (p[k - 1][u] == -1) {
            p[k][u] = -1;
            id[k][u] = -1;
        } else {
            p[k][u] = p[k - 1][p[k - 1][u]];
            id[k][u] = ptr;
            radj[id[k - 1][u]].pb(id[k][u]);
            radj[id[k][u] ^ 1].pb(id[k - 1][u] ^ 1);
            if (id[k - 1][p[k - 1][u]] != -1) {
                radj[id[k - 1][p[k - 1][u]]].pb(id[k][u]);
                radj[id[k][u] ^ 1].pb(id[k - 1][p[k - 1][u]] ^ 1);
            }
            ptr += 2;
        }
    }
    for (int v : adj[u]) {
        if (v == cp) continue;
        lvl[v] = lvl[u] + 1;
        dfs(v, u);
    }
}

int vis[MAXN * 80];
bool cyc;
void rdfs(int u) {
    vis[u] = 1;
    for (int v : radj[u]) {
        if (vis[v] == 1) {
            cyc = 1;
        } else if (!vis[v]) {
            rdfs(v);
        }
    }
    vis[u] = 2;
}

int main() {
#ifndef DEBUG
    ios::sync_with_stdio(0), cin.tie(0);
#endif
    cin >> q;
    while (q--) {
        cin >> n;
        REP (i, 0, n + 1) {
            adj[i].clear();
            mps[i] = 0, mpt[i] = 0;
            REP (j, 0, MAXL) {
                id[j][i] = -1;
                p[j][i] = -1;
            }
        }
        REP (i, 1, n) {
            int a, b; cin >> a >> b;
            adj[a].pb(b);
            adj[b].pb(a);
        }
        cin >> m;
        ptr = m;
        if (ptr & 1) {
            ptr++;
        }
        REP (i, 1, n + 1) {
            id[0][i] = ptr;
            ptr += 2;
        }
        REP (i, 0, m) {
            cin >> s[i] >> t[i];
            mps[s[i]] = i, mpt[t[i]] = i;
            radj[i].pb(id[0][s[i]]);
            radj[id[0][t[i]] ^ 1].pb(i);
        }
        dfs(1, -1);
        REP (i, 0, m) {
            int u = s[i], v = t[i];
            if (mpt[u] != 0) {
                radj[i].pb(mpt[u]);
            }
            if (mps[v] != 0) {
                radj[mps[v]].pb(i);
            }
            if (lvl[u] < lvl[v]) {
                swap(u, v);
            }
            auto jump = [&] (int &u, int k) {
                radj[id[k][u]].pb(i);
                radj[i].pb(id[k][u] ^ 1);
                u = p[k][u];
            };
            u = p[0][u];
            RREP (k, MAXL - 1, 0) {
                if (p[k][u] == -1) continue;
                if (lvl[p[k][u]] >= lvl[v]) {
                    jump(u, k);
                }
            }
            bool hasj = 0;
            if (lvl[u] < lvl[v]) {
                hasj = 1;
                v = p[0][v];
            }
            if (u == v) {
                if (hasj) {
                    jump(u, 0);
                }
                continue;
            }
            if (hasj) {
                jump(v, 0);
            } else {
                v = p[0][v];
            }
            jump(u, 0);
            RREP (k, MAXL - 1, 0) {
                if (p[k][u] != p[k][v]) {
                    jump(u, k);
                    jump(v, k);
                }
            }
            if (u != v) {
                jump(u, 0); jump(v, 0);
            }
            assert(u == v);
            jump(u, 0);
        }
        cyc = 0;
        REP (i, 0, m) {
            if (vis[i]) continue;
            rdfs(i);
            if (cyc) {
                break;
            }
        }
        if (cyc) {
            cout << "No\n";
        } else {
            cout << "Yes\n";
        }
        REP (i, 0, ptr) {
            radj[i].clear();
            vis[i] = 0;
        }
    }
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 208 ms 380960 KB Output is correct
2 Correct 184 ms 380896 KB Output is correct
3 Correct 186 ms 380988 KB Output is correct
4 Correct 211 ms 381072 KB Output is correct
5 Correct 225 ms 381088 KB Output is correct
6 Correct 191 ms 381092 KB Output is correct
7 Correct 189 ms 381144 KB Output is correct
8 Correct 208 ms 381224 KB Output is correct
9 Correct 417 ms 388100 KB Output is correct
10 Correct 1035 ms 553376 KB Output is correct
11 Correct 198 ms 381016 KB Output is correct
12 Correct 250 ms 381104 KB Output is correct
13 Correct 1243 ms 558116 KB Output is correct
14 Correct 1194 ms 558280 KB Output is correct
15 Correct 1769 ms 584476 KB Output is correct
16 Correct 2290 ms 641032 KB Output is correct
17 Correct 1408 ms 562444 KB Output is correct
18 Correct 1261 ms 562304 KB Output is correct
19 Correct 1394 ms 562388 KB Output is correct
20 Correct 1233 ms 562444 KB Output is correct
21 Correct 1473 ms 585064 KB Output is correct
22 Correct 1085 ms 557768 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 181 ms 380992 KB Output is correct
2 Correct 190 ms 380900 KB Output is correct
3 Correct 190 ms 381120 KB Output is correct
4 Correct 202 ms 381164 KB Output is correct
5 Correct 187 ms 381156 KB Output is correct
6 Correct 186 ms 381048 KB Output is correct
7 Correct 190 ms 381132 KB Output is correct
8 Correct 185 ms 381168 KB Output is correct
9 Correct 193 ms 381052 KB Output is correct
10 Correct 191 ms 381072 KB Output is correct
11 Correct 185 ms 381148 KB Output is correct
12 Correct 199 ms 381032 KB Output is correct
13 Correct 185 ms 381040 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 181 ms 380992 KB Output is correct
2 Correct 190 ms 380900 KB Output is correct
3 Correct 190 ms 381120 KB Output is correct
4 Correct 202 ms 381164 KB Output is correct
5 Correct 187 ms 381156 KB Output is correct
6 Correct 186 ms 381048 KB Output is correct
7 Correct 190 ms 381132 KB Output is correct
8 Correct 185 ms 381168 KB Output is correct
9 Correct 193 ms 381052 KB Output is correct
10 Correct 191 ms 381072 KB Output is correct
11 Correct 185 ms 381148 KB Output is correct
12 Correct 199 ms 381032 KB Output is correct
13 Correct 185 ms 381040 KB Output is correct
14 Correct 194 ms 381008 KB Output is correct
15 Correct 208 ms 381000 KB Output is correct
16 Correct 182 ms 381156 KB Output is correct
17 Correct 190 ms 381152 KB Output is correct
18 Correct 202 ms 381168 KB Output is correct
19 Correct 183 ms 380896 KB Output is correct
20 Correct 185 ms 381228 KB Output is correct
21 Correct 198 ms 381144 KB Output is correct
22 Correct 192 ms 381180 KB Output is correct
23 Correct 184 ms 380880 KB Output is correct
24 Correct 180 ms 380964 KB Output is correct
25 Correct 194 ms 381136 KB Output is correct
26 Correct 193 ms 381052 KB Output is correct
27 Correct 182 ms 381232 KB Output is correct
28 Correct 194 ms 380932 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 181 ms 380992 KB Output is correct
2 Correct 190 ms 380900 KB Output is correct
3 Correct 190 ms 381120 KB Output is correct
4 Correct 202 ms 381164 KB Output is correct
5 Correct 187 ms 381156 KB Output is correct
6 Correct 186 ms 381048 KB Output is correct
7 Correct 190 ms 381132 KB Output is correct
8 Correct 185 ms 381168 KB Output is correct
9 Correct 193 ms 381052 KB Output is correct
10 Correct 191 ms 381072 KB Output is correct
11 Correct 185 ms 381148 KB Output is correct
12 Correct 199 ms 381032 KB Output is correct
13 Correct 185 ms 381040 KB Output is correct
14 Correct 194 ms 381008 KB Output is correct
15 Correct 208 ms 381000 KB Output is correct
16 Correct 182 ms 381156 KB Output is correct
17 Correct 190 ms 381152 KB Output is correct
18 Correct 202 ms 381168 KB Output is correct
19 Correct 183 ms 380896 KB Output is correct
20 Correct 185 ms 381228 KB Output is correct
21 Correct 198 ms 381144 KB Output is correct
22 Correct 192 ms 381180 KB Output is correct
23 Correct 184 ms 380880 KB Output is correct
24 Correct 180 ms 380964 KB Output is correct
25 Correct 194 ms 381136 KB Output is correct
26 Correct 193 ms 381052 KB Output is correct
27 Correct 182 ms 381232 KB Output is correct
28 Correct 194 ms 380932 KB Output is correct
29 Correct 195 ms 381152 KB Output is correct
30 Correct 188 ms 381252 KB Output is correct
31 Correct 185 ms 381252 KB Output is correct
32 Correct 185 ms 381208 KB Output is correct
33 Correct 209 ms 381276 KB Output is correct
34 Correct 182 ms 381108 KB Output is correct
35 Correct 191 ms 381220 KB Output is correct
36 Correct 191 ms 381120 KB Output is correct
37 Correct 185 ms 381172 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 181 ms 380992 KB Output is correct
2 Correct 190 ms 380900 KB Output is correct
3 Correct 190 ms 381120 KB Output is correct
4 Correct 202 ms 381164 KB Output is correct
5 Correct 187 ms 381156 KB Output is correct
6 Correct 186 ms 381048 KB Output is correct
7 Correct 190 ms 381132 KB Output is correct
8 Correct 185 ms 381168 KB Output is correct
9 Correct 193 ms 381052 KB Output is correct
10 Correct 191 ms 381072 KB Output is correct
11 Correct 185 ms 381148 KB Output is correct
12 Correct 199 ms 381032 KB Output is correct
13 Correct 185 ms 381040 KB Output is correct
14 Correct 194 ms 381008 KB Output is correct
15 Correct 208 ms 381000 KB Output is correct
16 Correct 182 ms 381156 KB Output is correct
17 Correct 190 ms 381152 KB Output is correct
18 Correct 202 ms 381168 KB Output is correct
19 Correct 183 ms 380896 KB Output is correct
20 Correct 185 ms 381228 KB Output is correct
21 Correct 198 ms 381144 KB Output is correct
22 Correct 192 ms 381180 KB Output is correct
23 Correct 184 ms 380880 KB Output is correct
24 Correct 180 ms 380964 KB Output is correct
25 Correct 194 ms 381136 KB Output is correct
26 Correct 193 ms 381052 KB Output is correct
27 Correct 182 ms 381232 KB Output is correct
28 Correct 194 ms 380932 KB Output is correct
29 Correct 195 ms 381152 KB Output is correct
30 Correct 188 ms 381252 KB Output is correct
31 Correct 185 ms 381252 KB Output is correct
32 Correct 185 ms 381208 KB Output is correct
33 Correct 209 ms 381276 KB Output is correct
34 Correct 182 ms 381108 KB Output is correct
35 Correct 191 ms 381220 KB Output is correct
36 Correct 191 ms 381120 KB Output is correct
37 Correct 185 ms 381172 KB Output is correct
38 Correct 448 ms 388716 KB Output is correct
39 Correct 1012 ms 554532 KB Output is correct
40 Correct 325 ms 389716 KB Output is correct
41 Correct 286 ms 386364 KB Output is correct
42 Correct 250 ms 387392 KB Output is correct
43 Correct 386 ms 389232 KB Output is correct
44 Correct 212 ms 382124 KB Output is correct
45 Correct 523 ms 462596 KB Output is correct
46 Correct 503 ms 462892 KB Output is correct
47 Correct 810 ms 539988 KB Output is correct
48 Correct 821 ms 540100 KB Output is correct
49 Correct 571 ms 500300 KB Output is correct
50 Correct 563 ms 500440 KB Output is correct
51 Correct 686 ms 525620 KB Output is correct
52 Correct 656 ms 526596 KB Output is correct
53 Correct 214 ms 386440 KB Output is correct
54 Correct 501 ms 453724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 211 ms 380948 KB Output is correct
2 Correct 189 ms 380872 KB Output is correct
3 Correct 190 ms 380992 KB Output is correct
4 Correct 189 ms 380920 KB Output is correct
5 Correct 223 ms 381012 KB Output is correct
6 Correct 180 ms 381092 KB Output is correct
7 Correct 189 ms 381108 KB Output is correct
8 Correct 189 ms 380964 KB Output is correct
9 Correct 191 ms 380996 KB Output is correct
10 Correct 201 ms 381000 KB Output is correct
11 Correct 203 ms 381096 KB Output is correct
12 Correct 197 ms 381168 KB Output is correct
13 Correct 212 ms 381524 KB Output is correct
14 Correct 221 ms 381888 KB Output is correct
15 Correct 246 ms 381780 KB Output is correct
16 Correct 436 ms 442316 KB Output is correct
17 Correct 699 ms 454952 KB Output is correct
18 Correct 954 ms 474896 KB Output is correct
19 Correct 532 ms 444680 KB Output is correct
20 Correct 490 ms 442624 KB Output is correct
21 Correct 584 ms 443052 KB Output is correct
22 Correct 648 ms 453264 KB Output is correct
23 Correct 546 ms 452020 KB Output is correct
24 Correct 575 ms 450532 KB Output is correct
25 Correct 558 ms 451460 KB Output is correct
26 Correct 559 ms 452848 KB Output is correct
27 Correct 521 ms 435472 KB Output is correct
28 Correct 524 ms 440000 KB Output is correct
29 Correct 518 ms 437948 KB Output is correct
30 Correct 553 ms 434820 KB Output is correct
31 Correct 546 ms 435364 KB Output is correct
32 Correct 495 ms 433176 KB Output is correct
33 Correct 514 ms 434988 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 208 ms 380960 KB Output is correct
2 Correct 184 ms 380896 KB Output is correct
3 Correct 186 ms 380988 KB Output is correct
4 Correct 211 ms 381072 KB Output is correct
5 Correct 225 ms 381088 KB Output is correct
6 Correct 191 ms 381092 KB Output is correct
7 Correct 189 ms 381144 KB Output is correct
8 Correct 208 ms 381224 KB Output is correct
9 Correct 417 ms 388100 KB Output is correct
10 Correct 1035 ms 553376 KB Output is correct
11 Correct 198 ms 381016 KB Output is correct
12 Correct 250 ms 381104 KB Output is correct
13 Correct 1243 ms 558116 KB Output is correct
14 Correct 1194 ms 558280 KB Output is correct
15 Correct 1769 ms 584476 KB Output is correct
16 Correct 2290 ms 641032 KB Output is correct
17 Correct 1408 ms 562444 KB Output is correct
18 Correct 1261 ms 562304 KB Output is correct
19 Correct 1394 ms 562388 KB Output is correct
20 Correct 1233 ms 562444 KB Output is correct
21 Correct 1473 ms 585064 KB Output is correct
22 Correct 1085 ms 557768 KB Output is correct
23 Correct 181 ms 380992 KB Output is correct
24 Correct 190 ms 380900 KB Output is correct
25 Correct 190 ms 381120 KB Output is correct
26 Correct 202 ms 381164 KB Output is correct
27 Correct 187 ms 381156 KB Output is correct
28 Correct 186 ms 381048 KB Output is correct
29 Correct 190 ms 381132 KB Output is correct
30 Correct 185 ms 381168 KB Output is correct
31 Correct 193 ms 381052 KB Output is correct
32 Correct 191 ms 381072 KB Output is correct
33 Correct 185 ms 381148 KB Output is correct
34 Correct 199 ms 381032 KB Output is correct
35 Correct 185 ms 381040 KB Output is correct
36 Correct 194 ms 381008 KB Output is correct
37 Correct 208 ms 381000 KB Output is correct
38 Correct 182 ms 381156 KB Output is correct
39 Correct 190 ms 381152 KB Output is correct
40 Correct 202 ms 381168 KB Output is correct
41 Correct 183 ms 380896 KB Output is correct
42 Correct 185 ms 381228 KB Output is correct
43 Correct 198 ms 381144 KB Output is correct
44 Correct 192 ms 381180 KB Output is correct
45 Correct 184 ms 380880 KB Output is correct
46 Correct 180 ms 380964 KB Output is correct
47 Correct 194 ms 381136 KB Output is correct
48 Correct 193 ms 381052 KB Output is correct
49 Correct 182 ms 381232 KB Output is correct
50 Correct 194 ms 380932 KB Output is correct
51 Correct 195 ms 381152 KB Output is correct
52 Correct 188 ms 381252 KB Output is correct
53 Correct 185 ms 381252 KB Output is correct
54 Correct 185 ms 381208 KB Output is correct
55 Correct 209 ms 381276 KB Output is correct
56 Correct 182 ms 381108 KB Output is correct
57 Correct 191 ms 381220 KB Output is correct
58 Correct 191 ms 381120 KB Output is correct
59 Correct 185 ms 381172 KB Output is correct
60 Correct 448 ms 388716 KB Output is correct
61 Correct 1012 ms 554532 KB Output is correct
62 Correct 325 ms 389716 KB Output is correct
63 Correct 286 ms 386364 KB Output is correct
64 Correct 250 ms 387392 KB Output is correct
65 Correct 386 ms 389232 KB Output is correct
66 Correct 212 ms 382124 KB Output is correct
67 Correct 523 ms 462596 KB Output is correct
68 Correct 503 ms 462892 KB Output is correct
69 Correct 810 ms 539988 KB Output is correct
70 Correct 821 ms 540100 KB Output is correct
71 Correct 571 ms 500300 KB Output is correct
72 Correct 563 ms 500440 KB Output is correct
73 Correct 686 ms 525620 KB Output is correct
74 Correct 656 ms 526596 KB Output is correct
75 Correct 214 ms 386440 KB Output is correct
76 Correct 501 ms 453724 KB Output is correct
77 Correct 211 ms 380948 KB Output is correct
78 Correct 189 ms 380872 KB Output is correct
79 Correct 190 ms 380992 KB Output is correct
80 Correct 189 ms 380920 KB Output is correct
81 Correct 223 ms 381012 KB Output is correct
82 Correct 180 ms 381092 KB Output is correct
83 Correct 189 ms 381108 KB Output is correct
84 Correct 189 ms 380964 KB Output is correct
85 Correct 191 ms 380996 KB Output is correct
86 Correct 201 ms 381000 KB Output is correct
87 Correct 203 ms 381096 KB Output is correct
88 Correct 197 ms 381168 KB Output is correct
89 Correct 212 ms 381524 KB Output is correct
90 Correct 221 ms 381888 KB Output is correct
91 Correct 246 ms 381780 KB Output is correct
92 Correct 436 ms 442316 KB Output is correct
93 Correct 699 ms 454952 KB Output is correct
94 Correct 954 ms 474896 KB Output is correct
95 Correct 532 ms 444680 KB Output is correct
96 Correct 490 ms 442624 KB Output is correct
97 Correct 584 ms 443052 KB Output is correct
98 Correct 648 ms 453264 KB Output is correct
99 Correct 546 ms 452020 KB Output is correct
100 Correct 575 ms 450532 KB Output is correct
101 Correct 558 ms 451460 KB Output is correct
102 Correct 559 ms 452848 KB Output is correct
103 Correct 521 ms 435472 KB Output is correct
104 Correct 524 ms 440000 KB Output is correct
105 Correct 518 ms 437948 KB Output is correct
106 Correct 553 ms 434820 KB Output is correct
107 Correct 546 ms 435364 KB Output is correct
108 Correct 495 ms 433176 KB Output is correct
109 Correct 514 ms 434988 KB Output is correct
110 Correct 275 ms 382196 KB Output is correct
111 Correct 219 ms 381632 KB Output is correct
112 Correct 1670 ms 584256 KB Output is correct
113 Correct 1053 ms 545496 KB Output is correct
114 Correct 1182 ms 515276 KB Output is correct
115 Correct 256 ms 416472 KB Output is correct
116 Correct 614 ms 456436 KB Output is correct
117 Correct 1185 ms 491760 KB Output is correct
118 Correct 508 ms 451720 KB Output is correct
119 Correct 552 ms 451636 KB Output is correct
120 Correct 206 ms 387964 KB Output is correct
121 Correct 761 ms 470780 KB Output is correct
122 Correct 845 ms 467520 KB Output is correct
123 Correct 1135 ms 550476 KB Output is correct
124 Correct 1234 ms 550436 KB Output is correct
125 Correct 1102 ms 551248 KB Output is correct
126 Correct 2126 ms 630064 KB Output is correct
127 Correct 1688 ms 589312 KB Output is correct
128 Correct 1337 ms 583236 KB Output is correct
129 Correct 1398 ms 565712 KB Output is correct
130 Correct 1399 ms 584892 KB Output is correct