Submission #706550

# Submission time Handle Problem Language Result Execution time Memory
706550 2023-03-07T02:12:28 Z becaido Jail (JOI22_jail) C++17
61 / 100
5000 ms 30804 KB
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx,popcnt,sse4,abm")
#include <bits/stdc++.h>
using namespace std;

#ifdef WAIMAI
#define debug(HEHE...) cout << "[" << #HEHE << "] : ", dout(HEHE)
void dout() {cout << '\n';}
template<typename T, typename...U>
void dout(T t, U...u) {cout << t << (sizeof...(u) ? ", " : ""), dout(u...);}
#else
#define debug(...) 7122
#endif

#define ll long long
#define Waimai ios::sync_with_stdio(false), cin.tie(0)
#define FOR(x,a,b) for (int x = a, I = b; x <= I; x++)
#define pb emplace_back
#define F first
#define S second

const int INF = 1e9;
const int SIZE = 24e4 + 5;
const int H = __lg(SIZE) + 1;

int n, m;
bool ans;
vector<int> adj[SIZE];
int s[SIZE], t[SIZE], c[SIZE];
int h[SIZE], to[SIZE][H + 1];

vector<int> g[SIZE];
int deg[SIZE];
queue<int> q;

void dfs(int pos, int fa) {
    for (int np : adj[pos]) if (np != fa) {
        h[np] = h[pos] + 1;
        to[np][0] = pos;
        dfs(np, pos);
    }
}

int jump(int pos, int k) {
    if (k < 0) return 0;
    int cnt = 0;
    while (k) {
        if (k & 1) pos = to[pos][cnt];
        cnt++;
        k >>= 1;
    }
    return pos;
}

int lca(int a, int b) {
    if (h[a] < h[b]) swap(a, b);
    a = jump(a, h[a] - h[b]);
    if (a == b) return a;
    for (int i = H; i >= 0; i--) if (to[a][i] != to[b][i]) {
        a = to[a][i];
        b = to[b][i];
    }
    return to[a][0];
}

bool on_path(int pos, int a, int c, int b) {
    return h[pos] >= h[c] && (jump(a, h[a] - h[pos]) == pos || jump(b, h[b] - h[pos]) == pos);
}

void add_edge(int a, int b) {
    g[a].pb(b);
    deg[b]++;
}

void add(int a, int b) {
    vector<int> v;
    for (int x : {s[a], t[a]}) for (int y : {s[b], t[b]}) {
        int z = lca(x, y);
        if (on_path(z, s[a], c[a], t[a]) && on_path(z, s[b], c[b], t[b])) v.pb(z);
    }
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());

    if (v.size() == 0) return;
    if (v.size() == 1) {
        int k = v[0];
        if (s[a] == k) add_edge(a, b);
        else if (s[b] == k) add_edge(b, a);
        else if (t[a] == k) add_edge(b, a);
        else if (t[b] == k) add_edge(a, b);
        return;
    }
    if (v.size() == 3) {
        int mn = h[v[0]];
        for (int x : v) mn = min(mn, h[x]);
        for (int i = 0; i < 3; i++) if (h[v[i]] == mn) {v.erase(v.begin() + i); break;}
    }

    assert(v.size() == 2);
    int k1 = v[0], k2 = v[1];
    bool isa, ita, isb, itb;
    isa = s[a] == k1 || s[a] == k2;
    ita = t[a] == k1 || t[a] == k2;
    isb = s[b] == k1 || s[b] == k2;
    itb = t[b] == k1 || t[b] == k2;

    if ((isa && ita) || (isb && itb)) {
        ans = 0;
        return;
    }

    if ((isa && isb) || (ita && itb)) {
        ans = 0;
        return;
    }

    if (isa) add_edge(a, b);
    else if (isb) add_edge(b, a);
    else if (ita) add_edge(b, a);
    else if (itb) add_edge(a, b);
}

void solve() {
    cin >> n;
    FOR (i, 1, n) adj[i].clear();
    FOR (i, 2, n) {
        int a, b;
        cin >> a >> b;
        adj[a].pb(b);
        adj[b].pb(a);
    }
    h[1] = 1, dfs(1, 1);
    FOR (j, 1, H) FOR (i, 1, n) to[i][j] = to[to[i][j - 1]][j - 1];

    cin >> m;
    FOR (i, 1, m) {
        cin >> s[i] >> t[i];
        c[i] = lca(s[i], t[i]);
        g[i].clear();
        deg[i] = 0;
    }
    ans = 1;
    FOR (i, 1, m) FOR (j, i + 1, m) {
        add(i, j);
        if (!ans) {
            cout << "No\n";
            return;
        }
    }
    FOR (i, 1, m) if (!deg[i]) q.push(i);
    int cnt = 0;
    while (q.size()) {
        int pos = q.front();
        q.pop();
        cnt++;
        for (int np : g[pos]) if (--deg[np] == 0) q.push(np);
    }
    cout << (cnt == m ? "Yes" : "No") << '\n';
}

int main() {
    Waimai;
    int tt;
    cin >> tt;
    while (tt--) solve();
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11604 KB Output is correct
2 Correct 7 ms 11516 KB Output is correct
3 Correct 7 ms 11632 KB Output is correct
4 Correct 14 ms 11604 KB Output is correct
5 Correct 23 ms 11656 KB Output is correct
6 Correct 8 ms 11608 KB Output is correct
7 Correct 7 ms 11552 KB Output is correct
8 Correct 17 ms 11604 KB Output is correct
9 Correct 342 ms 13284 KB Output is correct
10 Correct 96 ms 29324 KB Output is correct
11 Correct 11 ms 11644 KB Output is correct
12 Correct 60 ms 11656 KB Output is correct
13 Execution timed out 5059 ms 29512 KB Time limit exceeded
14 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11604 KB Output is correct
2 Correct 8 ms 11604 KB Output is correct
3 Correct 8 ms 11604 KB Output is correct
4 Correct 11 ms 11548 KB Output is correct
5 Correct 8 ms 11784 KB Output is correct
6 Correct 8 ms 11604 KB Output is correct
7 Correct 8 ms 11604 KB Output is correct
8 Correct 8 ms 11604 KB Output is correct
9 Correct 7 ms 11604 KB Output is correct
10 Correct 8 ms 11604 KB Output is correct
11 Correct 8 ms 11604 KB Output is correct
12 Correct 7 ms 11604 KB Output is correct
13 Correct 7 ms 11604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11604 KB Output is correct
2 Correct 8 ms 11604 KB Output is correct
3 Correct 8 ms 11604 KB Output is correct
4 Correct 11 ms 11548 KB Output is correct
5 Correct 8 ms 11784 KB Output is correct
6 Correct 8 ms 11604 KB Output is correct
7 Correct 8 ms 11604 KB Output is correct
8 Correct 8 ms 11604 KB Output is correct
9 Correct 7 ms 11604 KB Output is correct
10 Correct 8 ms 11604 KB Output is correct
11 Correct 8 ms 11604 KB Output is correct
12 Correct 7 ms 11604 KB Output is correct
13 Correct 7 ms 11604 KB Output is correct
14 Correct 6 ms 11604 KB Output is correct
15 Correct 8 ms 11520 KB Output is correct
16 Correct 8 ms 11604 KB Output is correct
17 Correct 8 ms 11668 KB Output is correct
18 Correct 8 ms 11604 KB Output is correct
19 Correct 9 ms 11604 KB Output is correct
20 Correct 8 ms 11604 KB Output is correct
21 Correct 8 ms 11604 KB Output is correct
22 Correct 8 ms 11672 KB Output is correct
23 Correct 9 ms 11604 KB Output is correct
24 Correct 7 ms 11604 KB Output is correct
25 Correct 8 ms 11664 KB Output is correct
26 Correct 7 ms 11604 KB Output is correct
27 Correct 8 ms 11604 KB Output is correct
28 Correct 7 ms 11604 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11604 KB Output is correct
2 Correct 8 ms 11604 KB Output is correct
3 Correct 8 ms 11604 KB Output is correct
4 Correct 11 ms 11548 KB Output is correct
5 Correct 8 ms 11784 KB Output is correct
6 Correct 8 ms 11604 KB Output is correct
7 Correct 8 ms 11604 KB Output is correct
8 Correct 8 ms 11604 KB Output is correct
9 Correct 7 ms 11604 KB Output is correct
10 Correct 8 ms 11604 KB Output is correct
11 Correct 8 ms 11604 KB Output is correct
12 Correct 7 ms 11604 KB Output is correct
13 Correct 7 ms 11604 KB Output is correct
14 Correct 6 ms 11604 KB Output is correct
15 Correct 8 ms 11520 KB Output is correct
16 Correct 8 ms 11604 KB Output is correct
17 Correct 8 ms 11668 KB Output is correct
18 Correct 8 ms 11604 KB Output is correct
19 Correct 9 ms 11604 KB Output is correct
20 Correct 8 ms 11604 KB Output is correct
21 Correct 8 ms 11604 KB Output is correct
22 Correct 8 ms 11672 KB Output is correct
23 Correct 9 ms 11604 KB Output is correct
24 Correct 7 ms 11604 KB Output is correct
25 Correct 8 ms 11664 KB Output is correct
26 Correct 7 ms 11604 KB Output is correct
27 Correct 8 ms 11604 KB Output is correct
28 Correct 7 ms 11604 KB Output is correct
29 Correct 19 ms 11716 KB Output is correct
30 Correct 13 ms 11668 KB Output is correct
31 Correct 9 ms 11708 KB Output is correct
32 Correct 15 ms 11604 KB Output is correct
33 Correct 9 ms 11704 KB Output is correct
34 Correct 26 ms 11604 KB Output is correct
35 Correct 24 ms 11732 KB Output is correct
36 Correct 32 ms 11692 KB Output is correct
37 Correct 25 ms 11700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11604 KB Output is correct
2 Correct 8 ms 11604 KB Output is correct
3 Correct 8 ms 11604 KB Output is correct
4 Correct 11 ms 11548 KB Output is correct
5 Correct 8 ms 11784 KB Output is correct
6 Correct 8 ms 11604 KB Output is correct
7 Correct 8 ms 11604 KB Output is correct
8 Correct 8 ms 11604 KB Output is correct
9 Correct 7 ms 11604 KB Output is correct
10 Correct 8 ms 11604 KB Output is correct
11 Correct 8 ms 11604 KB Output is correct
12 Correct 7 ms 11604 KB Output is correct
13 Correct 7 ms 11604 KB Output is correct
14 Correct 6 ms 11604 KB Output is correct
15 Correct 8 ms 11520 KB Output is correct
16 Correct 8 ms 11604 KB Output is correct
17 Correct 8 ms 11668 KB Output is correct
18 Correct 8 ms 11604 KB Output is correct
19 Correct 9 ms 11604 KB Output is correct
20 Correct 8 ms 11604 KB Output is correct
21 Correct 8 ms 11604 KB Output is correct
22 Correct 8 ms 11672 KB Output is correct
23 Correct 9 ms 11604 KB Output is correct
24 Correct 7 ms 11604 KB Output is correct
25 Correct 8 ms 11664 KB Output is correct
26 Correct 7 ms 11604 KB Output is correct
27 Correct 8 ms 11604 KB Output is correct
28 Correct 7 ms 11604 KB Output is correct
29 Correct 19 ms 11716 KB Output is correct
30 Correct 13 ms 11668 KB Output is correct
31 Correct 9 ms 11708 KB Output is correct
32 Correct 15 ms 11604 KB Output is correct
33 Correct 9 ms 11704 KB Output is correct
34 Correct 26 ms 11604 KB Output is correct
35 Correct 24 ms 11732 KB Output is correct
36 Correct 32 ms 11692 KB Output is correct
37 Correct 25 ms 11700 KB Output is correct
38 Correct 349 ms 14312 KB Output is correct
39 Correct 96 ms 30804 KB Output is correct
40 Correct 424 ms 13756 KB Output is correct
41 Correct 984 ms 13508 KB Output is correct
42 Correct 480 ms 13744 KB Output is correct
43 Correct 36 ms 13504 KB Output is correct
44 Correct 495 ms 12072 KB Output is correct
45 Correct 119 ms 26188 KB Output is correct
46 Correct 156 ms 26212 KB Output is correct
47 Correct 168 ms 28276 KB Output is correct
48 Correct 216 ms 28184 KB Output is correct
49 Correct 60 ms 26316 KB Output is correct
50 Correct 159 ms 26328 KB Output is correct
51 Correct 67 ms 26820 KB Output is correct
52 Correct 72 ms 26860 KB Output is correct
53 Correct 521 ms 12760 KB Output is correct
54 Correct 150 ms 26204 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 8 ms 11604 KB Output is correct
2 Correct 9 ms 11636 KB Output is correct
3 Correct 7 ms 11604 KB Output is correct
4 Correct 7 ms 11604 KB Output is correct
5 Correct 11 ms 11604 KB Output is correct
6 Correct 7 ms 11604 KB Output is correct
7 Correct 7 ms 11568 KB Output is correct
8 Correct 7 ms 11640 KB Output is correct
9 Correct 7 ms 11604 KB Output is correct
10 Correct 7 ms 11604 KB Output is correct
11 Correct 7 ms 11604 KB Output is correct
12 Correct 29 ms 11676 KB Output is correct
13 Correct 42 ms 12116 KB Output is correct
14 Correct 179 ms 12516 KB Output is correct
15 Correct 139 ms 12268 KB Output is correct
16 Execution timed out 5016 ms 27028 KB Time limit exceeded
17 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 11604 KB Output is correct
2 Correct 7 ms 11516 KB Output is correct
3 Correct 7 ms 11632 KB Output is correct
4 Correct 14 ms 11604 KB Output is correct
5 Correct 23 ms 11656 KB Output is correct
6 Correct 8 ms 11608 KB Output is correct
7 Correct 7 ms 11552 KB Output is correct
8 Correct 17 ms 11604 KB Output is correct
9 Correct 342 ms 13284 KB Output is correct
10 Correct 96 ms 29324 KB Output is correct
11 Correct 11 ms 11644 KB Output is correct
12 Correct 60 ms 11656 KB Output is correct
13 Execution timed out 5059 ms 29512 KB Time limit exceeded
14 Halted 0 ms 0 KB -