Submission #710850

# Submission time Handle Problem Language Result Execution time Memory
710850 2023-03-16T02:11:58 Z Darren0724 Jail (JOI22_jail) C++17
100 / 100
1359 ms 341588 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(),x.end()
const long long INF=1e18;
const int N=120005;
const int K=18;
int n,m;
vector<vector<int>> adj;
vector<vector<int>> g;
vector<vector<int>> jump(K,vector<int>(N));
vector<int> deep(N);
vector<int> s1;
vector<int> t1;
void dfs(int k,int pa){
    //cout<<k<<' '<<pa<<endl;
    jump[0][k]=pa;
    for(int i=1;i<K;i++){
        jump[i][k]=jump[i-1][jump[i-1][k]];
    }
    for(int j:adj[k]){
        if(j==pa){
            continue;
        }
        deep[j]=deep[k]+1;
        dfs(j,k);
    }
}
int lca(int a,int b){
    if(deep[a]<deep[b]){
        swap(a,b);
    }
    int d=deep[a]-deep[b];
    for(int i=0;i<K;i++){
        if((1<<i)&d){
            a=jump[i][a];
        }
    }
    if(a==b){
        return b;
    }
    for(int i=K-1;i>=0;i--){
        if(jump[i][a]!=jump[i][b]){
            a=jump[i][a];
            b=jump[i][b];
        }
    }
    return jump[0][a];
}
int num1(int a,int b){
    return m+b*n+a;
}
int num2(int a,int b){
    return m+(b+K)*n+a;
}
void add1(int from,int to,int dis){
    //cout<<from<<' '<<to<<' '<<dis<<endl;
    if(dis==-1){
        return;
    }
    for(int i=0;i<K;i++){
        if(dis&(1<<i)){
            g[from].push_back(num1(to,i));
            to=jump[i][to];
        }
    }
}
void add2(int to,int from,int dis){
    //cout<<to<<' '<<from<<' '<<dis<<endl;
    if(dis==-1){
        return;
    }
    for(int i=0;i<K;i++){
        if(dis&(1<<i)){
            g[num2(from,i)].push_back(to);
            from=jump[i][from];
        }
    }
}
void solve(){
    adj.clear();
    g.clear();
    s1.resize(0);
    t1.resize(0);
    cin>>n;
    adj.resize(n);
    s1.resize(n,-1);
    t1.resize(n,-1);

    for(int i=1;i<n;i++){
        int a,b;cin>>a>>b;a--;b--;
        adj[a].push_back(b);
        adj[b].push_back(a);
    }
    dfs(0,0);
    cin>>m;
    int N=num2(n-1,K-1)+1;
    //cout<<N<<endl;
    g.resize(N);
    vector<int> s(m),t(m);
    for(int i=0;i<m;i++){
        cin>>s[i]>>t[i];s[i]--;t[i]--;
        s1[s[i]]=i;
        t1[t[i]]=i;
    }
    //cout<<"check"<<endl;
    for(int i=0;i<m;i++){
        int l=lca(s[i],t[i]);
        add1(i,jump[0][s[i]],deep[s[i]]-deep[l]);
        add1(i,t[i],deep[t[i]]-deep[l]);
    }
    for(int i=0;i<m;i++){
        int l=lca(s[i],t[i]);
        add2(i,s[i],deep[s[i]]-deep[l]);
        add2(i,jump[0][t[i]],deep[t[i]]-deep[l]);
    }
    //cout<<"check"<<endl;
    for(int j=K-1;j>0;j--){
        for(int i=0;i<n;i++){
            //cout<<i<<' '<<j<<endl;
            g[num1(i,j)].push_back(num1(i,j-1));
            g[num1(i,j)].push_back(num1(jump[j-1][i],j-1));
        }
    }
    //cout<<"check"<<endl;
    for(int j=K-1;j>0;j--){
        for(int i=0;i<n;i++){
            g[num2(i,j-1)].push_back(num2(i,j));
            g[num2(jump[j-1][i],j-1)].push_back(num2(i,j));
        }
    }
    //cout<<"check"<<endl;
    for(int i=0;i<n;i++){
        if(s1[i]!=-1){
            g[num1(i,0)].push_back(s1[i]);
        }
        if(t1[i]!=-1){
            g[t1[i]].push_back(num2(i,0));
        }
    }
    vector<int> in(N);
    for(int i=0;i<N;i++){
        //cout<<i<<": ";
        for(int j:g[i]){
            in[j]++;
            //cout<<j<<' ';
        }
        //cout<<endl;
    }
    queue<int> q;
    for(int i=0;i<N;i++){
        //cout<<i<<endl;
        if(in[i]==0){
            q.push(i);
        }
    }
    int cnt=0;
    while(q.size()){
        int p=q.front();
        q.pop();
        //cout<<p<<endl;
        cnt++;
        for(int j:g[p]){
            in[j]--;
            if(in[j]==0){
                q.push(j);
            }
        }
    }
    if(cnt==N){
        cout<<"Yes"<<endl;
    }
    else{
        cout<<"No"<<endl;
        /*for(int i=0;i<N;i++){
            if(in[i]){
                cout<<i<<' ';
            }
        }
        cout<<endl;*/
    }
}

signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    int t;cin>>t;
    while(t--){
        solve();
    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 9 ms 18216 KB Output is correct
2 Correct 9 ms 18216 KB Output is correct
3 Correct 9 ms 18316 KB Output is correct
4 Correct 194 ms 18944 KB Output is correct
5 Correct 411 ms 19160 KB Output is correct
6 Correct 25 ms 18856 KB Output is correct
7 Correct 24 ms 18856 KB Output is correct
8 Correct 26 ms 18856 KB Output is correct
9 Correct 510 ms 35288 KB Output is correct
10 Correct 633 ms 299984 KB Output is correct
11 Correct 89 ms 18372 KB Output is correct
12 Correct 445 ms 19744 KB Output is correct
13 Correct 744 ms 310908 KB Output is correct
14 Correct 648 ms 310940 KB Output is correct
15 Correct 862 ms 315056 KB Output is correct
16 Correct 1311 ms 335696 KB Output is correct
17 Correct 746 ms 315872 KB Output is correct
18 Correct 708 ms 315284 KB Output is correct
19 Correct 725 ms 313912 KB Output is correct
20 Correct 678 ms 313888 KB Output is correct
21 Correct 734 ms 318512 KB Output is correct
22 Correct 592 ms 307508 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 18216 KB Output is correct
2 Correct 9 ms 18216 KB Output is correct
3 Correct 27 ms 18864 KB Output is correct
4 Correct 28 ms 18876 KB Output is correct
5 Correct 26 ms 18856 KB Output is correct
6 Correct 30 ms 18856 KB Output is correct
7 Correct 27 ms 18856 KB Output is correct
8 Correct 26 ms 18848 KB Output is correct
9 Correct 26 ms 18856 KB Output is correct
10 Correct 27 ms 18924 KB Output is correct
11 Correct 25 ms 18748 KB Output is correct
12 Correct 18 ms 18728 KB Output is correct
13 Correct 18 ms 18856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 18216 KB Output is correct
2 Correct 9 ms 18216 KB Output is correct
3 Correct 27 ms 18864 KB Output is correct
4 Correct 28 ms 18876 KB Output is correct
5 Correct 26 ms 18856 KB Output is correct
6 Correct 30 ms 18856 KB Output is correct
7 Correct 27 ms 18856 KB Output is correct
8 Correct 26 ms 18848 KB Output is correct
9 Correct 26 ms 18856 KB Output is correct
10 Correct 27 ms 18924 KB Output is correct
11 Correct 25 ms 18748 KB Output is correct
12 Correct 18 ms 18728 KB Output is correct
13 Correct 18 ms 18856 KB Output is correct
14 Correct 9 ms 18344 KB Output is correct
15 Correct 9 ms 18216 KB Output is correct
16 Correct 28 ms 18752 KB Output is correct
17 Correct 26 ms 18996 KB Output is correct
18 Correct 27 ms 18876 KB Output is correct
19 Correct 10 ms 18216 KB Output is correct
20 Correct 27 ms 18856 KB Output is correct
21 Correct 26 ms 18856 KB Output is correct
22 Correct 25 ms 18948 KB Output is correct
23 Correct 10 ms 18224 KB Output is correct
24 Correct 10 ms 18480 KB Output is correct
25 Correct 26 ms 18984 KB Output is correct
26 Correct 12 ms 18728 KB Output is correct
27 Correct 26 ms 18880 KB Output is correct
28 Correct 10 ms 18232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 18216 KB Output is correct
2 Correct 9 ms 18216 KB Output is correct
3 Correct 27 ms 18864 KB Output is correct
4 Correct 28 ms 18876 KB Output is correct
5 Correct 26 ms 18856 KB Output is correct
6 Correct 30 ms 18856 KB Output is correct
7 Correct 27 ms 18856 KB Output is correct
8 Correct 26 ms 18848 KB Output is correct
9 Correct 26 ms 18856 KB Output is correct
10 Correct 27 ms 18924 KB Output is correct
11 Correct 25 ms 18748 KB Output is correct
12 Correct 18 ms 18728 KB Output is correct
13 Correct 18 ms 18856 KB Output is correct
14 Correct 9 ms 18344 KB Output is correct
15 Correct 9 ms 18216 KB Output is correct
16 Correct 28 ms 18752 KB Output is correct
17 Correct 26 ms 18996 KB Output is correct
18 Correct 27 ms 18876 KB Output is correct
19 Correct 10 ms 18216 KB Output is correct
20 Correct 27 ms 18856 KB Output is correct
21 Correct 26 ms 18856 KB Output is correct
22 Correct 25 ms 18948 KB Output is correct
23 Correct 10 ms 18224 KB Output is correct
24 Correct 10 ms 18480 KB Output is correct
25 Correct 26 ms 18984 KB Output is correct
26 Correct 12 ms 18728 KB Output is correct
27 Correct 26 ms 18880 KB Output is correct
28 Correct 10 ms 18232 KB Output is correct
29 Correct 27 ms 18948 KB Output is correct
30 Correct 28 ms 19048 KB Output is correct
31 Correct 28 ms 18984 KB Output is correct
32 Correct 30 ms 18936 KB Output is correct
33 Correct 29 ms 18928 KB Output is correct
34 Correct 23 ms 18816 KB Output is correct
35 Correct 24 ms 18756 KB Output is correct
36 Correct 21 ms 18864 KB Output is correct
37 Correct 17 ms 18728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 18216 KB Output is correct
2 Correct 9 ms 18216 KB Output is correct
3 Correct 27 ms 18864 KB Output is correct
4 Correct 28 ms 18876 KB Output is correct
5 Correct 26 ms 18856 KB Output is correct
6 Correct 30 ms 18856 KB Output is correct
7 Correct 27 ms 18856 KB Output is correct
8 Correct 26 ms 18848 KB Output is correct
9 Correct 26 ms 18856 KB Output is correct
10 Correct 27 ms 18924 KB Output is correct
11 Correct 25 ms 18748 KB Output is correct
12 Correct 18 ms 18728 KB Output is correct
13 Correct 18 ms 18856 KB Output is correct
14 Correct 9 ms 18344 KB Output is correct
15 Correct 9 ms 18216 KB Output is correct
16 Correct 28 ms 18752 KB Output is correct
17 Correct 26 ms 18996 KB Output is correct
18 Correct 27 ms 18876 KB Output is correct
19 Correct 10 ms 18216 KB Output is correct
20 Correct 27 ms 18856 KB Output is correct
21 Correct 26 ms 18856 KB Output is correct
22 Correct 25 ms 18948 KB Output is correct
23 Correct 10 ms 18224 KB Output is correct
24 Correct 10 ms 18480 KB Output is correct
25 Correct 26 ms 18984 KB Output is correct
26 Correct 12 ms 18728 KB Output is correct
27 Correct 26 ms 18880 KB Output is correct
28 Correct 10 ms 18232 KB Output is correct
29 Correct 27 ms 18948 KB Output is correct
30 Correct 28 ms 19048 KB Output is correct
31 Correct 28 ms 18984 KB Output is correct
32 Correct 30 ms 18936 KB Output is correct
33 Correct 29 ms 18928 KB Output is correct
34 Correct 23 ms 18816 KB Output is correct
35 Correct 24 ms 18756 KB Output is correct
36 Correct 21 ms 18864 KB Output is correct
37 Correct 17 ms 18728 KB Output is correct
38 Correct 549 ms 35416 KB Output is correct
39 Correct 647 ms 299936 KB Output is correct
40 Correct 562 ms 34944 KB Output is correct
41 Correct 532 ms 35120 KB Output is correct
42 Correct 475 ms 35664 KB Output is correct
43 Correct 542 ms 35536 KB Output is correct
44 Correct 95 ms 21212 KB Output is correct
45 Correct 683 ms 310156 KB Output is correct
46 Correct 669 ms 310312 KB Output is correct
47 Correct 691 ms 309128 KB Output is correct
48 Correct 704 ms 309060 KB Output is correct
49 Correct 603 ms 311728 KB Output is correct
50 Correct 635 ms 311700 KB Output is correct
51 Correct 690 ms 312148 KB Output is correct
52 Correct 705 ms 312056 KB Output is correct
53 Correct 142 ms 38584 KB Output is correct
54 Correct 727 ms 312084 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 18228 KB Output is correct
2 Correct 11 ms 18216 KB Output is correct
3 Correct 9 ms 18216 KB Output is correct
4 Correct 10 ms 18216 KB Output is correct
5 Correct 87 ms 18392 KB Output is correct
6 Correct 18 ms 18856 KB Output is correct
7 Correct 18 ms 18856 KB Output is correct
8 Correct 10 ms 18232 KB Output is correct
9 Correct 10 ms 18216 KB Output is correct
10 Correct 10 ms 18492 KB Output is correct
11 Correct 11 ms 18216 KB Output is correct
12 Correct 24 ms 18864 KB Output is correct
13 Correct 239 ms 19300 KB Output is correct
14 Correct 473 ms 19356 KB Output is correct
15 Correct 333 ms 19176 KB Output is correct
16 Correct 700 ms 312268 KB Output is correct
17 Correct 764 ms 321484 KB Output is correct
18 Correct 927 ms 333124 KB Output is correct
19 Correct 732 ms 314120 KB Output is correct
20 Correct 729 ms 314188 KB Output is correct
21 Correct 738 ms 314092 KB Output is correct
22 Correct 673 ms 322040 KB Output is correct
23 Correct 647 ms 321900 KB Output is correct
24 Correct 901 ms 321488 KB Output is correct
25 Correct 889 ms 321632 KB Output is correct
26 Correct 893 ms 321484 KB Output is correct
27 Correct 1188 ms 326596 KB Output is correct
28 Correct 684 ms 326416 KB Output is correct
29 Correct 674 ms 326340 KB Output is correct
30 Correct 1004 ms 317368 KB Output is correct
31 Correct 657 ms 317268 KB Output is correct
32 Correct 976 ms 318356 KB Output is correct
33 Correct 699 ms 318140 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 9 ms 18216 KB Output is correct
2 Correct 9 ms 18216 KB Output is correct
3 Correct 9 ms 18316 KB Output is correct
4 Correct 194 ms 18944 KB Output is correct
5 Correct 411 ms 19160 KB Output is correct
6 Correct 25 ms 18856 KB Output is correct
7 Correct 24 ms 18856 KB Output is correct
8 Correct 26 ms 18856 KB Output is correct
9 Correct 510 ms 35288 KB Output is correct
10 Correct 633 ms 299984 KB Output is correct
11 Correct 89 ms 18372 KB Output is correct
12 Correct 445 ms 19744 KB Output is correct
13 Correct 744 ms 310908 KB Output is correct
14 Correct 648 ms 310940 KB Output is correct
15 Correct 862 ms 315056 KB Output is correct
16 Correct 1311 ms 335696 KB Output is correct
17 Correct 746 ms 315872 KB Output is correct
18 Correct 708 ms 315284 KB Output is correct
19 Correct 725 ms 313912 KB Output is correct
20 Correct 678 ms 313888 KB Output is correct
21 Correct 734 ms 318512 KB Output is correct
22 Correct 592 ms 307508 KB Output is correct
23 Correct 9 ms 18216 KB Output is correct
24 Correct 9 ms 18216 KB Output is correct
25 Correct 27 ms 18864 KB Output is correct
26 Correct 28 ms 18876 KB Output is correct
27 Correct 26 ms 18856 KB Output is correct
28 Correct 30 ms 18856 KB Output is correct
29 Correct 27 ms 18856 KB Output is correct
30 Correct 26 ms 18848 KB Output is correct
31 Correct 26 ms 18856 KB Output is correct
32 Correct 27 ms 18924 KB Output is correct
33 Correct 25 ms 18748 KB Output is correct
34 Correct 18 ms 18728 KB Output is correct
35 Correct 18 ms 18856 KB Output is correct
36 Correct 9 ms 18344 KB Output is correct
37 Correct 9 ms 18216 KB Output is correct
38 Correct 28 ms 18752 KB Output is correct
39 Correct 26 ms 18996 KB Output is correct
40 Correct 27 ms 18876 KB Output is correct
41 Correct 10 ms 18216 KB Output is correct
42 Correct 27 ms 18856 KB Output is correct
43 Correct 26 ms 18856 KB Output is correct
44 Correct 25 ms 18948 KB Output is correct
45 Correct 10 ms 18224 KB Output is correct
46 Correct 10 ms 18480 KB Output is correct
47 Correct 26 ms 18984 KB Output is correct
48 Correct 12 ms 18728 KB Output is correct
49 Correct 26 ms 18880 KB Output is correct
50 Correct 10 ms 18232 KB Output is correct
51 Correct 27 ms 18948 KB Output is correct
52 Correct 28 ms 19048 KB Output is correct
53 Correct 28 ms 18984 KB Output is correct
54 Correct 30 ms 18936 KB Output is correct
55 Correct 29 ms 18928 KB Output is correct
56 Correct 23 ms 18816 KB Output is correct
57 Correct 24 ms 18756 KB Output is correct
58 Correct 21 ms 18864 KB Output is correct
59 Correct 17 ms 18728 KB Output is correct
60 Correct 549 ms 35416 KB Output is correct
61 Correct 647 ms 299936 KB Output is correct
62 Correct 562 ms 34944 KB Output is correct
63 Correct 532 ms 35120 KB Output is correct
64 Correct 475 ms 35664 KB Output is correct
65 Correct 542 ms 35536 KB Output is correct
66 Correct 95 ms 21212 KB Output is correct
67 Correct 683 ms 310156 KB Output is correct
68 Correct 669 ms 310312 KB Output is correct
69 Correct 691 ms 309128 KB Output is correct
70 Correct 704 ms 309060 KB Output is correct
71 Correct 603 ms 311728 KB Output is correct
72 Correct 635 ms 311700 KB Output is correct
73 Correct 690 ms 312148 KB Output is correct
74 Correct 705 ms 312056 KB Output is correct
75 Correct 142 ms 38584 KB Output is correct
76 Correct 727 ms 312084 KB Output is correct
77 Correct 10 ms 18228 KB Output is correct
78 Correct 11 ms 18216 KB Output is correct
79 Correct 9 ms 18216 KB Output is correct
80 Correct 10 ms 18216 KB Output is correct
81 Correct 87 ms 18392 KB Output is correct
82 Correct 18 ms 18856 KB Output is correct
83 Correct 18 ms 18856 KB Output is correct
84 Correct 10 ms 18232 KB Output is correct
85 Correct 10 ms 18216 KB Output is correct
86 Correct 10 ms 18492 KB Output is correct
87 Correct 11 ms 18216 KB Output is correct
88 Correct 24 ms 18864 KB Output is correct
89 Correct 239 ms 19300 KB Output is correct
90 Correct 473 ms 19356 KB Output is correct
91 Correct 333 ms 19176 KB Output is correct
92 Correct 700 ms 312268 KB Output is correct
93 Correct 764 ms 321484 KB Output is correct
94 Correct 927 ms 333124 KB Output is correct
95 Correct 732 ms 314120 KB Output is correct
96 Correct 729 ms 314188 KB Output is correct
97 Correct 738 ms 314092 KB Output is correct
98 Correct 673 ms 322040 KB Output is correct
99 Correct 647 ms 321900 KB Output is correct
100 Correct 901 ms 321488 KB Output is correct
101 Correct 889 ms 321632 KB Output is correct
102 Correct 893 ms 321484 KB Output is correct
103 Correct 1188 ms 326596 KB Output is correct
104 Correct 684 ms 326416 KB Output is correct
105 Correct 674 ms 326340 KB Output is correct
106 Correct 1004 ms 317368 KB Output is correct
107 Correct 657 ms 317268 KB Output is correct
108 Correct 976 ms 318356 KB Output is correct
109 Correct 699 ms 318140 KB Output is correct
110 Correct 429 ms 19888 KB Output is correct
111 Correct 344 ms 19228 KB Output is correct
112 Correct 964 ms 325980 KB Output is correct
113 Correct 995 ms 314652 KB Output is correct
114 Correct 761 ms 320940 KB Output is correct
115 Correct 493 ms 310200 KB Output is correct
116 Correct 905 ms 319696 KB Output is correct
117 Correct 1022 ms 336364 KB Output is correct
118 Correct 834 ms 312388 KB Output is correct
119 Correct 837 ms 312224 KB Output is correct
120 Correct 86 ms 43276 KB Output is correct
121 Correct 1180 ms 320364 KB Output is correct
122 Correct 1359 ms 320184 KB Output is correct
123 Correct 1111 ms 314652 KB Output is correct
124 Correct 769 ms 314628 KB Output is correct
125 Correct 1151 ms 315420 KB Output is correct
126 Correct 1358 ms 341588 KB Output is correct
127 Correct 860 ms 332876 KB Output is correct
128 Correct 730 ms 332264 KB Output is correct
129 Correct 840 ms 332648 KB Output is correct
130 Correct 745 ms 332736 KB Output is correct