Submission #994219

# Submission time Handle Problem Language Result Execution time Memory
994219 2024-06-07T09:18:25 Z vjudge1 Jail (JOI22_jail) C++14
72 / 100
4096 ms 1048576 KB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define se second

const int maxn = 120'000;
const int logn = 17;

int n, m;
vector<int> graph[1 + maxn];
int lift[1 + maxn][logn];
int dep[1 + maxn];
vector<int> dag[1 + maxn];
bool vis_dag[1 + maxn];
vector<int> top;
int pos[1 + maxn];
int in[1 + maxn];
int out[1 + maxn];
int timer;
int by_start[1 + maxn];
int by_end[1 + maxn];

void dfs_lift(int cur) {
    timer++;
    in[cur] = timer;
    for(int nei : graph[cur]) {
        if(nei != lift[cur][0]) {
            lift[nei][0] = cur;
            dep[nei] = dep[cur] + 1;
            dfs_lift(nei);
        }
    }
    timer++;
    out[cur] = timer;
}

int lca(int a, int b) {
    if(dep[a] > dep[b]) {
        swap(a, b);
    }
    for(int j = logn - 1; j >= 0; j--) {
        if(dep[b] - (1 << j) >= dep[a]) {
            b = lift[b][j];
        }
    }
    if(a == b) {
        return a;
    }
    for(int j = logn - 1; j >= 0; j--) {
        if(lift[a][j] != lift[b][j]) {
            a = lift[a][j];
            b = lift[b][j];
        }
    }
    return lift[b][0];
}

void add_edge(int a, int b) {
    dag[a].pb(b);
}

void dfs_top(int cur) {
    vis_dag[cur] = true;
    for(int nei : dag[cur]) {
        if(!vis_dag[nei]) {
            dfs_top(nei);
        }
    }
    top.pb(cur);
}

#define ancestor(a, b) (in[a] <= in[b] && out[a] >= out[b])

bool on_path(int a, int b, int c) {
    int ab = lca(a, b);
    return ancestor(ab, c) && (ancestor(c, a) || ancestor(c, b));
}

void add(int other_idx, int node) {
    if(by_start[node] != 0 && by_start[node] != other_idx) {
        add_edge(by_start[node], other_idx);
    }
    if(by_end[node] != 0 && by_end[node] != other_idx) {
        add_edge(other_idx, by_end[node]);
    }
}

bool solve() {
    cin >> n;
    for(int i = 1; i <= n - 1; i++) {
        int a, b;
        cin >> a >> b;
        graph[a].pb(b);
        graph[b].pb(a);
    }
    dfs_lift(1);
    lift[1][0] = 1;
    for(int j = 1; j < logn; j++) {
        for(int i = 1; i <= n; i++) {
            lift[i][j] = lift[lift[i][j - 1]][j - 1];
        }
    }
    cin >> m;
    vector<pii > pairs;
    for(int i = 1; i <= m; i++) {
        int a, b;
        cin >> a >> b;
        pairs.pb(mp(a, b));
        by_start[a] = i;
        by_end[b] = i;
    }
    for(int i = 0; i < m; i++) {
        int a = pairs[i].fi, b = pairs[i].se;
        int ab = lca(a, b);
        while(a != ab) {
            add(i + 1, a);
            a = lift[a][0];
        }
        while(b != ab) {
            add(i + 1, b);
            b = lift[b][0];
        }
        add(i + 1, ab);
    }
    for(int i = 1; i <= m; i++) {
        if(!vis_dag[i]) {
            dfs_top(i);
        }
    }
    reverse(top.begin(), top.end());
    int idx = 0;
    for(int x : top) {
        idx++;
        pos[x] = idx;
    }
    for(int a = 1; a <= m; a++) {
        for(int b : dag[a]) {
            if(pos[b] < pos[a]) {
                return false;
            }
        }
    }
    return true;
}

/*
int n, m;
vector<int> graph[1 + maxn];
int lift[1 + maxn][logn];
int dep[1 + maxn];
vector<int> dag[1 + maxn];
bool vis_dag[1 + maxn];
vector<int> top;
int pos[1 + maxn];
int in[1 + maxn];
int out[1 + maxn];
int timer;
int by_start[1 + maxn];
int by_end[1 + maxn];
*/

void reset() {
    for(int i = 1; i <= max(n, m); i++) {
        graph[i].clear();
        for(int j = 0; j < logn; j++) {
            lift[i][j] = 0;
        }
        dep[i] = 0;
        dag[i].clear();
        vis_dag[i] = false;
        pos[i] = 0;
        in[i] = 0;
        out[i] = 0;
        by_start[i] = 0;
        by_end[i] = 0;
    }
    top.clear();
    timer = 0;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
    cin >> T;
    while(T--) {
        cout << (solve() ? "Yes" : "No") << "\n";
        reset();
    }
}
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8596 KB Output is correct
4 Correct 7 ms 8656 KB Output is correct
5 Correct 14 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 3 ms 8540 KB Output is correct
9 Correct 62 ms 11292 KB Output is correct
10 Correct 212 ms 29524 KB Output is correct
11 Correct 5 ms 8540 KB Output is correct
12 Correct 25 ms 8668 KB Output is correct
13 Correct 103 ms 58672 KB Output is correct
14 Correct 131 ms 72392 KB Output is correct
15 Runtime error 4096 ms 1048576 KB Execution killed with signal 9
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 3 ms 8660 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 2 ms 8540 KB Output is correct
13 Correct 2 ms 8604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 3 ms 8660 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 2 ms 8540 KB Output is correct
13 Correct 2 ms 8604 KB Output is correct
14 Correct 3 ms 8540 KB Output is correct
15 Correct 2 ms 8540 KB Output is correct
16 Correct 2 ms 8540 KB Output is correct
17 Correct 2 ms 8540 KB Output is correct
18 Correct 2 ms 8664 KB Output is correct
19 Correct 2 ms 8540 KB Output is correct
20 Correct 3 ms 8540 KB Output is correct
21 Correct 2 ms 8540 KB Output is correct
22 Correct 2 ms 8540 KB Output is correct
23 Correct 2 ms 8540 KB Output is correct
24 Correct 2 ms 8540 KB Output is correct
25 Correct 3 ms 8540 KB Output is correct
26 Correct 2 ms 8540 KB Output is correct
27 Correct 2 ms 8540 KB Output is correct
28 Correct 2 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 3 ms 8660 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 2 ms 8540 KB Output is correct
13 Correct 2 ms 8604 KB Output is correct
14 Correct 3 ms 8540 KB Output is correct
15 Correct 2 ms 8540 KB Output is correct
16 Correct 2 ms 8540 KB Output is correct
17 Correct 2 ms 8540 KB Output is correct
18 Correct 2 ms 8664 KB Output is correct
19 Correct 2 ms 8540 KB Output is correct
20 Correct 3 ms 8540 KB Output is correct
21 Correct 2 ms 8540 KB Output is correct
22 Correct 2 ms 8540 KB Output is correct
23 Correct 2 ms 8540 KB Output is correct
24 Correct 2 ms 8540 KB Output is correct
25 Correct 3 ms 8540 KB Output is correct
26 Correct 2 ms 8540 KB Output is correct
27 Correct 2 ms 8540 KB Output is correct
28 Correct 2 ms 8540 KB Output is correct
29 Correct 3 ms 8540 KB Output is correct
30 Correct 3 ms 8540 KB Output is correct
31 Correct 3 ms 8540 KB Output is correct
32 Correct 2 ms 8540 KB Output is correct
33 Correct 2 ms 8540 KB Output is correct
34 Correct 3 ms 8540 KB Output is correct
35 Correct 3 ms 8540 KB Output is correct
36 Correct 2 ms 8680 KB Output is correct
37 Correct 2 ms 8540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8540 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 3 ms 8660 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 2 ms 8540 KB Output is correct
13 Correct 2 ms 8604 KB Output is correct
14 Correct 3 ms 8540 KB Output is correct
15 Correct 2 ms 8540 KB Output is correct
16 Correct 2 ms 8540 KB Output is correct
17 Correct 2 ms 8540 KB Output is correct
18 Correct 2 ms 8664 KB Output is correct
19 Correct 2 ms 8540 KB Output is correct
20 Correct 3 ms 8540 KB Output is correct
21 Correct 2 ms 8540 KB Output is correct
22 Correct 2 ms 8540 KB Output is correct
23 Correct 2 ms 8540 KB Output is correct
24 Correct 2 ms 8540 KB Output is correct
25 Correct 3 ms 8540 KB Output is correct
26 Correct 2 ms 8540 KB Output is correct
27 Correct 2 ms 8540 KB Output is correct
28 Correct 2 ms 8540 KB Output is correct
29 Correct 3 ms 8540 KB Output is correct
30 Correct 3 ms 8540 KB Output is correct
31 Correct 3 ms 8540 KB Output is correct
32 Correct 2 ms 8540 KB Output is correct
33 Correct 2 ms 8540 KB Output is correct
34 Correct 3 ms 8540 KB Output is correct
35 Correct 3 ms 8540 KB Output is correct
36 Correct 2 ms 8680 KB Output is correct
37 Correct 2 ms 8540 KB Output is correct
38 Correct 62 ms 11092 KB Output is correct
39 Correct 209 ms 29432 KB Output is correct
40 Correct 45 ms 10104 KB Output is correct
41 Correct 21 ms 9048 KB Output is correct
42 Correct 30 ms 10332 KB Output is correct
43 Correct 17 ms 9052 KB Output is correct
44 Correct 7 ms 9040 KB Output is correct
45 Correct 32 ms 20564 KB Output is correct
46 Correct 37 ms 20560 KB Output is correct
47 Correct 76 ms 24400 KB Output is correct
48 Correct 77 ms 24388 KB Output is correct
49 Correct 39 ms 20812 KB Output is correct
50 Correct 37 ms 20820 KB Output is correct
51 Correct 34 ms 21840 KB Output is correct
52 Correct 28 ms 21852 KB Output is correct
53 Correct 7 ms 9052 KB Output is correct
54 Correct 38 ms 20564 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8540 KB Output is correct
4 Correct 2 ms 8540 KB Output is correct
5 Correct 5 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 2 ms 8540 KB Output is correct
9 Correct 2 ms 8540 KB Output is correct
10 Correct 2 ms 8540 KB Output is correct
11 Correct 2 ms 8540 KB Output is correct
12 Correct 3 ms 8540 KB Output is correct
13 Correct 14 ms 8596 KB Output is correct
14 Correct 20 ms 8540 KB Output is correct
15 Correct 17 ms 8536 KB Output is correct
16 Correct 36 ms 21340 KB Output is correct
17 Correct 83 ms 28100 KB Output is correct
18 Correct 160 ms 51852 KB Output is correct
19 Correct 42 ms 21304 KB Output is correct
20 Correct 42 ms 21412 KB Output is correct
21 Correct 38 ms 21296 KB Output is correct
22 Correct 67 ms 27544 KB Output is correct
23 Correct 59 ms 27636 KB Output is correct
24 Correct 63 ms 27124 KB Output is correct
25 Correct 58 ms 27128 KB Output is correct
26 Correct 64 ms 27132 KB Output is correct
27 Correct 69 ms 28228 KB Output is correct
28 Correct 70 ms 32324 KB Output is correct
29 Correct 69 ms 29512 KB Output is correct
30 Correct 56 ms 25928 KB Output is correct
31 Correct 53 ms 26444 KB Output is correct
32 Correct 50 ms 24900 KB Output is correct
33 Correct 54 ms 26444 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 8536 KB Output is correct
2 Correct 2 ms 8540 KB Output is correct
3 Correct 2 ms 8596 KB Output is correct
4 Correct 7 ms 8656 KB Output is correct
5 Correct 14 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 2 ms 8540 KB Output is correct
8 Correct 3 ms 8540 KB Output is correct
9 Correct 62 ms 11292 KB Output is correct
10 Correct 212 ms 29524 KB Output is correct
11 Correct 5 ms 8540 KB Output is correct
12 Correct 25 ms 8668 KB Output is correct
13 Correct 103 ms 58672 KB Output is correct
14 Correct 131 ms 72392 KB Output is correct
15 Runtime error 4096 ms 1048576 KB Execution killed with signal 9
16 Halted 0 ms 0 KB -