답안 #582116

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
582116 2022-06-23T11:51:54 Z Lobo Jail (JOI22_jail) C++17
100 / 100
2133 ms 336292 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 = 12e4+10;

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

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

    //cria up
    up[u][0] = ++cnt;
    for(int i = 1; i <= 18; 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 <= 18; 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 = 18; i >= 0; i--) {
        if(h[pp[u][i]] >= h[v]) {
            u = pp[u][i];
        }
    }
    if(u == v) return u;
    for(int i = 18; 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-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 = 18; 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 = 18; 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;


    for(int i = 0; i <= cnt; i++) {
        gts[i].clear();
        gr[i] = 0;
    }
}

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();
    }

}
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 115828 KB Output is correct
2 Correct 69 ms 115844 KB Output is correct
3 Correct 66 ms 115908 KB Output is correct
4 Correct 161 ms 116116 KB Output is correct
5 Correct 278 ms 116172 KB Output is correct
6 Correct 68 ms 116276 KB Output is correct
7 Correct 79 ms 116316 KB Output is correct
8 Correct 68 ms 116312 KB Output is correct
9 Correct 677 ms 126256 KB Output is correct
10 Correct 1746 ms 315232 KB Output is correct
11 Correct 110 ms 115952 KB Output is correct
12 Correct 323 ms 116088 KB Output is correct
13 Correct 1917 ms 320540 KB Output is correct
14 Correct 1653 ms 320588 KB Output is correct
15 Correct 1636 ms 322512 KB Output is correct
16 Correct 2133 ms 334416 KB Output is correct
17 Correct 1825 ms 324332 KB Output is correct
18 Correct 1751 ms 324052 KB Output is correct
19 Correct 1936 ms 324252 KB Output is correct
20 Correct 1776 ms 324316 KB Output is correct
21 Correct 1536 ms 323836 KB Output is correct
22 Correct 1931 ms 319580 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 115912 KB Output is correct
2 Correct 79 ms 115840 KB Output is correct
3 Correct 72 ms 116328 KB Output is correct
4 Correct 85 ms 116340 KB Output is correct
5 Correct 70 ms 116328 KB Output is correct
6 Correct 89 ms 116300 KB Output is correct
7 Correct 70 ms 116264 KB Output is correct
8 Correct 79 ms 116340 KB Output is correct
9 Correct 70 ms 116276 KB Output is correct
10 Correct 82 ms 116268 KB Output is correct
11 Correct 77 ms 116344 KB Output is correct
12 Correct 79 ms 116296 KB Output is correct
13 Correct 75 ms 116308 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 115912 KB Output is correct
2 Correct 79 ms 115840 KB Output is correct
3 Correct 72 ms 116328 KB Output is correct
4 Correct 85 ms 116340 KB Output is correct
5 Correct 70 ms 116328 KB Output is correct
6 Correct 89 ms 116300 KB Output is correct
7 Correct 70 ms 116264 KB Output is correct
8 Correct 79 ms 116340 KB Output is correct
9 Correct 70 ms 116276 KB Output is correct
10 Correct 82 ms 116268 KB Output is correct
11 Correct 77 ms 116344 KB Output is correct
12 Correct 79 ms 116296 KB Output is correct
13 Correct 75 ms 116308 KB Output is correct
14 Correct 80 ms 115916 KB Output is correct
15 Correct 87 ms 115920 KB Output is correct
16 Correct 75 ms 116316 KB Output is correct
17 Correct 77 ms 116360 KB Output is correct
18 Correct 86 ms 116316 KB Output is correct
19 Correct 71 ms 115812 KB Output is correct
20 Correct 71 ms 116324 KB Output is correct
21 Correct 75 ms 116320 KB Output is correct
22 Correct 75 ms 116336 KB Output is correct
23 Correct 78 ms 115808 KB Output is correct
24 Correct 61 ms 116064 KB Output is correct
25 Correct 83 ms 116252 KB Output is correct
26 Correct 62 ms 116308 KB Output is correct
27 Correct 90 ms 116284 KB Output is correct
28 Correct 80 ms 115868 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 115912 KB Output is correct
2 Correct 79 ms 115840 KB Output is correct
3 Correct 72 ms 116328 KB Output is correct
4 Correct 85 ms 116340 KB Output is correct
5 Correct 70 ms 116328 KB Output is correct
6 Correct 89 ms 116300 KB Output is correct
7 Correct 70 ms 116264 KB Output is correct
8 Correct 79 ms 116340 KB Output is correct
9 Correct 70 ms 116276 KB Output is correct
10 Correct 82 ms 116268 KB Output is correct
11 Correct 77 ms 116344 KB Output is correct
12 Correct 79 ms 116296 KB Output is correct
13 Correct 75 ms 116308 KB Output is correct
14 Correct 80 ms 115916 KB Output is correct
15 Correct 87 ms 115920 KB Output is correct
16 Correct 75 ms 116316 KB Output is correct
17 Correct 77 ms 116360 KB Output is correct
18 Correct 86 ms 116316 KB Output is correct
19 Correct 71 ms 115812 KB Output is correct
20 Correct 71 ms 116324 KB Output is correct
21 Correct 75 ms 116320 KB Output is correct
22 Correct 75 ms 116336 KB Output is correct
23 Correct 78 ms 115808 KB Output is correct
24 Correct 61 ms 116064 KB Output is correct
25 Correct 83 ms 116252 KB Output is correct
26 Correct 62 ms 116308 KB Output is correct
27 Correct 90 ms 116284 KB Output is correct
28 Correct 80 ms 115868 KB Output is correct
29 Correct 73 ms 116272 KB Output is correct
30 Correct 72 ms 116348 KB Output is correct
31 Correct 83 ms 116376 KB Output is correct
32 Correct 78 ms 116372 KB Output is correct
33 Correct 84 ms 116272 KB Output is correct
34 Correct 73 ms 116216 KB Output is correct
35 Correct 80 ms 116248 KB Output is correct
36 Correct 67 ms 116300 KB Output is correct
37 Correct 90 ms 116228 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 65 ms 115912 KB Output is correct
2 Correct 79 ms 115840 KB Output is correct
3 Correct 72 ms 116328 KB Output is correct
4 Correct 85 ms 116340 KB Output is correct
5 Correct 70 ms 116328 KB Output is correct
6 Correct 89 ms 116300 KB Output is correct
7 Correct 70 ms 116264 KB Output is correct
8 Correct 79 ms 116340 KB Output is correct
9 Correct 70 ms 116276 KB Output is correct
10 Correct 82 ms 116268 KB Output is correct
11 Correct 77 ms 116344 KB Output is correct
12 Correct 79 ms 116296 KB Output is correct
13 Correct 75 ms 116308 KB Output is correct
14 Correct 80 ms 115916 KB Output is correct
15 Correct 87 ms 115920 KB Output is correct
16 Correct 75 ms 116316 KB Output is correct
17 Correct 77 ms 116360 KB Output is correct
18 Correct 86 ms 116316 KB Output is correct
19 Correct 71 ms 115812 KB Output is correct
20 Correct 71 ms 116324 KB Output is correct
21 Correct 75 ms 116320 KB Output is correct
22 Correct 75 ms 116336 KB Output is correct
23 Correct 78 ms 115808 KB Output is correct
24 Correct 61 ms 116064 KB Output is correct
25 Correct 83 ms 116252 KB Output is correct
26 Correct 62 ms 116308 KB Output is correct
27 Correct 90 ms 116284 KB Output is correct
28 Correct 80 ms 115868 KB Output is correct
29 Correct 73 ms 116272 KB Output is correct
30 Correct 72 ms 116348 KB Output is correct
31 Correct 83 ms 116376 KB Output is correct
32 Correct 78 ms 116372 KB Output is correct
33 Correct 84 ms 116272 KB Output is correct
34 Correct 73 ms 116216 KB Output is correct
35 Correct 80 ms 116248 KB Output is correct
36 Correct 67 ms 116300 KB Output is correct
37 Correct 90 ms 116228 KB Output is correct
38 Correct 631 ms 126164 KB Output is correct
39 Correct 1668 ms 315220 KB Output is correct
40 Correct 615 ms 127508 KB Output is correct
41 Correct 650 ms 127096 KB Output is correct
42 Correct 560 ms 127260 KB Output is correct
43 Correct 722 ms 126676 KB Output is correct
44 Correct 113 ms 117852 KB Output is correct
45 Correct 1252 ms 311120 KB Output is correct
46 Correct 1228 ms 311076 KB Output is correct
47 Correct 1665 ms 310220 KB Output is correct
48 Correct 1862 ms 310248 KB Output is correct
49 Correct 1134 ms 313604 KB Output is correct
50 Correct 1284 ms 313828 KB Output is correct
51 Correct 1506 ms 312400 KB Output is correct
52 Correct 1447 ms 312688 KB Output is correct
53 Correct 193 ms 129564 KB Output is correct
54 Correct 1390 ms 311200 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 115852 KB Output is correct
2 Correct 64 ms 115848 KB Output is correct
3 Correct 65 ms 115912 KB Output is correct
4 Correct 65 ms 115832 KB Output is correct
5 Correct 108 ms 115916 KB Output is correct
6 Correct 67 ms 116312 KB Output is correct
7 Correct 86 ms 116224 KB Output is correct
8 Correct 74 ms 115852 KB Output is correct
9 Correct 80 ms 115848 KB Output is correct
10 Correct 65 ms 116092 KB Output is correct
11 Correct 74 ms 115952 KB Output is correct
12 Correct 71 ms 116256 KB Output is correct
13 Correct 201 ms 116212 KB Output is correct
14 Correct 323 ms 116248 KB Output is correct
15 Correct 246 ms 116184 KB Output is correct
16 Correct 1287 ms 314516 KB Output is correct
17 Correct 1204 ms 326352 KB Output is correct
18 Correct 1393 ms 335412 KB Output is correct
19 Correct 1403 ms 314576 KB Output is correct
20 Correct 1279 ms 315520 KB Output is correct
21 Correct 1333 ms 314656 KB Output is correct
22 Correct 1178 ms 326296 KB Output is correct
23 Correct 1067 ms 326324 KB Output is correct
24 Correct 1204 ms 326332 KB Output is correct
25 Correct 1221 ms 326272 KB Output is correct
26 Correct 1235 ms 326236 KB Output is correct
27 Correct 1340 ms 328012 KB Output is correct
28 Correct 1122 ms 328136 KB Output is correct
29 Correct 1159 ms 328004 KB Output is correct
30 Correct 1568 ms 318976 KB Output is correct
31 Correct 1355 ms 318924 KB Output is correct
32 Correct 1178 ms 322908 KB Output is correct
33 Correct 1006 ms 322836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 73 ms 115828 KB Output is correct
2 Correct 69 ms 115844 KB Output is correct
3 Correct 66 ms 115908 KB Output is correct
4 Correct 161 ms 116116 KB Output is correct
5 Correct 278 ms 116172 KB Output is correct
6 Correct 68 ms 116276 KB Output is correct
7 Correct 79 ms 116316 KB Output is correct
8 Correct 68 ms 116312 KB Output is correct
9 Correct 677 ms 126256 KB Output is correct
10 Correct 1746 ms 315232 KB Output is correct
11 Correct 110 ms 115952 KB Output is correct
12 Correct 323 ms 116088 KB Output is correct
13 Correct 1917 ms 320540 KB Output is correct
14 Correct 1653 ms 320588 KB Output is correct
15 Correct 1636 ms 322512 KB Output is correct
16 Correct 2133 ms 334416 KB Output is correct
17 Correct 1825 ms 324332 KB Output is correct
18 Correct 1751 ms 324052 KB Output is correct
19 Correct 1936 ms 324252 KB Output is correct
20 Correct 1776 ms 324316 KB Output is correct
21 Correct 1536 ms 323836 KB Output is correct
22 Correct 1931 ms 319580 KB Output is correct
23 Correct 65 ms 115912 KB Output is correct
24 Correct 79 ms 115840 KB Output is correct
25 Correct 72 ms 116328 KB Output is correct
26 Correct 85 ms 116340 KB Output is correct
27 Correct 70 ms 116328 KB Output is correct
28 Correct 89 ms 116300 KB Output is correct
29 Correct 70 ms 116264 KB Output is correct
30 Correct 79 ms 116340 KB Output is correct
31 Correct 70 ms 116276 KB Output is correct
32 Correct 82 ms 116268 KB Output is correct
33 Correct 77 ms 116344 KB Output is correct
34 Correct 79 ms 116296 KB Output is correct
35 Correct 75 ms 116308 KB Output is correct
36 Correct 80 ms 115916 KB Output is correct
37 Correct 87 ms 115920 KB Output is correct
38 Correct 75 ms 116316 KB Output is correct
39 Correct 77 ms 116360 KB Output is correct
40 Correct 86 ms 116316 KB Output is correct
41 Correct 71 ms 115812 KB Output is correct
42 Correct 71 ms 116324 KB Output is correct
43 Correct 75 ms 116320 KB Output is correct
44 Correct 75 ms 116336 KB Output is correct
45 Correct 78 ms 115808 KB Output is correct
46 Correct 61 ms 116064 KB Output is correct
47 Correct 83 ms 116252 KB Output is correct
48 Correct 62 ms 116308 KB Output is correct
49 Correct 90 ms 116284 KB Output is correct
50 Correct 80 ms 115868 KB Output is correct
51 Correct 73 ms 116272 KB Output is correct
52 Correct 72 ms 116348 KB Output is correct
53 Correct 83 ms 116376 KB Output is correct
54 Correct 78 ms 116372 KB Output is correct
55 Correct 84 ms 116272 KB Output is correct
56 Correct 73 ms 116216 KB Output is correct
57 Correct 80 ms 116248 KB Output is correct
58 Correct 67 ms 116300 KB Output is correct
59 Correct 90 ms 116228 KB Output is correct
60 Correct 631 ms 126164 KB Output is correct
61 Correct 1668 ms 315220 KB Output is correct
62 Correct 615 ms 127508 KB Output is correct
63 Correct 650 ms 127096 KB Output is correct
64 Correct 560 ms 127260 KB Output is correct
65 Correct 722 ms 126676 KB Output is correct
66 Correct 113 ms 117852 KB Output is correct
67 Correct 1252 ms 311120 KB Output is correct
68 Correct 1228 ms 311076 KB Output is correct
69 Correct 1665 ms 310220 KB Output is correct
70 Correct 1862 ms 310248 KB Output is correct
71 Correct 1134 ms 313604 KB Output is correct
72 Correct 1284 ms 313828 KB Output is correct
73 Correct 1506 ms 312400 KB Output is correct
74 Correct 1447 ms 312688 KB Output is correct
75 Correct 193 ms 129564 KB Output is correct
76 Correct 1390 ms 311200 KB Output is correct
77 Correct 73 ms 115852 KB Output is correct
78 Correct 64 ms 115848 KB Output is correct
79 Correct 65 ms 115912 KB Output is correct
80 Correct 65 ms 115832 KB Output is correct
81 Correct 108 ms 115916 KB Output is correct
82 Correct 67 ms 116312 KB Output is correct
83 Correct 86 ms 116224 KB Output is correct
84 Correct 74 ms 115852 KB Output is correct
85 Correct 80 ms 115848 KB Output is correct
86 Correct 65 ms 116092 KB Output is correct
87 Correct 74 ms 115952 KB Output is correct
88 Correct 71 ms 116256 KB Output is correct
89 Correct 201 ms 116212 KB Output is correct
90 Correct 323 ms 116248 KB Output is correct
91 Correct 246 ms 116184 KB Output is correct
92 Correct 1287 ms 314516 KB Output is correct
93 Correct 1204 ms 326352 KB Output is correct
94 Correct 1393 ms 335412 KB Output is correct
95 Correct 1403 ms 314576 KB Output is correct
96 Correct 1279 ms 315520 KB Output is correct
97 Correct 1333 ms 314656 KB Output is correct
98 Correct 1178 ms 326296 KB Output is correct
99 Correct 1067 ms 326324 KB Output is correct
100 Correct 1204 ms 326332 KB Output is correct
101 Correct 1221 ms 326272 KB Output is correct
102 Correct 1235 ms 326236 KB Output is correct
103 Correct 1340 ms 328012 KB Output is correct
104 Correct 1122 ms 328136 KB Output is correct
105 Correct 1159 ms 328004 KB Output is correct
106 Correct 1568 ms 318976 KB Output is correct
107 Correct 1355 ms 318924 KB Output is correct
108 Correct 1178 ms 322908 KB Output is correct
109 Correct 1006 ms 322836 KB Output is correct
110 Correct 280 ms 116552 KB Output is correct
111 Correct 243 ms 116264 KB Output is correct
112 Correct 1757 ms 324052 KB Output is correct
113 Correct 1890 ms 314552 KB Output is correct
114 Correct 1330 ms 324720 KB Output is correct
115 Correct 1000 ms 312116 KB Output is correct
116 Correct 1258 ms 320036 KB Output is correct
117 Correct 1395 ms 336292 KB Output is correct
118 Correct 1190 ms 311364 KB Output is correct
119 Correct 1204 ms 311260 KB Output is correct
120 Correct 150 ms 133064 KB Output is correct
121 Correct 1379 ms 324452 KB Output is correct
122 Correct 1474 ms 321428 KB Output is correct
123 Correct 1870 ms 316336 KB Output is correct
124 Correct 1451 ms 316084 KB Output is correct
125 Correct 1903 ms 316512 KB Output is correct
126 Correct 1873 ms 336012 KB Output is correct
127 Correct 1648 ms 325920 KB Output is correct
128 Correct 1507 ms 325924 KB Output is correct
129 Correct 1737 ms 326012 KB Output is correct
130 Correct 1599 ms 326096 KB Output is correct