답안 #710852

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
710852 2023-03-16T02:19:28 Z Darren0724 Jail (JOI22_jail) C++17
100 / 100
1318 ms 287696 KB
#include<bits/stdc++.h>
using namespace std;
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){
    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){
    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){
    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;
    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;
    }
    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]);
    }
    for(int j=K-1;j>0;j--){
        for(int i=0;i<n;i++){
            g[num1(i,j)].push_back(num1(i,j-1));
            g[num1(i,j)].push_back(num1(jump[j-1][i],j-1));
        }
    }
    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));
        }
    }
    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++){
        for(int j:g[i]){
            in[j]++;
        }
    }
    queue<int> q;
    for(int i=0;i<N;i++){
        if(in[i]==0){
            q.push(i);
        }
    }
    int cnt=0;
    while(q.size()){
        int p=q.front();
        q.pop();
        cnt++;
        for(int j:g[p]){
            in[j]--;
            if(in[j]==0){
                q.push(j);
            }
        }
    }
    if(cnt==N){
        cout<<"Yes"<<'\n';
    }
    else{
        cout<<"No"<<'\n';
    }
}

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

    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9212 KB Output is correct
2 Correct 5 ms 9340 KB Output is correct
3 Correct 5 ms 9208 KB Output is correct
4 Correct 172 ms 10008 KB Output is correct
5 Correct 368 ms 10176 KB Output is correct
6 Correct 20 ms 9832 KB Output is correct
7 Correct 21 ms 9940 KB Output is correct
8 Correct 21 ms 9848 KB Output is correct
9 Correct 464 ms 23768 KB Output is correct
10 Correct 565 ms 270732 KB Output is correct
11 Correct 72 ms 9484 KB Output is correct
12 Correct 414 ms 10672 KB Output is correct
13 Correct 664 ms 275936 KB Output is correct
14 Correct 561 ms 275852 KB Output is correct
15 Correct 775 ms 277204 KB Output is correct
16 Correct 1204 ms 286616 KB Output is correct
17 Correct 645 ms 277060 KB Output is correct
18 Correct 667 ms 281696 KB Output is correct
19 Correct 679 ms 277348 KB Output is correct
20 Correct 591 ms 277304 KB Output is correct
21 Correct 690 ms 278744 KB Output is correct
22 Correct 537 ms 276292 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9248 KB Output is correct
2 Correct 5 ms 9324 KB Output is correct
3 Correct 23 ms 9896 KB Output is correct
4 Correct 21 ms 9856 KB Output is correct
5 Correct 22 ms 9852 KB Output is correct
6 Correct 22 ms 9852 KB Output is correct
7 Correct 21 ms 9844 KB Output is correct
8 Correct 23 ms 9852 KB Output is correct
9 Correct 22 ms 9852 KB Output is correct
10 Correct 21 ms 9848 KB Output is correct
11 Correct 18 ms 9908 KB Output is correct
12 Correct 13 ms 9916 KB Output is correct
13 Correct 13 ms 9836 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9248 KB Output is correct
2 Correct 5 ms 9324 KB Output is correct
3 Correct 23 ms 9896 KB Output is correct
4 Correct 21 ms 9856 KB Output is correct
5 Correct 22 ms 9852 KB Output is correct
6 Correct 22 ms 9852 KB Output is correct
7 Correct 21 ms 9844 KB Output is correct
8 Correct 23 ms 9852 KB Output is correct
9 Correct 22 ms 9852 KB Output is correct
10 Correct 21 ms 9848 KB Output is correct
11 Correct 18 ms 9908 KB Output is correct
12 Correct 13 ms 9916 KB Output is correct
13 Correct 13 ms 9836 KB Output is correct
14 Correct 5 ms 9212 KB Output is correct
15 Correct 5 ms 9340 KB Output is correct
16 Correct 23 ms 9852 KB Output is correct
17 Correct 21 ms 10108 KB Output is correct
18 Correct 21 ms 9852 KB Output is correct
19 Correct 6 ms 9340 KB Output is correct
20 Correct 25 ms 9852 KB Output is correct
21 Correct 22 ms 9840 KB Output is correct
22 Correct 22 ms 9852 KB Output is correct
23 Correct 5 ms 9268 KB Output is correct
24 Correct 7 ms 9596 KB Output is correct
25 Correct 21 ms 9980 KB Output is correct
26 Correct 9 ms 9852 KB Output is correct
27 Correct 24 ms 9848 KB Output is correct
28 Correct 6 ms 9268 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9248 KB Output is correct
2 Correct 5 ms 9324 KB Output is correct
3 Correct 23 ms 9896 KB Output is correct
4 Correct 21 ms 9856 KB Output is correct
5 Correct 22 ms 9852 KB Output is correct
6 Correct 22 ms 9852 KB Output is correct
7 Correct 21 ms 9844 KB Output is correct
8 Correct 23 ms 9852 KB Output is correct
9 Correct 22 ms 9852 KB Output is correct
10 Correct 21 ms 9848 KB Output is correct
11 Correct 18 ms 9908 KB Output is correct
12 Correct 13 ms 9916 KB Output is correct
13 Correct 13 ms 9836 KB Output is correct
14 Correct 5 ms 9212 KB Output is correct
15 Correct 5 ms 9340 KB Output is correct
16 Correct 23 ms 9852 KB Output is correct
17 Correct 21 ms 10108 KB Output is correct
18 Correct 21 ms 9852 KB Output is correct
19 Correct 6 ms 9340 KB Output is correct
20 Correct 25 ms 9852 KB Output is correct
21 Correct 22 ms 9840 KB Output is correct
22 Correct 22 ms 9852 KB Output is correct
23 Correct 5 ms 9268 KB Output is correct
24 Correct 7 ms 9596 KB Output is correct
25 Correct 21 ms 9980 KB Output is correct
26 Correct 9 ms 9852 KB Output is correct
27 Correct 24 ms 9848 KB Output is correct
28 Correct 6 ms 9268 KB Output is correct
29 Correct 25 ms 9852 KB Output is correct
30 Correct 21 ms 10108 KB Output is correct
31 Correct 25 ms 10108 KB Output is correct
32 Correct 24 ms 9852 KB Output is correct
33 Correct 22 ms 9964 KB Output is correct
34 Correct 18 ms 9724 KB Output is correct
35 Correct 20 ms 9716 KB Output is correct
36 Correct 16 ms 9968 KB Output is correct
37 Correct 14 ms 9724 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9248 KB Output is correct
2 Correct 5 ms 9324 KB Output is correct
3 Correct 23 ms 9896 KB Output is correct
4 Correct 21 ms 9856 KB Output is correct
5 Correct 22 ms 9852 KB Output is correct
6 Correct 22 ms 9852 KB Output is correct
7 Correct 21 ms 9844 KB Output is correct
8 Correct 23 ms 9852 KB Output is correct
9 Correct 22 ms 9852 KB Output is correct
10 Correct 21 ms 9848 KB Output is correct
11 Correct 18 ms 9908 KB Output is correct
12 Correct 13 ms 9916 KB Output is correct
13 Correct 13 ms 9836 KB Output is correct
14 Correct 5 ms 9212 KB Output is correct
15 Correct 5 ms 9340 KB Output is correct
16 Correct 23 ms 9852 KB Output is correct
17 Correct 21 ms 10108 KB Output is correct
18 Correct 21 ms 9852 KB Output is correct
19 Correct 6 ms 9340 KB Output is correct
20 Correct 25 ms 9852 KB Output is correct
21 Correct 22 ms 9840 KB Output is correct
22 Correct 22 ms 9852 KB Output is correct
23 Correct 5 ms 9268 KB Output is correct
24 Correct 7 ms 9596 KB Output is correct
25 Correct 21 ms 9980 KB Output is correct
26 Correct 9 ms 9852 KB Output is correct
27 Correct 24 ms 9848 KB Output is correct
28 Correct 6 ms 9268 KB Output is correct
29 Correct 25 ms 9852 KB Output is correct
30 Correct 21 ms 10108 KB Output is correct
31 Correct 25 ms 10108 KB Output is correct
32 Correct 24 ms 9852 KB Output is correct
33 Correct 22 ms 9964 KB Output is correct
34 Correct 18 ms 9724 KB Output is correct
35 Correct 20 ms 9716 KB Output is correct
36 Correct 16 ms 9968 KB Output is correct
37 Correct 14 ms 9724 KB Output is correct
38 Correct 468 ms 23648 KB Output is correct
39 Correct 557 ms 270600 KB Output is correct
40 Correct 506 ms 23904 KB Output is correct
41 Correct 495 ms 23384 KB Output is correct
42 Correct 428 ms 24156 KB Output is correct
43 Correct 557 ms 23752 KB Output is correct
44 Correct 80 ms 11792 KB Output is correct
45 Correct 630 ms 271796 KB Output is correct
46 Correct 618 ms 271732 KB Output is correct
47 Correct 620 ms 268500 KB Output is correct
48 Correct 635 ms 268532 KB Output is correct
49 Correct 549 ms 272488 KB Output is correct
50 Correct 566 ms 272456 KB Output is correct
51 Correct 627 ms 271732 KB Output is correct
52 Correct 616 ms 271728 KB Output is correct
53 Correct 126 ms 27512 KB Output is correct
54 Correct 663 ms 272544 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9212 KB Output is correct
2 Correct 6 ms 9340 KB Output is correct
3 Correct 5 ms 9212 KB Output is correct
4 Correct 5 ms 9260 KB Output is correct
5 Correct 70 ms 9492 KB Output is correct
6 Correct 12 ms 9852 KB Output is correct
7 Correct 12 ms 9852 KB Output is correct
8 Correct 6 ms 9340 KB Output is correct
9 Correct 6 ms 9320 KB Output is correct
10 Correct 6 ms 9596 KB Output is correct
11 Correct 6 ms 9316 KB Output is correct
12 Correct 18 ms 9980 KB Output is correct
13 Correct 215 ms 10084 KB Output is correct
14 Correct 406 ms 10112 KB Output is correct
15 Correct 307 ms 10048 KB Output is correct
16 Correct 625 ms 273684 KB Output is correct
17 Correct 689 ms 279548 KB Output is correct
18 Correct 788 ms 286688 KB Output is correct
19 Correct 689 ms 274944 KB Output is correct
20 Correct 687 ms 274676 KB Output is correct
21 Correct 683 ms 274800 KB Output is correct
22 Correct 632 ms 280060 KB Output is correct
23 Correct 641 ms 280000 KB Output is correct
24 Correct 852 ms 279680 KB Output is correct
25 Correct 872 ms 279824 KB Output is correct
26 Correct 822 ms 279796 KB Output is correct
27 Correct 1145 ms 284764 KB Output is correct
28 Correct 614 ms 284908 KB Output is correct
29 Correct 634 ms 284656 KB Output is correct
30 Correct 949 ms 278520 KB Output is correct
31 Correct 613 ms 278456 KB Output is correct
32 Correct 914 ms 278244 KB Output is correct
33 Correct 641 ms 278132 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9212 KB Output is correct
2 Correct 5 ms 9340 KB Output is correct
3 Correct 5 ms 9208 KB Output is correct
4 Correct 172 ms 10008 KB Output is correct
5 Correct 368 ms 10176 KB Output is correct
6 Correct 20 ms 9832 KB Output is correct
7 Correct 21 ms 9940 KB Output is correct
8 Correct 21 ms 9848 KB Output is correct
9 Correct 464 ms 23768 KB Output is correct
10 Correct 565 ms 270732 KB Output is correct
11 Correct 72 ms 9484 KB Output is correct
12 Correct 414 ms 10672 KB Output is correct
13 Correct 664 ms 275936 KB Output is correct
14 Correct 561 ms 275852 KB Output is correct
15 Correct 775 ms 277204 KB Output is correct
16 Correct 1204 ms 286616 KB Output is correct
17 Correct 645 ms 277060 KB Output is correct
18 Correct 667 ms 281696 KB Output is correct
19 Correct 679 ms 277348 KB Output is correct
20 Correct 591 ms 277304 KB Output is correct
21 Correct 690 ms 278744 KB Output is correct
22 Correct 537 ms 276292 KB Output is correct
23 Correct 5 ms 9248 KB Output is correct
24 Correct 5 ms 9324 KB Output is correct
25 Correct 23 ms 9896 KB Output is correct
26 Correct 21 ms 9856 KB Output is correct
27 Correct 22 ms 9852 KB Output is correct
28 Correct 22 ms 9852 KB Output is correct
29 Correct 21 ms 9844 KB Output is correct
30 Correct 23 ms 9852 KB Output is correct
31 Correct 22 ms 9852 KB Output is correct
32 Correct 21 ms 9848 KB Output is correct
33 Correct 18 ms 9908 KB Output is correct
34 Correct 13 ms 9916 KB Output is correct
35 Correct 13 ms 9836 KB Output is correct
36 Correct 5 ms 9212 KB Output is correct
37 Correct 5 ms 9340 KB Output is correct
38 Correct 23 ms 9852 KB Output is correct
39 Correct 21 ms 10108 KB Output is correct
40 Correct 21 ms 9852 KB Output is correct
41 Correct 6 ms 9340 KB Output is correct
42 Correct 25 ms 9852 KB Output is correct
43 Correct 22 ms 9840 KB Output is correct
44 Correct 22 ms 9852 KB Output is correct
45 Correct 5 ms 9268 KB Output is correct
46 Correct 7 ms 9596 KB Output is correct
47 Correct 21 ms 9980 KB Output is correct
48 Correct 9 ms 9852 KB Output is correct
49 Correct 24 ms 9848 KB Output is correct
50 Correct 6 ms 9268 KB Output is correct
51 Correct 25 ms 9852 KB Output is correct
52 Correct 21 ms 10108 KB Output is correct
53 Correct 25 ms 10108 KB Output is correct
54 Correct 24 ms 9852 KB Output is correct
55 Correct 22 ms 9964 KB Output is correct
56 Correct 18 ms 9724 KB Output is correct
57 Correct 20 ms 9716 KB Output is correct
58 Correct 16 ms 9968 KB Output is correct
59 Correct 14 ms 9724 KB Output is correct
60 Correct 468 ms 23648 KB Output is correct
61 Correct 557 ms 270600 KB Output is correct
62 Correct 506 ms 23904 KB Output is correct
63 Correct 495 ms 23384 KB Output is correct
64 Correct 428 ms 24156 KB Output is correct
65 Correct 557 ms 23752 KB Output is correct
66 Correct 80 ms 11792 KB Output is correct
67 Correct 630 ms 271796 KB Output is correct
68 Correct 618 ms 271732 KB Output is correct
69 Correct 620 ms 268500 KB Output is correct
70 Correct 635 ms 268532 KB Output is correct
71 Correct 549 ms 272488 KB Output is correct
72 Correct 566 ms 272456 KB Output is correct
73 Correct 627 ms 271732 KB Output is correct
74 Correct 616 ms 271728 KB Output is correct
75 Correct 126 ms 27512 KB Output is correct
76 Correct 663 ms 272544 KB Output is correct
77 Correct 5 ms 9212 KB Output is correct
78 Correct 6 ms 9340 KB Output is correct
79 Correct 5 ms 9212 KB Output is correct
80 Correct 5 ms 9260 KB Output is correct
81 Correct 70 ms 9492 KB Output is correct
82 Correct 12 ms 9852 KB Output is correct
83 Correct 12 ms 9852 KB Output is correct
84 Correct 6 ms 9340 KB Output is correct
85 Correct 6 ms 9320 KB Output is correct
86 Correct 6 ms 9596 KB Output is correct
87 Correct 6 ms 9316 KB Output is correct
88 Correct 18 ms 9980 KB Output is correct
89 Correct 215 ms 10084 KB Output is correct
90 Correct 406 ms 10112 KB Output is correct
91 Correct 307 ms 10048 KB Output is correct
92 Correct 625 ms 273684 KB Output is correct
93 Correct 689 ms 279548 KB Output is correct
94 Correct 788 ms 286688 KB Output is correct
95 Correct 689 ms 274944 KB Output is correct
96 Correct 687 ms 274676 KB Output is correct
97 Correct 683 ms 274800 KB Output is correct
98 Correct 632 ms 280060 KB Output is correct
99 Correct 641 ms 280000 KB Output is correct
100 Correct 852 ms 279680 KB Output is correct
101 Correct 872 ms 279824 KB Output is correct
102 Correct 822 ms 279796 KB Output is correct
103 Correct 1145 ms 284764 KB Output is correct
104 Correct 614 ms 284908 KB Output is correct
105 Correct 634 ms 284656 KB Output is correct
106 Correct 949 ms 278520 KB Output is correct
107 Correct 613 ms 278456 KB Output is correct
108 Correct 914 ms 278244 KB Output is correct
109 Correct 641 ms 278132 KB Output is correct
110 Correct 413 ms 10432 KB Output is correct
111 Correct 318 ms 10096 KB Output is correct
112 Correct 900 ms 278080 KB Output is correct
113 Correct 919 ms 271712 KB Output is correct
114 Correct 730 ms 280688 KB Output is correct
115 Correct 471 ms 273428 KB Output is correct
116 Correct 859 ms 278948 KB Output is correct
117 Correct 908 ms 287696 KB Output is correct
118 Correct 727 ms 272608 KB Output is correct
119 Correct 703 ms 272520 KB Output is correct
120 Correct 79 ms 31860 KB Output is correct
121 Correct 1073 ms 278596 KB Output is correct
122 Correct 1189 ms 278548 KB Output is correct
123 Correct 1050 ms 272348 KB Output is correct
124 Correct 716 ms 272436 KB Output is correct
125 Correct 1036 ms 272536 KB Output is correct
126 Correct 1318 ms 286736 KB Output is correct
127 Correct 769 ms 282056 KB Output is correct
128 Correct 662 ms 281708 KB Output is correct
129 Correct 795 ms 282036 KB Output is correct
130 Correct 726 ms 281952 KB Output is correct