# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
885275 |
2023-12-09T11:55:51 Z |
Ahmed57 |
Jail (JOI22_jail) |
C++17 |
|
334 ms |
313840 KB |
#include <bits/stdc++.h>
using namespace std;
int P[120001][17];
int lola[120001][17];
int lolb[120001][17];
int lolc[120001],dep[120001];
vector<int> adj[120001],tree[120005+120000*17];
void dfs(int i,int pr){
P[i][0] = pr;
dep[i] = dep[pr]+1;
for(int j = 1;j<17;j++){
P[i][j] = P[P[i][j-1]][j-1];
}
for(auto j:adj[i]){
if(j==pr)continue;
dfs(j,i);
}
}
int go(int x,int len){
for(int i = 16;i>=0;i--){
if(len>=(1<<i)){
x = P[x][i];
len-=(1<<i);
}
}
return x;
}
signed main(){
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//freopen("input.txt","r",stdin);
//freopen("out.txt","w",stdout);
int t;cin>>t;while(t--){
int n;
cin>>n;
for(int i = 1;i<=n;i++){
adj[i].clear();
}
for(int i = 1;i<n;i++){
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
int q;cin>>q;
vector<pair<int,int>> v;v.push_back({0,0});
for(int i = 1;i<=q;i++){
int a,b;cin>>a>>b;
v.push_back({a,b});
}
dfs(1,0);
int NODES = 0;
for(int i = 1;i<=q;i++){
lolc[i] = ++NODES;
//cout<<lolc[i]<<endl;
}
for(int i = 1;i<=n;i++){
for(int j = 0;j<=16;j++){
lola[i][j] = ++NODES;
lolb[i][j] = ++NODES;
}
}
for(int i = 1;i<=n;i++){
for(int j = 1;j<=16;j++){
tree[lolb[i][j]].push_back(lolb[i][j-1]);
tree[lolb[i][j]].push_back(lolb[P[i][j-1]][j-1]);
tree[lola[i][j-1]].push_back(lola[i][j]);
tree[lola[P[i][j-1]][j-1]].push_back(lola[i][j]);
}
}
//cout<<tree[0].size()<<endl;
for(int i = 1;i<=q;i++){
tree[lolc[i]].push_back(lola[v[i].first][0]);
tree[lolb[v[i].second][0]].push_back(lolc[i]);
}
for(int i = 1;i<=q;i++){
int x,y;
x = v[i].first , y = v[i].second;
if(dep[x]>dep[y]&&go(x,dep[x]-dep[y])==y){
y=go(x,dep[x]-dep[y]-1);
}else y = P[y][0];
//cout<<x<<" "<<y<<endl;
if(dep[x]<dep[y])swap(x,y);
for(int j =16;j>=0;j--){
if(dep[x]-(1<<j)>=dep[y]){
assert(lolc[i]!=0);
tree[lolc[i]].push_back(lolb[x][j]);
x = P[x][j];
}
}
for(int j = 16;j>=0;j--){
if(P[x][j]!=P[y][j]){
assert(lolc[i]!=0);
tree[lolc[i]].push_back(lolb[x][j]);
tree[lolc[i]].push_back(lolb[y][j]);
x = P[x][j];y = P[y][j];
}
}
assert(lolc[i]!=0);
tree[lolc[i]].push_back(lolb[x][0]);
if(x!=y)tree[lolc[i]].push_back(lolb[y][1]);
x = v[i].first , y = v[i].second;
if(dep[y]>dep[x]&&go(y,dep[y]-dep[x])==x){
x=go(y,dep[y]-dep[x]-1);
}else x = P[x][0];
if(dep[x]<dep[y])swap(x,y);
for(int j = 16;j>=0;j--){
if(dep[x]-(1<<j)>=dep[y]){
assert(lola[x][j]!=0);
tree[lola[x][j]].push_back(lolc[i]);
x = P[x][j];
}
}
for(int j =16;j>=0;j--){
if(P[x][j]!=P[y][j]){
assert(lola[x][j]!=0);
assert(lola[y][j]!=0);
tree[lola[x][j]].push_back(lolc[i]);
tree[lola[y][j]].push_back(lolc[i]);
x = P[x][j];y = P[y][j];
}
}
assert(lola[x][0]!=0);
tree[lola[x][0]].push_back(lolc[i]);
if(x!=y){assert(lola[y][1]!=0);tree[lola[y][1]].push_back(lolc[i]);}
}
int deg[NODES+1] = {0};
int vis[NODES+1] = {0};
//cout<<"ggg"<<endl;
tree[0].clear();
for(int i = 0;i<=NODES;i++){
for(auto j:tree[i]){
//if(j>NODES||j==0)assert(0);
deg[j]++;
}
}
queue<int> qe;
for(int i = 0;i<=NODES;i++){
if(deg[i]==0){
qe.push(i);
}
}
int cnt = 0;
while(!qe.empty()){
int x = qe.front();qe.pop();
if(vis[x]){
//cout<<x<<endl;
}
vis[x] = 1;
cnt++;
for(auto j:tree[x]){
deg[j]--;
if(deg[j]==0)qe.push(j);
}
}
for(int i = 0;i<=NODES;i++){
//cout<<vis[i]<<" "<<i<<endl;
}
if(cnt==NODES+1)cout<<"Yes\n";
else cout<<"No\n";
for(int i = 0;i<=NODES;i++)tree[i].clear();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
59996 KB |
Output is correct |
2 |
Correct |
12 ms |
60084 KB |
Output is correct |
3 |
Correct |
12 ms |
59996 KB |
Output is correct |
4 |
Correct |
48 ms |
60552 KB |
Output is correct |
5 |
Correct |
88 ms |
61012 KB |
Output is correct |
6 |
Correct |
16 ms |
60504 KB |
Output is correct |
7 |
Correct |
17 ms |
60508 KB |
Output is correct |
8 |
Correct |
18 ms |
60352 KB |
Output is correct |
9 |
Correct |
199 ms |
69460 KB |
Output is correct |
10 |
Runtime error |
334 ms |
313804 KB |
Execution killed with signal 11 |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
60144 KB |
Output is correct |
2 |
Correct |
12 ms |
59996 KB |
Output is correct |
3 |
Correct |
16 ms |
60508 KB |
Output is correct |
4 |
Correct |
17 ms |
60508 KB |
Output is correct |
5 |
Correct |
17 ms |
60544 KB |
Output is correct |
6 |
Correct |
17 ms |
60508 KB |
Output is correct |
7 |
Correct |
17 ms |
60508 KB |
Output is correct |
8 |
Correct |
18 ms |
60508 KB |
Output is correct |
9 |
Correct |
17 ms |
60504 KB |
Output is correct |
10 |
Correct |
18 ms |
60404 KB |
Output is correct |
11 |
Correct |
17 ms |
60508 KB |
Output is correct |
12 |
Correct |
15 ms |
60500 KB |
Output is correct |
13 |
Correct |
15 ms |
60508 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
60144 KB |
Output is correct |
2 |
Correct |
12 ms |
59996 KB |
Output is correct |
3 |
Correct |
16 ms |
60508 KB |
Output is correct |
4 |
Correct |
17 ms |
60508 KB |
Output is correct |
5 |
Correct |
17 ms |
60544 KB |
Output is correct |
6 |
Correct |
17 ms |
60508 KB |
Output is correct |
7 |
Correct |
17 ms |
60508 KB |
Output is correct |
8 |
Correct |
18 ms |
60508 KB |
Output is correct |
9 |
Correct |
17 ms |
60504 KB |
Output is correct |
10 |
Correct |
18 ms |
60404 KB |
Output is correct |
11 |
Correct |
17 ms |
60508 KB |
Output is correct |
12 |
Correct |
15 ms |
60500 KB |
Output is correct |
13 |
Correct |
15 ms |
60508 KB |
Output is correct |
14 |
Correct |
13 ms |
59996 KB |
Output is correct |
15 |
Correct |
12 ms |
60164 KB |
Output is correct |
16 |
Correct |
17 ms |
60508 KB |
Output is correct |
17 |
Correct |
17 ms |
60556 KB |
Output is correct |
18 |
Correct |
17 ms |
60508 KB |
Output is correct |
19 |
Correct |
13 ms |
59992 KB |
Output is correct |
20 |
Correct |
18 ms |
60508 KB |
Output is correct |
21 |
Correct |
18 ms |
60508 KB |
Output is correct |
22 |
Correct |
16 ms |
60504 KB |
Output is correct |
23 |
Correct |
12 ms |
59992 KB |
Output is correct |
24 |
Correct |
13 ms |
60140 KB |
Output is correct |
25 |
Correct |
18 ms |
60336 KB |
Output is correct |
26 |
Correct |
14 ms |
60248 KB |
Output is correct |
27 |
Correct |
17 ms |
60508 KB |
Output is correct |
28 |
Correct |
12 ms |
59996 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
60144 KB |
Output is correct |
2 |
Correct |
12 ms |
59996 KB |
Output is correct |
3 |
Correct |
16 ms |
60508 KB |
Output is correct |
4 |
Correct |
17 ms |
60508 KB |
Output is correct |
5 |
Correct |
17 ms |
60544 KB |
Output is correct |
6 |
Correct |
17 ms |
60508 KB |
Output is correct |
7 |
Correct |
17 ms |
60508 KB |
Output is correct |
8 |
Correct |
18 ms |
60508 KB |
Output is correct |
9 |
Correct |
17 ms |
60504 KB |
Output is correct |
10 |
Correct |
18 ms |
60404 KB |
Output is correct |
11 |
Correct |
17 ms |
60508 KB |
Output is correct |
12 |
Correct |
15 ms |
60500 KB |
Output is correct |
13 |
Correct |
15 ms |
60508 KB |
Output is correct |
14 |
Correct |
13 ms |
59996 KB |
Output is correct |
15 |
Correct |
12 ms |
60164 KB |
Output is correct |
16 |
Correct |
17 ms |
60508 KB |
Output is correct |
17 |
Correct |
17 ms |
60556 KB |
Output is correct |
18 |
Correct |
17 ms |
60508 KB |
Output is correct |
19 |
Correct |
13 ms |
59992 KB |
Output is correct |
20 |
Correct |
18 ms |
60508 KB |
Output is correct |
21 |
Correct |
18 ms |
60508 KB |
Output is correct |
22 |
Correct |
16 ms |
60504 KB |
Output is correct |
23 |
Correct |
12 ms |
59992 KB |
Output is correct |
24 |
Correct |
13 ms |
60140 KB |
Output is correct |
25 |
Correct |
18 ms |
60336 KB |
Output is correct |
26 |
Correct |
14 ms |
60248 KB |
Output is correct |
27 |
Correct |
17 ms |
60508 KB |
Output is correct |
28 |
Correct |
12 ms |
59996 KB |
Output is correct |
29 |
Correct |
17 ms |
60412 KB |
Output is correct |
30 |
Correct |
18 ms |
60508 KB |
Output is correct |
31 |
Correct |
18 ms |
60504 KB |
Output is correct |
32 |
Correct |
18 ms |
60508 KB |
Output is correct |
33 |
Correct |
17 ms |
60508 KB |
Output is correct |
34 |
Correct |
17 ms |
60252 KB |
Output is correct |
35 |
Correct |
16 ms |
60504 KB |
Output is correct |
36 |
Correct |
16 ms |
60508 KB |
Output is correct |
37 |
Correct |
15 ms |
60252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
60144 KB |
Output is correct |
2 |
Correct |
12 ms |
59996 KB |
Output is correct |
3 |
Correct |
16 ms |
60508 KB |
Output is correct |
4 |
Correct |
17 ms |
60508 KB |
Output is correct |
5 |
Correct |
17 ms |
60544 KB |
Output is correct |
6 |
Correct |
17 ms |
60508 KB |
Output is correct |
7 |
Correct |
17 ms |
60508 KB |
Output is correct |
8 |
Correct |
18 ms |
60508 KB |
Output is correct |
9 |
Correct |
17 ms |
60504 KB |
Output is correct |
10 |
Correct |
18 ms |
60404 KB |
Output is correct |
11 |
Correct |
17 ms |
60508 KB |
Output is correct |
12 |
Correct |
15 ms |
60500 KB |
Output is correct |
13 |
Correct |
15 ms |
60508 KB |
Output is correct |
14 |
Correct |
13 ms |
59996 KB |
Output is correct |
15 |
Correct |
12 ms |
60164 KB |
Output is correct |
16 |
Correct |
17 ms |
60508 KB |
Output is correct |
17 |
Correct |
17 ms |
60556 KB |
Output is correct |
18 |
Correct |
17 ms |
60508 KB |
Output is correct |
19 |
Correct |
13 ms |
59992 KB |
Output is correct |
20 |
Correct |
18 ms |
60508 KB |
Output is correct |
21 |
Correct |
18 ms |
60508 KB |
Output is correct |
22 |
Correct |
16 ms |
60504 KB |
Output is correct |
23 |
Correct |
12 ms |
59992 KB |
Output is correct |
24 |
Correct |
13 ms |
60140 KB |
Output is correct |
25 |
Correct |
18 ms |
60336 KB |
Output is correct |
26 |
Correct |
14 ms |
60248 KB |
Output is correct |
27 |
Correct |
17 ms |
60508 KB |
Output is correct |
28 |
Correct |
12 ms |
59996 KB |
Output is correct |
29 |
Correct |
17 ms |
60412 KB |
Output is correct |
30 |
Correct |
18 ms |
60508 KB |
Output is correct |
31 |
Correct |
18 ms |
60504 KB |
Output is correct |
32 |
Correct |
18 ms |
60508 KB |
Output is correct |
33 |
Correct |
17 ms |
60508 KB |
Output is correct |
34 |
Correct |
17 ms |
60252 KB |
Output is correct |
35 |
Correct |
16 ms |
60504 KB |
Output is correct |
36 |
Correct |
16 ms |
60508 KB |
Output is correct |
37 |
Correct |
15 ms |
60252 KB |
Output is correct |
38 |
Correct |
204 ms |
69712 KB |
Output is correct |
39 |
Runtime error |
331 ms |
313840 KB |
Execution killed with signal 11 |
40 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
59996 KB |
Output is correct |
2 |
Correct |
13 ms |
60248 KB |
Output is correct |
3 |
Correct |
12 ms |
60136 KB |
Output is correct |
4 |
Correct |
12 ms |
60252 KB |
Output is correct |
5 |
Correct |
27 ms |
60252 KB |
Output is correct |
6 |
Correct |
14 ms |
60500 KB |
Output is correct |
7 |
Correct |
14 ms |
60296 KB |
Output is correct |
8 |
Correct |
13 ms |
59996 KB |
Output is correct |
9 |
Correct |
12 ms |
59996 KB |
Output is correct |
10 |
Correct |
13 ms |
60252 KB |
Output is correct |
11 |
Correct |
14 ms |
59996 KB |
Output is correct |
12 |
Correct |
16 ms |
60488 KB |
Output is correct |
13 |
Correct |
63 ms |
60680 KB |
Output is correct |
14 |
Correct |
111 ms |
61228 KB |
Output is correct |
15 |
Correct |
86 ms |
60812 KB |
Output is correct |
16 |
Runtime error |
304 ms |
305644 KB |
Execution killed with signal 6 |
17 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
59996 KB |
Output is correct |
2 |
Correct |
12 ms |
60084 KB |
Output is correct |
3 |
Correct |
12 ms |
59996 KB |
Output is correct |
4 |
Correct |
48 ms |
60552 KB |
Output is correct |
5 |
Correct |
88 ms |
61012 KB |
Output is correct |
6 |
Correct |
16 ms |
60504 KB |
Output is correct |
7 |
Correct |
17 ms |
60508 KB |
Output is correct |
8 |
Correct |
18 ms |
60352 KB |
Output is correct |
9 |
Correct |
199 ms |
69460 KB |
Output is correct |
10 |
Runtime error |
334 ms |
313804 KB |
Execution killed with signal 11 |
11 |
Halted |
0 ms |
0 KB |
- |