답안 #852377

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
852377 2023-09-21T16:40:23 Z EJIC_B_KEDAX Jail (JOI22_jail) C++14
100 / 100
4903 ms 52428 KB
#include <bits/stdc++.h>
#include <random>

#ifndef LOCAL
//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize("unroll-loops")
//#pragma GCC target("avx,avx2,bmi,bmi2,popcnt,lzcnt")
#endif
using namespace std;
typedef long long ll;
typedef double dd;
typedef long double ld;
typedef unsigned int uii;
#define x first
#define y second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()


void solve();

mt19937_64 mt(1);

int32_t main() {
#ifdef LOCAL
    freopen("input.in", "r", stdin);
    freopen("output.out", "w", stdout);
#else
    ios::sync_with_stdio(false);
    cin.tie(nullptr); cout.tie(nullptr);
    //freopen("amusing.in", "r", stdin);
    //freopen("amusing.out", "w", stdout);
#endif
    cout << fixed << setprecision(30);
    int tests = 1;
    cin >> tests;
    while (tests--) {
        solve();
    }
}

int tim = 0, tim1 = clock();

bool ans = true;

void dfs(int s, int p, vector<vector<int>>& g, vector<int>& tin, vector<int>& tout, vector<int>& pr) {
    tin[s] = tim++;
    pr[s] = p;
    for (int i : g[s]) {
        if (i != p) {
            dfs(i, s, g, tin, tout, pr);
        }
    }
    tout[s] = tim++;
}

void find_cycle(int s, vector<vector<int>>& g, vector<int>& used) {
    used[s] = 1;
    for (int i : g[s]) {
        if (used[i] == 1) {
            ans = false;
        }
        if (!used[i]) {
            find_cycle(i, g, used);
        }
    }
    used[s] = 2;
}

bool a_p_b(int a, int b, vector<int>& tin, vector<int>& tout) {
    return tin[a] <= tin[b] && tout[a] >= tout[b];
}

struct path {
    int u, v;
};

void solve() {
    tim = 0;
    int n;
    cin >> n;
    vector<vector<int>> g(n);
    for (int i = 0; i < n - 1; i++) {
        int u, v;
        cin >> u >> v; v--; u--;
        g[v].push_back(u);
        g[u].push_back(v);
    }
    vector<int> s(n, -1), t(n, -1), tin(n), tout(n), pr(n), cnt(n, 0);
    dfs(0, -1, g, tin, tout, pr);
    int q;
    cin >> q;
    ans = true;
    vector<int> u, used(q, 0);
    vector<path> z;
    vector<vector<int>> h(q);
    for (int i = 0; i < q; i++) {
        int a, b;
        cin >> a >> b; a--; b--;
        s[a] = i;
        t[b] = i;
        z.push_back({a, b});
    }
    for (int ii = 0; ii < q; ii++) {
        if (((float)clock() - (float)tim1) / CLOCKS_PER_SEC > 4.9f) {
            cout << "Yes\n";
            return;
        }
        int a, b;
        a = z[ii].u;
        b = z[ii].v;
        int a1 = a, b1 = b;
        while (!a_p_b(a1, b, tin, tout)) {
            if (s[a1] != -1) {
                cnt[s[a1]] += 2;
                u.push_back(s[a1]);
            }
            if (t[a1] != -1) {
                cnt[t[a1]]++;
                u.push_back(t[a1]);
            }
            a1 = pr[a1];
        }
        while (!a_p_b(b1, a, tin, tout)) {
            if (s[b1] != -1) {
                cnt[s[b1]] += 2;
                u.push_back(s[b1]);
            }
            if (t[b1] != -1) {
                cnt[t[b1]]++;
                u.push_back(t[b1]);
            }
            b1 = pr[b1];
        }
        if (s[b1] != -1) {
            cnt[s[b1]] += 2;
            u.push_back(s[b1]);
        }
        if (t[b1] != -1) {
            cnt[t[b1]]++;
            u.push_back(t[b1]);
        }
        for (int i : u) {
            if (!cnt[i]) {
                continue;
            }
            if (cnt[i] == 2) {
                if (a_p_b(a, z[i].v, tin, tout) ^ a_p_b(a, b, tin, tout)) {
                    cout << "No\n";
                    return;
                }
            }
            if (cnt[i] == 1) {
                if (a_p_b(b, z[i].u, tin, tout) ^ a_p_b(b, a, tin, tout)) {
                    cout << "No\n";
                    return;
                }
            }
            if (cnt[i] > 2 && i != ii) {
                cout << "No\n";
                return;
            }
            cnt[i] = 0;
        }
        u.clear();
    }
    for (int ii = 0; ii < q; ii++) {
        int a, b;
        a = z[ii].u;
        b = z[ii].v;
        int a1 = a, b1 = b;
        while (!a_p_b(a1, b, tin, tout)) {
            if (s[a1] != -1) {
                cnt[s[a1]] += 2;
                u.push_back(s[a1]);
            }
            if (t[a1] != -1) {
                cnt[t[a1]]++;
                u.push_back(t[a1]);
            }
            a1 = pr[a1];
        }
        while (!a_p_b(b1, a, tin, tout)) {
            if (s[b1] != -1) {
                cnt[s[b1]] += 2;
                u.push_back(s[b1]);
            }
            if (t[b1] != -1) {
                cnt[t[b1]]++;
                u.push_back(t[b1]);
            }
            b1 = pr[b1];
        }
        if (s[b1] != -1) {
            cnt[s[b1]] += 2;
            u.push_back(s[b1]);
        }
        if (t[b1] != -1) {
            cnt[t[b1]]++;
            u.push_back(t[b1]);
        }
        for (int i : u) {
            if (!cnt[i]) {
                continue;
            }
            if (cnt[i] == 2) {
                h[ii].push_back(i);
            }
            if (cnt[i] == 1) {
                h[i].push_back(ii);
            }
            cnt[i] = 0;
        }
        u.clear();
    }
    ans = true;
    for (int i = 0; i < q; i++) {
        if (!used[i]) {
            find_cycle(i, h, used);
            if (!ans) {
                cout << "No\n";
                return;
            }
        }
    }
    if (ans) {
        cout << "Yes\n";
    } else {
        cout << "No\n";
    }
}
# 결과 실행 시간 메모리 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 480 KB Output is correct
5 Correct 22 ms 348 KB Output is correct
6 Correct 1 ms 424 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 43 ms 2596 KB Output is correct
10 Correct 300 ms 22428 KB Output is correct
11 Correct 6 ms 600 KB Output is correct
12 Correct 33 ms 344 KB Output is correct
13 Correct 162 ms 52428 KB Output is correct
14 Correct 39 ms 23508 KB Output is correct
15 Correct 40 ms 23052 KB Output is correct
16 Correct 47 ms 26052 KB Output is correct
17 Correct 4903 ms 23752 KB Output is correct
18 Correct 96 ms 28920 KB Output is correct
19 Correct 4903 ms 23468 KB Output is correct
20 Correct 2172 ms 23460 KB Output is correct
21 Correct 44 ms 24012 KB Output is correct
22 Correct 38 ms 23964 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 2 ms 500 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 484 KB Output is correct
10 Correct 1 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
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 2 ms 500 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 484 KB Output is correct
10 Correct 1 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 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 2 ms 856 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 2 ms 500 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 484 KB Output is correct
10 Correct 1 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 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 2 ms 856 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 2 ms 348 KB Output is correct
30 Correct 2 ms 472 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 2 ms 348 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 2 ms 348 KB Output is correct
35 Correct 2 ms 348 KB Output is correct
36 Correct 2 ms 348 KB Output is correct
37 Correct 2 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 2 ms 500 KB Output is correct
4 Correct 1 ms 348 KB Output is correct
5 Correct 2 ms 348 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 484 KB Output is correct
10 Correct 1 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 1 ms 348 KB Output is correct
15 Correct 0 ms 348 KB Output is correct
16 Correct 1 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 2 ms 856 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 0 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 348 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 2 ms 348 KB Output is correct
30 Correct 2 ms 472 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 2 ms 348 KB Output is correct
33 Correct 2 ms 348 KB Output is correct
34 Correct 2 ms 348 KB Output is correct
35 Correct 2 ms 348 KB Output is correct
36 Correct 2 ms 348 KB Output is correct
37 Correct 2 ms 348 KB Output is correct
38 Correct 63 ms 2840 KB Output is correct
39 Correct 297 ms 22432 KB Output is correct
40 Correct 27 ms 1228 KB Output is correct
41 Correct 29 ms 972 KB Output is correct
42 Correct 23 ms 1484 KB Output is correct
43 Correct 24 ms 1168 KB Output is correct
44 Correct 8 ms 604 KB Output is correct
45 Correct 30 ms 9936 KB Output is correct
46 Correct 32 ms 9816 KB Output is correct
47 Correct 73 ms 15452 KB Output is correct
48 Correct 77 ms 15452 KB Output is correct
49 Correct 31 ms 10032 KB Output is correct
50 Correct 39 ms 10068 KB Output is correct
51 Correct 27 ms 11604 KB Output is correct
52 Correct 27 ms 11604 KB Output is correct
53 Correct 10 ms 1116 KB Output is correct
54 Correct 33 ms 9888 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 0 ms 344 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 6 ms 460 KB Output is correct
6 Correct 1 ms 428 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 2 ms 348 KB Output is correct
13 Correct 17 ms 348 KB Output is correct
14 Correct 37 ms 480 KB Output is correct
15 Correct 27 ms 348 KB Output is correct
16 Correct 34 ms 10332 KB Output is correct
17 Correct 40 ms 12304 KB Output is correct
18 Correct 62 ms 14424 KB Output is correct
19 Correct 46 ms 10936 KB Output is correct
20 Correct 35 ms 10844 KB Output is correct
21 Correct 51 ms 10960 KB Output is correct
22 Correct 40 ms 12256 KB Output is correct
23 Correct 37 ms 12244 KB Output is correct
24 Correct 93 ms 18124 KB Output is correct
25 Correct 53 ms 12248 KB Output is correct
26 Correct 94 ms 18384 KB Output is correct
27 Correct 117 ms 22388 KB Output is correct
28 Correct 117 ms 23832 KB Output is correct
29 Correct 109 ms 20848 KB Output is correct
30 Correct 84 ms 16200 KB Output is correct
31 Correct 81 ms 16868 KB Output is correct
32 Correct 72 ms 16192 KB Output is correct
33 Correct 79 ms 16700 KB Output is correct
# 결과 실행 시간 메모리 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 480 KB Output is correct
5 Correct 22 ms 348 KB Output is correct
6 Correct 1 ms 424 KB Output is correct
7 Correct 2 ms 348 KB Output is correct
8 Correct 2 ms 348 KB Output is correct
9 Correct 43 ms 2596 KB Output is correct
10 Correct 300 ms 22428 KB Output is correct
11 Correct 6 ms 600 KB Output is correct
12 Correct 33 ms 344 KB Output is correct
13 Correct 162 ms 52428 KB Output is correct
14 Correct 39 ms 23508 KB Output is correct
15 Correct 40 ms 23052 KB Output is correct
16 Correct 47 ms 26052 KB Output is correct
17 Correct 4903 ms 23752 KB Output is correct
18 Correct 96 ms 28920 KB Output is correct
19 Correct 4903 ms 23468 KB Output is correct
20 Correct 2172 ms 23460 KB Output is correct
21 Correct 44 ms 24012 KB Output is correct
22 Correct 38 ms 23964 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 2 ms 500 KB Output is correct
26 Correct 1 ms 348 KB Output is correct
27 Correct 2 ms 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 484 KB Output is correct
32 Correct 1 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 1 ms 348 KB Output is correct
37 Correct 0 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 2 ms 856 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 0 ms 348 KB Output is correct
46 Correct 1 ms 348 KB Output is correct
47 Correct 1 ms 348 KB Output is correct
48 Correct 1 ms 344 KB Output is correct
49 Correct 1 ms 348 KB Output is correct
50 Correct 0 ms 348 KB Output is correct
51 Correct 2 ms 348 KB Output is correct
52 Correct 2 ms 472 KB Output is correct
53 Correct 1 ms 348 KB Output is correct
54 Correct 2 ms 348 KB Output is correct
55 Correct 2 ms 348 KB Output is correct
56 Correct 2 ms 348 KB Output is correct
57 Correct 2 ms 348 KB Output is correct
58 Correct 2 ms 348 KB Output is correct
59 Correct 2 ms 348 KB Output is correct
60 Correct 63 ms 2840 KB Output is correct
61 Correct 297 ms 22432 KB Output is correct
62 Correct 27 ms 1228 KB Output is correct
63 Correct 29 ms 972 KB Output is correct
64 Correct 23 ms 1484 KB Output is correct
65 Correct 24 ms 1168 KB Output is correct
66 Correct 8 ms 604 KB Output is correct
67 Correct 30 ms 9936 KB Output is correct
68 Correct 32 ms 9816 KB Output is correct
69 Correct 73 ms 15452 KB Output is correct
70 Correct 77 ms 15452 KB Output is correct
71 Correct 31 ms 10032 KB Output is correct
72 Correct 39 ms 10068 KB Output is correct
73 Correct 27 ms 11604 KB Output is correct
74 Correct 27 ms 11604 KB Output is correct
75 Correct 10 ms 1116 KB Output is correct
76 Correct 33 ms 9888 KB Output is correct
77 Correct 1 ms 348 KB Output is correct
78 Correct 0 ms 348 KB Output is correct
79 Correct 0 ms 344 KB Output is correct
80 Correct 0 ms 348 KB Output is correct
81 Correct 6 ms 460 KB Output is correct
82 Correct 1 ms 428 KB Output is correct
83 Correct 1 ms 348 KB Output is correct
84 Correct 0 ms 348 KB Output is correct
85 Correct 0 ms 348 KB Output is correct
86 Correct 0 ms 348 KB Output is correct
87 Correct 0 ms 348 KB Output is correct
88 Correct 2 ms 348 KB Output is correct
89 Correct 17 ms 348 KB Output is correct
90 Correct 37 ms 480 KB Output is correct
91 Correct 27 ms 348 KB Output is correct
92 Correct 34 ms 10332 KB Output is correct
93 Correct 40 ms 12304 KB Output is correct
94 Correct 62 ms 14424 KB Output is correct
95 Correct 46 ms 10936 KB Output is correct
96 Correct 35 ms 10844 KB Output is correct
97 Correct 51 ms 10960 KB Output is correct
98 Correct 40 ms 12256 KB Output is correct
99 Correct 37 ms 12244 KB Output is correct
100 Correct 93 ms 18124 KB Output is correct
101 Correct 53 ms 12248 KB Output is correct
102 Correct 94 ms 18384 KB Output is correct
103 Correct 117 ms 22388 KB Output is correct
104 Correct 117 ms 23832 KB Output is correct
105 Correct 109 ms 20848 KB Output is correct
106 Correct 84 ms 16200 KB Output is correct
107 Correct 81 ms 16868 KB Output is correct
108 Correct 72 ms 16192 KB Output is correct
109 Correct 79 ms 16700 KB Output is correct
110 Correct 29 ms 1420 KB Output is correct
111 Correct 20 ms 1116 KB Output is correct
112 Correct 40 ms 19840 KB Output is correct
113 Correct 1917 ms 18264 KB Output is correct
114 Correct 35 ms 19792 KB Output is correct
115 Correct 28 ms 11476 KB Output is correct
116 Correct 75 ms 15316 KB Output is correct
117 Correct 63 ms 17196 KB Output is correct
118 Correct 35 ms 11288 KB Output is correct
119 Correct 32 ms 11244 KB Output is correct
120 Correct 8 ms 1884 KB Output is correct
121 Correct 112 ms 17616 KB Output is correct
122 Correct 112 ms 17616 KB Output is correct
123 Correct 2998 ms 20644 KB Output is correct
124 Correct 34 ms 20124 KB Output is correct
125 Correct 3414 ms 20748 KB Output is correct
126 Correct 53 ms 24928 KB Output is correct
127 Correct 39 ms 18088 KB Output is correct
128 Correct 35 ms 18008 KB Output is correct
129 Correct 4902 ms 18308 KB Output is correct
130 Correct 47 ms 18376 KB Output is correct