답안 #632669

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
632669 2022-08-20T14:28:06 Z Mahdi Jail (JOI22_jail) C++17
100 / 100
4874 ms 1039248 KB
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
#define all(v) v.begin(), v.end()
#define F first
#define S second
typedef pair<int, int> pii;
typedef long long ll;
const int N=12e4+5;
int n, m, nm, sq, s[N], t[N], dis[N], pr[N][18], fk[N];
vector<int>g[N], h[3*N], nd;
bool mk[3*N];
pii st[N];
     
void dfs(const int &v, const int &p=-1){
    for(int u: g[v]){
        if(u!=p){
            pr[u][0]=v;
            for(int i=1;i<17;++i)
                pr[u][i]=pr[pr[u][i-1]][i-1];
            dis[u]=dis[v]+1;
            dfs(u, v);
        }   
    }
}
     
inline int par(int v, int p){
    for(int i=0;p>0;++i){
        if(p&1)
            v=pr[v][i];
        p>>=1;
    }
    return v;
}
     
inline int lca(int u, int v){
    u=par(u, dis[u]-dis[v]);
    v=par(v, dis[v]-dis[u]);
    if(u==v)
        return v;
    for(int i=16;i>=0;--i){
        if(pr[u][i]!=pr[v][i]){
            v=pr[v][i];
            u=pr[u][i];
        }
    }
    return pr[v][0];
}
     
void pre(){
    nm=2*n+m;
    for(int i=0;i<nm;++i)
        h[i].clear();
    for(int v=0;v<n;++v){
        int u=v, w=par(v, sq);
        fk[v]=w;
        while(u!=w){
            if(s[u]!=-1)
                h[s[u]+2*n].push_back(v);
            if(t[u]!=-1)
                h[v+n].push_back(t[u]+2*n);
            u=pr[u][0];
        }
    }
}
     
inline void opr(int i, int v, int u){
    int w=lca(u, v);
    int x=pr[v][0];
    int y=fk[x];
    while(dis[y]>=dis[w]){
        h[x].push_back(i);
        x=y;
        y=fk[x];
        if(x==w)
            break;
    }
    for(;dis[x]>=dis[w];x=pr[x][0]){
        if(s[x]!=-1)
            h[s[x]+2*n].push_back(i);
        if(x==w)
            break;
    }
    x=v;
    y=fk[x];
    while(dis[y]>=dis[w]){
        h[i].push_back(x+n);
        x=y;
        y=fk[x];
        if(x==w)
            break;
    }
    for(;dis[x]>=dis[w];x=pr[x][0]){
        if(t[x]!=-1)
            h[i].push_back(t[x]+2*n);
        if(x==w)
            break;
    }
    x=pr[u][0];
    y=fk[x];
    while(dis[y]>=dis[w]){
        h[i].push_back(x+n);
        x=y;
        y=fk[x];
        if(x==w)
            break;
    }
    for(;dis[x]>dis[w];x=pr[x][0]){
        if(t[x]!=-1)
            h[i].push_back(t[x]+2*n);
    }
    x=u;
    y=fk[x];
    while(dis[y]>=dis[w]){
        h[x].push_back(i);
        x=y;
        y=fk[x];
        if(x==w)
        break;
    }
    for(;dis[x]>dis[w];x=pr[x][0]){
        if(s[x]!=-1)
            h[s[x]+2*n].push_back(i);
    }
}
     
void sfd(const int &v){
    mk[v]=1;
    for(int u: h[v]){
        if(!mk[u])
            sfd(u);
    }
    nd.push_back(v);
}
     
void sol(){
    cin>>n;
    sq=sqrt(n);
    for(int i=0;i<n;++i)
        g[i].clear();
    for(int i=1;i<n;++i){
        int u, v;
        cin>>u>>v;
        g[--v].push_back(--u);
        g[u].push_back(v);
    }
    cin>>m;
    memset(s, -1, 4*n);
    memset(t, -1, 4*n);
    for(int i=0;i<m;++i){
        int u, v;
        cin>>u>>v;
        s[--u]=i;
        t[--v]=i;
        st[i]={u, v};
    }
    dfs(0);
    pre();    
    for(int i=0;i<m;++i)
        opr(2*n+i, st[i].F, st[i].S);
    nd.clear();
    memset(mk, 0, nm);
    for(int i=0;i<nm;++i){
        if(!mk[i])
            sfd(i);
    }
    memset(mk, 0, nm);
    for(int i=0;i<nm;++i){
        mk[nd[i]]=1;
        for(int u: h[nd[i]]){
            if(!mk[u]){
                cout<<"No\n";
                return;
            }
        }
    }
    cout<<"Yes\n";
}
     
int main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    int T;
    cin>>T;
    while(T--)
        sol();
}
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11604 KB Output is correct
2 Correct 8 ms 11604 KB Output is correct
3 Correct 8 ms 11604 KB Output is correct
4 Correct 22 ms 11668 KB Output is correct
5 Correct 29 ms 11612 KB Output is correct
6 Correct 7 ms 11604 KB Output is correct
7 Correct 7 ms 11604 KB Output is correct
8 Correct 10 ms 11732 KB Output is correct
9 Correct 91 ms 14416 KB Output is correct
10 Correct 200 ms 44752 KB Output is correct
11 Correct 12 ms 11604 KB Output is correct
12 Correct 49 ms 11604 KB Output is correct
13 Correct 735 ms 308440 KB Output is correct
14 Correct 678 ms 322112 KB Output is correct
15 Correct 1749 ms 412508 KB Output is correct
16 Correct 4710 ms 1039248 KB Output is correct
17 Correct 1299 ms 466196 KB Output is correct
18 Correct 1148 ms 524356 KB Output is correct
19 Correct 1093 ms 422608 KB Output is correct
20 Correct 1057 ms 422352 KB Output is correct
21 Correct 1354 ms 482028 KB Output is correct
22 Correct 596 ms 282812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11604 KB Output is correct
2 Correct 8 ms 11584 KB Output is correct
3 Correct 7 ms 11604 KB Output is correct
4 Correct 8 ms 11680 KB Output is correct
5 Correct 7 ms 11564 KB Output is correct
6 Correct 8 ms 11608 KB Output is correct
7 Correct 7 ms 11672 KB Output is correct
8 Correct 7 ms 11676 KB Output is correct
9 Correct 7 ms 11604 KB Output is correct
10 Correct 7 ms 11560 KB Output is correct
11 Correct 7 ms 11604 KB Output is correct
12 Correct 6 ms 11604 KB Output is correct
13 Correct 8 ms 11604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11604 KB Output is correct
2 Correct 8 ms 11584 KB Output is correct
3 Correct 7 ms 11604 KB Output is correct
4 Correct 8 ms 11680 KB Output is correct
5 Correct 7 ms 11564 KB Output is correct
6 Correct 8 ms 11608 KB Output is correct
7 Correct 7 ms 11672 KB Output is correct
8 Correct 7 ms 11676 KB Output is correct
9 Correct 7 ms 11604 KB Output is correct
10 Correct 7 ms 11560 KB Output is correct
11 Correct 7 ms 11604 KB Output is correct
12 Correct 6 ms 11604 KB Output is correct
13 Correct 8 ms 11604 KB Output is correct
14 Correct 6 ms 11604 KB Output is correct
15 Correct 6 ms 11604 KB Output is correct
16 Correct 8 ms 11604 KB Output is correct
17 Correct 7 ms 11604 KB Output is correct
18 Correct 7 ms 11604 KB Output is correct
19 Correct 6 ms 11600 KB Output is correct
20 Correct 7 ms 11604 KB Output is correct
21 Correct 7 ms 11604 KB Output is correct
22 Correct 7 ms 11572 KB Output is correct
23 Correct 6 ms 11604 KB Output is correct
24 Correct 7 ms 11604 KB Output is correct
25 Correct 7 ms 11604 KB Output is correct
26 Correct 6 ms 11604 KB Output is correct
27 Correct 7 ms 11676 KB Output is correct
28 Correct 6 ms 11604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11604 KB Output is correct
2 Correct 8 ms 11584 KB Output is correct
3 Correct 7 ms 11604 KB Output is correct
4 Correct 8 ms 11680 KB Output is correct
5 Correct 7 ms 11564 KB Output is correct
6 Correct 8 ms 11608 KB Output is correct
7 Correct 7 ms 11672 KB Output is correct
8 Correct 7 ms 11676 KB Output is correct
9 Correct 7 ms 11604 KB Output is correct
10 Correct 7 ms 11560 KB Output is correct
11 Correct 7 ms 11604 KB Output is correct
12 Correct 6 ms 11604 KB Output is correct
13 Correct 8 ms 11604 KB Output is correct
14 Correct 6 ms 11604 KB Output is correct
15 Correct 6 ms 11604 KB Output is correct
16 Correct 8 ms 11604 KB Output is correct
17 Correct 7 ms 11604 KB Output is correct
18 Correct 7 ms 11604 KB Output is correct
19 Correct 6 ms 11600 KB Output is correct
20 Correct 7 ms 11604 KB Output is correct
21 Correct 7 ms 11604 KB Output is correct
22 Correct 7 ms 11572 KB Output is correct
23 Correct 6 ms 11604 KB Output is correct
24 Correct 7 ms 11604 KB Output is correct
25 Correct 7 ms 11604 KB Output is correct
26 Correct 6 ms 11604 KB Output is correct
27 Correct 7 ms 11676 KB Output is correct
28 Correct 6 ms 11604 KB Output is correct
29 Correct 9 ms 11732 KB Output is correct
30 Correct 10 ms 11604 KB Output is correct
31 Correct 10 ms 11720 KB Output is correct
32 Correct 8 ms 11604 KB Output is correct
33 Correct 10 ms 11604 KB Output is correct
34 Correct 8 ms 11604 KB Output is correct
35 Correct 8 ms 11604 KB Output is correct
36 Correct 7 ms 11720 KB Output is correct
37 Correct 7 ms 11604 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11604 KB Output is correct
2 Correct 8 ms 11584 KB Output is correct
3 Correct 7 ms 11604 KB Output is correct
4 Correct 8 ms 11680 KB Output is correct
5 Correct 7 ms 11564 KB Output is correct
6 Correct 8 ms 11608 KB Output is correct
7 Correct 7 ms 11672 KB Output is correct
8 Correct 7 ms 11676 KB Output is correct
9 Correct 7 ms 11604 KB Output is correct
10 Correct 7 ms 11560 KB Output is correct
11 Correct 7 ms 11604 KB Output is correct
12 Correct 6 ms 11604 KB Output is correct
13 Correct 8 ms 11604 KB Output is correct
14 Correct 6 ms 11604 KB Output is correct
15 Correct 6 ms 11604 KB Output is correct
16 Correct 8 ms 11604 KB Output is correct
17 Correct 7 ms 11604 KB Output is correct
18 Correct 7 ms 11604 KB Output is correct
19 Correct 6 ms 11600 KB Output is correct
20 Correct 7 ms 11604 KB Output is correct
21 Correct 7 ms 11604 KB Output is correct
22 Correct 7 ms 11572 KB Output is correct
23 Correct 6 ms 11604 KB Output is correct
24 Correct 7 ms 11604 KB Output is correct
25 Correct 7 ms 11604 KB Output is correct
26 Correct 6 ms 11604 KB Output is correct
27 Correct 7 ms 11676 KB Output is correct
28 Correct 6 ms 11604 KB Output is correct
29 Correct 9 ms 11732 KB Output is correct
30 Correct 10 ms 11604 KB Output is correct
31 Correct 10 ms 11720 KB Output is correct
32 Correct 8 ms 11604 KB Output is correct
33 Correct 10 ms 11604 KB Output is correct
34 Correct 8 ms 11604 KB Output is correct
35 Correct 8 ms 11604 KB Output is correct
36 Correct 7 ms 11720 KB Output is correct
37 Correct 7 ms 11604 KB Output is correct
38 Correct 84 ms 14756 KB Output is correct
39 Correct 196 ms 45284 KB Output is correct
40 Correct 70 ms 14108 KB Output is correct
41 Correct 46 ms 13152 KB Output is correct
42 Correct 57 ms 13900 KB Output is correct
43 Correct 62 ms 13048 KB Output is correct
44 Correct 14 ms 11860 KB Output is correct
45 Correct 205 ms 28704 KB Output is correct
46 Correct 200 ms 28596 KB Output is correct
47 Correct 262 ms 33996 KB Output is correct
48 Correct 265 ms 34072 KB Output is correct
49 Correct 373 ms 28924 KB Output is correct
50 Correct 386 ms 28060 KB Output is correct
51 Correct 304 ms 31684 KB Output is correct
52 Correct 311 ms 32020 KB Output is correct
53 Correct 18 ms 13168 KB Output is correct
54 Correct 127 ms 27720 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 8 ms 11604 KB Output is correct
2 Correct 6 ms 11604 KB Output is correct
3 Correct 6 ms 11532 KB Output is correct
4 Correct 6 ms 11604 KB Output is correct
5 Correct 12 ms 11604 KB Output is correct
6 Correct 9 ms 11656 KB Output is correct
7 Correct 6 ms 11604 KB Output is correct
8 Correct 6 ms 11636 KB Output is correct
9 Correct 6 ms 11560 KB Output is correct
10 Correct 6 ms 11604 KB Output is correct
11 Correct 6 ms 11588 KB Output is correct
12 Correct 8 ms 11604 KB Output is correct
13 Correct 28 ms 11732 KB Output is correct
14 Correct 37 ms 11604 KB Output is correct
15 Correct 32 ms 11672 KB Output is correct
16 Correct 95 ms 30884 KB Output is correct
17 Correct 229 ms 42716 KB Output is correct
18 Correct 433 ms 61592 KB Output is correct
19 Correct 95 ms 29504 KB Output is correct
20 Correct 115 ms 30600 KB Output is correct
21 Correct 108 ms 30376 KB Output is correct
22 Correct 166 ms 41320 KB Output is correct
23 Correct 163 ms 40896 KB Output is correct
24 Correct 184 ms 40896 KB Output is correct
25 Correct 149 ms 41088 KB Output is correct
26 Correct 163 ms 41048 KB Output is correct
27 Correct 181 ms 39784 KB Output is correct
28 Correct 190 ms 42312 KB Output is correct
29 Correct 175 ms 40624 KB Output is correct
30 Correct 131 ms 36708 KB Output is correct
31 Correct 130 ms 37576 KB Output is correct
32 Correct 162 ms 34868 KB Output is correct
33 Correct 170 ms 35528 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 11604 KB Output is correct
2 Correct 8 ms 11604 KB Output is correct
3 Correct 8 ms 11604 KB Output is correct
4 Correct 22 ms 11668 KB Output is correct
5 Correct 29 ms 11612 KB Output is correct
6 Correct 7 ms 11604 KB Output is correct
7 Correct 7 ms 11604 KB Output is correct
8 Correct 10 ms 11732 KB Output is correct
9 Correct 91 ms 14416 KB Output is correct
10 Correct 200 ms 44752 KB Output is correct
11 Correct 12 ms 11604 KB Output is correct
12 Correct 49 ms 11604 KB Output is correct
13 Correct 735 ms 308440 KB Output is correct
14 Correct 678 ms 322112 KB Output is correct
15 Correct 1749 ms 412508 KB Output is correct
16 Correct 4710 ms 1039248 KB Output is correct
17 Correct 1299 ms 466196 KB Output is correct
18 Correct 1148 ms 524356 KB Output is correct
19 Correct 1093 ms 422608 KB Output is correct
20 Correct 1057 ms 422352 KB Output is correct
21 Correct 1354 ms 482028 KB Output is correct
22 Correct 596 ms 282812 KB Output is correct
23 Correct 6 ms 11604 KB Output is correct
24 Correct 8 ms 11584 KB Output is correct
25 Correct 7 ms 11604 KB Output is correct
26 Correct 8 ms 11680 KB Output is correct
27 Correct 7 ms 11564 KB Output is correct
28 Correct 8 ms 11608 KB Output is correct
29 Correct 7 ms 11672 KB Output is correct
30 Correct 7 ms 11676 KB Output is correct
31 Correct 7 ms 11604 KB Output is correct
32 Correct 7 ms 11560 KB Output is correct
33 Correct 7 ms 11604 KB Output is correct
34 Correct 6 ms 11604 KB Output is correct
35 Correct 8 ms 11604 KB Output is correct
36 Correct 6 ms 11604 KB Output is correct
37 Correct 6 ms 11604 KB Output is correct
38 Correct 8 ms 11604 KB Output is correct
39 Correct 7 ms 11604 KB Output is correct
40 Correct 7 ms 11604 KB Output is correct
41 Correct 6 ms 11600 KB Output is correct
42 Correct 7 ms 11604 KB Output is correct
43 Correct 7 ms 11604 KB Output is correct
44 Correct 7 ms 11572 KB Output is correct
45 Correct 6 ms 11604 KB Output is correct
46 Correct 7 ms 11604 KB Output is correct
47 Correct 7 ms 11604 KB Output is correct
48 Correct 6 ms 11604 KB Output is correct
49 Correct 7 ms 11676 KB Output is correct
50 Correct 6 ms 11604 KB Output is correct
51 Correct 9 ms 11732 KB Output is correct
52 Correct 10 ms 11604 KB Output is correct
53 Correct 10 ms 11720 KB Output is correct
54 Correct 8 ms 11604 KB Output is correct
55 Correct 10 ms 11604 KB Output is correct
56 Correct 8 ms 11604 KB Output is correct
57 Correct 8 ms 11604 KB Output is correct
58 Correct 7 ms 11720 KB Output is correct
59 Correct 7 ms 11604 KB Output is correct
60 Correct 84 ms 14756 KB Output is correct
61 Correct 196 ms 45284 KB Output is correct
62 Correct 70 ms 14108 KB Output is correct
63 Correct 46 ms 13152 KB Output is correct
64 Correct 57 ms 13900 KB Output is correct
65 Correct 62 ms 13048 KB Output is correct
66 Correct 14 ms 11860 KB Output is correct
67 Correct 205 ms 28704 KB Output is correct
68 Correct 200 ms 28596 KB Output is correct
69 Correct 262 ms 33996 KB Output is correct
70 Correct 265 ms 34072 KB Output is correct
71 Correct 373 ms 28924 KB Output is correct
72 Correct 386 ms 28060 KB Output is correct
73 Correct 304 ms 31684 KB Output is correct
74 Correct 311 ms 32020 KB Output is correct
75 Correct 18 ms 13168 KB Output is correct
76 Correct 127 ms 27720 KB Output is correct
77 Correct 8 ms 11604 KB Output is correct
78 Correct 6 ms 11604 KB Output is correct
79 Correct 6 ms 11532 KB Output is correct
80 Correct 6 ms 11604 KB Output is correct
81 Correct 12 ms 11604 KB Output is correct
82 Correct 9 ms 11656 KB Output is correct
83 Correct 6 ms 11604 KB Output is correct
84 Correct 6 ms 11636 KB Output is correct
85 Correct 6 ms 11560 KB Output is correct
86 Correct 6 ms 11604 KB Output is correct
87 Correct 6 ms 11588 KB Output is correct
88 Correct 8 ms 11604 KB Output is correct
89 Correct 28 ms 11732 KB Output is correct
90 Correct 37 ms 11604 KB Output is correct
91 Correct 32 ms 11672 KB Output is correct
92 Correct 95 ms 30884 KB Output is correct
93 Correct 229 ms 42716 KB Output is correct
94 Correct 433 ms 61592 KB Output is correct
95 Correct 95 ms 29504 KB Output is correct
96 Correct 115 ms 30600 KB Output is correct
97 Correct 108 ms 30376 KB Output is correct
98 Correct 166 ms 41320 KB Output is correct
99 Correct 163 ms 40896 KB Output is correct
100 Correct 184 ms 40896 KB Output is correct
101 Correct 149 ms 41088 KB Output is correct
102 Correct 163 ms 41048 KB Output is correct
103 Correct 181 ms 39784 KB Output is correct
104 Correct 190 ms 42312 KB Output is correct
105 Correct 175 ms 40624 KB Output is correct
106 Correct 131 ms 36708 KB Output is correct
107 Correct 130 ms 37576 KB Output is correct
108 Correct 162 ms 34868 KB Output is correct
109 Correct 170 ms 35528 KB Output is correct
110 Correct 42 ms 12620 KB Output is correct
111 Correct 31 ms 12308 KB Output is correct
112 Correct 2025 ms 404908 KB Output is correct
113 Correct 529 ms 50536 KB Output is correct
114 Correct 891 ms 217772 KB Output is correct
115 Correct 43 ms 29036 KB Output is correct
116 Correct 228 ms 48648 KB Output is correct
117 Correct 878 ms 107224 KB Output is correct
118 Correct 158 ms 28872 KB Output is correct
119 Correct 136 ms 28860 KB Output is correct
120 Correct 21 ms 15504 KB Output is correct
121 Correct 414 ms 71232 KB Output is correct
122 Correct 315 ms 63676 KB Output is correct
123 Correct 525 ms 62588 KB Output is correct
124 Correct 515 ms 64976 KB Output is correct
125 Correct 553 ms 66384 KB Output is correct
126 Correct 4874 ms 941172 KB Output is correct
127 Correct 1070 ms 378392 KB Output is correct
128 Correct 944 ms 373252 KB Output is correct
129 Correct 1008 ms 370872 KB Output is correct
130 Correct 931 ms 370872 KB Output is correct