Submission #1089876

# Submission time Handle Problem Language Result Execution time Memory
1089876 2024-09-17T10:49:25 Z Neco_arc Jail (JOI22_jail) C++17
100 / 100
878 ms 317972 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) 120009

using namespace std;

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

int n, q, N;
int deg[40 * maxn], 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[40 * maxn];

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);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 49 ms 115792 KB Output is correct
2 Correct 49 ms 115796 KB Output is correct
3 Correct 48 ms 115792 KB Output is correct
4 Correct 72 ms 116308 KB Output is correct
5 Correct 90 ms 116672 KB Output is correct
6 Correct 47 ms 116308 KB Output is correct
7 Correct 49 ms 116096 KB Output is correct
8 Correct 53 ms 116312 KB Output is correct
9 Correct 141 ms 124996 KB Output is correct
10 Correct 429 ms 296032 KB Output is correct
11 Correct 62 ms 116048 KB Output is correct
12 Correct 110 ms 117076 KB Output is correct
13 Correct 586 ms 301580 KB Output is correct
14 Correct 411 ms 301648 KB Output is correct
15 Correct 558 ms 304468 KB Output is correct
16 Correct 836 ms 317972 KB Output is correct
17 Correct 462 ms 303856 KB Output is correct
18 Correct 451 ms 307280 KB Output is correct
19 Correct 524 ms 303984 KB Output is correct
20 Correct 477 ms 303956 KB Output is correct
21 Correct 474 ms 304724 KB Output is correct
22 Correct 403 ms 302156 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 115792 KB Output is correct
2 Correct 49 ms 115796 KB Output is correct
3 Correct 53 ms 116464 KB Output is correct
4 Correct 51 ms 116048 KB Output is correct
5 Correct 52 ms 116304 KB Output is correct
6 Correct 49 ms 116056 KB Output is correct
7 Correct 50 ms 116056 KB Output is correct
8 Correct 50 ms 116168 KB Output is correct
9 Correct 56 ms 116096 KB Output is correct
10 Correct 52 ms 116316 KB Output is correct
11 Correct 53 ms 116228 KB Output is correct
12 Correct 50 ms 116052 KB Output is correct
13 Correct 52 ms 116228 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 115792 KB Output is correct
2 Correct 49 ms 115796 KB Output is correct
3 Correct 53 ms 116464 KB Output is correct
4 Correct 51 ms 116048 KB Output is correct
5 Correct 52 ms 116304 KB Output is correct
6 Correct 49 ms 116056 KB Output is correct
7 Correct 50 ms 116056 KB Output is correct
8 Correct 50 ms 116168 KB Output is correct
9 Correct 56 ms 116096 KB Output is correct
10 Correct 52 ms 116316 KB Output is correct
11 Correct 53 ms 116228 KB Output is correct
12 Correct 50 ms 116052 KB Output is correct
13 Correct 52 ms 116228 KB Output is correct
14 Correct 49 ms 115796 KB Output is correct
15 Correct 53 ms 116032 KB Output is correct
16 Correct 54 ms 116308 KB Output is correct
17 Correct 51 ms 116048 KB Output is correct
18 Correct 51 ms 116308 KB Output is correct
19 Correct 50 ms 116048 KB Output is correct
20 Correct 51 ms 116276 KB Output is correct
21 Correct 51 ms 116312 KB Output is correct
22 Correct 50 ms 116304 KB Output is correct
23 Correct 50 ms 115796 KB Output is correct
24 Correct 48 ms 116048 KB Output is correct
25 Correct 59 ms 116444 KB Output is correct
26 Correct 49 ms 116064 KB Output is correct
27 Correct 51 ms 116048 KB Output is correct
28 Correct 52 ms 116060 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 115792 KB Output is correct
2 Correct 49 ms 115796 KB Output is correct
3 Correct 53 ms 116464 KB Output is correct
4 Correct 51 ms 116048 KB Output is correct
5 Correct 52 ms 116304 KB Output is correct
6 Correct 49 ms 116056 KB Output is correct
7 Correct 50 ms 116056 KB Output is correct
8 Correct 50 ms 116168 KB Output is correct
9 Correct 56 ms 116096 KB Output is correct
10 Correct 52 ms 116316 KB Output is correct
11 Correct 53 ms 116228 KB Output is correct
12 Correct 50 ms 116052 KB Output is correct
13 Correct 52 ms 116228 KB Output is correct
14 Correct 49 ms 115796 KB Output is correct
15 Correct 53 ms 116032 KB Output is correct
16 Correct 54 ms 116308 KB Output is correct
17 Correct 51 ms 116048 KB Output is correct
18 Correct 51 ms 116308 KB Output is correct
19 Correct 50 ms 116048 KB Output is correct
20 Correct 51 ms 116276 KB Output is correct
21 Correct 51 ms 116312 KB Output is correct
22 Correct 50 ms 116304 KB Output is correct
23 Correct 50 ms 115796 KB Output is correct
24 Correct 48 ms 116048 KB Output is correct
25 Correct 59 ms 116444 KB Output is correct
26 Correct 49 ms 116064 KB Output is correct
27 Correct 51 ms 116048 KB Output is correct
28 Correct 52 ms 116060 KB Output is correct
29 Correct 51 ms 116316 KB Output is correct
30 Correct 50 ms 116308 KB Output is correct
31 Correct 51 ms 116304 KB Output is correct
32 Correct 52 ms 116120 KB Output is correct
33 Correct 56 ms 116216 KB Output is correct
34 Correct 56 ms 116052 KB Output is correct
35 Correct 53 ms 116060 KB Output is correct
36 Correct 53 ms 116052 KB Output is correct
37 Correct 55 ms 116052 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 115792 KB Output is correct
2 Correct 49 ms 115796 KB Output is correct
3 Correct 53 ms 116464 KB Output is correct
4 Correct 51 ms 116048 KB Output is correct
5 Correct 52 ms 116304 KB Output is correct
6 Correct 49 ms 116056 KB Output is correct
7 Correct 50 ms 116056 KB Output is correct
8 Correct 50 ms 116168 KB Output is correct
9 Correct 56 ms 116096 KB Output is correct
10 Correct 52 ms 116316 KB Output is correct
11 Correct 53 ms 116228 KB Output is correct
12 Correct 50 ms 116052 KB Output is correct
13 Correct 52 ms 116228 KB Output is correct
14 Correct 49 ms 115796 KB Output is correct
15 Correct 53 ms 116032 KB Output is correct
16 Correct 54 ms 116308 KB Output is correct
17 Correct 51 ms 116048 KB Output is correct
18 Correct 51 ms 116308 KB Output is correct
19 Correct 50 ms 116048 KB Output is correct
20 Correct 51 ms 116276 KB Output is correct
21 Correct 51 ms 116312 KB Output is correct
22 Correct 50 ms 116304 KB Output is correct
23 Correct 50 ms 115796 KB Output is correct
24 Correct 48 ms 116048 KB Output is correct
25 Correct 59 ms 116444 KB Output is correct
26 Correct 49 ms 116064 KB Output is correct
27 Correct 51 ms 116048 KB Output is correct
28 Correct 52 ms 116060 KB Output is correct
29 Correct 51 ms 116316 KB Output is correct
30 Correct 50 ms 116308 KB Output is correct
31 Correct 51 ms 116304 KB Output is correct
32 Correct 52 ms 116120 KB Output is correct
33 Correct 56 ms 116216 KB Output is correct
34 Correct 56 ms 116052 KB Output is correct
35 Correct 53 ms 116060 KB Output is correct
36 Correct 53 ms 116052 KB Output is correct
37 Correct 55 ms 116052 KB Output is correct
38 Correct 139 ms 125008 KB Output is correct
39 Correct 438 ms 296280 KB Output is correct
40 Correct 153 ms 125332 KB Output is correct
41 Correct 127 ms 122280 KB Output is correct
42 Correct 110 ms 124308 KB Output is correct
43 Correct 149 ms 125148 KB Output is correct
44 Correct 60 ms 117072 KB Output is correct
45 Correct 336 ms 216804 KB Output is correct
46 Correct 352 ms 216800 KB Output is correct
47 Correct 429 ms 283980 KB Output is correct
48 Correct 431 ms 284024 KB Output is correct
49 Correct 360 ms 250084 KB Output is correct
50 Correct 382 ms 250212 KB Output is correct
51 Correct 464 ms 273268 KB Output is correct
52 Correct 496 ms 273236 KB Output is correct
53 Correct 76 ms 123176 KB Output is correct
54 Correct 360 ms 208864 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 48 ms 116060 KB Output is correct
2 Correct 46 ms 115796 KB Output is correct
3 Correct 49 ms 115792 KB Output is correct
4 Correct 54 ms 115796 KB Output is correct
5 Correct 57 ms 116048 KB Output is correct
6 Correct 50 ms 116068 KB Output is correct
7 Correct 49 ms 116048 KB Output is correct
8 Correct 48 ms 115796 KB Output is correct
9 Correct 54 ms 115792 KB Output is correct
10 Correct 50 ms 116048 KB Output is correct
11 Correct 50 ms 115948 KB Output is correct
12 Correct 49 ms 116168 KB Output is correct
13 Correct 79 ms 116512 KB Output is correct
14 Correct 105 ms 117016 KB Output is correct
15 Correct 90 ms 116816 KB Output is correct
16 Correct 298 ms 209916 KB Output is correct
17 Correct 360 ms 204124 KB Output is correct
18 Correct 459 ms 212720 KB Output is correct
19 Correct 305 ms 198816 KB Output is correct
20 Correct 291 ms 198116 KB Output is correct
21 Correct 297 ms 198152 KB Output is correct
22 Correct 335 ms 202192 KB Output is correct
23 Correct 341 ms 202172 KB Output is correct
24 Correct 380 ms 202032 KB Output is correct
25 Correct 407 ms 202844 KB Output is correct
26 Correct 410 ms 202336 KB Output is correct
27 Correct 243 ms 189168 KB Output is correct
28 Correct 238 ms 189168 KB Output is correct
29 Correct 225 ms 189164 KB Output is correct
30 Correct 285 ms 187264 KB Output is correct
31 Correct 267 ms 187520 KB Output is correct
32 Correct 262 ms 185216 KB Output is correct
33 Correct 225 ms 185168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 49 ms 115792 KB Output is correct
2 Correct 49 ms 115796 KB Output is correct
3 Correct 48 ms 115792 KB Output is correct
4 Correct 72 ms 116308 KB Output is correct
5 Correct 90 ms 116672 KB Output is correct
6 Correct 47 ms 116308 KB Output is correct
7 Correct 49 ms 116096 KB Output is correct
8 Correct 53 ms 116312 KB Output is correct
9 Correct 141 ms 124996 KB Output is correct
10 Correct 429 ms 296032 KB Output is correct
11 Correct 62 ms 116048 KB Output is correct
12 Correct 110 ms 117076 KB Output is correct
13 Correct 586 ms 301580 KB Output is correct
14 Correct 411 ms 301648 KB Output is correct
15 Correct 558 ms 304468 KB Output is correct
16 Correct 836 ms 317972 KB Output is correct
17 Correct 462 ms 303856 KB Output is correct
18 Correct 451 ms 307280 KB Output is correct
19 Correct 524 ms 303984 KB Output is correct
20 Correct 477 ms 303956 KB Output is correct
21 Correct 474 ms 304724 KB Output is correct
22 Correct 403 ms 302156 KB Output is correct
23 Correct 52 ms 115792 KB Output is correct
24 Correct 49 ms 115796 KB Output is correct
25 Correct 53 ms 116464 KB Output is correct
26 Correct 51 ms 116048 KB Output is correct
27 Correct 52 ms 116304 KB Output is correct
28 Correct 49 ms 116056 KB Output is correct
29 Correct 50 ms 116056 KB Output is correct
30 Correct 50 ms 116168 KB Output is correct
31 Correct 56 ms 116096 KB Output is correct
32 Correct 52 ms 116316 KB Output is correct
33 Correct 53 ms 116228 KB Output is correct
34 Correct 50 ms 116052 KB Output is correct
35 Correct 52 ms 116228 KB Output is correct
36 Correct 49 ms 115796 KB Output is correct
37 Correct 53 ms 116032 KB Output is correct
38 Correct 54 ms 116308 KB Output is correct
39 Correct 51 ms 116048 KB Output is correct
40 Correct 51 ms 116308 KB Output is correct
41 Correct 50 ms 116048 KB Output is correct
42 Correct 51 ms 116276 KB Output is correct
43 Correct 51 ms 116312 KB Output is correct
44 Correct 50 ms 116304 KB Output is correct
45 Correct 50 ms 115796 KB Output is correct
46 Correct 48 ms 116048 KB Output is correct
47 Correct 59 ms 116444 KB Output is correct
48 Correct 49 ms 116064 KB Output is correct
49 Correct 51 ms 116048 KB Output is correct
50 Correct 52 ms 116060 KB Output is correct
51 Correct 51 ms 116316 KB Output is correct
52 Correct 50 ms 116308 KB Output is correct
53 Correct 51 ms 116304 KB Output is correct
54 Correct 52 ms 116120 KB Output is correct
55 Correct 56 ms 116216 KB Output is correct
56 Correct 56 ms 116052 KB Output is correct
57 Correct 53 ms 116060 KB Output is correct
58 Correct 53 ms 116052 KB Output is correct
59 Correct 55 ms 116052 KB Output is correct
60 Correct 139 ms 125008 KB Output is correct
61 Correct 438 ms 296280 KB Output is correct
62 Correct 153 ms 125332 KB Output is correct
63 Correct 127 ms 122280 KB Output is correct
64 Correct 110 ms 124308 KB Output is correct
65 Correct 149 ms 125148 KB Output is correct
66 Correct 60 ms 117072 KB Output is correct
67 Correct 336 ms 216804 KB Output is correct
68 Correct 352 ms 216800 KB Output is correct
69 Correct 429 ms 283980 KB Output is correct
70 Correct 431 ms 284024 KB Output is correct
71 Correct 360 ms 250084 KB Output is correct
72 Correct 382 ms 250212 KB Output is correct
73 Correct 464 ms 273268 KB Output is correct
74 Correct 496 ms 273236 KB Output is correct
75 Correct 76 ms 123176 KB Output is correct
76 Correct 360 ms 208864 KB Output is correct
77 Correct 48 ms 116060 KB Output is correct
78 Correct 46 ms 115796 KB Output is correct
79 Correct 49 ms 115792 KB Output is correct
80 Correct 54 ms 115796 KB Output is correct
81 Correct 57 ms 116048 KB Output is correct
82 Correct 50 ms 116068 KB Output is correct
83 Correct 49 ms 116048 KB Output is correct
84 Correct 48 ms 115796 KB Output is correct
85 Correct 54 ms 115792 KB Output is correct
86 Correct 50 ms 116048 KB Output is correct
87 Correct 50 ms 115948 KB Output is correct
88 Correct 49 ms 116168 KB Output is correct
89 Correct 79 ms 116512 KB Output is correct
90 Correct 105 ms 117016 KB Output is correct
91 Correct 90 ms 116816 KB Output is correct
92 Correct 298 ms 209916 KB Output is correct
93 Correct 360 ms 204124 KB Output is correct
94 Correct 459 ms 212720 KB Output is correct
95 Correct 305 ms 198816 KB Output is correct
96 Correct 291 ms 198116 KB Output is correct
97 Correct 297 ms 198152 KB Output is correct
98 Correct 335 ms 202192 KB Output is correct
99 Correct 341 ms 202172 KB Output is correct
100 Correct 380 ms 202032 KB Output is correct
101 Correct 407 ms 202844 KB Output is correct
102 Correct 410 ms 202336 KB Output is correct
103 Correct 243 ms 189168 KB Output is correct
104 Correct 238 ms 189168 KB Output is correct
105 Correct 225 ms 189164 KB Output is correct
106 Correct 285 ms 187264 KB Output is correct
107 Correct 267 ms 187520 KB Output is correct
108 Correct 262 ms 185216 KB Output is correct
109 Correct 225 ms 185168 KB Output is correct
110 Correct 115 ms 117236 KB Output is correct
111 Correct 88 ms 116656 KB Output is correct
112 Correct 570 ms 297716 KB Output is correct
113 Correct 510 ms 287056 KB Output is correct
114 Correct 380 ms 248176 KB Output is correct
115 Correct 146 ms 178568 KB Output is correct
116 Correct 348 ms 213472 KB Output is correct
117 Correct 592 ms 224244 KB Output is correct
118 Correct 358 ms 206256 KB Output is correct
119 Correct 354 ms 206436 KB Output is correct
120 Correct 79 ms 124496 KB Output is correct
121 Correct 529 ms 223440 KB Output is correct
122 Correct 492 ms 220644 KB Output is correct
123 Correct 592 ms 291668 KB Output is correct
124 Correct 494 ms 292552 KB Output is correct
125 Correct 655 ms 292180 KB Output is correct
126 Correct 849 ms 313180 KB Output is correct
127 Correct 527 ms 293072 KB Output is correct
128 Correct 508 ms 293072 KB Output is correct
129 Correct 878 ms 293372 KB Output is correct
130 Correct 503 ms 293380 KB Output is correct