답안 #1089875

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1089875 2024-09-17T10:47:30 Z Neco_arc Jail (JOI22_jail) C++17
100 / 100
951 ms 418180 KB
#include <bits/stdc++.h>
#ifdef LOCAL
#include <bits/debug.hpp>
#endif // LOCAL

#define ll long long
#define all(x) x.begin(), x.end()
#define Neco "Jail"
#define resp(x) sort(all(x)), x.resize(unique(all(x)) - x.begin())
#define getbit(x,i) ((x >> i)&1)
#define _left id * 2, l, mid
#define _right id * 2 + 1, mid + 1, r
#define cntbit(x) __builtin_popcountll(x)
#define fi(i, a, b) for(int i = a; i <= b; i++)
#define fid(i, a, b) for(int i = a; i >= b; i--)
#define maxn (int) 200009

using namespace std;

const ll mod = 1e9 + 7; //972663749
const ll base = 911382323;

int n, q, N;
int deg[9000009], h[maxn];
int cha[maxn][18], Sx[maxn][18], Tx[maxn][18];
int Sv[maxn], Tv[maxn], S[maxn], T[maxn];
vector<int> edges[maxn], g[9000009];

void pre_dfs(int u, int par) {
    for(int v : edges[u]) if(v != par) {
        h[v] = h[u] + 1, cha[v][0] = u;
        fi(i, 1, 17) cha[v][i] = cha[cha[v][i - 1]][i - 1];

        pre_dfs(v, u);
    }
}

int lca(int u, int v) {
    if(h[u] < h[v]) swap(u, v);
    fid(i, 17, 0) if(h[u] - (1 << i) >= h[v]) u = cha[u][i];
    fid(i, 17, 0) if(cha[u][i] != cha[v][i]) u = cha[u][i], v = cha[v][i];
    return u == v ? u : cha[u][0];
}

void AddS(int id, int x, int h) {
    fid(i, 17, 0) if(getbit(h, i)) {
        g[Sx[x][i]].push_back(id);
        x = cha[x][i];
    }
}

void AddT(int id, int x, int h) {
    fid(i, 17, 0) if(getbit(h, i)) {
        g[id].push_back(Tx[x][i]);
        x = cha[x][i];
    }
}

bool TopoSort() {
    queue<int> q;
    fi(u, 1, N) for(int v : g[u]) ++deg[v];
    fi(i, 1, N) {
        if(!deg[i]) q.push(i);
    }

    int cnt = 0;
    while(!q.empty()) {
        ++cnt;
        int u = q.front(); q.pop();
        for(int v : g[u]) {
            --deg[v];
            if(!deg[v]) q.push(v);
        }
    }

    return cnt == N;

}

void reset() {
    fi(i, 1, n) fi(j, 0, 17) cha[i][j] = 0;
    fi(i, 1, n) edges[i].clear(), Sv[i] = Tv[i] = 0;
    fi(i, 1, N) g[i].clear(), deg[i] = 0;
    N = 0;
}

void solve()
{

    cin >> n;
    fi(i, 1, n - 1) {
        int x, y; cin >> x >> y;
        edges[x].push_back(y), edges[y].push_back(x);
    }

    cin >> q;
    fi(i, 1, q) {
        cin >> S[i] >> T[i];
        Sv[S[i]] = i, Tv[T[i]] = i;
    }

    N = q;
    pre_dfs(1, 0);

    fi(j, 0, 17) fi(i, 1, n) {
        Tx[i][j] = ++N;
        Sx[i][j] = ++N;
    }

    fi(j, 0, 17) fi(i, 1, n) if(cha[i][j]) {
        int p = cha[i][j], ids = Sx[i][j + 1], idt = Tx[i][j + 1];
        g[Sx[i][j]].push_back(ids);
        g[idt].push_back(Tx[i][j]);

//        cout << Sx[i][j] << " " << ids << '\n';

        if(p) {
            g[Sx[p][j]].push_back(ids);
//            cout << Sx[p][j] << " " << ids << '\n';
            g[idt].push_back(Tx[p][j]);
        }
    }


    fi(i, 1, n) {
        int p = cha[i][0];
        if(Sv[p]) g[Sv[p]].push_back(Sx[i][0]);
        if(Tv[p]) g[Tx[i][0]].push_back(Tv[p]);
    }


    fi(i, 1, q) {
        int x = S[i], y = T[i], p = lca(x, y);
        if(x == y) continue;

        int Ls = h[x] - h[p], Lt = h[y] - h[p];

        if(Ls >= 2) {
            AddS(i, x, Ls - 1);
            AddT(i, x, Ls - 1);
        }

        if(Lt >= 2) {
            AddS(i, y, Lt - 1);
            AddT(i, y, Lt - 1);
        }

        int tv[] = {x, y, p};
        for(int u : tv) {
            if(Sv[u] && Sv[u] != i) g[Sv[u]].push_back(i);
            if(Tv[u] && Tv[u] != i) g[i].push_back(Tv[u]);
        }
    }

    if(TopoSort()) cout << "Yes\n";
    else cout << "No\n";

    reset();
}


int main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    if(fopen(Neco".inp", "r")) {
        freopen(Neco".inp", "r", stdin);
        freopen(Neco".out", "w", stdout);
    }


    int nTest = 1;
    cin >> nTest;


    while(nTest--)
    {
        solve();
    }


    return 0;
}

Compilation message

jail.cpp: In function 'int main()':
jail.cpp:170:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  170 |         freopen(Neco".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
jail.cpp:171:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  171 |         freopen(Neco".out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 87 ms 216504 KB Output is correct
2 Correct 88 ms 216404 KB Output is correct
3 Correct 90 ms 216400 KB Output is correct
4 Correct 111 ms 216952 KB Output is correct
5 Correct 140 ms 217264 KB Output is correct
6 Correct 86 ms 216680 KB Output is correct
7 Correct 84 ms 216660 KB Output is correct
8 Correct 85 ms 216656 KB Output is correct
9 Correct 167 ms 225672 KB Output is correct
10 Correct 432 ms 396576 KB Output is correct
11 Correct 90 ms 216660 KB Output is correct
12 Correct 141 ms 217600 KB Output is correct
13 Correct 566 ms 401984 KB Output is correct
14 Correct 424 ms 402052 KB Output is correct
15 Correct 643 ms 404796 KB Output is correct
16 Correct 951 ms 418180 KB Output is correct
17 Correct 529 ms 404432 KB Output is correct
18 Correct 535 ms 407124 KB Output is correct
19 Correct 585 ms 404564 KB Output is correct
20 Correct 513 ms 404480 KB Output is correct
21 Correct 512 ms 405076 KB Output is correct
22 Correct 441 ms 402508 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 216404 KB Output is correct
2 Correct 89 ms 216444 KB Output is correct
3 Correct 91 ms 216656 KB Output is correct
4 Correct 90 ms 216684 KB Output is correct
5 Correct 93 ms 216656 KB Output is correct
6 Correct 89 ms 216744 KB Output is correct
7 Correct 92 ms 216660 KB Output is correct
8 Correct 92 ms 216792 KB Output is correct
9 Correct 96 ms 216656 KB Output is correct
10 Correct 95 ms 216656 KB Output is correct
11 Correct 90 ms 216656 KB Output is correct
12 Correct 89 ms 216660 KB Output is correct
13 Correct 89 ms 216660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 216404 KB Output is correct
2 Correct 89 ms 216444 KB Output is correct
3 Correct 91 ms 216656 KB Output is correct
4 Correct 90 ms 216684 KB Output is correct
5 Correct 93 ms 216656 KB Output is correct
6 Correct 89 ms 216744 KB Output is correct
7 Correct 92 ms 216660 KB Output is correct
8 Correct 92 ms 216792 KB Output is correct
9 Correct 96 ms 216656 KB Output is correct
10 Correct 95 ms 216656 KB Output is correct
11 Correct 90 ms 216656 KB Output is correct
12 Correct 89 ms 216660 KB Output is correct
13 Correct 89 ms 216660 KB Output is correct
14 Correct 89 ms 216460 KB Output is correct
15 Correct 88 ms 216404 KB Output is correct
16 Correct 91 ms 216656 KB Output is correct
17 Correct 94 ms 216660 KB Output is correct
18 Correct 97 ms 216660 KB Output is correct
19 Correct 89 ms 216400 KB Output is correct
20 Correct 98 ms 216796 KB Output is correct
21 Correct 104 ms 216916 KB Output is correct
22 Correct 87 ms 216660 KB Output is correct
23 Correct 89 ms 216460 KB Output is correct
24 Correct 88 ms 216628 KB Output is correct
25 Correct 103 ms 216660 KB Output is correct
26 Correct 88 ms 216656 KB Output is correct
27 Correct 99 ms 216660 KB Output is correct
28 Correct 89 ms 216352 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 216404 KB Output is correct
2 Correct 89 ms 216444 KB Output is correct
3 Correct 91 ms 216656 KB Output is correct
4 Correct 90 ms 216684 KB Output is correct
5 Correct 93 ms 216656 KB Output is correct
6 Correct 89 ms 216744 KB Output is correct
7 Correct 92 ms 216660 KB Output is correct
8 Correct 92 ms 216792 KB Output is correct
9 Correct 96 ms 216656 KB Output is correct
10 Correct 95 ms 216656 KB Output is correct
11 Correct 90 ms 216656 KB Output is correct
12 Correct 89 ms 216660 KB Output is correct
13 Correct 89 ms 216660 KB Output is correct
14 Correct 89 ms 216460 KB Output is correct
15 Correct 88 ms 216404 KB Output is correct
16 Correct 91 ms 216656 KB Output is correct
17 Correct 94 ms 216660 KB Output is correct
18 Correct 97 ms 216660 KB Output is correct
19 Correct 89 ms 216400 KB Output is correct
20 Correct 98 ms 216796 KB Output is correct
21 Correct 104 ms 216916 KB Output is correct
22 Correct 87 ms 216660 KB Output is correct
23 Correct 89 ms 216460 KB Output is correct
24 Correct 88 ms 216628 KB Output is correct
25 Correct 103 ms 216660 KB Output is correct
26 Correct 88 ms 216656 KB Output is correct
27 Correct 99 ms 216660 KB Output is correct
28 Correct 89 ms 216352 KB Output is correct
29 Correct 96 ms 216660 KB Output is correct
30 Correct 96 ms 216836 KB Output is correct
31 Correct 95 ms 216672 KB Output is correct
32 Correct 101 ms 216660 KB Output is correct
33 Correct 95 ms 216656 KB Output is correct
34 Correct 94 ms 216688 KB Output is correct
35 Correct 90 ms 216536 KB Output is correct
36 Correct 100 ms 216660 KB Output is correct
37 Correct 90 ms 216660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 89 ms 216404 KB Output is correct
2 Correct 89 ms 216444 KB Output is correct
3 Correct 91 ms 216656 KB Output is correct
4 Correct 90 ms 216684 KB Output is correct
5 Correct 93 ms 216656 KB Output is correct
6 Correct 89 ms 216744 KB Output is correct
7 Correct 92 ms 216660 KB Output is correct
8 Correct 92 ms 216792 KB Output is correct
9 Correct 96 ms 216656 KB Output is correct
10 Correct 95 ms 216656 KB Output is correct
11 Correct 90 ms 216656 KB Output is correct
12 Correct 89 ms 216660 KB Output is correct
13 Correct 89 ms 216660 KB Output is correct
14 Correct 89 ms 216460 KB Output is correct
15 Correct 88 ms 216404 KB Output is correct
16 Correct 91 ms 216656 KB Output is correct
17 Correct 94 ms 216660 KB Output is correct
18 Correct 97 ms 216660 KB Output is correct
19 Correct 89 ms 216400 KB Output is correct
20 Correct 98 ms 216796 KB Output is correct
21 Correct 104 ms 216916 KB Output is correct
22 Correct 87 ms 216660 KB Output is correct
23 Correct 89 ms 216460 KB Output is correct
24 Correct 88 ms 216628 KB Output is correct
25 Correct 103 ms 216660 KB Output is correct
26 Correct 88 ms 216656 KB Output is correct
27 Correct 99 ms 216660 KB Output is correct
28 Correct 89 ms 216352 KB Output is correct
29 Correct 96 ms 216660 KB Output is correct
30 Correct 96 ms 216836 KB Output is correct
31 Correct 95 ms 216672 KB Output is correct
32 Correct 101 ms 216660 KB Output is correct
33 Correct 95 ms 216656 KB Output is correct
34 Correct 94 ms 216688 KB Output is correct
35 Correct 90 ms 216536 KB Output is correct
36 Correct 100 ms 216660 KB Output is correct
37 Correct 90 ms 216660 KB Output is correct
38 Correct 176 ms 225052 KB Output is correct
39 Correct 488 ms 396260 KB Output is correct
40 Correct 179 ms 225456 KB Output is correct
41 Correct 179 ms 222360 KB Output is correct
42 Correct 148 ms 224552 KB Output is correct
43 Correct 193 ms 225256 KB Output is correct
44 Correct 99 ms 217428 KB Output is correct
45 Correct 378 ms 316936 KB Output is correct
46 Correct 381 ms 316900 KB Output is correct
47 Correct 465 ms 384008 KB Output is correct
48 Correct 489 ms 384084 KB Output is correct
49 Correct 413 ms 350188 KB Output is correct
50 Correct 435 ms 350372 KB Output is correct
51 Correct 549 ms 373360 KB Output is correct
52 Correct 502 ms 373328 KB Output is correct
53 Correct 118 ms 223532 KB Output is correct
54 Correct 449 ms 309264 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 94 ms 216364 KB Output is correct
2 Correct 89 ms 216308 KB Output is correct
3 Correct 106 ms 216328 KB Output is correct
4 Correct 86 ms 216316 KB Output is correct
5 Correct 101 ms 216656 KB Output is correct
6 Correct 92 ms 216516 KB Output is correct
7 Correct 93 ms 216696 KB Output is correct
8 Correct 106 ms 216420 KB Output is correct
9 Correct 91 ms 216492 KB Output is correct
10 Correct 94 ms 216400 KB Output is correct
11 Correct 99 ms 216492 KB Output is correct
12 Correct 98 ms 216660 KB Output is correct
13 Correct 127 ms 217168 KB Output is correct
14 Correct 156 ms 217424 KB Output is correct
15 Correct 143 ms 217428 KB Output is correct
16 Correct 332 ms 309760 KB Output is correct
17 Correct 405 ms 303964 KB Output is correct
18 Correct 498 ms 311760 KB Output is correct
19 Correct 311 ms 298664 KB Output is correct
20 Correct 306 ms 297956 KB Output is correct
21 Correct 332 ms 298240 KB Output is correct
22 Correct 338 ms 301920 KB Output is correct
23 Correct 357 ms 301748 KB Output is correct
24 Correct 424 ms 301784 KB Output is correct
25 Correct 436 ms 302672 KB Output is correct
26 Correct 411 ms 302184 KB Output is correct
27 Correct 267 ms 289776 KB Output is correct
28 Correct 242 ms 289752 KB Output is correct
29 Correct 247 ms 289776 KB Output is correct
30 Correct 297 ms 288132 KB Output is correct
31 Correct 290 ms 288128 KB Output is correct
32 Correct 321 ms 286084 KB Output is correct
33 Correct 306 ms 285828 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 87 ms 216504 KB Output is correct
2 Correct 88 ms 216404 KB Output is correct
3 Correct 90 ms 216400 KB Output is correct
4 Correct 111 ms 216952 KB Output is correct
5 Correct 140 ms 217264 KB Output is correct
6 Correct 86 ms 216680 KB Output is correct
7 Correct 84 ms 216660 KB Output is correct
8 Correct 85 ms 216656 KB Output is correct
9 Correct 167 ms 225672 KB Output is correct
10 Correct 432 ms 396576 KB Output is correct
11 Correct 90 ms 216660 KB Output is correct
12 Correct 141 ms 217600 KB Output is correct
13 Correct 566 ms 401984 KB Output is correct
14 Correct 424 ms 402052 KB Output is correct
15 Correct 643 ms 404796 KB Output is correct
16 Correct 951 ms 418180 KB Output is correct
17 Correct 529 ms 404432 KB Output is correct
18 Correct 535 ms 407124 KB Output is correct
19 Correct 585 ms 404564 KB Output is correct
20 Correct 513 ms 404480 KB Output is correct
21 Correct 512 ms 405076 KB Output is correct
22 Correct 441 ms 402508 KB Output is correct
23 Correct 89 ms 216404 KB Output is correct
24 Correct 89 ms 216444 KB Output is correct
25 Correct 91 ms 216656 KB Output is correct
26 Correct 90 ms 216684 KB Output is correct
27 Correct 93 ms 216656 KB Output is correct
28 Correct 89 ms 216744 KB Output is correct
29 Correct 92 ms 216660 KB Output is correct
30 Correct 92 ms 216792 KB Output is correct
31 Correct 96 ms 216656 KB Output is correct
32 Correct 95 ms 216656 KB Output is correct
33 Correct 90 ms 216656 KB Output is correct
34 Correct 89 ms 216660 KB Output is correct
35 Correct 89 ms 216660 KB Output is correct
36 Correct 89 ms 216460 KB Output is correct
37 Correct 88 ms 216404 KB Output is correct
38 Correct 91 ms 216656 KB Output is correct
39 Correct 94 ms 216660 KB Output is correct
40 Correct 97 ms 216660 KB Output is correct
41 Correct 89 ms 216400 KB Output is correct
42 Correct 98 ms 216796 KB Output is correct
43 Correct 104 ms 216916 KB Output is correct
44 Correct 87 ms 216660 KB Output is correct
45 Correct 89 ms 216460 KB Output is correct
46 Correct 88 ms 216628 KB Output is correct
47 Correct 103 ms 216660 KB Output is correct
48 Correct 88 ms 216656 KB Output is correct
49 Correct 99 ms 216660 KB Output is correct
50 Correct 89 ms 216352 KB Output is correct
51 Correct 96 ms 216660 KB Output is correct
52 Correct 96 ms 216836 KB Output is correct
53 Correct 95 ms 216672 KB Output is correct
54 Correct 101 ms 216660 KB Output is correct
55 Correct 95 ms 216656 KB Output is correct
56 Correct 94 ms 216688 KB Output is correct
57 Correct 90 ms 216536 KB Output is correct
58 Correct 100 ms 216660 KB Output is correct
59 Correct 90 ms 216660 KB Output is correct
60 Correct 176 ms 225052 KB Output is correct
61 Correct 488 ms 396260 KB Output is correct
62 Correct 179 ms 225456 KB Output is correct
63 Correct 179 ms 222360 KB Output is correct
64 Correct 148 ms 224552 KB Output is correct
65 Correct 193 ms 225256 KB Output is correct
66 Correct 99 ms 217428 KB Output is correct
67 Correct 378 ms 316936 KB Output is correct
68 Correct 381 ms 316900 KB Output is correct
69 Correct 465 ms 384008 KB Output is correct
70 Correct 489 ms 384084 KB Output is correct
71 Correct 413 ms 350188 KB Output is correct
72 Correct 435 ms 350372 KB Output is correct
73 Correct 549 ms 373360 KB Output is correct
74 Correct 502 ms 373328 KB Output is correct
75 Correct 118 ms 223532 KB Output is correct
76 Correct 449 ms 309264 KB Output is correct
77 Correct 94 ms 216364 KB Output is correct
78 Correct 89 ms 216308 KB Output is correct
79 Correct 106 ms 216328 KB Output is correct
80 Correct 86 ms 216316 KB Output is correct
81 Correct 101 ms 216656 KB Output is correct
82 Correct 92 ms 216516 KB Output is correct
83 Correct 93 ms 216696 KB Output is correct
84 Correct 106 ms 216420 KB Output is correct
85 Correct 91 ms 216492 KB Output is correct
86 Correct 94 ms 216400 KB Output is correct
87 Correct 99 ms 216492 KB Output is correct
88 Correct 98 ms 216660 KB Output is correct
89 Correct 127 ms 217168 KB Output is correct
90 Correct 156 ms 217424 KB Output is correct
91 Correct 143 ms 217428 KB Output is correct
92 Correct 332 ms 309760 KB Output is correct
93 Correct 405 ms 303964 KB Output is correct
94 Correct 498 ms 311760 KB Output is correct
95 Correct 311 ms 298664 KB Output is correct
96 Correct 306 ms 297956 KB Output is correct
97 Correct 332 ms 298240 KB Output is correct
98 Correct 338 ms 301920 KB Output is correct
99 Correct 357 ms 301748 KB Output is correct
100 Correct 424 ms 301784 KB Output is correct
101 Correct 436 ms 302672 KB Output is correct
102 Correct 411 ms 302184 KB Output is correct
103 Correct 267 ms 289776 KB Output is correct
104 Correct 242 ms 289752 KB Output is correct
105 Correct 247 ms 289776 KB Output is correct
106 Correct 297 ms 288132 KB Output is correct
107 Correct 290 ms 288128 KB Output is correct
108 Correct 321 ms 286084 KB Output is correct
109 Correct 306 ms 285828 KB Output is correct
110 Correct 146 ms 217628 KB Output is correct
111 Correct 131 ms 217168 KB Output is correct
112 Correct 681 ms 398328 KB Output is correct
113 Correct 615 ms 387464 KB Output is correct
114 Correct 455 ms 348788 KB Output is correct
115 Correct 182 ms 279012 KB Output is correct
116 Correct 387 ms 314212 KB Output is correct
117 Correct 600 ms 324844 KB Output is correct
118 Correct 392 ms 306892 KB Output is correct
119 Correct 415 ms 306912 KB Output is correct
120 Correct 107 ms 224912 KB Output is correct
121 Correct 578 ms 323960 KB Output is correct
122 Correct 542 ms 321508 KB Output is correct
123 Correct 636 ms 392252 KB Output is correct
124 Correct 561 ms 392924 KB Output is correct
125 Correct 813 ms 392280 KB Output is correct
126 Correct 897 ms 413792 KB Output is correct
127 Correct 553 ms 393932 KB Output is correct
128 Correct 529 ms 393936 KB Output is correct
129 Correct 921 ms 393936 KB Output is correct
130 Correct 557 ms 394324 KB Output is correct