Submission #966784

# Submission time Handle Problem Language Result Execution time Memory
966784 2024-04-20T10:54:56 Z LOLOLO Jail (JOI22_jail) C++17
100 / 100
1317 ms 340976 KB
#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define           f    first
#define           s    second
#define           pb    push_back
#define           ep    emplace
#define           eb    emplace_back
#define           lb    lower_bound
#define           ub    upper_bound
#define       all(x)    x.begin(), x.end()
#define      rall(x)    x.rbegin(), x.rend()
#define   uniquev(v)    sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define     mem(f,x)    memset(f , x , sizeof(f))
#define        sz(x)    (int)(x).size()
#define  __lcm(a, b)    (1ll * ((a) / __gcd((a), (b))) * (b))
#define          mxx    *max_element
#define          mnn    *min_element
#define    cntbit(x)    __builtin_popcountll(x)
#define       len(x)    (int)(x.length())

const int N = 1.25e5;
vector <int> ed[N], ed2[N * 40];
int in[N], ou[N], dfstimer = 1, p[N][20], vi[N][20], vo[N][20], dep[N], st[N], en[N], cnt = 0;
char used[N * 40];

bool check_cycle(int u) {
    if (used[u] == 1)
        return 0;

    used[u] = 1; 
    for (auto x : ed2[u]) {
        if (used[x] != 2 && !check_cycle(x))
            return 0;
    }

    used[u] = 2;
    return 1;
}

void dfs(int u, int v) {
    vi[u][0] = ++cnt;
    vo[u][0] = ++cnt;
    if (st[u]) {
        ed2[st[u]].pb(vo[u][0]);
    }

    if (en[u]) {
        ed2[vi[u][0]].pb(en[u]);
    }

    dep[u] = dep[v] + 1;
    p[u][0] = v;

    for (int i = 1; i < 20; i++) {
        p[u][i] = p[p[u][i - 1]][i - 1];
        vi[u][i] = ++cnt;
        vo[u][i] = ++cnt;
        ed2[vo[u][i - 1]].pb(vo[u][i]);
        ed2[vo[p[u][i - 1]][i - 1]].pb(vo[u][i]);
        ed2[vi[u][i]].pb(vi[u][i - 1]);
        ed2[vi[u][i]].pb(vi[p[u][i - 1]][i - 1]);
    }

    ++dfstimer;
    in[u] = dfstimer;
    for (auto x : ed[u]) {
        if (x == v)
            continue;

        dfs(x, u);
    }

    ou[u] = dfstimer;
}

bool is(int a, int b) {
    return in[a] <= in[b] && ou[a] >= in[b]; 
}

int lca(int a, int b) {
    if (is(a, b))
        return a;

    if (is(b, a))
        return b;

    for (int i = 19; i >= 0; i--)
        if (is(p[a][i], b) == 0)
            a = p[a][i];

    return p[a][0];
}

int dis(int a, int b) {
    int c = lca(a, b);
    return dep[a] + dep[b] - dep[c] * 2;
}

void addedge(int a, int k, int id) {
    for (int j = 0; j < 20; j++) {
        if (k & (1 << j)) {
            ed2[id].pb(vi[a][j]);
            ed2[vo[a][j]].pb(id);
            a = p[a][j];
        }
    }
}


string solve() {
    int n;
    cin >> n;

    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        ed[a].pb(b);
        ed[b].pb(a);
    }

    int m;
    cin >> m;

    vector <pair <int, int>> save;

    for (int i = 1; i <= m; i++) {
        int s, t;
        cin >> s >> t;
        save.pb({s, t});
        st[s] = i, en[t] = i;
    }

    cnt = m;
    dfs(1, 1);

    int id = 0;
    for (auto x : save) {
        id++;
        int s = x.f, t = x.s;

        int a = s, b = t;
        if (dep[s] > dep[t])
            swap(s, t);

        int c = lca(s, t);
        if (s == c) {
            addedge(p[t][0], dep[t] - dep[c] - 1, id);
        } else {
            addedge(p[s][0], dep[s] - dep[c], id);
            addedge(p[t][0], dep[t] - dep[c], id);
        }

        if (en[a]) {
            ed2[id].pb(en[a]);
        }

        if (st[b]) {
            ed2[st[b]].pb(id);
        }
    }

    int is = 0;
    for (int i = 1; i <= m; i++) {
        if (used[i] == 0 && check_cycle(i) == 0) {
            is = 1;
            break;
        }
    }

    for (int i = 1; i <= n; i++) {
        ed[i].clear();
        st[i] = en[i] = dep[i] = 0;
    }

    for (int i = 1; i <= cnt; i++) {
        ed2[i].clear();
        used[i] = 0;
    }

    if (is == 0)
        return "Yes";

    return "No";
}

int main() {
    ios_base::sync_with_stdio(false); 
    cin.tie(NULL);
    cout.tie(NULL);

    int t = 1;
    cin >> t;

    while (t--) {
        //solve();
        cout << solve() << "\n";
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 70 ms 130644 KB Output is correct
2 Correct 30 ms 130644 KB Output is correct
3 Correct 29 ms 130416 KB Output is correct
4 Correct 57 ms 131160 KB Output is correct
5 Correct 74 ms 131664 KB Output is correct
6 Correct 31 ms 130904 KB Output is correct
7 Correct 34 ms 130908 KB Output is correct
8 Correct 33 ms 130908 KB Output is correct
9 Correct 199 ms 142436 KB Output is correct
10 Correct 1002 ms 318304 KB Output is correct
11 Correct 38 ms 130652 KB Output is correct
12 Correct 90 ms 132024 KB Output is correct
13 Correct 1161 ms 324036 KB Output is correct
14 Correct 851 ms 323800 KB Output is correct
15 Correct 1016 ms 325908 KB Output is correct
16 Correct 1266 ms 338652 KB Output is correct
17 Correct 1198 ms 327628 KB Output is correct
18 Correct 1298 ms 328336 KB Output is correct
19 Correct 1185 ms 327816 KB Output is correct
20 Correct 996 ms 327624 KB Output is correct
21 Correct 920 ms 326860 KB Output is correct
22 Correct 819 ms 323016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 130648 KB Output is correct
2 Correct 31 ms 130648 KB Output is correct
3 Correct 31 ms 130908 KB Output is correct
4 Correct 32 ms 130908 KB Output is correct
5 Correct 32 ms 130904 KB Output is correct
6 Correct 32 ms 130900 KB Output is correct
7 Correct 32 ms 130896 KB Output is correct
8 Correct 31 ms 130896 KB Output is correct
9 Correct 31 ms 130892 KB Output is correct
10 Correct 32 ms 130908 KB Output is correct
11 Correct 32 ms 130908 KB Output is correct
12 Correct 31 ms 130980 KB Output is correct
13 Correct 30 ms 130900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 130648 KB Output is correct
2 Correct 31 ms 130648 KB Output is correct
3 Correct 31 ms 130908 KB Output is correct
4 Correct 32 ms 130908 KB Output is correct
5 Correct 32 ms 130904 KB Output is correct
6 Correct 32 ms 130900 KB Output is correct
7 Correct 32 ms 130896 KB Output is correct
8 Correct 31 ms 130896 KB Output is correct
9 Correct 31 ms 130892 KB Output is correct
10 Correct 32 ms 130908 KB Output is correct
11 Correct 32 ms 130908 KB Output is correct
12 Correct 31 ms 130980 KB Output is correct
13 Correct 30 ms 130900 KB Output is correct
14 Correct 29 ms 130576 KB Output is correct
15 Correct 31 ms 130644 KB Output is correct
16 Correct 31 ms 130908 KB Output is correct
17 Correct 32 ms 130896 KB Output is correct
18 Correct 32 ms 130896 KB Output is correct
19 Correct 29 ms 130652 KB Output is correct
20 Correct 35 ms 130908 KB Output is correct
21 Correct 33 ms 130904 KB Output is correct
22 Correct 33 ms 130980 KB Output is correct
23 Correct 29 ms 130648 KB Output is correct
24 Correct 30 ms 130652 KB Output is correct
25 Correct 31 ms 130904 KB Output is correct
26 Correct 30 ms 130908 KB Output is correct
27 Correct 33 ms 130908 KB Output is correct
28 Correct 30 ms 130536 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 130648 KB Output is correct
2 Correct 31 ms 130648 KB Output is correct
3 Correct 31 ms 130908 KB Output is correct
4 Correct 32 ms 130908 KB Output is correct
5 Correct 32 ms 130904 KB Output is correct
6 Correct 32 ms 130900 KB Output is correct
7 Correct 32 ms 130896 KB Output is correct
8 Correct 31 ms 130896 KB Output is correct
9 Correct 31 ms 130892 KB Output is correct
10 Correct 32 ms 130908 KB Output is correct
11 Correct 32 ms 130908 KB Output is correct
12 Correct 31 ms 130980 KB Output is correct
13 Correct 30 ms 130900 KB Output is correct
14 Correct 29 ms 130576 KB Output is correct
15 Correct 31 ms 130644 KB Output is correct
16 Correct 31 ms 130908 KB Output is correct
17 Correct 32 ms 130896 KB Output is correct
18 Correct 32 ms 130896 KB Output is correct
19 Correct 29 ms 130652 KB Output is correct
20 Correct 35 ms 130908 KB Output is correct
21 Correct 33 ms 130904 KB Output is correct
22 Correct 33 ms 130980 KB Output is correct
23 Correct 29 ms 130648 KB Output is correct
24 Correct 30 ms 130652 KB Output is correct
25 Correct 31 ms 130904 KB Output is correct
26 Correct 30 ms 130908 KB Output is correct
27 Correct 33 ms 130908 KB Output is correct
28 Correct 30 ms 130536 KB Output is correct
29 Correct 31 ms 130904 KB Output is correct
30 Correct 32 ms 131152 KB Output is correct
31 Correct 33 ms 131000 KB Output is correct
32 Correct 32 ms 130908 KB Output is correct
33 Correct 32 ms 130908 KB Output is correct
34 Correct 32 ms 130896 KB Output is correct
35 Correct 32 ms 130908 KB Output is correct
36 Correct 31 ms 130908 KB Output is correct
37 Correct 31 ms 130796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 130648 KB Output is correct
2 Correct 31 ms 130648 KB Output is correct
3 Correct 31 ms 130908 KB Output is correct
4 Correct 32 ms 130908 KB Output is correct
5 Correct 32 ms 130904 KB Output is correct
6 Correct 32 ms 130900 KB Output is correct
7 Correct 32 ms 130896 KB Output is correct
8 Correct 31 ms 130896 KB Output is correct
9 Correct 31 ms 130892 KB Output is correct
10 Correct 32 ms 130908 KB Output is correct
11 Correct 32 ms 130908 KB Output is correct
12 Correct 31 ms 130980 KB Output is correct
13 Correct 30 ms 130900 KB Output is correct
14 Correct 29 ms 130576 KB Output is correct
15 Correct 31 ms 130644 KB Output is correct
16 Correct 31 ms 130908 KB Output is correct
17 Correct 32 ms 130896 KB Output is correct
18 Correct 32 ms 130896 KB Output is correct
19 Correct 29 ms 130652 KB Output is correct
20 Correct 35 ms 130908 KB Output is correct
21 Correct 33 ms 130904 KB Output is correct
22 Correct 33 ms 130980 KB Output is correct
23 Correct 29 ms 130648 KB Output is correct
24 Correct 30 ms 130652 KB Output is correct
25 Correct 31 ms 130904 KB Output is correct
26 Correct 30 ms 130908 KB Output is correct
27 Correct 33 ms 130908 KB Output is correct
28 Correct 30 ms 130536 KB Output is correct
29 Correct 31 ms 130904 KB Output is correct
30 Correct 32 ms 131152 KB Output is correct
31 Correct 33 ms 131000 KB Output is correct
32 Correct 32 ms 130908 KB Output is correct
33 Correct 32 ms 130908 KB Output is correct
34 Correct 32 ms 130896 KB Output is correct
35 Correct 32 ms 130908 KB Output is correct
36 Correct 31 ms 130908 KB Output is correct
37 Correct 31 ms 130796 KB Output is correct
38 Correct 245 ms 142356 KB Output is correct
39 Correct 948 ms 318200 KB Output is correct
40 Correct 152 ms 143952 KB Output is correct
41 Correct 133 ms 143188 KB Output is correct
42 Correct 114 ms 143444 KB Output is correct
43 Correct 177 ms 143328 KB Output is correct
44 Correct 44 ms 132444 KB Output is correct
45 Correct 464 ms 313844 KB Output is correct
46 Correct 482 ms 313592 KB Output is correct
47 Correct 807 ms 313392 KB Output is correct
48 Correct 785 ms 313812 KB Output is correct
49 Correct 441 ms 314604 KB Output is correct
50 Correct 530 ms 314776 KB Output is correct
51 Correct 658 ms 314924 KB Output is correct
52 Correct 614 ms 314804 KB Output is correct
53 Correct 73 ms 144288 KB Output is correct
54 Correct 460 ms 314404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 31 ms 130640 KB Output is correct
2 Correct 30 ms 130480 KB Output is correct
3 Correct 29 ms 130904 KB Output is correct
4 Correct 28 ms 130636 KB Output is correct
5 Correct 39 ms 130572 KB Output is correct
6 Correct 30 ms 130908 KB Output is correct
7 Correct 31 ms 130908 KB Output is correct
8 Correct 29 ms 130652 KB Output is correct
9 Correct 29 ms 130648 KB Output is correct
10 Correct 30 ms 130652 KB Output is correct
11 Correct 31 ms 130460 KB Output is correct
12 Correct 34 ms 130900 KB Output is correct
13 Correct 59 ms 131388 KB Output is correct
14 Correct 83 ms 131668 KB Output is correct
15 Correct 72 ms 131472 KB Output is correct
16 Correct 442 ms 315756 KB Output is correct
17 Correct 545 ms 323332 KB Output is correct
18 Correct 715 ms 333312 KB Output is correct
19 Correct 512 ms 317348 KB Output is correct
20 Correct 478 ms 317092 KB Output is correct
21 Correct 523 ms 317220 KB Output is correct
22 Correct 620 ms 322852 KB Output is correct
23 Correct 603 ms 323024 KB Output is correct
24 Correct 656 ms 322508 KB Output is correct
25 Correct 617 ms 322792 KB Output is correct
26 Correct 644 ms 322956 KB Output is correct
27 Correct 657 ms 332952 KB Output is correct
28 Correct 722 ms 337224 KB Output is correct
29 Correct 578 ms 334144 KB Output is correct
30 Correct 611 ms 326988 KB Output is correct
31 Correct 624 ms 328472 KB Output is correct
32 Correct 707 ms 324424 KB Output is correct
33 Correct 651 ms 328432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 70 ms 130644 KB Output is correct
2 Correct 30 ms 130644 KB Output is correct
3 Correct 29 ms 130416 KB Output is correct
4 Correct 57 ms 131160 KB Output is correct
5 Correct 74 ms 131664 KB Output is correct
6 Correct 31 ms 130904 KB Output is correct
7 Correct 34 ms 130908 KB Output is correct
8 Correct 33 ms 130908 KB Output is correct
9 Correct 199 ms 142436 KB Output is correct
10 Correct 1002 ms 318304 KB Output is correct
11 Correct 38 ms 130652 KB Output is correct
12 Correct 90 ms 132024 KB Output is correct
13 Correct 1161 ms 324036 KB Output is correct
14 Correct 851 ms 323800 KB Output is correct
15 Correct 1016 ms 325908 KB Output is correct
16 Correct 1266 ms 338652 KB Output is correct
17 Correct 1198 ms 327628 KB Output is correct
18 Correct 1298 ms 328336 KB Output is correct
19 Correct 1185 ms 327816 KB Output is correct
20 Correct 996 ms 327624 KB Output is correct
21 Correct 920 ms 326860 KB Output is correct
22 Correct 819 ms 323016 KB Output is correct
23 Correct 29 ms 130648 KB Output is correct
24 Correct 31 ms 130648 KB Output is correct
25 Correct 31 ms 130908 KB Output is correct
26 Correct 32 ms 130908 KB Output is correct
27 Correct 32 ms 130904 KB Output is correct
28 Correct 32 ms 130900 KB Output is correct
29 Correct 32 ms 130896 KB Output is correct
30 Correct 31 ms 130896 KB Output is correct
31 Correct 31 ms 130892 KB Output is correct
32 Correct 32 ms 130908 KB Output is correct
33 Correct 32 ms 130908 KB Output is correct
34 Correct 31 ms 130980 KB Output is correct
35 Correct 30 ms 130900 KB Output is correct
36 Correct 29 ms 130576 KB Output is correct
37 Correct 31 ms 130644 KB Output is correct
38 Correct 31 ms 130908 KB Output is correct
39 Correct 32 ms 130896 KB Output is correct
40 Correct 32 ms 130896 KB Output is correct
41 Correct 29 ms 130652 KB Output is correct
42 Correct 35 ms 130908 KB Output is correct
43 Correct 33 ms 130904 KB Output is correct
44 Correct 33 ms 130980 KB Output is correct
45 Correct 29 ms 130648 KB Output is correct
46 Correct 30 ms 130652 KB Output is correct
47 Correct 31 ms 130904 KB Output is correct
48 Correct 30 ms 130908 KB Output is correct
49 Correct 33 ms 130908 KB Output is correct
50 Correct 30 ms 130536 KB Output is correct
51 Correct 31 ms 130904 KB Output is correct
52 Correct 32 ms 131152 KB Output is correct
53 Correct 33 ms 131000 KB Output is correct
54 Correct 32 ms 130908 KB Output is correct
55 Correct 32 ms 130908 KB Output is correct
56 Correct 32 ms 130896 KB Output is correct
57 Correct 32 ms 130908 KB Output is correct
58 Correct 31 ms 130908 KB Output is correct
59 Correct 31 ms 130796 KB Output is correct
60 Correct 245 ms 142356 KB Output is correct
61 Correct 948 ms 318200 KB Output is correct
62 Correct 152 ms 143952 KB Output is correct
63 Correct 133 ms 143188 KB Output is correct
64 Correct 114 ms 143444 KB Output is correct
65 Correct 177 ms 143328 KB Output is correct
66 Correct 44 ms 132444 KB Output is correct
67 Correct 464 ms 313844 KB Output is correct
68 Correct 482 ms 313592 KB Output is correct
69 Correct 807 ms 313392 KB Output is correct
70 Correct 785 ms 313812 KB Output is correct
71 Correct 441 ms 314604 KB Output is correct
72 Correct 530 ms 314776 KB Output is correct
73 Correct 658 ms 314924 KB Output is correct
74 Correct 614 ms 314804 KB Output is correct
75 Correct 73 ms 144288 KB Output is correct
76 Correct 460 ms 314404 KB Output is correct
77 Correct 31 ms 130640 KB Output is correct
78 Correct 30 ms 130480 KB Output is correct
79 Correct 29 ms 130904 KB Output is correct
80 Correct 28 ms 130636 KB Output is correct
81 Correct 39 ms 130572 KB Output is correct
82 Correct 30 ms 130908 KB Output is correct
83 Correct 31 ms 130908 KB Output is correct
84 Correct 29 ms 130652 KB Output is correct
85 Correct 29 ms 130648 KB Output is correct
86 Correct 30 ms 130652 KB Output is correct
87 Correct 31 ms 130460 KB Output is correct
88 Correct 34 ms 130900 KB Output is correct
89 Correct 59 ms 131388 KB Output is correct
90 Correct 83 ms 131668 KB Output is correct
91 Correct 72 ms 131472 KB Output is correct
92 Correct 442 ms 315756 KB Output is correct
93 Correct 545 ms 323332 KB Output is correct
94 Correct 715 ms 333312 KB Output is correct
95 Correct 512 ms 317348 KB Output is correct
96 Correct 478 ms 317092 KB Output is correct
97 Correct 523 ms 317220 KB Output is correct
98 Correct 620 ms 322852 KB Output is correct
99 Correct 603 ms 323024 KB Output is correct
100 Correct 656 ms 322508 KB Output is correct
101 Correct 617 ms 322792 KB Output is correct
102 Correct 644 ms 322956 KB Output is correct
103 Correct 657 ms 332952 KB Output is correct
104 Correct 722 ms 337224 KB Output is correct
105 Correct 578 ms 334144 KB Output is correct
106 Correct 611 ms 326988 KB Output is correct
107 Correct 624 ms 328472 KB Output is correct
108 Correct 707 ms 324424 KB Output is correct
109 Correct 651 ms 328432 KB Output is correct
110 Correct 81 ms 132076 KB Output is correct
111 Correct 71 ms 131588 KB Output is correct
112 Correct 975 ms 328076 KB Output is correct
113 Correct 956 ms 318144 KB Output is correct
114 Correct 651 ms 326836 KB Output is correct
115 Correct 362 ms 315124 KB Output is correct
116 Correct 627 ms 321516 KB Output is correct
117 Correct 823 ms 335812 KB Output is correct
118 Correct 451 ms 314480 KB Output is correct
119 Correct 450 ms 314220 KB Output is correct
120 Correct 70 ms 146768 KB Output is correct
121 Correct 704 ms 321484 KB Output is correct
122 Correct 674 ms 321492 KB Output is correct
123 Correct 1094 ms 319488 KB Output is correct
124 Correct 885 ms 319248 KB Output is correct
125 Correct 1040 ms 320088 KB Output is correct
126 Correct 1317 ms 340976 KB Output is correct
127 Correct 902 ms 328212 KB Output is correct
128 Correct 795 ms 328288 KB Output is correct
129 Correct 945 ms 328932 KB Output is correct
130 Correct 827 ms 328236 KB Output is correct