Submission #593018

# Submission time Handle Problem Language Result Execution time Memory
593018 2022-07-10T07:56:45 Z jhnah917 Jail (JOI22_jail) C++14
72 / 100
5000 ms 483328 KB
#include <bits/stdc++.h>
using namespace std;

int N, K, P[22][121212], D[121212], C[121212];
vector<int> G[121212], G2[121212];
vector<pair<int,int>> Type[121212];
int In[121212];
void AddEdge(int s, int e){ G2[s].push_back(e); In[e] += 1; }

void Clear(){
    for(int i=1; i<=N; i++) G[i].clear();
    for(int i=1; i<=N; i++) G2[i].clear();
    for(int i=1; i<=N; i++) Type[i].clear();
    fill(In+1, In+N+1, 0);
}

void DFS(int v, int b=-1){
    for(auto i : G[v]) if(i != b) P[0][i] = v, D[i] = D[v] + 1, DFS(i, v);
}

int Kth(int v, int k){
    for(int i=0; k; i++, k>>=1) if(k & 1) v = P[i][v];
    return v;
}

int LCA(int u, int v){
    if(D[u] < D[v]) swap(u, v);
    int diff = D[u] - D[v];
    for(int i=0; diff; i++, diff>>=1) if(diff & 1) u = P[i][u];
    if(u == v) return u;
    for(int i=21; i>=0; i--) if(P[i][u] != P[i][v]) u = P[i][u], v = P[i][v];
    return P[0][u];
}

bool OnPath(int u, int v, int x){
    auto check = [&](int up, int dw, int x) -> bool {
        if(D[x] < D[up] || D[dw] < D[x]) return false;
        return Kth(dw, D[dw] - D[x]) == x;
    };
    int l = LCA(u, v);
    return check(l, u, x) || check(l, v, x);
}

bool TopSort(){
    queue<int> Q;
    for(int i=1; i<=K; i++) if(!In[i]) Q.push(i);
    while(!Q.empty()){
        int v = Q.front(); Q.pop();
        for(auto i : G2[v]) if(!--In[i]) Q.push(i);
    }
    return *max_element(In+1, In+N+1) == 0;
}

int S[121212], T[121212];
void Graph5(){
    for(int i=1; i<=K; i++){
        for(int j=1; j<=K; j++){
            if(i == j) continue;
            if(OnPath(S[i], T[i], S[j])) AddEdge(j, i);
            if(OnPath(S[i], T[i], T[j])) AddEdge(i, j);
        }
    }
}

void Graph6(){
    for(int i=1; i<=K; i++){
        Type[S[i]].emplace_back(i, 1);
        Type[T[i]].emplace_back(i, 2);
    }
    for(int i=1; i<=K; i++){
        int a = S[i], b = T[i], l = LCA(a, b);
        for(auto [v,t] : Type[l]){
            if(v == i) continue;
            if(t == 1) AddEdge(v, i);
            if(t == 2) AddEdge(i, v);
        }
        while(a != l){
            for(auto [v,t] : Type[a]){
                if(v == i) continue;
                if(t == 1) AddEdge(v, i);
                if(t == 2) AddEdge(i, v);
            }
            a = P[0][a];
        }
        while(b != l){
            for(auto [v,t] : Type[b]){
                if(v == i) continue;
                if(t == 1) AddEdge(v, i);
                if(t == 2) AddEdge(i, v);
            }
            b = P[0][b];
        }
    }
}

void Solve(){
    cin >> N; Clear();
    for(int i=1,s,e; i<N; i++) cin >> s >> e, G[s].push_back(e), G[e].push_back(s);
    DFS(1);
    for(int i=1; i<22; i++) for(int j=1; j<=N; j++) P[i][j] = P[i-1][P[i-1][j]];

    cin >> K;
    for(int i=1; i<=K; i++) cin >> S[i] >> T[i];
    if(K <= 500) Graph5();
    else Graph6();

    cout << (TopSort() ? "Yes" : "No") << "\n";
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(nullptr);
    int TC; cin >> TC;
    for(int tc=1; tc<=TC; tc++) Solve();
}

Compilation message

jail.cpp: In function 'void Graph6()':
jail.cpp:72:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   72 |         for(auto [v,t] : Type[l]){
      |                  ^
jail.cpp:78:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   78 |             for(auto [v,t] : Type[a]){
      |                      ^
jail.cpp:86:22: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   86 |             for(auto [v,t] : Type[b]){
      |                      ^
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8916 KB Output is correct
2 Correct 5 ms 8916 KB Output is correct
3 Correct 4 ms 8916 KB Output is correct
4 Correct 13 ms 9252 KB Output is correct
5 Correct 22 ms 9144 KB Output is correct
6 Correct 6 ms 9076 KB Output is correct
7 Correct 6 ms 9016 KB Output is correct
8 Correct 12 ms 9160 KB Output is correct
9 Correct 245 ms 12344 KB Output is correct
10 Correct 73 ms 33012 KB Output is correct
11 Correct 10 ms 9016 KB Output is correct
12 Correct 121 ms 9276 KB Output is correct
13 Correct 169 ms 64884 KB Output is correct
14 Correct 185 ms 78792 KB Output is correct
15 Execution timed out 5091 ms 483328 KB Time limit exceeded
16 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8916 KB Output is correct
2 Correct 4 ms 8916 KB Output is correct
3 Correct 6 ms 9076 KB Output is correct
4 Correct 5 ms 9012 KB Output is correct
5 Correct 6 ms 9044 KB Output is correct
6 Correct 6 ms 9044 KB Output is correct
7 Correct 5 ms 9044 KB Output is correct
8 Correct 6 ms 9044 KB Output is correct
9 Correct 6 ms 9068 KB Output is correct
10 Correct 6 ms 9020 KB Output is correct
11 Correct 5 ms 9044 KB Output is correct
12 Correct 5 ms 9012 KB Output is correct
13 Correct 5 ms 9016 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8916 KB Output is correct
2 Correct 4 ms 8916 KB Output is correct
3 Correct 6 ms 9076 KB Output is correct
4 Correct 5 ms 9012 KB Output is correct
5 Correct 6 ms 9044 KB Output is correct
6 Correct 6 ms 9044 KB Output is correct
7 Correct 5 ms 9044 KB Output is correct
8 Correct 6 ms 9044 KB Output is correct
9 Correct 6 ms 9068 KB Output is correct
10 Correct 6 ms 9020 KB Output is correct
11 Correct 5 ms 9044 KB Output is correct
12 Correct 5 ms 9012 KB Output is correct
13 Correct 5 ms 9016 KB Output is correct
14 Correct 5 ms 8872 KB Output is correct
15 Correct 5 ms 8916 KB Output is correct
16 Correct 6 ms 9072 KB Output is correct
17 Correct 6 ms 9124 KB Output is correct
18 Correct 6 ms 9016 KB Output is correct
19 Correct 5 ms 8916 KB Output is correct
20 Correct 6 ms 9044 KB Output is correct
21 Correct 6 ms 9044 KB Output is correct
22 Correct 6 ms 9068 KB Output is correct
23 Correct 5 ms 8872 KB Output is correct
24 Correct 5 ms 9004 KB Output is correct
25 Correct 6 ms 9020 KB Output is correct
26 Correct 5 ms 8924 KB Output is correct
27 Correct 6 ms 9044 KB Output is correct
28 Correct 5 ms 8916 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8916 KB Output is correct
2 Correct 4 ms 8916 KB Output is correct
3 Correct 6 ms 9076 KB Output is correct
4 Correct 5 ms 9012 KB Output is correct
5 Correct 6 ms 9044 KB Output is correct
6 Correct 6 ms 9044 KB Output is correct
7 Correct 5 ms 9044 KB Output is correct
8 Correct 6 ms 9044 KB Output is correct
9 Correct 6 ms 9068 KB Output is correct
10 Correct 6 ms 9020 KB Output is correct
11 Correct 5 ms 9044 KB Output is correct
12 Correct 5 ms 9012 KB Output is correct
13 Correct 5 ms 9016 KB Output is correct
14 Correct 5 ms 8872 KB Output is correct
15 Correct 5 ms 8916 KB Output is correct
16 Correct 6 ms 9072 KB Output is correct
17 Correct 6 ms 9124 KB Output is correct
18 Correct 6 ms 9016 KB Output is correct
19 Correct 5 ms 8916 KB Output is correct
20 Correct 6 ms 9044 KB Output is correct
21 Correct 6 ms 9044 KB Output is correct
22 Correct 6 ms 9068 KB Output is correct
23 Correct 5 ms 8872 KB Output is correct
24 Correct 5 ms 9004 KB Output is correct
25 Correct 6 ms 9020 KB Output is correct
26 Correct 5 ms 8924 KB Output is correct
27 Correct 6 ms 9044 KB Output is correct
28 Correct 5 ms 8916 KB Output is correct
29 Correct 13 ms 9140 KB Output is correct
30 Correct 15 ms 9012 KB Output is correct
31 Correct 13 ms 9044 KB Output is correct
32 Correct 10 ms 8916 KB Output is correct
33 Correct 6 ms 9020 KB Output is correct
34 Correct 25 ms 9084 KB Output is correct
35 Correct 29 ms 9044 KB Output is correct
36 Correct 20 ms 9068 KB Output is correct
37 Correct 14 ms 9044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8916 KB Output is correct
2 Correct 4 ms 8916 KB Output is correct
3 Correct 6 ms 9076 KB Output is correct
4 Correct 5 ms 9012 KB Output is correct
5 Correct 6 ms 9044 KB Output is correct
6 Correct 6 ms 9044 KB Output is correct
7 Correct 5 ms 9044 KB Output is correct
8 Correct 6 ms 9044 KB Output is correct
9 Correct 6 ms 9068 KB Output is correct
10 Correct 6 ms 9020 KB Output is correct
11 Correct 5 ms 9044 KB Output is correct
12 Correct 5 ms 9012 KB Output is correct
13 Correct 5 ms 9016 KB Output is correct
14 Correct 5 ms 8872 KB Output is correct
15 Correct 5 ms 8916 KB Output is correct
16 Correct 6 ms 9072 KB Output is correct
17 Correct 6 ms 9124 KB Output is correct
18 Correct 6 ms 9016 KB Output is correct
19 Correct 5 ms 8916 KB Output is correct
20 Correct 6 ms 9044 KB Output is correct
21 Correct 6 ms 9044 KB Output is correct
22 Correct 6 ms 9068 KB Output is correct
23 Correct 5 ms 8872 KB Output is correct
24 Correct 5 ms 9004 KB Output is correct
25 Correct 6 ms 9020 KB Output is correct
26 Correct 5 ms 8924 KB Output is correct
27 Correct 6 ms 9044 KB Output is correct
28 Correct 5 ms 8916 KB Output is correct
29 Correct 13 ms 9140 KB Output is correct
30 Correct 15 ms 9012 KB Output is correct
31 Correct 13 ms 9044 KB Output is correct
32 Correct 10 ms 8916 KB Output is correct
33 Correct 6 ms 9020 KB Output is correct
34 Correct 25 ms 9084 KB Output is correct
35 Correct 29 ms 9044 KB Output is correct
36 Correct 20 ms 9068 KB Output is correct
37 Correct 14 ms 9044 KB Output is correct
38 Correct 245 ms 12316 KB Output is correct
39 Correct 66 ms 33076 KB Output is correct
40 Correct 543 ms 11212 KB Output is correct
41 Correct 579 ms 10096 KB Output is correct
42 Correct 492 ms 11588 KB Output is correct
43 Correct 30 ms 10148 KB Output is correct
44 Correct 468 ms 9472 KB Output is correct
45 Correct 86 ms 24268 KB Output is correct
46 Correct 89 ms 24288 KB Output is correct
47 Correct 87 ms 28000 KB Output is correct
48 Correct 95 ms 27924 KB Output is correct
49 Correct 94 ms 24372 KB Output is correct
50 Correct 111 ms 24360 KB Output is correct
51 Correct 48 ms 25412 KB Output is correct
52 Correct 52 ms 25420 KB Output is correct
53 Correct 385 ms 10208 KB Output is correct
54 Correct 104 ms 24244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 8952 KB Output is correct
2 Correct 5 ms 8916 KB Output is correct
3 Correct 5 ms 8916 KB Output is correct
4 Correct 5 ms 8916 KB Output is correct
5 Correct 12 ms 9136 KB Output is correct
6 Correct 5 ms 8980 KB Output is correct
7 Correct 5 ms 9044 KB Output is correct
8 Correct 6 ms 8872 KB Output is correct
9 Correct 6 ms 8916 KB Output is correct
10 Correct 6 ms 9004 KB Output is correct
11 Correct 5 ms 8916 KB Output is correct
12 Correct 21 ms 9064 KB Output is correct
13 Correct 206 ms 9332 KB Output is correct
14 Correct 110 ms 9232 KB Output is correct
15 Correct 171 ms 9156 KB Output is correct
16 Correct 66 ms 25292 KB Output is correct
17 Correct 197 ms 32968 KB Output is correct
18 Correct 534 ms 53272 KB Output is correct
19 Correct 73 ms 25920 KB Output is correct
20 Correct 73 ms 25860 KB Output is correct
21 Correct 72 ms 25876 KB Output is correct
22 Correct 174 ms 34400 KB Output is correct
23 Correct 130 ms 34880 KB Output is correct
24 Correct 134 ms 34460 KB Output is correct
25 Correct 134 ms 34604 KB Output is correct
26 Correct 148 ms 34736 KB Output is correct
27 Correct 213 ms 33060 KB Output is correct
28 Correct 196 ms 33092 KB Output is correct
29 Correct 227 ms 35944 KB Output is correct
30 Correct 113 ms 32432 KB Output is correct
31 Correct 124 ms 32400 KB Output is correct
32 Correct 121 ms 32436 KB Output is correct
33 Correct 121 ms 32424 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 8916 KB Output is correct
2 Correct 5 ms 8916 KB Output is correct
3 Correct 4 ms 8916 KB Output is correct
4 Correct 13 ms 9252 KB Output is correct
5 Correct 22 ms 9144 KB Output is correct
6 Correct 6 ms 9076 KB Output is correct
7 Correct 6 ms 9016 KB Output is correct
8 Correct 12 ms 9160 KB Output is correct
9 Correct 245 ms 12344 KB Output is correct
10 Correct 73 ms 33012 KB Output is correct
11 Correct 10 ms 9016 KB Output is correct
12 Correct 121 ms 9276 KB Output is correct
13 Correct 169 ms 64884 KB Output is correct
14 Correct 185 ms 78792 KB Output is correct
15 Execution timed out 5091 ms 483328 KB Time limit exceeded
16 Halted 0 ms 0 KB -