Submission #701005

# Submission time Handle Problem Language Result Execution time Memory
701005 2023-02-19T17:21:12 Z dongliu0426 Jail (JOI22_jail) C++17
100 / 100
1750 ms 307536 KB
#include<iostream>
#include<vector>
#include<queue>
using namespace std;

const int N=1.2e5,H=17,N_=N+2*N*H;

vector<int>g[N],q[N_];
int p[H][N],d[N],w[N_];

inline int z(int i,int h,int t){
 return (i*H+h)*2+t;
}

void dfs(int i){
 for(int h=0;h<H-1;h++){
  p[h+1][i]=p[h][p[h][i]];
  q[z(i,h,0)].push_back(z(i,h+1,0));
  q[z(p[h][i],h,0)].push_back(z(i,h+1,0));
  q[z(i,h+1,1)].push_back(z(i,h,1));
  q[z(i,h+1,1)].push_back(z(p[h][i],h,1));
 }
 for(int j:g[i])
  if(p[0][i]!=j){
   p[0][j]=i;
   d[j]=d[i]+1;
   dfs(j);
  }
}

int lca(int i,int j){
 if(d[i]<d[j])
  swap(i,j);
 int k=d[i]-d[j];
 for(int h=0;h<H;h++)
  if(k>>h&1)
   i=p[h][i];
 if(i==j)
  return i;
 for(int h=H-1;h>=0;h--)
  if(p[h][i]!=p[h][j])
   i=p[h][i],j=p[h][j];
 return p[0][i];
}


template<class F>
void up(int i,int k,const F&f) {
 if(k<0)
  return;
 for(int h=0;h<H;h++)
  if(k>>h&1)
   f(i,h),i=p[h][i];
 f(i,0);
}

int main() {
 ios::sync_with_stdio(0),cin.tie(0);
 int t;
 cin>>t;
 while(t--){
  int n;
  cin>>n;
  for(int h=0;h<n-1;h++){
   int i,j;
   cin>>i>>j,i--,j--;
   g[i].push_back(j),g[j].push_back(i);
  }
  dfs(0);
  int m;
  cin>>m;
  for(int h=0;h<m;h++){
   int i,j;
   cin>>i>>j,i--,j--;
   q[n*H*2+h].push_back(z(i,0,0));
   q[z(j,0,1)].push_back(n*H*2+h);
   int f=lca(i,j);
   if(i!=f){
    up(p[0][i],d[i]-d[f]-1,[&](int u,int v){
     q[z(u,v,0)].push_back(n*H*2+h);
    });
    up(j,d[j]-d[f]-1,[&](int u,int v){
     q[z(u,v,0)].push_back(n*H*2+h);
    });
   }else
    up(j,d[j]-d[f]-1,[&](int u,int v){
     q[z(u,v,0)].push_back(n*H*2+h);
    });
   if(j!=f){
    up(p[0][j],d[j]-d[f]-1,[&](int u,int v){
     q[n*H*2+h].push_back(z(u,v,1));
    });
    up(i,d[i]-d[f]-1,[&](int u,int v){
     q[n*H*2+h].push_back(z(u,v,1));
    });
   }else
    up(i,d[i]-d[f]-1,[&](int u,int v){
     q[n*H*2+h].push_back(z(u,v,1));
    });
  }
  for(int i=0;i<n*H*2+m;i++)
   for(int j:q[i])
    w[j]++;
  vector<int>o;
  for(int i=0;i<n*H*2+m;i++)
   if(!w[i])
    o.push_back(i);
  for(int u=0;u<(int)o.size();u++){
   int i=o[u];
   for(int j:q[i])
    if(--w[j]==0)
     o.push_back(j);
  }
  cout<<((int)o.size()==n*H*2+m?"Yes\n":"No\n");
  for(int i=0;i<n;i++)
   g[i].clear();
  for(int i=0;i<n*H*2+m;i++)
   q[i].clear(),w[i]=0;
 }
 return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 56 ms 101796 KB Output is correct
2 Correct 52 ms 101836 KB Output is correct
3 Correct 48 ms 101764 KB Output is correct
4 Correct 113 ms 102400 KB Output is correct
5 Correct 189 ms 102732 KB Output is correct
6 Correct 59 ms 102252 KB Output is correct
7 Correct 61 ms 102324 KB Output is correct
8 Correct 58 ms 102244 KB Output is correct
9 Correct 620 ms 113256 KB Output is correct
10 Correct 1431 ms 281628 KB Output is correct
11 Correct 72 ms 101964 KB Output is correct
12 Correct 193 ms 103040 KB Output is correct
13 Correct 1360 ms 286328 KB Output is correct
14 Correct 1102 ms 277876 KB Output is correct
15 Correct 1221 ms 273540 KB Output is correct
16 Correct 1415 ms 281744 KB Output is correct
17 Correct 1502 ms 289072 KB Output is correct
18 Correct 1593 ms 307536 KB Output is correct
19 Correct 1501 ms 289244 KB Output is correct
20 Correct 1380 ms 289052 KB Output is correct
21 Correct 1071 ms 275004 KB Output is correct
22 Correct 1256 ms 278244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 101836 KB Output is correct
2 Correct 53 ms 101852 KB Output is correct
3 Correct 59 ms 102380 KB Output is correct
4 Correct 56 ms 102340 KB Output is correct
5 Correct 56 ms 102300 KB Output is correct
6 Correct 56 ms 102336 KB Output is correct
7 Correct 59 ms 102284 KB Output is correct
8 Correct 59 ms 102284 KB Output is correct
9 Correct 58 ms 102348 KB Output is correct
10 Correct 53 ms 102288 KB Output is correct
11 Correct 53 ms 102308 KB Output is correct
12 Correct 52 ms 102300 KB Output is correct
13 Correct 50 ms 102220 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 101836 KB Output is correct
2 Correct 53 ms 101852 KB Output is correct
3 Correct 59 ms 102380 KB Output is correct
4 Correct 56 ms 102340 KB Output is correct
5 Correct 56 ms 102300 KB Output is correct
6 Correct 56 ms 102336 KB Output is correct
7 Correct 59 ms 102284 KB Output is correct
8 Correct 59 ms 102284 KB Output is correct
9 Correct 58 ms 102348 KB Output is correct
10 Correct 53 ms 102288 KB Output is correct
11 Correct 53 ms 102308 KB Output is correct
12 Correct 52 ms 102300 KB Output is correct
13 Correct 50 ms 102220 KB Output is correct
14 Correct 45 ms 101776 KB Output is correct
15 Correct 46 ms 101856 KB Output is correct
16 Correct 60 ms 102336 KB Output is correct
17 Correct 53 ms 102268 KB Output is correct
18 Correct 56 ms 102356 KB Output is correct
19 Correct 54 ms 101816 KB Output is correct
20 Correct 54 ms 102284 KB Output is correct
21 Correct 56 ms 102304 KB Output is correct
22 Correct 55 ms 102276 KB Output is correct
23 Correct 48 ms 101836 KB Output is correct
24 Correct 47 ms 101964 KB Output is correct
25 Correct 61 ms 102328 KB Output is correct
26 Correct 49 ms 102216 KB Output is correct
27 Correct 54 ms 102248 KB Output is correct
28 Correct 47 ms 101836 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 101836 KB Output is correct
2 Correct 53 ms 101852 KB Output is correct
3 Correct 59 ms 102380 KB Output is correct
4 Correct 56 ms 102340 KB Output is correct
5 Correct 56 ms 102300 KB Output is correct
6 Correct 56 ms 102336 KB Output is correct
7 Correct 59 ms 102284 KB Output is correct
8 Correct 59 ms 102284 KB Output is correct
9 Correct 58 ms 102348 KB Output is correct
10 Correct 53 ms 102288 KB Output is correct
11 Correct 53 ms 102308 KB Output is correct
12 Correct 52 ms 102300 KB Output is correct
13 Correct 50 ms 102220 KB Output is correct
14 Correct 45 ms 101776 KB Output is correct
15 Correct 46 ms 101856 KB Output is correct
16 Correct 60 ms 102336 KB Output is correct
17 Correct 53 ms 102268 KB Output is correct
18 Correct 56 ms 102356 KB Output is correct
19 Correct 54 ms 101816 KB Output is correct
20 Correct 54 ms 102284 KB Output is correct
21 Correct 56 ms 102304 KB Output is correct
22 Correct 55 ms 102276 KB Output is correct
23 Correct 48 ms 101836 KB Output is correct
24 Correct 47 ms 101964 KB Output is correct
25 Correct 61 ms 102328 KB Output is correct
26 Correct 49 ms 102216 KB Output is correct
27 Correct 54 ms 102248 KB Output is correct
28 Correct 47 ms 101836 KB Output is correct
29 Correct 55 ms 102364 KB Output is correct
30 Correct 57 ms 102232 KB Output is correct
31 Correct 55 ms 102348 KB Output is correct
32 Correct 62 ms 102408 KB Output is correct
33 Correct 58 ms 102348 KB Output is correct
34 Correct 56 ms 102336 KB Output is correct
35 Correct 59 ms 102164 KB Output is correct
36 Correct 53 ms 102252 KB Output is correct
37 Correct 57 ms 102200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 47 ms 101836 KB Output is correct
2 Correct 53 ms 101852 KB Output is correct
3 Correct 59 ms 102380 KB Output is correct
4 Correct 56 ms 102340 KB Output is correct
5 Correct 56 ms 102300 KB Output is correct
6 Correct 56 ms 102336 KB Output is correct
7 Correct 59 ms 102284 KB Output is correct
8 Correct 59 ms 102284 KB Output is correct
9 Correct 58 ms 102348 KB Output is correct
10 Correct 53 ms 102288 KB Output is correct
11 Correct 53 ms 102308 KB Output is correct
12 Correct 52 ms 102300 KB Output is correct
13 Correct 50 ms 102220 KB Output is correct
14 Correct 45 ms 101776 KB Output is correct
15 Correct 46 ms 101856 KB Output is correct
16 Correct 60 ms 102336 KB Output is correct
17 Correct 53 ms 102268 KB Output is correct
18 Correct 56 ms 102356 KB Output is correct
19 Correct 54 ms 101816 KB Output is correct
20 Correct 54 ms 102284 KB Output is correct
21 Correct 56 ms 102304 KB Output is correct
22 Correct 55 ms 102276 KB Output is correct
23 Correct 48 ms 101836 KB Output is correct
24 Correct 47 ms 101964 KB Output is correct
25 Correct 61 ms 102328 KB Output is correct
26 Correct 49 ms 102216 KB Output is correct
27 Correct 54 ms 102248 KB Output is correct
28 Correct 47 ms 101836 KB Output is correct
29 Correct 55 ms 102364 KB Output is correct
30 Correct 57 ms 102232 KB Output is correct
31 Correct 55 ms 102348 KB Output is correct
32 Correct 62 ms 102408 KB Output is correct
33 Correct 58 ms 102348 KB Output is correct
34 Correct 56 ms 102336 KB Output is correct
35 Correct 59 ms 102164 KB Output is correct
36 Correct 53 ms 102252 KB Output is correct
37 Correct 57 ms 102200 KB Output is correct
38 Correct 528 ms 113276 KB Output is correct
39 Correct 1469 ms 281632 KB Output is correct
40 Correct 645 ms 113708 KB Output is correct
41 Correct 646 ms 113252 KB Output is correct
42 Correct 526 ms 114064 KB Output is correct
43 Correct 748 ms 114240 KB Output is correct
44 Correct 92 ms 103964 KB Output is correct
45 Correct 1364 ms 275200 KB Output is correct
46 Correct 1324 ms 275056 KB Output is correct
47 Correct 1514 ms 275472 KB Output is correct
48 Correct 1575 ms 275620 KB Output is correct
49 Correct 1065 ms 276192 KB Output is correct
50 Correct 1188 ms 276232 KB Output is correct
51 Correct 1296 ms 276180 KB Output is correct
52 Correct 1262 ms 276192 KB Output is correct
53 Correct 165 ms 116192 KB Output is correct
54 Correct 1428 ms 275608 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 53 ms 101836 KB Output is correct
2 Correct 48 ms 101776 KB Output is correct
3 Correct 55 ms 101752 KB Output is correct
4 Correct 53 ms 101804 KB Output is correct
5 Correct 79 ms 101964 KB Output is correct
6 Correct 55 ms 102204 KB Output is correct
7 Correct 51 ms 102296 KB Output is correct
8 Correct 48 ms 101752 KB Output is correct
9 Correct 47 ms 101804 KB Output is correct
10 Correct 53 ms 102048 KB Output is correct
11 Correct 55 ms 101860 KB Output is correct
12 Correct 59 ms 102172 KB Output is correct
13 Correct 133 ms 102540 KB Output is correct
14 Correct 229 ms 103068 KB Output is correct
15 Correct 169 ms 102732 KB Output is correct
16 Correct 1384 ms 276956 KB Output is correct
17 Correct 1264 ms 274716 KB Output is correct
18 Correct 1359 ms 281556 KB Output is correct
19 Correct 1452 ms 278020 KB Output is correct
20 Correct 1375 ms 277892 KB Output is correct
21 Correct 1391 ms 278032 KB Output is correct
22 Correct 1468 ms 282768 KB Output is correct
23 Correct 1083 ms 274556 KB Output is correct
24 Correct 1599 ms 282724 KB Output is correct
25 Correct 1386 ms 282876 KB Output is correct
26 Correct 1611 ms 282728 KB Output is correct
27 Correct 1404 ms 302648 KB Output is correct
28 Correct 1136 ms 277980 KB Output is correct
29 Correct 1234 ms 278132 KB Output is correct
30 Correct 1446 ms 280688 KB Output is correct
31 Correct 1356 ms 280720 KB Output is correct
32 Correct 1399 ms 280796 KB Output is correct
33 Correct 1066 ms 272404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 101796 KB Output is correct
2 Correct 52 ms 101836 KB Output is correct
3 Correct 48 ms 101764 KB Output is correct
4 Correct 113 ms 102400 KB Output is correct
5 Correct 189 ms 102732 KB Output is correct
6 Correct 59 ms 102252 KB Output is correct
7 Correct 61 ms 102324 KB Output is correct
8 Correct 58 ms 102244 KB Output is correct
9 Correct 620 ms 113256 KB Output is correct
10 Correct 1431 ms 281628 KB Output is correct
11 Correct 72 ms 101964 KB Output is correct
12 Correct 193 ms 103040 KB Output is correct
13 Correct 1360 ms 286328 KB Output is correct
14 Correct 1102 ms 277876 KB Output is correct
15 Correct 1221 ms 273540 KB Output is correct
16 Correct 1415 ms 281744 KB Output is correct
17 Correct 1502 ms 289072 KB Output is correct
18 Correct 1593 ms 307536 KB Output is correct
19 Correct 1501 ms 289244 KB Output is correct
20 Correct 1380 ms 289052 KB Output is correct
21 Correct 1071 ms 275004 KB Output is correct
22 Correct 1256 ms 278244 KB Output is correct
23 Correct 47 ms 101836 KB Output is correct
24 Correct 53 ms 101852 KB Output is correct
25 Correct 59 ms 102380 KB Output is correct
26 Correct 56 ms 102340 KB Output is correct
27 Correct 56 ms 102300 KB Output is correct
28 Correct 56 ms 102336 KB Output is correct
29 Correct 59 ms 102284 KB Output is correct
30 Correct 59 ms 102284 KB Output is correct
31 Correct 58 ms 102348 KB Output is correct
32 Correct 53 ms 102288 KB Output is correct
33 Correct 53 ms 102308 KB Output is correct
34 Correct 52 ms 102300 KB Output is correct
35 Correct 50 ms 102220 KB Output is correct
36 Correct 45 ms 101776 KB Output is correct
37 Correct 46 ms 101856 KB Output is correct
38 Correct 60 ms 102336 KB Output is correct
39 Correct 53 ms 102268 KB Output is correct
40 Correct 56 ms 102356 KB Output is correct
41 Correct 54 ms 101816 KB Output is correct
42 Correct 54 ms 102284 KB Output is correct
43 Correct 56 ms 102304 KB Output is correct
44 Correct 55 ms 102276 KB Output is correct
45 Correct 48 ms 101836 KB Output is correct
46 Correct 47 ms 101964 KB Output is correct
47 Correct 61 ms 102328 KB Output is correct
48 Correct 49 ms 102216 KB Output is correct
49 Correct 54 ms 102248 KB Output is correct
50 Correct 47 ms 101836 KB Output is correct
51 Correct 55 ms 102364 KB Output is correct
52 Correct 57 ms 102232 KB Output is correct
53 Correct 55 ms 102348 KB Output is correct
54 Correct 62 ms 102408 KB Output is correct
55 Correct 58 ms 102348 KB Output is correct
56 Correct 56 ms 102336 KB Output is correct
57 Correct 59 ms 102164 KB Output is correct
58 Correct 53 ms 102252 KB Output is correct
59 Correct 57 ms 102200 KB Output is correct
60 Correct 528 ms 113276 KB Output is correct
61 Correct 1469 ms 281632 KB Output is correct
62 Correct 645 ms 113708 KB Output is correct
63 Correct 646 ms 113252 KB Output is correct
64 Correct 526 ms 114064 KB Output is correct
65 Correct 748 ms 114240 KB Output is correct
66 Correct 92 ms 103964 KB Output is correct
67 Correct 1364 ms 275200 KB Output is correct
68 Correct 1324 ms 275056 KB Output is correct
69 Correct 1514 ms 275472 KB Output is correct
70 Correct 1575 ms 275620 KB Output is correct
71 Correct 1065 ms 276192 KB Output is correct
72 Correct 1188 ms 276232 KB Output is correct
73 Correct 1296 ms 276180 KB Output is correct
74 Correct 1262 ms 276192 KB Output is correct
75 Correct 165 ms 116192 KB Output is correct
76 Correct 1428 ms 275608 KB Output is correct
77 Correct 53 ms 101836 KB Output is correct
78 Correct 48 ms 101776 KB Output is correct
79 Correct 55 ms 101752 KB Output is correct
80 Correct 53 ms 101804 KB Output is correct
81 Correct 79 ms 101964 KB Output is correct
82 Correct 55 ms 102204 KB Output is correct
83 Correct 51 ms 102296 KB Output is correct
84 Correct 48 ms 101752 KB Output is correct
85 Correct 47 ms 101804 KB Output is correct
86 Correct 53 ms 102048 KB Output is correct
87 Correct 55 ms 101860 KB Output is correct
88 Correct 59 ms 102172 KB Output is correct
89 Correct 133 ms 102540 KB Output is correct
90 Correct 229 ms 103068 KB Output is correct
91 Correct 169 ms 102732 KB Output is correct
92 Correct 1384 ms 276956 KB Output is correct
93 Correct 1264 ms 274716 KB Output is correct
94 Correct 1359 ms 281556 KB Output is correct
95 Correct 1452 ms 278020 KB Output is correct
96 Correct 1375 ms 277892 KB Output is correct
97 Correct 1391 ms 278032 KB Output is correct
98 Correct 1468 ms 282768 KB Output is correct
99 Correct 1083 ms 274556 KB Output is correct
100 Correct 1599 ms 282724 KB Output is correct
101 Correct 1386 ms 282876 KB Output is correct
102 Correct 1611 ms 282728 KB Output is correct
103 Correct 1404 ms 302648 KB Output is correct
104 Correct 1136 ms 277980 KB Output is correct
105 Correct 1234 ms 278132 KB Output is correct
106 Correct 1446 ms 280688 KB Output is correct
107 Correct 1356 ms 280720 KB Output is correct
108 Correct 1399 ms 280796 KB Output is correct
109 Correct 1066 ms 272404 KB Output is correct
110 Correct 212 ms 103148 KB Output is correct
111 Correct 170 ms 102708 KB Output is correct
112 Correct 1361 ms 278528 KB Output is correct
113 Correct 1663 ms 279072 KB Output is correct
114 Correct 1012 ms 278504 KB Output is correct
115 Correct 772 ms 276288 KB Output is correct
116 Correct 1576 ms 281412 KB Output is correct
117 Correct 1360 ms 283144 KB Output is correct
118 Correct 1495 ms 275672 KB Output is correct
119 Correct 1535 ms 275784 KB Output is correct
120 Correct 136 ms 117704 KB Output is correct
121 Correct 1671 ms 282248 KB Output is correct
122 Correct 1625 ms 282188 KB Output is correct
123 Correct 1615 ms 280624 KB Output is correct
124 Correct 1372 ms 272376 KB Output is correct
125 Correct 1750 ms 281160 KB Output is correct
126 Correct 1526 ms 283896 KB Output is correct
127 Correct 1546 ms 288036 KB Output is correct
128 Correct 1395 ms 287800 KB Output is correct
129 Correct 1609 ms 287980 KB Output is correct
130 Correct 1412 ms 288108 KB Output is correct