Submission #1089874

# Submission time Handle Problem Language Result Execution time Memory
1089874 2024-09-17T10:46:27 Z Neco_arc Jail (JOI22_jail) C++17
100 / 100
965 ms 423364 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][20], Sx[maxn][19], Tx[maxn][20];
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, 19) 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, 19, 0) if(h[u] - (1 << i) >= h[v]) u = cha[u][i];
    fid(i, 19, 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, 19, 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, 19, 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, 19) 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, 19) fi(i, 1, n) {
        Tx[i][j] = ++N;
        Sx[i][j] = ++N;
    }

    fi(j, 0, 19) 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);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 88 ms 216400 KB Output is correct
2 Correct 90 ms 216352 KB Output is correct
3 Correct 89 ms 216428 KB Output is correct
4 Correct 114 ms 216916 KB Output is correct
5 Correct 131 ms 217384 KB Output is correct
6 Correct 97 ms 216728 KB Output is correct
7 Correct 86 ms 216836 KB Output is correct
8 Correct 90 ms 216660 KB Output is correct
9 Correct 174 ms 225764 KB Output is correct
10 Correct 496 ms 400888 KB Output is correct
11 Correct 102 ms 216468 KB Output is correct
12 Correct 141 ms 217668 KB Output is correct
13 Correct 608 ms 407500 KB Output is correct
14 Correct 430 ms 409064 KB Output is correct
15 Correct 609 ms 409684 KB Output is correct
16 Correct 965 ms 423364 KB Output is correct
17 Correct 582 ms 409172 KB Output is correct
18 Correct 553 ms 412492 KB Output is correct
19 Correct 575 ms 409356 KB Output is correct
20 Correct 521 ms 409168 KB Output is correct
21 Correct 556 ms 410908 KB Output is correct
22 Correct 443 ms 407412 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 216360 KB Output is correct
2 Correct 85 ms 216540 KB Output is correct
3 Correct 90 ms 216656 KB Output is correct
4 Correct 93 ms 216660 KB Output is correct
5 Correct 90 ms 216656 KB Output is correct
6 Correct 88 ms 216780 KB Output is correct
7 Correct 92 ms 216660 KB Output is correct
8 Correct 92 ms 216608 KB Output is correct
9 Correct 93 ms 216660 KB Output is correct
10 Correct 95 ms 216656 KB Output is correct
11 Correct 92 ms 216660 KB Output is correct
12 Correct 89 ms 216532 KB Output is correct
13 Correct 97 ms 216656 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 216360 KB Output is correct
2 Correct 85 ms 216540 KB Output is correct
3 Correct 90 ms 216656 KB Output is correct
4 Correct 93 ms 216660 KB Output is correct
5 Correct 90 ms 216656 KB Output is correct
6 Correct 88 ms 216780 KB Output is correct
7 Correct 92 ms 216660 KB Output is correct
8 Correct 92 ms 216608 KB Output is correct
9 Correct 93 ms 216660 KB Output is correct
10 Correct 95 ms 216656 KB Output is correct
11 Correct 92 ms 216660 KB Output is correct
12 Correct 89 ms 216532 KB Output is correct
13 Correct 97 ms 216656 KB Output is correct
14 Correct 86 ms 216500 KB Output is correct
15 Correct 92 ms 216396 KB Output is correct
16 Correct 92 ms 216632 KB Output is correct
17 Correct 94 ms 216660 KB Output is correct
18 Correct 89 ms 216656 KB Output is correct
19 Correct 93 ms 216652 KB Output is correct
20 Correct 94 ms 216700 KB Output is correct
21 Correct 88 ms 216660 KB Output is correct
22 Correct 91 ms 216656 KB Output is correct
23 Correct 91 ms 216356 KB Output is correct
24 Correct 87 ms 216404 KB Output is correct
25 Correct 90 ms 216660 KB Output is correct
26 Correct 87 ms 216696 KB Output is correct
27 Correct 93 ms 216660 KB Output is correct
28 Correct 94 ms 216552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 216360 KB Output is correct
2 Correct 85 ms 216540 KB Output is correct
3 Correct 90 ms 216656 KB Output is correct
4 Correct 93 ms 216660 KB Output is correct
5 Correct 90 ms 216656 KB Output is correct
6 Correct 88 ms 216780 KB Output is correct
7 Correct 92 ms 216660 KB Output is correct
8 Correct 92 ms 216608 KB Output is correct
9 Correct 93 ms 216660 KB Output is correct
10 Correct 95 ms 216656 KB Output is correct
11 Correct 92 ms 216660 KB Output is correct
12 Correct 89 ms 216532 KB Output is correct
13 Correct 97 ms 216656 KB Output is correct
14 Correct 86 ms 216500 KB Output is correct
15 Correct 92 ms 216396 KB Output is correct
16 Correct 92 ms 216632 KB Output is correct
17 Correct 94 ms 216660 KB Output is correct
18 Correct 89 ms 216656 KB Output is correct
19 Correct 93 ms 216652 KB Output is correct
20 Correct 94 ms 216700 KB Output is correct
21 Correct 88 ms 216660 KB Output is correct
22 Correct 91 ms 216656 KB Output is correct
23 Correct 91 ms 216356 KB Output is correct
24 Correct 87 ms 216404 KB Output is correct
25 Correct 90 ms 216660 KB Output is correct
26 Correct 87 ms 216696 KB Output is correct
27 Correct 93 ms 216660 KB Output is correct
28 Correct 94 ms 216552 KB Output is correct
29 Correct 98 ms 216656 KB Output is correct
30 Correct 93 ms 216660 KB Output is correct
31 Correct 96 ms 216708 KB Output is correct
32 Correct 101 ms 216712 KB Output is correct
33 Correct 94 ms 216656 KB Output is correct
34 Correct 99 ms 216660 KB Output is correct
35 Correct 94 ms 216660 KB Output is correct
36 Correct 91 ms 216700 KB Output is correct
37 Correct 88 ms 216660 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 216360 KB Output is correct
2 Correct 85 ms 216540 KB Output is correct
3 Correct 90 ms 216656 KB Output is correct
4 Correct 93 ms 216660 KB Output is correct
5 Correct 90 ms 216656 KB Output is correct
6 Correct 88 ms 216780 KB Output is correct
7 Correct 92 ms 216660 KB Output is correct
8 Correct 92 ms 216608 KB Output is correct
9 Correct 93 ms 216660 KB Output is correct
10 Correct 95 ms 216656 KB Output is correct
11 Correct 92 ms 216660 KB Output is correct
12 Correct 89 ms 216532 KB Output is correct
13 Correct 97 ms 216656 KB Output is correct
14 Correct 86 ms 216500 KB Output is correct
15 Correct 92 ms 216396 KB Output is correct
16 Correct 92 ms 216632 KB Output is correct
17 Correct 94 ms 216660 KB Output is correct
18 Correct 89 ms 216656 KB Output is correct
19 Correct 93 ms 216652 KB Output is correct
20 Correct 94 ms 216700 KB Output is correct
21 Correct 88 ms 216660 KB Output is correct
22 Correct 91 ms 216656 KB Output is correct
23 Correct 91 ms 216356 KB Output is correct
24 Correct 87 ms 216404 KB Output is correct
25 Correct 90 ms 216660 KB Output is correct
26 Correct 87 ms 216696 KB Output is correct
27 Correct 93 ms 216660 KB Output is correct
28 Correct 94 ms 216552 KB Output is correct
29 Correct 98 ms 216656 KB Output is correct
30 Correct 93 ms 216660 KB Output is correct
31 Correct 96 ms 216708 KB Output is correct
32 Correct 101 ms 216712 KB Output is correct
33 Correct 94 ms 216656 KB Output is correct
34 Correct 99 ms 216660 KB Output is correct
35 Correct 94 ms 216660 KB Output is correct
36 Correct 91 ms 216700 KB Output is correct
37 Correct 88 ms 216660 KB Output is correct
38 Correct 175 ms 225608 KB Output is correct
39 Correct 506 ms 400740 KB Output is correct
40 Correct 183 ms 226084 KB Output is correct
41 Correct 178 ms 222980 KB Output is correct
42 Correct 154 ms 225268 KB Output is correct
43 Correct 198 ms 225864 KB Output is correct
44 Correct 102 ms 217596 KB Output is correct
45 Correct 400 ms 332592 KB Output is correct
46 Correct 407 ms 322528 KB Output is correct
47 Correct 497 ms 392384 KB Output is correct
48 Correct 491 ms 392160 KB Output is correct
49 Correct 459 ms 362040 KB Output is correct
50 Correct 451 ms 355920 KB Output is correct
51 Correct 508 ms 378220 KB Output is correct
52 Correct 522 ms 382424 KB Output is correct
53 Correct 120 ms 224852 KB Output is correct
54 Correct 435 ms 325596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 93 ms 216408 KB Output is correct
2 Correct 94 ms 216400 KB Output is correct
3 Correct 91 ms 216400 KB Output is correct
4 Correct 87 ms 216476 KB Output is correct
5 Correct 100 ms 216660 KB Output is correct
6 Correct 89 ms 216500 KB Output is correct
7 Correct 89 ms 216660 KB Output is correct
8 Correct 94 ms 216400 KB Output is correct
9 Correct 97 ms 216404 KB Output is correct
10 Correct 93 ms 216544 KB Output is correct
11 Correct 90 ms 216544 KB Output is correct
12 Correct 91 ms 216744 KB Output is correct
13 Correct 128 ms 217172 KB Output is correct
14 Correct 165 ms 217476 KB Output is correct
15 Correct 136 ms 217168 KB Output is correct
16 Correct 351 ms 316772 KB Output is correct
17 Correct 423 ms 322692 KB Output is correct
18 Correct 581 ms 318504 KB Output is correct
19 Correct 375 ms 317148 KB Output is correct
20 Correct 364 ms 316384 KB Output is correct
21 Correct 360 ms 316728 KB Output is correct
22 Correct 420 ms 321152 KB Output is correct
23 Correct 403 ms 321152 KB Output is correct
24 Correct 452 ms 307584 KB Output is correct
25 Correct 441 ms 308224 KB Output is correct
26 Correct 418 ms 307848 KB Output is correct
27 Correct 268 ms 294380 KB Output is correct
28 Correct 260 ms 294376 KB Output is correct
29 Correct 256 ms 294380 KB Output is correct
30 Correct 333 ms 308128 KB Output is correct
31 Correct 319 ms 308056 KB Output is correct
32 Correct 341 ms 291296 KB Output is correct
33 Correct 319 ms 291340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 88 ms 216400 KB Output is correct
2 Correct 90 ms 216352 KB Output is correct
3 Correct 89 ms 216428 KB Output is correct
4 Correct 114 ms 216916 KB Output is correct
5 Correct 131 ms 217384 KB Output is correct
6 Correct 97 ms 216728 KB Output is correct
7 Correct 86 ms 216836 KB Output is correct
8 Correct 90 ms 216660 KB Output is correct
9 Correct 174 ms 225764 KB Output is correct
10 Correct 496 ms 400888 KB Output is correct
11 Correct 102 ms 216468 KB Output is correct
12 Correct 141 ms 217668 KB Output is correct
13 Correct 608 ms 407500 KB Output is correct
14 Correct 430 ms 409064 KB Output is correct
15 Correct 609 ms 409684 KB Output is correct
16 Correct 965 ms 423364 KB Output is correct
17 Correct 582 ms 409172 KB Output is correct
18 Correct 553 ms 412492 KB Output is correct
19 Correct 575 ms 409356 KB Output is correct
20 Correct 521 ms 409168 KB Output is correct
21 Correct 556 ms 410908 KB Output is correct
22 Correct 443 ms 407412 KB Output is correct
23 Correct 91 ms 216360 KB Output is correct
24 Correct 85 ms 216540 KB Output is correct
25 Correct 90 ms 216656 KB Output is correct
26 Correct 93 ms 216660 KB Output is correct
27 Correct 90 ms 216656 KB Output is correct
28 Correct 88 ms 216780 KB Output is correct
29 Correct 92 ms 216660 KB Output is correct
30 Correct 92 ms 216608 KB Output is correct
31 Correct 93 ms 216660 KB Output is correct
32 Correct 95 ms 216656 KB Output is correct
33 Correct 92 ms 216660 KB Output is correct
34 Correct 89 ms 216532 KB Output is correct
35 Correct 97 ms 216656 KB Output is correct
36 Correct 86 ms 216500 KB Output is correct
37 Correct 92 ms 216396 KB Output is correct
38 Correct 92 ms 216632 KB Output is correct
39 Correct 94 ms 216660 KB Output is correct
40 Correct 89 ms 216656 KB Output is correct
41 Correct 93 ms 216652 KB Output is correct
42 Correct 94 ms 216700 KB Output is correct
43 Correct 88 ms 216660 KB Output is correct
44 Correct 91 ms 216656 KB Output is correct
45 Correct 91 ms 216356 KB Output is correct
46 Correct 87 ms 216404 KB Output is correct
47 Correct 90 ms 216660 KB Output is correct
48 Correct 87 ms 216696 KB Output is correct
49 Correct 93 ms 216660 KB Output is correct
50 Correct 94 ms 216552 KB Output is correct
51 Correct 98 ms 216656 KB Output is correct
52 Correct 93 ms 216660 KB Output is correct
53 Correct 96 ms 216708 KB Output is correct
54 Correct 101 ms 216712 KB Output is correct
55 Correct 94 ms 216656 KB Output is correct
56 Correct 99 ms 216660 KB Output is correct
57 Correct 94 ms 216660 KB Output is correct
58 Correct 91 ms 216700 KB Output is correct
59 Correct 88 ms 216660 KB Output is correct
60 Correct 175 ms 225608 KB Output is correct
61 Correct 506 ms 400740 KB Output is correct
62 Correct 183 ms 226084 KB Output is correct
63 Correct 178 ms 222980 KB Output is correct
64 Correct 154 ms 225268 KB Output is correct
65 Correct 198 ms 225864 KB Output is correct
66 Correct 102 ms 217596 KB Output is correct
67 Correct 400 ms 332592 KB Output is correct
68 Correct 407 ms 322528 KB Output is correct
69 Correct 497 ms 392384 KB Output is correct
70 Correct 491 ms 392160 KB Output is correct
71 Correct 459 ms 362040 KB Output is correct
72 Correct 451 ms 355920 KB Output is correct
73 Correct 508 ms 378220 KB Output is correct
74 Correct 522 ms 382424 KB Output is correct
75 Correct 120 ms 224852 KB Output is correct
76 Correct 435 ms 325596 KB Output is correct
77 Correct 93 ms 216408 KB Output is correct
78 Correct 94 ms 216400 KB Output is correct
79 Correct 91 ms 216400 KB Output is correct
80 Correct 87 ms 216476 KB Output is correct
81 Correct 100 ms 216660 KB Output is correct
82 Correct 89 ms 216500 KB Output is correct
83 Correct 89 ms 216660 KB Output is correct
84 Correct 94 ms 216400 KB Output is correct
85 Correct 97 ms 216404 KB Output is correct
86 Correct 93 ms 216544 KB Output is correct
87 Correct 90 ms 216544 KB Output is correct
88 Correct 91 ms 216744 KB Output is correct
89 Correct 128 ms 217172 KB Output is correct
90 Correct 165 ms 217476 KB Output is correct
91 Correct 136 ms 217168 KB Output is correct
92 Correct 351 ms 316772 KB Output is correct
93 Correct 423 ms 322692 KB Output is correct
94 Correct 581 ms 318504 KB Output is correct
95 Correct 375 ms 317148 KB Output is correct
96 Correct 364 ms 316384 KB Output is correct
97 Correct 360 ms 316728 KB Output is correct
98 Correct 420 ms 321152 KB Output is correct
99 Correct 403 ms 321152 KB Output is correct
100 Correct 452 ms 307584 KB Output is correct
101 Correct 441 ms 308224 KB Output is correct
102 Correct 418 ms 307848 KB Output is correct
103 Correct 268 ms 294380 KB Output is correct
104 Correct 260 ms 294376 KB Output is correct
105 Correct 256 ms 294380 KB Output is correct
106 Correct 333 ms 308128 KB Output is correct
107 Correct 319 ms 308056 KB Output is correct
108 Correct 341 ms 291296 KB Output is correct
109 Correct 319 ms 291340 KB Output is correct
110 Correct 161 ms 217684 KB Output is correct
111 Correct 139 ms 217168 KB Output is correct
112 Correct 697 ms 403324 KB Output is correct
113 Correct 645 ms 395112 KB Output is correct
114 Correct 458 ms 355296 KB Output is correct
115 Correct 194 ms 299968 KB Output is correct
116 Correct 407 ms 331216 KB Output is correct
117 Correct 621 ms 330616 KB Output is correct
118 Correct 412 ms 324396 KB Output is correct
119 Correct 419 ms 324008 KB Output is correct
120 Correct 113 ms 226132 KB Output is correct
121 Correct 728 ms 339460 KB Output is correct
122 Correct 541 ms 336988 KB Output is correct
123 Correct 670 ms 399448 KB Output is correct
124 Correct 547 ms 398948 KB Output is correct
125 Correct 674 ms 399756 KB Output is correct
126 Correct 965 ms 419876 KB Output is correct
127 Correct 617 ms 402384 KB Output is correct
128 Correct 563 ms 402544 KB Output is correct
129 Correct 942 ms 398868 KB Output is correct
130 Correct 570 ms 402416 KB Output is correct