Submission #601353

# Submission time Handle Problem Language Result Execution time Memory
601353 2022-07-21T18:08:57 Z dxz05 Jail (JOI22_jail) C++14
77 / 100
5000 ms 665800 KB
//#pragma GCC optimize("Ofast,O2,O3,unroll-loops")
//#pragma GCC target("avx2")

#include <bits/stdc++.h>

using namespace std;

void debug_out() { cerr << endl; }

template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
    cerr << "[" << H << "]";
    debug_out(T...);
}

#ifdef dddxxz
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define SZ(s) ((int)s.size())
#define all(x) (x).begin(), (x).end()
#define lla(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
#define MP make_pair

clock_t startTime;

double getCurrentTime() {
    return (double) (clock() - startTime) / CLOCKS_PER_SEC;
}

mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());

typedef long long ll;
const int MOD = 1e9 + 7;
const int INF = 1000000101;
const long long LLINF = 1223372000000000555;
const int N = 2e5 + 3e2;
const int M = 5555;

vector<int> g[N];

int tin[N], tout[N], timer = 0;
int up[N][18], depth[N];

inline void dfs(int v, int p){
    tin[v] = ++timer;
    depth[v] = depth[p] + 1;

    up[v][0] = p;
    for (int i = 1; i < 18; i++){
        up[v][i] = up[up[v][i - 1]][i - 1];
    }
    for (int u : g[v]){
        if (u != p) dfs(u, v);
    }
    tout[v] = ++timer;
}

inline bool upper(int u, int v){
    return tin[u] <= tin[v] && tout[v] <= tout[u];
}

inline int lca(int u, int v){
    if (upper(u, v)) return u;
    if (upper(v, u)) return v;
    for (int i = 17; i >= 0; i--){
        if (!upper(up[u][i], v)) u = up[u][i];
    }
    return up[u][0];
}

inline int dist(int u, int v){
    return depth[u] + depth[v] - 2 * depth[lca(u, v)];
}

inline bool in(int a, int b, int c){
    return dist(a, c) == dist(a, b) + dist(b, c);
}

vector<int> gg[N];

int color[N];
bool dfs(int v){
    color[v] = 1;
    for (int u : gg[v]){
        if (color[u] == 1) return true;
        if (color[u] == 0 && dfs(u)) return true;
    }
    color[v] = 2;
    return false;
}

struct SparseTable{
    vector<vector<int>> mn, mx;
    vector<int> Log;

    void init(vector<int> &a){
        int n = a.size();
        mn.assign(n, vector<int>(20, 0));
        mx.assign(n, vector<int>(20, 0));
        Log.resize(n + 1);
        Log[1] = 0;
        for (int i = 2; i <= n; i++) Log[i] = Log[i / 2] + 1;

        for (int i = 0; i < n; i++){
            mn[i][0] = mx[i][0] = a[i];
        }

        for (int j = 1; j < 20; j++){
            for (int i = 0; i < n; i++){
                if (i + (1 << j) - 1 >= n) continue;
                mn[i][j] = min(mn[i][j - 1], mn[i + (1 << (j - 1))][j - 1]);
                mx[i][j] = max(mx[i][j - 1], mx[i + (1 << (j - 1))][j - 1]);
            }
        }

    }

    int get_min(int l, int r){
        if (l > r) return 1e9;
        int x = Log[r - l + 1];
        return min(mn[l][x], mn[r - (1 << x) + 1][x]);
    }

    int get_max(int l, int r){
        if (l > r) return -1e9;
        int x = Log[r - l + 1];
        return max(mx[l][x], mx[r - (1 << x) + 1][x]);
    }

};

void solve(int TC) {
    int n;
    cin >> n;

    timer = 0;
    for (int i = 0; i < n; i++) g[i].clear();

    bool ok1 = true;
    for (int i = 0; i < n - 1; i++){
        int a, b;
        cin >> a >> b;
        --a, --b;
        g[a].push_back(b);
        g[b].push_back(a);
        if (a != i || b != i + 1) ok1 = false;
    }

    dfs(0, 0);

    int m;
    cin >> m;

    vector<int> S(m), T(m);
    vector<int> ids(n, -1), idt(n, -1);

    for (int i = 0; i < m; i++){
        cin >> S[i] >> T[i];
        S[i]--, T[i]--;
        ids[S[i]] = i;
        idt[T[i]] = i;
    }

    if (ok1){
        vector<int> left(n, 0), right(n, 0);
        vector<int> vv(n, INF);
        for (int i = 0; i < m; i++){
            if (S[i] < T[i]){
                right[S[i]]++;
                right[T[i]]--;
                vv[S[i]] = T[i];
            } else {
                left[T[i]]++;
                left[S[i]]--;
                vv[T[i]] = S[i];
            }
        }

        for (int i = 1; i < n; i++) left[i] += left[i - 1], right[i] += right[i - 1];

        for (int i = 0; i < n; i++){
            if (left[i] && right[i]){
                cout << "No\n";
                return;
            }
        }

        SparseTable st;
        st.init(vv);

        for (int i = 0; i < m; i++){
            int l = min(S[i], T[i]), r = max(S[i], T[i]);
            if (st.get_min(l + 1, r - 1) < r){
                cout << "No\n";
                return;
            }
        }

        cout << "Yes\n";
        return;
    }

    for (int i = 0; i < m; i++){
        gg[i].clear();
        color[i] = 0;
    }

    for (int i = 0; i < m; i++){
        int s = S[i], t = T[i];
        int l = lca(s, t);
        while (upper(l, s)){
            int j = ids[s];
            if (j != -1 && j != i) gg[j].push_back(i);
            j = idt[s];
            if (j != -1 && j != i) gg[i].push_back(j);
            if (s == 0) break;
            s = up[s][0];
        }

        while (upper(l, t)){
            int j = ids[t];
            if (j != -1 && j != i) gg[j].push_back(i);
            j = idt[t];
            if (j != -1 && j != i) gg[i].push_back(j);
            if (t == 0) break;
            t = up[t][0];
        }
    }

    for (int i = 0; i < m; i++){
        if (color[i] == 0 && dfs(i)){
            cout << "No\n";
            return;
        }
    }

    cout << "Yes\n";

}

int main() {
    startTime = clock();
    ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);

#ifdef dddxxz
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif

    int TC = 1;
    cin >> TC;

    for (int test = 1; test <= TC; test++) {
        //debug(test);
        //cout << "Case #" << test << ": ";
        solve(test);
    }

#ifdef dddxxz
    cerr << endl << "Time: " << int(getCurrentTime() * 1000) << " ms" << endl;
#endif

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9704 KB Output is correct
2 Correct 5 ms 9764 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 19 ms 9768 KB Output is correct
5 Correct 30 ms 9808 KB Output is correct
6 Correct 7 ms 9816 KB Output is correct
7 Correct 8 ms 9812 KB Output is correct
8 Correct 7 ms 9812 KB Output is correct
9 Correct 46 ms 12200 KB Output is correct
10 Correct 100 ms 58280 KB Output is correct
11 Correct 11 ms 9684 KB Output is correct
12 Correct 38 ms 9816 KB Output is correct
13 Correct 110 ms 58636 KB Output is correct
14 Correct 57 ms 30088 KB Output is correct
15 Correct 60 ms 30056 KB Output is correct
16 Correct 68 ms 30604 KB Output is correct
17 Correct 108 ms 58828 KB Output is correct
18 Correct 128 ms 59296 KB Output is correct
19 Correct 111 ms 58840 KB Output is correct
20 Correct 70 ms 30156 KB Output is correct
21 Correct 57 ms 30132 KB Output is correct
22 Correct 108 ms 58820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9812 KB Output is correct
3 Correct 6 ms 9812 KB Output is correct
4 Correct 7 ms 9684 KB Output is correct
5 Correct 7 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 7 ms 9684 KB Output is correct
8 Correct 7 ms 9684 KB Output is correct
9 Correct 7 ms 9684 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 6 ms 9684 KB Output is correct
12 Correct 6 ms 9684 KB Output is correct
13 Correct 6 ms 9708 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9812 KB Output is correct
3 Correct 6 ms 9812 KB Output is correct
4 Correct 7 ms 9684 KB Output is correct
5 Correct 7 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 7 ms 9684 KB Output is correct
8 Correct 7 ms 9684 KB Output is correct
9 Correct 7 ms 9684 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 6 ms 9684 KB Output is correct
12 Correct 6 ms 9684 KB Output is correct
13 Correct 6 ms 9708 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9760 KB Output is correct
16 Correct 7 ms 9812 KB Output is correct
17 Correct 7 ms 9764 KB Output is correct
18 Correct 6 ms 9684 KB Output is correct
19 Correct 6 ms 9684 KB Output is correct
20 Correct 7 ms 9684 KB Output is correct
21 Correct 6 ms 9800 KB Output is correct
22 Correct 6 ms 9684 KB Output is correct
23 Correct 5 ms 9684 KB Output is correct
24 Correct 5 ms 9776 KB Output is correct
25 Correct 7 ms 9684 KB Output is correct
26 Correct 6 ms 9684 KB Output is correct
27 Correct 6 ms 9756 KB Output is correct
28 Correct 5 ms 9684 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9812 KB Output is correct
3 Correct 6 ms 9812 KB Output is correct
4 Correct 7 ms 9684 KB Output is correct
5 Correct 7 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 7 ms 9684 KB Output is correct
8 Correct 7 ms 9684 KB Output is correct
9 Correct 7 ms 9684 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 6 ms 9684 KB Output is correct
12 Correct 6 ms 9684 KB Output is correct
13 Correct 6 ms 9708 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9760 KB Output is correct
16 Correct 7 ms 9812 KB Output is correct
17 Correct 7 ms 9764 KB Output is correct
18 Correct 6 ms 9684 KB Output is correct
19 Correct 6 ms 9684 KB Output is correct
20 Correct 7 ms 9684 KB Output is correct
21 Correct 6 ms 9800 KB Output is correct
22 Correct 6 ms 9684 KB Output is correct
23 Correct 5 ms 9684 KB Output is correct
24 Correct 5 ms 9776 KB Output is correct
25 Correct 7 ms 9684 KB Output is correct
26 Correct 6 ms 9684 KB Output is correct
27 Correct 6 ms 9756 KB Output is correct
28 Correct 5 ms 9684 KB Output is correct
29 Correct 7 ms 9812 KB Output is correct
30 Correct 9 ms 9812 KB Output is correct
31 Correct 8 ms 9824 KB Output is correct
32 Correct 8 ms 9684 KB Output is correct
33 Correct 7 ms 9684 KB Output is correct
34 Correct 7 ms 9684 KB Output is correct
35 Correct 9 ms 9832 KB Output is correct
36 Correct 8 ms 9812 KB Output is correct
37 Correct 8 ms 9816 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9812 KB Output is correct
3 Correct 6 ms 9812 KB Output is correct
4 Correct 7 ms 9684 KB Output is correct
5 Correct 7 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 7 ms 9684 KB Output is correct
8 Correct 7 ms 9684 KB Output is correct
9 Correct 7 ms 9684 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 6 ms 9684 KB Output is correct
12 Correct 6 ms 9684 KB Output is correct
13 Correct 6 ms 9708 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9760 KB Output is correct
16 Correct 7 ms 9812 KB Output is correct
17 Correct 7 ms 9764 KB Output is correct
18 Correct 6 ms 9684 KB Output is correct
19 Correct 6 ms 9684 KB Output is correct
20 Correct 7 ms 9684 KB Output is correct
21 Correct 6 ms 9800 KB Output is correct
22 Correct 6 ms 9684 KB Output is correct
23 Correct 5 ms 9684 KB Output is correct
24 Correct 5 ms 9776 KB Output is correct
25 Correct 7 ms 9684 KB Output is correct
26 Correct 6 ms 9684 KB Output is correct
27 Correct 6 ms 9756 KB Output is correct
28 Correct 5 ms 9684 KB Output is correct
29 Correct 7 ms 9812 KB Output is correct
30 Correct 9 ms 9812 KB Output is correct
31 Correct 8 ms 9824 KB Output is correct
32 Correct 8 ms 9684 KB Output is correct
33 Correct 7 ms 9684 KB Output is correct
34 Correct 7 ms 9684 KB Output is correct
35 Correct 9 ms 9832 KB Output is correct
36 Correct 8 ms 9812 KB Output is correct
37 Correct 8 ms 9816 KB Output is correct
38 Correct 41 ms 13276 KB Output is correct
39 Correct 103 ms 59684 KB Output is correct
40 Correct 73 ms 12804 KB Output is correct
41 Correct 42 ms 11444 KB Output is correct
42 Correct 67 ms 12860 KB Output is correct
43 Correct 38 ms 11700 KB Output is correct
44 Correct 13 ms 10204 KB Output is correct
45 Correct 71 ms 25724 KB Output is correct
46 Correct 75 ms 25744 KB Output is correct
47 Correct 152 ms 27700 KB Output is correct
48 Correct 167 ms 27692 KB Output is correct
49 Correct 70 ms 25756 KB Output is correct
50 Correct 72 ms 25832 KB Output is correct
51 Correct 82 ms 26136 KB Output is correct
52 Correct 75 ms 26316 KB Output is correct
53 Correct 16 ms 10964 KB Output is correct
54 Correct 96 ms 25688 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Correct 7 ms 9684 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 14 ms 9684 KB Output is correct
6 Correct 7 ms 9796 KB Output is correct
7 Correct 12 ms 9684 KB Output is correct
8 Correct 7 ms 9684 KB Output is correct
9 Correct 5 ms 9684 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 5 ms 9684 KB Output is correct
12 Correct 6 ms 9812 KB Output is correct
13 Correct 27 ms 10388 KB Output is correct
14 Correct 38 ms 10520 KB Output is correct
15 Correct 35 ms 10452 KB Output is correct
16 Correct 119 ms 26324 KB Output is correct
17 Correct 185 ms 32384 KB Output is correct
18 Correct 346 ms 53648 KB Output is correct
19 Correct 97 ms 26324 KB Output is correct
20 Correct 120 ms 26220 KB Output is correct
21 Correct 115 ms 26536 KB Output is correct
22 Correct 132 ms 33160 KB Output is correct
23 Correct 145 ms 33768 KB Output is correct
24 Correct 130 ms 32248 KB Output is correct
25 Correct 118 ms 33412 KB Output is correct
26 Correct 135 ms 33400 KB Output is correct
27 Correct 176 ms 32980 KB Output is correct
28 Correct 170 ms 38340 KB Output is correct
29 Correct 146 ms 35348 KB Output is correct
30 Correct 118 ms 31428 KB Output is correct
31 Correct 124 ms 32068 KB Output is correct
32 Correct 137 ms 30724 KB Output is correct
33 Correct 118 ms 32052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 9704 KB Output is correct
2 Correct 5 ms 9764 KB Output is correct
3 Correct 5 ms 9684 KB Output is correct
4 Correct 19 ms 9768 KB Output is correct
5 Correct 30 ms 9808 KB Output is correct
6 Correct 7 ms 9816 KB Output is correct
7 Correct 8 ms 9812 KB Output is correct
8 Correct 7 ms 9812 KB Output is correct
9 Correct 46 ms 12200 KB Output is correct
10 Correct 100 ms 58280 KB Output is correct
11 Correct 11 ms 9684 KB Output is correct
12 Correct 38 ms 9816 KB Output is correct
13 Correct 110 ms 58636 KB Output is correct
14 Correct 57 ms 30088 KB Output is correct
15 Correct 60 ms 30056 KB Output is correct
16 Correct 68 ms 30604 KB Output is correct
17 Correct 108 ms 58828 KB Output is correct
18 Correct 128 ms 59296 KB Output is correct
19 Correct 111 ms 58840 KB Output is correct
20 Correct 70 ms 30156 KB Output is correct
21 Correct 57 ms 30132 KB Output is correct
22 Correct 108 ms 58820 KB Output is correct
23 Correct 5 ms 9684 KB Output is correct
24 Correct 5 ms 9812 KB Output is correct
25 Correct 6 ms 9812 KB Output is correct
26 Correct 7 ms 9684 KB Output is correct
27 Correct 7 ms 9684 KB Output is correct
28 Correct 6 ms 9684 KB Output is correct
29 Correct 7 ms 9684 KB Output is correct
30 Correct 7 ms 9684 KB Output is correct
31 Correct 7 ms 9684 KB Output is correct
32 Correct 6 ms 9684 KB Output is correct
33 Correct 6 ms 9684 KB Output is correct
34 Correct 6 ms 9684 KB Output is correct
35 Correct 6 ms 9708 KB Output is correct
36 Correct 5 ms 9684 KB Output is correct
37 Correct 5 ms 9760 KB Output is correct
38 Correct 7 ms 9812 KB Output is correct
39 Correct 7 ms 9764 KB Output is correct
40 Correct 6 ms 9684 KB Output is correct
41 Correct 6 ms 9684 KB Output is correct
42 Correct 7 ms 9684 KB Output is correct
43 Correct 6 ms 9800 KB Output is correct
44 Correct 6 ms 9684 KB Output is correct
45 Correct 5 ms 9684 KB Output is correct
46 Correct 5 ms 9776 KB Output is correct
47 Correct 7 ms 9684 KB Output is correct
48 Correct 6 ms 9684 KB Output is correct
49 Correct 6 ms 9756 KB Output is correct
50 Correct 5 ms 9684 KB Output is correct
51 Correct 7 ms 9812 KB Output is correct
52 Correct 9 ms 9812 KB Output is correct
53 Correct 8 ms 9824 KB Output is correct
54 Correct 8 ms 9684 KB Output is correct
55 Correct 7 ms 9684 KB Output is correct
56 Correct 7 ms 9684 KB Output is correct
57 Correct 9 ms 9832 KB Output is correct
58 Correct 8 ms 9812 KB Output is correct
59 Correct 8 ms 9816 KB Output is correct
60 Correct 41 ms 13276 KB Output is correct
61 Correct 103 ms 59684 KB Output is correct
62 Correct 73 ms 12804 KB Output is correct
63 Correct 42 ms 11444 KB Output is correct
64 Correct 67 ms 12860 KB Output is correct
65 Correct 38 ms 11700 KB Output is correct
66 Correct 13 ms 10204 KB Output is correct
67 Correct 71 ms 25724 KB Output is correct
68 Correct 75 ms 25744 KB Output is correct
69 Correct 152 ms 27700 KB Output is correct
70 Correct 167 ms 27692 KB Output is correct
71 Correct 70 ms 25756 KB Output is correct
72 Correct 72 ms 25832 KB Output is correct
73 Correct 82 ms 26136 KB Output is correct
74 Correct 75 ms 26316 KB Output is correct
75 Correct 16 ms 10964 KB Output is correct
76 Correct 96 ms 25688 KB Output is correct
77 Correct 6 ms 9684 KB Output is correct
78 Correct 7 ms 9684 KB Output is correct
79 Correct 5 ms 9684 KB Output is correct
80 Correct 6 ms 9684 KB Output is correct
81 Correct 14 ms 9684 KB Output is correct
82 Correct 7 ms 9796 KB Output is correct
83 Correct 12 ms 9684 KB Output is correct
84 Correct 7 ms 9684 KB Output is correct
85 Correct 5 ms 9684 KB Output is correct
86 Correct 6 ms 9684 KB Output is correct
87 Correct 5 ms 9684 KB Output is correct
88 Correct 6 ms 9812 KB Output is correct
89 Correct 27 ms 10388 KB Output is correct
90 Correct 38 ms 10520 KB Output is correct
91 Correct 35 ms 10452 KB Output is correct
92 Correct 119 ms 26324 KB Output is correct
93 Correct 185 ms 32384 KB Output is correct
94 Correct 346 ms 53648 KB Output is correct
95 Correct 97 ms 26324 KB Output is correct
96 Correct 120 ms 26220 KB Output is correct
97 Correct 115 ms 26536 KB Output is correct
98 Correct 132 ms 33160 KB Output is correct
99 Correct 145 ms 33768 KB Output is correct
100 Correct 130 ms 32248 KB Output is correct
101 Correct 118 ms 33412 KB Output is correct
102 Correct 135 ms 33400 KB Output is correct
103 Correct 176 ms 32980 KB Output is correct
104 Correct 170 ms 38340 KB Output is correct
105 Correct 146 ms 35348 KB Output is correct
106 Correct 118 ms 31428 KB Output is correct
107 Correct 124 ms 32068 KB Output is correct
108 Correct 137 ms 30724 KB Output is correct
109 Correct 118 ms 32052 KB Output is correct
110 Correct 37 ms 10708 KB Output is correct
111 Correct 26 ms 10404 KB Output is correct
112 Execution timed out 5070 ms 665800 KB Time limit exceeded
113 Halted 0 ms 0 KB -