Submission #890959

# Submission time Handle Problem Language Result Execution time Memory
890959 2023-12-22T05:37:31 Z vjudge1 Jail (JOI22_jail) C++17
66 / 100
5000 ms 34440 KB
#include <bits/stdc++.h>

using namespace std;

#define all(x) x.begin(), x.end()
#define ar array
#define pb push_back
#define ln '\n'
#define int long long

using i64 = long long;

template <class F, class _S>
bool chmin(F &u, const _S &v){
    bool flag = false;
    if ( u > v ){
        u = v; flag |= true;
    }
    return flag;
}

template <class F, class _S>
bool chmax(F &u, const _S &v){
    bool flag = false;
    if ( u < v ){
        u = v; flag |= true;
    }
    return flag;
}

void solve(){
    int n; cin >> n;
    vector <vector<int>> G(n);
    bool Qflag = true;
    for ( int i = 0; i + 1 < n; i++ ){
        int u, v; cin >> u >> v;
        --u, --v;
        G[u].pb(v), G[v].pb(u);
        Qflag &= (u + 1 == v);
    }
    int m; cin >> m;
    vector <int> S(m), T(m);
    for ( int i = 0; i < m; i++ ){
        cin >> S[i] >> T[i];
        --S[i], --T[i];
    }
    if ( Qflag ){
        vector <int> pf(n + 1);
        vector <ar<int,2>> A;
        for ( int i = 0; i < m; i++ ){
            if ( S[i] > T[i] ){
                pf[T[i]]++;
                pf[S[i] + 1]--;
            }
            A.pb({min(S[i], T[i]), max(S[i], T[i])});
        }
        for ( int i = 1; i < n; i++ ){
            pf[i] += pf[i - 1];
        }
        vector <int> P(n);
        for ( int i = 0; i < n; i++ ){
            P[i] = (i ? P[i - 1] : 0) + pf[i];
        }
        for ( int i = 0; i < m; i++ ){
            if ( S[i] < T[i] ){
                int s = P[T[i]] - (S[i] ? P[S[i] - 1] : 0);
                if ( s ){
                    cout << "No\n";
                    return;
                }
            }
        }
        sort(all(A));
        int mx = -1;
        for ( auto &[l, r]: A ){
            if ( mx > r ){
                cout << "No\n";
                return;
            } chmax(mx, r);
        }
        cout << "Yes\n"; return;
    }
    vector <vector<int>> up(20, vector <int> (n));
    vector <int> d(n), tin(n), tout(n);
    int timer = 0;
    auto dfs = [&](auto dfs, int u, int p) -> void{
        up[0][u] = p;
        for ( int i = 1; i < 20; i++ ){
            up[i][u] = up[i - 1][up[i - 1][u]];
        }
        tin[u] = ++timer;
        for ( auto &v: G[u] ){
            if ( v != p ){
                d[v] = d[u] + 1;
                dfs(dfs, v, u);
            }
        } tout[u] = timer;
    };
    dfs(dfs, 0, 0);
    auto lca = [&](int u, int v){
        if ( d[u] < d[v] ) swap(u, v);
        int df = d[u] - d[v];
        for ( int i = 0; i < 20; i++ ){
            if ( df >> i & 1 ){
                u = up[i][u];
            }
        }
        if ( u == v ){
            return u;
        }
        for ( int i = 19; i >= 0; i-- ){
            if ( up[i][u] != up[i][v] ){
                u = up[i][u];
                v = up[i][v];
            }
        }
        return up[0][u];
    };
    auto isPar = [&](int u, int v){
        return tin[u] <= tin[v] && tout[u] >= tout[v];
    };
    auto onPath = [&](int u, int x, int v){
        return (isPar(x, u) || isPar(x, v)) && isPar(lca(u, v), x);
    };
    auto bad = [&](int j, int i){
        return onPath(S[i], S[j], T[i]) || onPath(S[j], T[i], T[j]);
    };
    vector <vector<int>> P(m);
    for ( int i = 0; i < m; i++ ){
        for ( int j = i + 1; j < m; j++ ){
            if ( bad(i, j) ){
                P[i].pb(j);
            }
            if ( bad(j, i) ){
                P[j].pb(i);
            }
        }
    }
    vector <int> us(m);
    auto dfs2 = [&](auto dfs2, int u) -> bool{
        us[u] = 1;
        for ( auto &v: P[u] ){
            if ( !us[v] ){
                if ( !dfs2(dfs2, v) ){
                    return false;
                }
            } else if ( us[v] == 1 ){
                return false;
            }
        }
        us[u] = 2;
        return true;
    };
    bool flag = true;
    for ( int i = 0; i < m; i++ ){
        if ( !us[i] ){
            flag &= dfs2(dfs2, i);
        }
    }
    cout << (flag ? "Yes\n" : "No\n");
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int t; cin >> t;
    while ( t-- ){
        solve();
    }

    cout << '\n';
}
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 10 ms 460 KB Output is correct
5 Correct 21 ms 464 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 23 ms 908 KB Output is correct
10 Correct 25 ms 8768 KB Output is correct
11 Correct 5 ms 348 KB Output is correct
12 Correct 25 ms 472 KB Output is correct
13 Correct 42 ms 10496 KB Output is correct
14 Correct 31 ms 10448 KB Output is correct
15 Correct 43 ms 10440 KB Output is correct
16 Correct 41 ms 12484 KB Output is correct
17 Correct 34 ms 10704 KB Output is correct
18 Correct 44 ms 12484 KB Output is correct
19 Correct 34 ms 10692 KB Output is correct
20 Correct 33 ms 10704 KB Output is correct
21 Correct 33 ms 10692 KB Output is correct
22 Correct 38 ms 10700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 356 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 356 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 2 ms 348 KB Output is correct
17 Correct 2 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 516 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 0 ms 600 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 356 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 2 ms 348 KB Output is correct
17 Correct 2 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 516 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 0 ms 600 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 4 ms 348 KB Output is correct
31 Correct 5 ms 604 KB Output is correct
32 Correct 3 ms 348 KB Output is correct
33 Correct 3 ms 348 KB Output is correct
34 Correct 4 ms 348 KB Output is correct
35 Correct 8 ms 604 KB Output is correct
36 Correct 4 ms 348 KB Output is correct
37 Correct 3 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 1 ms 348 KB Output is correct
6 Correct 2 ms 512 KB Output is correct
7 Correct 2 ms 356 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 2 ms 348 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 348 KB Output is correct
14 Correct 0 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 2 ms 348 KB Output is correct
17 Correct 2 ms 348 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 0 ms 348 KB Output is correct
20 Correct 1 ms 516 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 344 KB Output is correct
23 Correct 0 ms 600 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 2 ms 348 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 4 ms 348 KB Output is correct
31 Correct 5 ms 604 KB Output is correct
32 Correct 3 ms 348 KB Output is correct
33 Correct 3 ms 348 KB Output is correct
34 Correct 4 ms 348 KB Output is correct
35 Correct 8 ms 604 KB Output is correct
36 Correct 4 ms 348 KB Output is correct
37 Correct 3 ms 348 KB Output is correct
38 Correct 24 ms 2048 KB Output is correct
39 Correct 26 ms 10320 KB Output is correct
40 Correct 201 ms 3692 KB Output is correct
41 Correct 100 ms 2712 KB Output is correct
42 Correct 140 ms 3648 KB Output is correct
43 Correct 31 ms 2764 KB Output is correct
44 Correct 55 ms 860 KB Output is correct
45 Correct 82 ms 30116 KB Output is correct
46 Correct 69 ms 30124 KB Output is correct
47 Correct 61 ms 33448 KB Output is correct
48 Correct 53 ms 33376 KB Output is correct
49 Correct 61 ms 30376 KB Output is correct
50 Correct 57 ms 30420 KB Output is correct
51 Correct 49 ms 31396 KB Output is correct
52 Correct 60 ms 31576 KB Output is correct
53 Correct 63 ms 2564 KB Output is correct
54 Correct 79 ms 30972 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 4 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 600 KB Output is correct
12 Correct 4 ms 348 KB Output is correct
13 Correct 70 ms 860 KB Output is correct
14 Correct 44 ms 1116 KB Output is correct
15 Correct 53 ms 1196 KB Output is correct
16 Correct 1671 ms 31820 KB Output is correct
17 Execution timed out 5096 ms 34440 KB Time limit exceeded
18 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 10 ms 460 KB Output is correct
5 Correct 21 ms 464 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 23 ms 908 KB Output is correct
10 Correct 25 ms 8768 KB Output is correct
11 Correct 5 ms 348 KB Output is correct
12 Correct 25 ms 472 KB Output is correct
13 Correct 42 ms 10496 KB Output is correct
14 Correct 31 ms 10448 KB Output is correct
15 Correct 43 ms 10440 KB Output is correct
16 Correct 41 ms 12484 KB Output is correct
17 Correct 34 ms 10704 KB Output is correct
18 Correct 44 ms 12484 KB Output is correct
19 Correct 34 ms 10692 KB Output is correct
20 Correct 33 ms 10704 KB Output is correct
21 Correct 33 ms 10692 KB Output is correct
22 Correct 38 ms 10700 KB Output is correct
23 Correct 0 ms 344 KB Output is correct
24 Correct 0 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 2 ms 348 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 2 ms 512 KB Output is correct
29 Correct 2 ms 356 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 2 ms 348 KB Output is correct
32 Correct 2 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 0 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 2 ms 348 KB Output is correct
39 Correct 2 ms 348 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 0 ms 348 KB Output is correct
42 Correct 1 ms 516 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 344 KB Output is correct
45 Correct 0 ms 600 KB Output is correct
46 Correct 0 ms 348 KB Output is correct
47 Correct 2 ms 348 KB Output is correct
48 Correct 1 ms 348 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 1 ms 344 KB Output is correct
52 Correct 4 ms 348 KB Output is correct
53 Correct 5 ms 604 KB Output is correct
54 Correct 3 ms 348 KB Output is correct
55 Correct 3 ms 348 KB Output is correct
56 Correct 4 ms 348 KB Output is correct
57 Correct 8 ms 604 KB Output is correct
58 Correct 4 ms 348 KB Output is correct
59 Correct 3 ms 348 KB Output is correct
60 Correct 24 ms 2048 KB Output is correct
61 Correct 26 ms 10320 KB Output is correct
62 Correct 201 ms 3692 KB Output is correct
63 Correct 100 ms 2712 KB Output is correct
64 Correct 140 ms 3648 KB Output is correct
65 Correct 31 ms 2764 KB Output is correct
66 Correct 55 ms 860 KB Output is correct
67 Correct 82 ms 30116 KB Output is correct
68 Correct 69 ms 30124 KB Output is correct
69 Correct 61 ms 33448 KB Output is correct
70 Correct 53 ms 33376 KB Output is correct
71 Correct 61 ms 30376 KB Output is correct
72 Correct 57 ms 30420 KB Output is correct
73 Correct 49 ms 31396 KB Output is correct
74 Correct 60 ms 31576 KB Output is correct
75 Correct 63 ms 2564 KB Output is correct
76 Correct 79 ms 30972 KB Output is correct
77 Correct 0 ms 348 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 0 ms 348 KB Output is correct
80 Correct 0 ms 348 KB Output is correct
81 Correct 4 ms 348 KB Output is correct
82 Correct 1 ms 348 KB Output is correct
83 Correct 1 ms 348 KB Output is correct
84 Correct 1 ms 348 KB Output is correct
85 Correct 1 ms 348 KB Output is correct
86 Correct 1 ms 348 KB Output is correct
87 Correct 1 ms 600 KB Output is correct
88 Correct 4 ms 348 KB Output is correct
89 Correct 70 ms 860 KB Output is correct
90 Correct 44 ms 1116 KB Output is correct
91 Correct 53 ms 1196 KB Output is correct
92 Correct 1671 ms 31820 KB Output is correct
93 Execution timed out 5096 ms 34440 KB Time limit exceeded
94 Halted 0 ms 0 KB -