Submission #581629

# Submission time Handle Problem Language Result Execution time Memory
581629 2022-06-22T23:40:44 Z Lobo Jail (JOI22_jail) C++17
100 / 100
2386 ms 584340 KB
#include<bits/stdc++.h>
using namespace std;

const long long inf = (long long) 1e18 + 10;
const int inf1 = (int) 1e9 + 10;
#define int long long
#define dbl long double
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define all(x) x.begin(), x.end()

const int maxn = 2e5+10;

int n, m, pp[maxn][25], pai[maxn], gr[50*maxn], h[maxn], s[maxn], t[maxn], up[maxn][25], dw[maxn][25];
vector<int> g[maxn], gts[50*maxn];
int cnt = 0;

void dfslca(int u, int ant) {
    pai[u] = ant;
    pp[u][0] = ant;
    for(int i = 1; i <= 20; i++) {
        pp[u][i] = pp[pp[u][i-1]][i-1];
    }

    //cria up
    up[u][0] = ++cnt;
    for(int i = 1; i <= 20; i++) {
        //liga dos de baixo pra cima
        up[u][i] = ++cnt;
        gts[up[u][i-1]].pb(up[u][i]);
        gts[up[pp[u][i-1]][i-1]].pb(up[u][i]);
    }

    //cria dw
    dw[u][0] = ++cnt;
    for(int i = 1; i <= 20; i++) {
        //liga do de cima para os debaixo
        dw[u][i] = ++cnt;
        gts[dw[u][i]].pb(dw[u][i-1]);
        gts[dw[u][i]].pb(dw[pp[u][i-1]][i-1]);
    }

    for(auto v : g[u]) if(v != ant) {
        h[v] = h[u]+1;
        dfslca(v,u);
    }
}

int lca(int u, int v) {
    if(h[u] < h[v]) swap(u,v);
    for(int i = 20; i >= 0; i--) {
        if(h[pp[u][i]] >= h[v]) {
            u = pp[u][i];
        }
    }
    if(u == v) return u;
    for(int i = 20; i >= 0; i--) {
        if(pp[u][i] != pp[v][i]) {
            u = pp[u][i];
            v = pp[v][i];
        }
    }
    return pp[u][0];
}

void solve() {
    cin >> n;
    cnt = 0;
    for(int i = 0; i <= n; i++) {
        g[i].clear();
    }
    for(int i = 0; i <= n*50; i++) {
        gts[i].clear();
        gr[i] = 0;
    }
    for(int i = 0; i < n-1; i++) {
        int u,v; cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    cin >> m;
    cnt = m;
    h[0] = 0;
    h[1] = 1;
    dfslca(1,0);
    for(int i = 1; i <= m; i++) {
        cin >> s[i] >> t[i];
        if(s[i] == t[i]) return;
        //liga do i para s[i] e t[i]
        gts[i].pb(up[s[i]][0]);
        gts[dw[t[i]][0]].pb(i);
    }

    for(int i = 1; i <= m; i++) {
        int lc = lca(s[i],t[i]);
        // cout << s[i] << " "<< t[i] << " " << lc << endl;
        int v;
        //nao quero ligar do s[i] ate mim, entao faco o dw separado
        gts[i].pb(dw[s[i]][0]);
        v = pai[s[i]];
        //quero ir ate um antes do lc
        for(int j = 20; j >= 0; j--) {
            if(v != 0 && h[pp[v][j]] >= h[lc]) {
                gts[i].pb(dw[v][j]);
                gts[up[v][j]].pb(i);
                v = pp[v][j];
            }
        }
        
        gts[up[t[i]][0]].pb(i);
        v = pai[t[i]];
        //quero ir ate um antes do lc
        for(int j = 20; j >= 0; j--) {
            if(v != 0 && h[pp[v][j]] >= h[lc]) {
                gts[i].pb(dw[v][j]);
                gts[up[v][j]].pb(i);
                v = pp[v][j];
            }
        }

        if(lc != s[i] && lc != t[i]) {
            gts[i].pb(dw[lc][0]);
            gts[up[lc][0]].pb(i);
        }
    }

    for(int i = 1; i <= cnt; i++) {
        for(auto v : gts[i]) {
            gr[v]++;
        }
    }

    queue<int> q;
    for(int i = 1; i <= cnt; i++) {
        // cout << i << " " << gr[i] << endl;
        if(gr[i] == 0) q.push(i);
    }

    while(q.size()) {
        int u = q.front();
        q.pop();
        for(auto v : gts[u]) {
            if(--gr[v] == 0) q.push(v);
        }
    }

    string ans = "Yes";
    for(int i = 1; i <= m; i++) {
        if(gr[i] != 0) ans = "No";
    }
    cout << ans << endl;
}

int32_t main() {
    ios::sync_with_stdio(false); cin.tie(0);

    // freopen("in.in", "r", stdin);
    // freopen("out.out", "w", stdout);
    int tt = 1;
    cin >> tt;
    while(tt--) {
        solve();
    }

}
# Verdict Execution time Memory Grader output
1 Correct 109 ms 239920 KB Output is correct
2 Correct 114 ms 239812 KB Output is correct
3 Correct 109 ms 239812 KB Output is correct
4 Correct 212 ms 240212 KB Output is correct
5 Correct 339 ms 240452 KB Output is correct
6 Correct 123 ms 240588 KB Output is correct
7 Correct 120 ms 240600 KB Output is correct
8 Correct 122 ms 240636 KB Output is correct
9 Correct 745 ms 256180 KB Output is correct
10 Correct 1799 ms 531136 KB Output is correct
11 Correct 157 ms 240096 KB Output is correct
12 Correct 382 ms 240488 KB Output is correct
13 Correct 2178 ms 545260 KB Output is correct
14 Correct 1771 ms 546200 KB Output is correct
15 Correct 1827 ms 557376 KB Output is correct
16 Correct 2386 ms 584340 KB Output is correct
17 Correct 2066 ms 567524 KB Output is correct
18 Correct 2041 ms 548496 KB Output is correct
19 Correct 2120 ms 562044 KB Output is correct
20 Correct 1935 ms 562112 KB Output is correct
21 Correct 1613 ms 560736 KB Output is correct
22 Correct 1729 ms 540156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 115 ms 239832 KB Output is correct
2 Correct 148 ms 239840 KB Output is correct
3 Correct 124 ms 240552 KB Output is correct
4 Correct 124 ms 240496 KB Output is correct
5 Correct 129 ms 240592 KB Output is correct
6 Correct 129 ms 240604 KB Output is correct
7 Correct 124 ms 240552 KB Output is correct
8 Correct 123 ms 240516 KB Output is correct
9 Correct 130 ms 240604 KB Output is correct
10 Correct 134 ms 240676 KB Output is correct
11 Correct 124 ms 240516 KB Output is correct
12 Correct 116 ms 240520 KB Output is correct
13 Correct 123 ms 240504 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 115 ms 239832 KB Output is correct
2 Correct 148 ms 239840 KB Output is correct
3 Correct 124 ms 240552 KB Output is correct
4 Correct 124 ms 240496 KB Output is correct
5 Correct 129 ms 240592 KB Output is correct
6 Correct 129 ms 240604 KB Output is correct
7 Correct 124 ms 240552 KB Output is correct
8 Correct 123 ms 240516 KB Output is correct
9 Correct 130 ms 240604 KB Output is correct
10 Correct 134 ms 240676 KB Output is correct
11 Correct 124 ms 240516 KB Output is correct
12 Correct 116 ms 240520 KB Output is correct
13 Correct 123 ms 240504 KB Output is correct
14 Correct 112 ms 239892 KB Output is correct
15 Correct 111 ms 239904 KB Output is correct
16 Correct 121 ms 240548 KB Output is correct
17 Correct 124 ms 240620 KB Output is correct
18 Correct 126 ms 240584 KB Output is correct
19 Correct 111 ms 239820 KB Output is correct
20 Correct 125 ms 240548 KB Output is correct
21 Correct 123 ms 240584 KB Output is correct
22 Correct 121 ms 240592 KB Output is correct
23 Correct 117 ms 240020 KB Output is correct
24 Correct 115 ms 240240 KB Output is correct
25 Correct 137 ms 240428 KB Output is correct
26 Correct 122 ms 240416 KB Output is correct
27 Correct 128 ms 240588 KB Output is correct
28 Correct 115 ms 239920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 115 ms 239832 KB Output is correct
2 Correct 148 ms 239840 KB Output is correct
3 Correct 124 ms 240552 KB Output is correct
4 Correct 124 ms 240496 KB Output is correct
5 Correct 129 ms 240592 KB Output is correct
6 Correct 129 ms 240604 KB Output is correct
7 Correct 124 ms 240552 KB Output is correct
8 Correct 123 ms 240516 KB Output is correct
9 Correct 130 ms 240604 KB Output is correct
10 Correct 134 ms 240676 KB Output is correct
11 Correct 124 ms 240516 KB Output is correct
12 Correct 116 ms 240520 KB Output is correct
13 Correct 123 ms 240504 KB Output is correct
14 Correct 112 ms 239892 KB Output is correct
15 Correct 111 ms 239904 KB Output is correct
16 Correct 121 ms 240548 KB Output is correct
17 Correct 124 ms 240620 KB Output is correct
18 Correct 126 ms 240584 KB Output is correct
19 Correct 111 ms 239820 KB Output is correct
20 Correct 125 ms 240548 KB Output is correct
21 Correct 123 ms 240584 KB Output is correct
22 Correct 121 ms 240592 KB Output is correct
23 Correct 117 ms 240020 KB Output is correct
24 Correct 115 ms 240240 KB Output is correct
25 Correct 137 ms 240428 KB Output is correct
26 Correct 122 ms 240416 KB Output is correct
27 Correct 128 ms 240588 KB Output is correct
28 Correct 115 ms 239920 KB Output is correct
29 Correct 124 ms 240600 KB Output is correct
30 Correct 123 ms 240712 KB Output is correct
31 Correct 123 ms 240788 KB Output is correct
32 Correct 131 ms 240628 KB Output is correct
33 Correct 138 ms 240636 KB Output is correct
34 Correct 118 ms 240372 KB Output is correct
35 Correct 122 ms 240404 KB Output is correct
36 Correct 131 ms 240460 KB Output is correct
37 Correct 123 ms 240432 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 115 ms 239832 KB Output is correct
2 Correct 148 ms 239840 KB Output is correct
3 Correct 124 ms 240552 KB Output is correct
4 Correct 124 ms 240496 KB Output is correct
5 Correct 129 ms 240592 KB Output is correct
6 Correct 129 ms 240604 KB Output is correct
7 Correct 124 ms 240552 KB Output is correct
8 Correct 123 ms 240516 KB Output is correct
9 Correct 130 ms 240604 KB Output is correct
10 Correct 134 ms 240676 KB Output is correct
11 Correct 124 ms 240516 KB Output is correct
12 Correct 116 ms 240520 KB Output is correct
13 Correct 123 ms 240504 KB Output is correct
14 Correct 112 ms 239892 KB Output is correct
15 Correct 111 ms 239904 KB Output is correct
16 Correct 121 ms 240548 KB Output is correct
17 Correct 124 ms 240620 KB Output is correct
18 Correct 126 ms 240584 KB Output is correct
19 Correct 111 ms 239820 KB Output is correct
20 Correct 125 ms 240548 KB Output is correct
21 Correct 123 ms 240584 KB Output is correct
22 Correct 121 ms 240592 KB Output is correct
23 Correct 117 ms 240020 KB Output is correct
24 Correct 115 ms 240240 KB Output is correct
25 Correct 137 ms 240428 KB Output is correct
26 Correct 122 ms 240416 KB Output is correct
27 Correct 128 ms 240588 KB Output is correct
28 Correct 115 ms 239920 KB Output is correct
29 Correct 124 ms 240600 KB Output is correct
30 Correct 123 ms 240712 KB Output is correct
31 Correct 123 ms 240788 KB Output is correct
32 Correct 131 ms 240628 KB Output is correct
33 Correct 138 ms 240636 KB Output is correct
34 Correct 118 ms 240372 KB Output is correct
35 Correct 122 ms 240404 KB Output is correct
36 Correct 131 ms 240460 KB Output is correct
37 Correct 123 ms 240432 KB Output is correct
38 Correct 728 ms 256076 KB Output is correct
39 Correct 1732 ms 530840 KB Output is correct
40 Correct 713 ms 258748 KB Output is correct
41 Correct 759 ms 257688 KB Output is correct
42 Correct 703 ms 257772 KB Output is correct
43 Correct 886 ms 257376 KB Output is correct
44 Correct 175 ms 242928 KB Output is correct
45 Correct 1395 ms 536536 KB Output is correct
46 Correct 1377 ms 536700 KB Output is correct
47 Correct 1824 ms 535824 KB Output is correct
48 Correct 1948 ms 536068 KB Output is correct
49 Correct 1318 ms 542840 KB Output is correct
50 Correct 1321 ms 543304 KB Output is correct
51 Correct 1551 ms 541348 KB Output is correct
52 Correct 1603 ms 542128 KB Output is correct
53 Correct 233 ms 261024 KB Output is correct
54 Correct 1454 ms 536420 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 117 ms 239864 KB Output is correct
2 Correct 108 ms 239828 KB Output is correct
3 Correct 112 ms 239908 KB Output is correct
4 Correct 112 ms 239860 KB Output is correct
5 Correct 147 ms 240000 KB Output is correct
6 Correct 112 ms 240544 KB Output is correct
7 Correct 113 ms 240460 KB Output is correct
8 Correct 107 ms 239856 KB Output is correct
9 Correct 110 ms 239984 KB Output is correct
10 Correct 110 ms 240180 KB Output is correct
11 Correct 108 ms 239960 KB Output is correct
12 Correct 116 ms 240424 KB Output is correct
13 Correct 241 ms 240460 KB Output is correct
14 Correct 438 ms 240524 KB Output is correct
15 Correct 288 ms 240420 KB Output is correct
16 Correct 1377 ms 541764 KB Output is correct
17 Correct 1292 ms 562768 KB Output is correct
18 Correct 1482 ms 576076 KB Output is correct
19 Correct 1514 ms 540600 KB Output is correct
20 Correct 1421 ms 542724 KB Output is correct
21 Correct 1500 ms 541084 KB Output is correct
22 Correct 1294 ms 562812 KB Output is correct
23 Correct 1206 ms 562612 KB Output is correct
24 Correct 1290 ms 562852 KB Output is correct
25 Correct 1267 ms 562708 KB Output is correct
26 Correct 1292 ms 562696 KB Output is correct
27 Correct 1211 ms 566724 KB Output is correct
28 Correct 1109 ms 566612 KB Output is correct
29 Correct 1131 ms 566688 KB Output is correct
30 Correct 1620 ms 551776 KB Output is correct
31 Correct 1555 ms 551712 KB Output is correct
32 Correct 1175 ms 560680 KB Output is correct
33 Correct 1103 ms 560492 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 109 ms 239920 KB Output is correct
2 Correct 114 ms 239812 KB Output is correct
3 Correct 109 ms 239812 KB Output is correct
4 Correct 212 ms 240212 KB Output is correct
5 Correct 339 ms 240452 KB Output is correct
6 Correct 123 ms 240588 KB Output is correct
7 Correct 120 ms 240600 KB Output is correct
8 Correct 122 ms 240636 KB Output is correct
9 Correct 745 ms 256180 KB Output is correct
10 Correct 1799 ms 531136 KB Output is correct
11 Correct 157 ms 240096 KB Output is correct
12 Correct 382 ms 240488 KB Output is correct
13 Correct 2178 ms 545260 KB Output is correct
14 Correct 1771 ms 546200 KB Output is correct
15 Correct 1827 ms 557376 KB Output is correct
16 Correct 2386 ms 584340 KB Output is correct
17 Correct 2066 ms 567524 KB Output is correct
18 Correct 2041 ms 548496 KB Output is correct
19 Correct 2120 ms 562044 KB Output is correct
20 Correct 1935 ms 562112 KB Output is correct
21 Correct 1613 ms 560736 KB Output is correct
22 Correct 1729 ms 540156 KB Output is correct
23 Correct 115 ms 239832 KB Output is correct
24 Correct 148 ms 239840 KB Output is correct
25 Correct 124 ms 240552 KB Output is correct
26 Correct 124 ms 240496 KB Output is correct
27 Correct 129 ms 240592 KB Output is correct
28 Correct 129 ms 240604 KB Output is correct
29 Correct 124 ms 240552 KB Output is correct
30 Correct 123 ms 240516 KB Output is correct
31 Correct 130 ms 240604 KB Output is correct
32 Correct 134 ms 240676 KB Output is correct
33 Correct 124 ms 240516 KB Output is correct
34 Correct 116 ms 240520 KB Output is correct
35 Correct 123 ms 240504 KB Output is correct
36 Correct 112 ms 239892 KB Output is correct
37 Correct 111 ms 239904 KB Output is correct
38 Correct 121 ms 240548 KB Output is correct
39 Correct 124 ms 240620 KB Output is correct
40 Correct 126 ms 240584 KB Output is correct
41 Correct 111 ms 239820 KB Output is correct
42 Correct 125 ms 240548 KB Output is correct
43 Correct 123 ms 240584 KB Output is correct
44 Correct 121 ms 240592 KB Output is correct
45 Correct 117 ms 240020 KB Output is correct
46 Correct 115 ms 240240 KB Output is correct
47 Correct 137 ms 240428 KB Output is correct
48 Correct 122 ms 240416 KB Output is correct
49 Correct 128 ms 240588 KB Output is correct
50 Correct 115 ms 239920 KB Output is correct
51 Correct 124 ms 240600 KB Output is correct
52 Correct 123 ms 240712 KB Output is correct
53 Correct 123 ms 240788 KB Output is correct
54 Correct 131 ms 240628 KB Output is correct
55 Correct 138 ms 240636 KB Output is correct
56 Correct 118 ms 240372 KB Output is correct
57 Correct 122 ms 240404 KB Output is correct
58 Correct 131 ms 240460 KB Output is correct
59 Correct 123 ms 240432 KB Output is correct
60 Correct 728 ms 256076 KB Output is correct
61 Correct 1732 ms 530840 KB Output is correct
62 Correct 713 ms 258748 KB Output is correct
63 Correct 759 ms 257688 KB Output is correct
64 Correct 703 ms 257772 KB Output is correct
65 Correct 886 ms 257376 KB Output is correct
66 Correct 175 ms 242928 KB Output is correct
67 Correct 1395 ms 536536 KB Output is correct
68 Correct 1377 ms 536700 KB Output is correct
69 Correct 1824 ms 535824 KB Output is correct
70 Correct 1948 ms 536068 KB Output is correct
71 Correct 1318 ms 542840 KB Output is correct
72 Correct 1321 ms 543304 KB Output is correct
73 Correct 1551 ms 541348 KB Output is correct
74 Correct 1603 ms 542128 KB Output is correct
75 Correct 233 ms 261024 KB Output is correct
76 Correct 1454 ms 536420 KB Output is correct
77 Correct 117 ms 239864 KB Output is correct
78 Correct 108 ms 239828 KB Output is correct
79 Correct 112 ms 239908 KB Output is correct
80 Correct 112 ms 239860 KB Output is correct
81 Correct 147 ms 240000 KB Output is correct
82 Correct 112 ms 240544 KB Output is correct
83 Correct 113 ms 240460 KB Output is correct
84 Correct 107 ms 239856 KB Output is correct
85 Correct 110 ms 239984 KB Output is correct
86 Correct 110 ms 240180 KB Output is correct
87 Correct 108 ms 239960 KB Output is correct
88 Correct 116 ms 240424 KB Output is correct
89 Correct 241 ms 240460 KB Output is correct
90 Correct 438 ms 240524 KB Output is correct
91 Correct 288 ms 240420 KB Output is correct
92 Correct 1377 ms 541764 KB Output is correct
93 Correct 1292 ms 562768 KB Output is correct
94 Correct 1482 ms 576076 KB Output is correct
95 Correct 1514 ms 540600 KB Output is correct
96 Correct 1421 ms 542724 KB Output is correct
97 Correct 1500 ms 541084 KB Output is correct
98 Correct 1294 ms 562812 KB Output is correct
99 Correct 1206 ms 562612 KB Output is correct
100 Correct 1290 ms 562852 KB Output is correct
101 Correct 1267 ms 562708 KB Output is correct
102 Correct 1292 ms 562696 KB Output is correct
103 Correct 1211 ms 566724 KB Output is correct
104 Correct 1109 ms 566612 KB Output is correct
105 Correct 1131 ms 566688 KB Output is correct
106 Correct 1620 ms 551776 KB Output is correct
107 Correct 1555 ms 551712 KB Output is correct
108 Correct 1175 ms 560680 KB Output is correct
109 Correct 1103 ms 560492 KB Output is correct
110 Correct 367 ms 242124 KB Output is correct
111 Correct 293 ms 241252 KB Output is correct
112 Correct 1838 ms 562916 KB Output is correct
113 Correct 2145 ms 544544 KB Output is correct
114 Correct 1558 ms 559008 KB Output is correct
115 Correct 1279 ms 536552 KB Output is correct
116 Correct 1701 ms 553208 KB Output is correct
117 Correct 1608 ms 582100 KB Output is correct
118 Correct 1439 ms 537716 KB Output is correct
119 Correct 1419 ms 537992 KB Output is correct
120 Correct 203 ms 266188 KB Output is correct
121 Correct 1590 ms 562092 KB Output is correct
122 Correct 1806 ms 555324 KB Output is correct
123 Correct 2264 ms 549312 KB Output is correct
124 Correct 1756 ms 549112 KB Output is correct
125 Correct 2258 ms 547492 KB Output is correct
126 Correct 2279 ms 582440 KB Output is correct
127 Correct 1941 ms 567992 KB Output is correct
128 Correct 1801 ms 567672 KB Output is correct
129 Correct 2063 ms 568060 KB Output is correct
130 Correct 1823 ms 568084 KB Output is correct