Submission #584871

# Submission time Handle Problem Language Result Execution time Memory
584871 2022-06-28T05:57:07 Z CodePlatina Jail (JOI22_jail) C++17
100 / 100
555 ms 97668 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
#include <tuple>
#define pii pair<int, int>
#define piii pair<int, pii>
#define pll pair<long long, long long>
#define plll pair<long long, pll>
#define tiii tuple<int, int, int>
#define tiiii tuple<int, int, int, int>
#define ff first
#define ss second
#define ee ss.ff
#define rr ss.ss
#define DEBUG
const int INF = (int)1e9 + 7;

using namespace std;

int n, m;
vector<int> gph[121212];
vector<int> ord[1212121];
int par[121212];
int sub[121212];
int sz[1212121];
int in[121212];
int out[121212];
int up[121212];
int st[121212];
int ed[121212];
int cnt;

void dfs(int x, int p)
{
    sub[x] = 1;
    par[x] = p;
    vector<int> tmp;
    for(auto y : gph[x]) if(y != p) dfs(y, x), sub[x] += sub[y], tmp.push_back(y);
    gph[x] = tmp;
}

void dfs2(int x, int p)
{
    in[x] = cnt++;
    up[x] = (gph[p][0] == x ? up[p] : x);
    for(auto &y : gph[x]) if(sub[y] > sub[gph[x][0]]) swap(y, gph[x][0]);
    for(auto y : gph[x]) dfs2(y, x);
    out[x] = cnt;
}

void init()
{
    for(int i = 0; i < n; ++i)
    {
        if(st[i] != -1) ord[st[i]].push_back(i + n + m);
        if(ed[i] != -1) ord[i + 3 * n + m].push_back(ed[i]);
    }
    for(int i = n - 1; i >= 1; --i)
    {
        ord[2 * i + m].push_back(i + m);
        ord[2 * i + 1 + m].push_back(i + m);
        ord[i + 2 * n + m].push_back(2 * i + 2 * n + m);
        ord[i + 2 * n + m].push_back(2 * i + 1 + 2 * n + m);
    }
}

void upd(int l, int r, int i)
{
    for(int x = l + n, y = r + n; x != y; x >>= 1, y >>= 1)
    {
        if(x & 1)
        {
            ord[x + m].push_back(i);
            ord[i].push_back(x + 2 * n + m);
            ++x;
        }
        if(y & 1)
        {
            --y;
            ord[y + m].push_back(i);
            ord[i].push_back(y + 2 * n + m);
        }
    }
}

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

    int T; cin >> T;
    while(T--)
    {
        cin >> n;
        for(int i = 0; i < n; ++i) gph[i].clear(), par[i] = in[i] = out[i] = sub[i] = up[i] = 0, st[i] = ed[i] = -1;
        cnt = 0;

        for(int i = 1; i < n; ++i)
        {
            int x, y; cin >> x >> y; --x; --y;
            gph[x].push_back(y);
            gph[y].push_back(x);
        }
        dfs(0, 0);
        dfs2(0, 0);

        cin >> m;
        for(int i = 0; i < m + 4 * n; ++i) ord[i].clear(), ord[i].shrink_to_fit(), sz[i] = 0;
        pii qry[m];
        for(auto &[x, y] : qry) cin >> x >> y, --x, --y;

        for(int i = 0; i < m; ++i) st[in[qry[i].ff]] = i, ed[in[qry[i].ss]] = i;
        init();

        for(int i = 0; i < m; ++i)
        {
            auto [x, y] = qry[i];
            if(st[in[y]] != -1) ord[st[in[y]]].push_back(i);
            if(ed[in[x]] != -1) ord[i].push_back(ed[in[x]]);
            int xx = 0, yy = 0;
            while(up[x] != up[y])
            {
                if(in[x] < in[y]) swap(x, y), swap(xx, yy);
                upd(in[up[x]], in[x] + xx, i);
                xx = 1;
                x = par[up[x]];
            }
            if(in[x] > in[y]) swap(x, y), swap(xx, yy);
            upd(in[x] + 1 - xx, in[y] + yy, i);
        }

        vector<int> V;
        for(int i = 0; i < m + 4 * n; ++i) for(auto x : ord[i]) ++sz[x];
        for(int i = 0; i < m + 4 * n; ++i) if(!sz[i]) V.push_back(i);
        int c = 0;
        while(V.size())
        {
            int x = V.back(); V.pop_back(); ++c;
            for(auto y : ord[x]) if(!--sz[y]) V.push_back(y);
        }

        cout << (c == m + 4 * n ? "Yes" : "No") << '\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 17 ms 31572 KB Output is correct
2 Correct 17 ms 31632 KB Output is correct
3 Correct 16 ms 31596 KB Output is correct
4 Correct 36 ms 32032 KB Output is correct
5 Correct 59 ms 32136 KB Output is correct
6 Correct 21 ms 31772 KB Output is correct
7 Correct 19 ms 31752 KB Output is correct
8 Correct 21 ms 31700 KB Output is correct
9 Correct 88 ms 34112 KB Output is correct
10 Correct 94 ms 68152 KB Output is correct
11 Correct 26 ms 31828 KB Output is correct
12 Correct 74 ms 32184 KB Output is correct
13 Correct 144 ms 75248 KB Output is correct
14 Correct 125 ms 74440 KB Output is correct
15 Correct 230 ms 80012 KB Output is correct
16 Correct 416 ms 97668 KB Output is correct
17 Correct 178 ms 84440 KB Output is correct
18 Correct 128 ms 75412 KB Output is correct
19 Correct 190 ms 82860 KB Output is correct
20 Correct 170 ms 82980 KB Output is correct
21 Correct 163 ms 84088 KB Output is correct
22 Correct 95 ms 71748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 31640 KB Output is correct
2 Correct 16 ms 31676 KB Output is correct
3 Correct 18 ms 31752 KB Output is correct
4 Correct 19 ms 31664 KB Output is correct
5 Correct 20 ms 31708 KB Output is correct
6 Correct 20 ms 31828 KB Output is correct
7 Correct 19 ms 31660 KB Output is correct
8 Correct 21 ms 31700 KB Output is correct
9 Correct 19 ms 31700 KB Output is correct
10 Correct 19 ms 31768 KB Output is correct
11 Correct 18 ms 31644 KB Output is correct
12 Correct 17 ms 31700 KB Output is correct
13 Correct 17 ms 31660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 31640 KB Output is correct
2 Correct 16 ms 31676 KB Output is correct
3 Correct 18 ms 31752 KB Output is correct
4 Correct 19 ms 31664 KB Output is correct
5 Correct 20 ms 31708 KB Output is correct
6 Correct 20 ms 31828 KB Output is correct
7 Correct 19 ms 31660 KB Output is correct
8 Correct 21 ms 31700 KB Output is correct
9 Correct 19 ms 31700 KB Output is correct
10 Correct 19 ms 31768 KB Output is correct
11 Correct 18 ms 31644 KB Output is correct
12 Correct 17 ms 31700 KB Output is correct
13 Correct 17 ms 31660 KB Output is correct
14 Correct 16 ms 31572 KB Output is correct
15 Correct 16 ms 31640 KB Output is correct
16 Correct 18 ms 31700 KB Output is correct
17 Correct 21 ms 31712 KB Output is correct
18 Correct 20 ms 31700 KB Output is correct
19 Correct 16 ms 31572 KB Output is correct
20 Correct 21 ms 31660 KB Output is correct
21 Correct 21 ms 31672 KB Output is correct
22 Correct 19 ms 31784 KB Output is correct
23 Correct 16 ms 31640 KB Output is correct
24 Correct 16 ms 31700 KB Output is correct
25 Correct 18 ms 31700 KB Output is correct
26 Correct 17 ms 31716 KB Output is correct
27 Correct 19 ms 31700 KB Output is correct
28 Correct 17 ms 31572 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 31640 KB Output is correct
2 Correct 16 ms 31676 KB Output is correct
3 Correct 18 ms 31752 KB Output is correct
4 Correct 19 ms 31664 KB Output is correct
5 Correct 20 ms 31708 KB Output is correct
6 Correct 20 ms 31828 KB Output is correct
7 Correct 19 ms 31660 KB Output is correct
8 Correct 21 ms 31700 KB Output is correct
9 Correct 19 ms 31700 KB Output is correct
10 Correct 19 ms 31768 KB Output is correct
11 Correct 18 ms 31644 KB Output is correct
12 Correct 17 ms 31700 KB Output is correct
13 Correct 17 ms 31660 KB Output is correct
14 Correct 16 ms 31572 KB Output is correct
15 Correct 16 ms 31640 KB Output is correct
16 Correct 18 ms 31700 KB Output is correct
17 Correct 21 ms 31712 KB Output is correct
18 Correct 20 ms 31700 KB Output is correct
19 Correct 16 ms 31572 KB Output is correct
20 Correct 21 ms 31660 KB Output is correct
21 Correct 21 ms 31672 KB Output is correct
22 Correct 19 ms 31784 KB Output is correct
23 Correct 16 ms 31640 KB Output is correct
24 Correct 16 ms 31700 KB Output is correct
25 Correct 18 ms 31700 KB Output is correct
26 Correct 17 ms 31716 KB Output is correct
27 Correct 19 ms 31700 KB Output is correct
28 Correct 17 ms 31572 KB Output is correct
29 Correct 17 ms 31788 KB Output is correct
30 Correct 18 ms 31700 KB Output is correct
31 Correct 20 ms 31700 KB Output is correct
32 Correct 20 ms 31740 KB Output is correct
33 Correct 21 ms 31704 KB Output is correct
34 Correct 19 ms 31780 KB Output is correct
35 Correct 19 ms 31780 KB Output is correct
36 Correct 20 ms 31700 KB Output is correct
37 Correct 19 ms 31700 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 31640 KB Output is correct
2 Correct 16 ms 31676 KB Output is correct
3 Correct 18 ms 31752 KB Output is correct
4 Correct 19 ms 31664 KB Output is correct
5 Correct 20 ms 31708 KB Output is correct
6 Correct 20 ms 31828 KB Output is correct
7 Correct 19 ms 31660 KB Output is correct
8 Correct 21 ms 31700 KB Output is correct
9 Correct 19 ms 31700 KB Output is correct
10 Correct 19 ms 31768 KB Output is correct
11 Correct 18 ms 31644 KB Output is correct
12 Correct 17 ms 31700 KB Output is correct
13 Correct 17 ms 31660 KB Output is correct
14 Correct 16 ms 31572 KB Output is correct
15 Correct 16 ms 31640 KB Output is correct
16 Correct 18 ms 31700 KB Output is correct
17 Correct 21 ms 31712 KB Output is correct
18 Correct 20 ms 31700 KB Output is correct
19 Correct 16 ms 31572 KB Output is correct
20 Correct 21 ms 31660 KB Output is correct
21 Correct 21 ms 31672 KB Output is correct
22 Correct 19 ms 31784 KB Output is correct
23 Correct 16 ms 31640 KB Output is correct
24 Correct 16 ms 31700 KB Output is correct
25 Correct 18 ms 31700 KB Output is correct
26 Correct 17 ms 31716 KB Output is correct
27 Correct 19 ms 31700 KB Output is correct
28 Correct 17 ms 31572 KB Output is correct
29 Correct 17 ms 31788 KB Output is correct
30 Correct 18 ms 31700 KB Output is correct
31 Correct 20 ms 31700 KB Output is correct
32 Correct 20 ms 31740 KB Output is correct
33 Correct 21 ms 31704 KB Output is correct
34 Correct 19 ms 31780 KB Output is correct
35 Correct 19 ms 31780 KB Output is correct
36 Correct 20 ms 31700 KB Output is correct
37 Correct 19 ms 31700 KB Output is correct
38 Correct 81 ms 34080 KB Output is correct
39 Correct 87 ms 68132 KB Output is correct
40 Correct 90 ms 33676 KB Output is correct
41 Correct 98 ms 33348 KB Output is correct
42 Correct 76 ms 33828 KB Output is correct
43 Correct 73 ms 33520 KB Output is correct
44 Correct 32 ms 32188 KB Output is correct
45 Correct 111 ms 53284 KB Output is correct
46 Correct 104 ms 53136 KB Output is correct
47 Correct 88 ms 60544 KB Output is correct
48 Correct 89 ms 60608 KB Output is correct
49 Correct 102 ms 53448 KB Output is correct
50 Correct 107 ms 53504 KB Output is correct
51 Correct 87 ms 55540 KB Output is correct
52 Correct 86 ms 55560 KB Output is correct
53 Correct 38 ms 33320 KB Output is correct
54 Correct 121 ms 53100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 31572 KB Output is correct
2 Correct 19 ms 31572 KB Output is correct
3 Correct 17 ms 31580 KB Output is correct
4 Correct 16 ms 31660 KB Output is correct
5 Correct 29 ms 31784 KB Output is correct
6 Correct 17 ms 31700 KB Output is correct
7 Correct 17 ms 31732 KB Output is correct
8 Correct 17 ms 31580 KB Output is correct
9 Correct 16 ms 31568 KB Output is correct
10 Correct 16 ms 31700 KB Output is correct
11 Correct 17 ms 31644 KB Output is correct
12 Correct 18 ms 31752 KB Output is correct
13 Correct 55 ms 32204 KB Output is correct
14 Correct 88 ms 32328 KB Output is correct
15 Correct 66 ms 32204 KB Output is correct
16 Correct 132 ms 55204 KB Output is correct
17 Correct 245 ms 65188 KB Output is correct
18 Correct 388 ms 78748 KB Output is correct
19 Correct 192 ms 57320 KB Output is correct
20 Correct 178 ms 57264 KB Output is correct
21 Correct 197 ms 57264 KB Output is correct
22 Correct 212 ms 64328 KB Output is correct
23 Correct 175 ms 63900 KB Output is correct
24 Correct 223 ms 64420 KB Output is correct
25 Correct 230 ms 63896 KB Output is correct
26 Correct 213 ms 64152 KB Output is correct
27 Correct 249 ms 63228 KB Output is correct
28 Correct 194 ms 62756 KB Output is correct
29 Correct 176 ms 62748 KB Output is correct
30 Correct 235 ms 57692 KB Output is correct
31 Correct 226 ms 57776 KB Output is correct
32 Correct 208 ms 57764 KB Output is correct
33 Correct 178 ms 57776 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 17 ms 31572 KB Output is correct
2 Correct 17 ms 31632 KB Output is correct
3 Correct 16 ms 31596 KB Output is correct
4 Correct 36 ms 32032 KB Output is correct
5 Correct 59 ms 32136 KB Output is correct
6 Correct 21 ms 31772 KB Output is correct
7 Correct 19 ms 31752 KB Output is correct
8 Correct 21 ms 31700 KB Output is correct
9 Correct 88 ms 34112 KB Output is correct
10 Correct 94 ms 68152 KB Output is correct
11 Correct 26 ms 31828 KB Output is correct
12 Correct 74 ms 32184 KB Output is correct
13 Correct 144 ms 75248 KB Output is correct
14 Correct 125 ms 74440 KB Output is correct
15 Correct 230 ms 80012 KB Output is correct
16 Correct 416 ms 97668 KB Output is correct
17 Correct 178 ms 84440 KB Output is correct
18 Correct 128 ms 75412 KB Output is correct
19 Correct 190 ms 82860 KB Output is correct
20 Correct 170 ms 82980 KB Output is correct
21 Correct 163 ms 84088 KB Output is correct
22 Correct 95 ms 71748 KB Output is correct
23 Correct 16 ms 31640 KB Output is correct
24 Correct 16 ms 31676 KB Output is correct
25 Correct 18 ms 31752 KB Output is correct
26 Correct 19 ms 31664 KB Output is correct
27 Correct 20 ms 31708 KB Output is correct
28 Correct 20 ms 31828 KB Output is correct
29 Correct 19 ms 31660 KB Output is correct
30 Correct 21 ms 31700 KB Output is correct
31 Correct 19 ms 31700 KB Output is correct
32 Correct 19 ms 31768 KB Output is correct
33 Correct 18 ms 31644 KB Output is correct
34 Correct 17 ms 31700 KB Output is correct
35 Correct 17 ms 31660 KB Output is correct
36 Correct 16 ms 31572 KB Output is correct
37 Correct 16 ms 31640 KB Output is correct
38 Correct 18 ms 31700 KB Output is correct
39 Correct 21 ms 31712 KB Output is correct
40 Correct 20 ms 31700 KB Output is correct
41 Correct 16 ms 31572 KB Output is correct
42 Correct 21 ms 31660 KB Output is correct
43 Correct 21 ms 31672 KB Output is correct
44 Correct 19 ms 31784 KB Output is correct
45 Correct 16 ms 31640 KB Output is correct
46 Correct 16 ms 31700 KB Output is correct
47 Correct 18 ms 31700 KB Output is correct
48 Correct 17 ms 31716 KB Output is correct
49 Correct 19 ms 31700 KB Output is correct
50 Correct 17 ms 31572 KB Output is correct
51 Correct 17 ms 31788 KB Output is correct
52 Correct 18 ms 31700 KB Output is correct
53 Correct 20 ms 31700 KB Output is correct
54 Correct 20 ms 31740 KB Output is correct
55 Correct 21 ms 31704 KB Output is correct
56 Correct 19 ms 31780 KB Output is correct
57 Correct 19 ms 31780 KB Output is correct
58 Correct 20 ms 31700 KB Output is correct
59 Correct 19 ms 31700 KB Output is correct
60 Correct 81 ms 34080 KB Output is correct
61 Correct 87 ms 68132 KB Output is correct
62 Correct 90 ms 33676 KB Output is correct
63 Correct 98 ms 33348 KB Output is correct
64 Correct 76 ms 33828 KB Output is correct
65 Correct 73 ms 33520 KB Output is correct
66 Correct 32 ms 32188 KB Output is correct
67 Correct 111 ms 53284 KB Output is correct
68 Correct 104 ms 53136 KB Output is correct
69 Correct 88 ms 60544 KB Output is correct
70 Correct 89 ms 60608 KB Output is correct
71 Correct 102 ms 53448 KB Output is correct
72 Correct 107 ms 53504 KB Output is correct
73 Correct 87 ms 55540 KB Output is correct
74 Correct 86 ms 55560 KB Output is correct
75 Correct 38 ms 33320 KB Output is correct
76 Correct 121 ms 53100 KB Output is correct
77 Correct 16 ms 31572 KB Output is correct
78 Correct 19 ms 31572 KB Output is correct
79 Correct 17 ms 31580 KB Output is correct
80 Correct 16 ms 31660 KB Output is correct
81 Correct 29 ms 31784 KB Output is correct
82 Correct 17 ms 31700 KB Output is correct
83 Correct 17 ms 31732 KB Output is correct
84 Correct 17 ms 31580 KB Output is correct
85 Correct 16 ms 31568 KB Output is correct
86 Correct 16 ms 31700 KB Output is correct
87 Correct 17 ms 31644 KB Output is correct
88 Correct 18 ms 31752 KB Output is correct
89 Correct 55 ms 32204 KB Output is correct
90 Correct 88 ms 32328 KB Output is correct
91 Correct 66 ms 32204 KB Output is correct
92 Correct 132 ms 55204 KB Output is correct
93 Correct 245 ms 65188 KB Output is correct
94 Correct 388 ms 78748 KB Output is correct
95 Correct 192 ms 57320 KB Output is correct
96 Correct 178 ms 57264 KB Output is correct
97 Correct 197 ms 57264 KB Output is correct
98 Correct 212 ms 64328 KB Output is correct
99 Correct 175 ms 63900 KB Output is correct
100 Correct 223 ms 64420 KB Output is correct
101 Correct 230 ms 63896 KB Output is correct
102 Correct 213 ms 64152 KB Output is correct
103 Correct 249 ms 63228 KB Output is correct
104 Correct 194 ms 62756 KB Output is correct
105 Correct 176 ms 62748 KB Output is correct
106 Correct 235 ms 57692 KB Output is correct
107 Correct 226 ms 57776 KB Output is correct
108 Correct 208 ms 57764 KB Output is correct
109 Correct 178 ms 57776 KB Output is correct
110 Correct 97 ms 32208 KB Output is correct
111 Correct 59 ms 32184 KB Output is correct
112 Correct 302 ms 74716 KB Output is correct
113 Correct 192 ms 66548 KB Output is correct
114 Correct 206 ms 72676 KB Output is correct
115 Correct 101 ms 54672 KB Output is correct
116 Correct 181 ms 59368 KB Output is correct
117 Correct 555 ms 91280 KB Output is correct
118 Correct 122 ms 54152 KB Output is correct
119 Correct 111 ms 54120 KB Output is correct
120 Correct 28 ms 34132 KB Output is correct
121 Correct 228 ms 61528 KB Output is correct
122 Correct 236 ms 61552 KB Output is correct
123 Correct 181 ms 70120 KB Output is correct
124 Correct 182 ms 70088 KB Output is correct
125 Correct 182 ms 70964 KB Output is correct
126 Correct 412 ms 95204 KB Output is correct
127 Correct 216 ms 77288 KB Output is correct
128 Correct 183 ms 78716 KB Output is correct
129 Correct 207 ms 77244 KB Output is correct
130 Correct 194 ms 78512 KB Output is correct