Submission #552164

# Submission time Handle Problem Language Result Execution time Memory
552164 2022-04-22T14:28:58 Z errorgorn Jail (JOI22_jail) C++17
100 / 100
2113 ms 390880 KB
// Super Idol的笑容
//    都没你的甜
//  八月正午的阳光
//    都没你耀眼
//  热爱105°C的你
// 滴滴清纯的蒸馏水
 
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace std;
using namespace __gnu_pbds;
using namespace __gnu_cxx;
 
#define int long long
#define ll long long
#define ii pair<ll,ll>
#define iii pair<ii,ll>
#define fi first
#define se second
#define endl '\n'
#define debug(x) cout << #x << ": " << x << endl
 
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
 
#define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
 
#define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
//change less to less_equal for non distinct pbds, but erase will bug
 
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
 
int n,q;
vector<int> al[120005];
 
int d[120005];
int tkd[120005][19];
ii idx[120005][19];
vector<int> al2[5000005];
int IDX;
 
void dfs(int i,int p){
	for (auto it:al[i]){
		if (it==p) continue;
		
		d[it]=d[i]+1;
		int curr=tkd[it][0]=i;
		
		idx[it][0]={IDX,IDX+1};
		al2[IDX++].clear();
		al2[IDX++].clear();
		
		rep(x,0,19){
			if (tkd[curr][x]==-1) continue;
			tkd[it][x+1]=tkd[curr][x];
			
			idx[it][x+1].fi=IDX;
			al2[IDX].clear();
			al2[IDX].pub(idx[it][x].fi);
			al2[IDX].pub(idx[curr][x].fi);
			IDX++;
			
			idx[it][x+1].se=IDX;
			al2[IDX].clear();
			al2[idx[it][x].se].pub(IDX);
			al2[idx[curr][x].se].pub(IDX);
			IDX++;
			
			curr=tkd[it][x+1];
		}
		
		dfs(it,i);
	}
}
 
int mov(int i,int k){
	rep(x,0,19) if (k&(1<<x)) i=tkd[i][x];
	return i;
}
 
int lca(int i,int j){
	if (d[i]<d[j]) swap(i,j);
	i=mov(i,d[i]-d[j]);
	if (i==j) return i;
	
	rep(x,19,0) if (tkd[i][x]!=tkd[j][x]){
		i=tkd[i][x];
		j=tkd[j][x];
	}
	
	return tkd[i][0];
}
 
int dist(int i,int j){
	int g=lca(i,j);
	return d[i]+d[j]-2*d[g];
}
 
int vis[5000005];
bool cyc;
void dfs2(int i){
	vis[i]=1;
	
	for (auto it:al2[i]){
		if (vis[it]==1) cyc=true;
		if (vis[it]==0) dfs2(it);
	}
	
	vis[i]=2;
}
 
signed main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	cin.exceptions(ios::badbit | ios::failbit);
	
	int TC;
	cin>>TC;
	while (TC--){
		cin>>n;
		
		rep(x,1,n+1) al[x].clear();
		
		int a,b;
		rep(x,1,n){
			cin>>a>>b;
			al[a].pub(b);
			al[b].pub(a);
		}
		cin>>q;
		
		rep(x,0,q) al2[x].clear();
		IDX=q;
		
		idx[1][0]={IDX,IDX+1};
		al2[IDX++].clear();
		al2[IDX++].clear();
		
		rep(x,1,n+1) rep(y,0,19) tkd[x][y]=-1;
		dfs(1,-1);
		
		//rep(x,1,n+1) cout<<idx[x][0].fi<<" "<<idx[x][0].se<<endl;
		
		rep(i,0,q){
			cin>>a>>b;
			al2[i].pub(idx[a][0].fi);
			al2[i].pub(idx[a][0].se);
			al2[idx[b][0].fi].pub(i);
			al2[idx[b][0].se].pub(i);
			
			int g=lca(a,b);
			
			if (a!=g){
				int curr=tkd[a][0];
				rep(x,19,0) if (tkd[curr][x]!=-1 && d[g]<=d[tkd[curr][x]]){
					al2[i].pub(idx[curr][x].fi);
					al2[idx[curr][x].se].pub(i);
					curr=tkd[curr][x];
				}
			}
			if (b!=g){
				int curr=tkd[b][0];
				rep(x,19,0) if (tkd[curr][x]!=-1 && d[g]<=d[tkd[curr][x]]){
					al2[i].pub(idx[curr][x].fi);
					al2[idx[curr][x].se].pub(i);
					curr=tkd[curr][x];
				}
			}
			
			if (g!=a && g!=b){
				al2[i].pub(idx[g][0].fi);
				al2[idx[g][0].se].pub(i);
			}
		}
		
		// rep(x,0,IDX){
			// for (auto y:al2[x]) cout<<x<<" "<<y<<endl;
		// }
		
		cyc=false;
		rep(x,0,IDX) vis[x]=0;
		rep(x,0,IDX) if (vis[x]==0) dfs2(x);
		
		if (!cyc) cout<<"Yes"<<endl;
		else cout<<"No"<<endl;
	}
}
# Verdict Execution time Memory Grader output
1 Correct 72 ms 120568 KB Output is correct
2 Correct 70 ms 120544 KB Output is correct
3 Correct 57 ms 120504 KB Output is correct
4 Correct 82 ms 120656 KB Output is correct
5 Correct 111 ms 120644 KB Output is correct
6 Correct 60 ms 120852 KB Output is correct
7 Correct 64 ms 120780 KB Output is correct
8 Correct 65 ms 120828 KB Output is correct
9 Correct 345 ms 130912 KB Output is correct
10 Correct 1186 ms 338696 KB Output is correct
11 Correct 72 ms 120780 KB Output is correct
12 Correct 128 ms 121532 KB Output is correct
13 Correct 1110 ms 352644 KB Output is correct
14 Correct 1022 ms 353080 KB Output is correct
15 Correct 1553 ms 375820 KB Output is correct
16 Correct 2047 ms 390880 KB Output is correct
17 Correct 1430 ms 379044 KB Output is correct
18 Correct 1129 ms 349148 KB Output is correct
19 Correct 1334 ms 374468 KB Output is correct
20 Correct 1360 ms 374408 KB Output is correct
21 Correct 1305 ms 388960 KB Output is correct
22 Correct 856 ms 343900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 120496 KB Output is correct
2 Correct 63 ms 120572 KB Output is correct
3 Correct 64 ms 120780 KB Output is correct
4 Correct 72 ms 120812 KB Output is correct
5 Correct 63 ms 120832 KB Output is correct
6 Correct 61 ms 120776 KB Output is correct
7 Correct 60 ms 120732 KB Output is correct
8 Correct 61 ms 120836 KB Output is correct
9 Correct 63 ms 120824 KB Output is correct
10 Correct 69 ms 120812 KB Output is correct
11 Correct 60 ms 120812 KB Output is correct
12 Correct 60 ms 120688 KB Output is correct
13 Correct 62 ms 120648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 120496 KB Output is correct
2 Correct 63 ms 120572 KB Output is correct
3 Correct 64 ms 120780 KB Output is correct
4 Correct 72 ms 120812 KB Output is correct
5 Correct 63 ms 120832 KB Output is correct
6 Correct 61 ms 120776 KB Output is correct
7 Correct 60 ms 120732 KB Output is correct
8 Correct 61 ms 120836 KB Output is correct
9 Correct 63 ms 120824 KB Output is correct
10 Correct 69 ms 120812 KB Output is correct
11 Correct 60 ms 120812 KB Output is correct
12 Correct 60 ms 120688 KB Output is correct
13 Correct 62 ms 120648 KB Output is correct
14 Correct 64 ms 120540 KB Output is correct
15 Correct 61 ms 120468 KB Output is correct
16 Correct 61 ms 120744 KB Output is correct
17 Correct 62 ms 120780 KB Output is correct
18 Correct 61 ms 120856 KB Output is correct
19 Correct 67 ms 120460 KB Output is correct
20 Correct 64 ms 120848 KB Output is correct
21 Correct 61 ms 120860 KB Output is correct
22 Correct 61 ms 120792 KB Output is correct
23 Correct 60 ms 120568 KB Output is correct
24 Correct 68 ms 120652 KB Output is correct
25 Correct 66 ms 120808 KB Output is correct
26 Correct 60 ms 120776 KB Output is correct
27 Correct 69 ms 120780 KB Output is correct
28 Correct 61 ms 120520 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 120496 KB Output is correct
2 Correct 63 ms 120572 KB Output is correct
3 Correct 64 ms 120780 KB Output is correct
4 Correct 72 ms 120812 KB Output is correct
5 Correct 63 ms 120832 KB Output is correct
6 Correct 61 ms 120776 KB Output is correct
7 Correct 60 ms 120732 KB Output is correct
8 Correct 61 ms 120836 KB Output is correct
9 Correct 63 ms 120824 KB Output is correct
10 Correct 69 ms 120812 KB Output is correct
11 Correct 60 ms 120812 KB Output is correct
12 Correct 60 ms 120688 KB Output is correct
13 Correct 62 ms 120648 KB Output is correct
14 Correct 64 ms 120540 KB Output is correct
15 Correct 61 ms 120468 KB Output is correct
16 Correct 61 ms 120744 KB Output is correct
17 Correct 62 ms 120780 KB Output is correct
18 Correct 61 ms 120856 KB Output is correct
19 Correct 67 ms 120460 KB Output is correct
20 Correct 64 ms 120848 KB Output is correct
21 Correct 61 ms 120860 KB Output is correct
22 Correct 61 ms 120792 KB Output is correct
23 Correct 60 ms 120568 KB Output is correct
24 Correct 68 ms 120652 KB Output is correct
25 Correct 66 ms 120808 KB Output is correct
26 Correct 60 ms 120776 KB Output is correct
27 Correct 69 ms 120780 KB Output is correct
28 Correct 61 ms 120520 KB Output is correct
29 Correct 65 ms 120844 KB Output is correct
30 Correct 64 ms 120804 KB Output is correct
31 Correct 62 ms 120832 KB Output is correct
32 Correct 63 ms 120904 KB Output is correct
33 Correct 64 ms 120876 KB Output is correct
34 Correct 73 ms 120800 KB Output is correct
35 Correct 63 ms 120780 KB Output is correct
36 Correct 58 ms 120768 KB Output is correct
37 Correct 59 ms 120892 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 59 ms 120496 KB Output is correct
2 Correct 63 ms 120572 KB Output is correct
3 Correct 64 ms 120780 KB Output is correct
4 Correct 72 ms 120812 KB Output is correct
5 Correct 63 ms 120832 KB Output is correct
6 Correct 61 ms 120776 KB Output is correct
7 Correct 60 ms 120732 KB Output is correct
8 Correct 61 ms 120836 KB Output is correct
9 Correct 63 ms 120824 KB Output is correct
10 Correct 69 ms 120812 KB Output is correct
11 Correct 60 ms 120812 KB Output is correct
12 Correct 60 ms 120688 KB Output is correct
13 Correct 62 ms 120648 KB Output is correct
14 Correct 64 ms 120540 KB Output is correct
15 Correct 61 ms 120468 KB Output is correct
16 Correct 61 ms 120744 KB Output is correct
17 Correct 62 ms 120780 KB Output is correct
18 Correct 61 ms 120856 KB Output is correct
19 Correct 67 ms 120460 KB Output is correct
20 Correct 64 ms 120848 KB Output is correct
21 Correct 61 ms 120860 KB Output is correct
22 Correct 61 ms 120792 KB Output is correct
23 Correct 60 ms 120568 KB Output is correct
24 Correct 68 ms 120652 KB Output is correct
25 Correct 66 ms 120808 KB Output is correct
26 Correct 60 ms 120776 KB Output is correct
27 Correct 69 ms 120780 KB Output is correct
28 Correct 61 ms 120520 KB Output is correct
29 Correct 65 ms 120844 KB Output is correct
30 Correct 64 ms 120804 KB Output is correct
31 Correct 62 ms 120832 KB Output is correct
32 Correct 63 ms 120904 KB Output is correct
33 Correct 64 ms 120876 KB Output is correct
34 Correct 73 ms 120800 KB Output is correct
35 Correct 63 ms 120780 KB Output is correct
36 Correct 58 ms 120768 KB Output is correct
37 Correct 59 ms 120892 KB Output is correct
38 Correct 393 ms 130992 KB Output is correct
39 Correct 1281 ms 338996 KB Output is correct
40 Correct 261 ms 132720 KB Output is correct
41 Correct 157 ms 128624 KB Output is correct
42 Correct 132 ms 129920 KB Output is correct
43 Correct 307 ms 132716 KB Output is correct
44 Correct 73 ms 121984 KB Output is correct
45 Correct 317 ms 237684 KB Output is correct
46 Correct 398 ms 237956 KB Output is correct
47 Correct 886 ms 331252 KB Output is correct
48 Correct 946 ms 331184 KB Output is correct
49 Correct 517 ms 282224 KB Output is correct
50 Correct 469 ms 282192 KB Output is correct
51 Correct 605 ms 313788 KB Output is correct
52 Correct 606 ms 314636 KB Output is correct
53 Correct 86 ms 128380 KB Output is correct
54 Correct 307 ms 227676 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 57 ms 120504 KB Output is correct
2 Correct 60 ms 120564 KB Output is correct
3 Correct 69 ms 120516 KB Output is correct
4 Correct 59 ms 120516 KB Output is correct
5 Correct 66 ms 120568 KB Output is correct
6 Correct 58 ms 120628 KB Output is correct
7 Correct 60 ms 120720 KB Output is correct
8 Correct 60 ms 120592 KB Output is correct
9 Correct 67 ms 120548 KB Output is correct
10 Correct 65 ms 120672 KB Output is correct
11 Correct 57 ms 120464 KB Output is correct
12 Correct 61 ms 120780 KB Output is correct
13 Correct 90 ms 120764 KB Output is correct
14 Correct 122 ms 120652 KB Output is correct
15 Correct 97 ms 120776 KB Output is correct
16 Correct 279 ms 212672 KB Output is correct
17 Correct 499 ms 226120 KB Output is correct
18 Correct 701 ms 242872 KB Output is correct
19 Correct 305 ms 214876 KB Output is correct
20 Correct 307 ms 213656 KB Output is correct
21 Correct 312 ms 214036 KB Output is correct
22 Correct 421 ms 222500 KB Output is correct
23 Correct 345 ms 220740 KB Output is correct
24 Correct 352 ms 220868 KB Output is correct
25 Correct 371 ms 221600 KB Output is correct
26 Correct 345 ms 221204 KB Output is correct
27 Correct 382 ms 206056 KB Output is correct
28 Correct 389 ms 214472 KB Output is correct
29 Correct 380 ms 208488 KB Output is correct
30 Correct 303 ms 202060 KB Output is correct
31 Correct 310 ms 203208 KB Output is correct
32 Correct 283 ms 200316 KB Output is correct
33 Correct 278 ms 203160 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 72 ms 120568 KB Output is correct
2 Correct 70 ms 120544 KB Output is correct
3 Correct 57 ms 120504 KB Output is correct
4 Correct 82 ms 120656 KB Output is correct
5 Correct 111 ms 120644 KB Output is correct
6 Correct 60 ms 120852 KB Output is correct
7 Correct 64 ms 120780 KB Output is correct
8 Correct 65 ms 120828 KB Output is correct
9 Correct 345 ms 130912 KB Output is correct
10 Correct 1186 ms 338696 KB Output is correct
11 Correct 72 ms 120780 KB Output is correct
12 Correct 128 ms 121532 KB Output is correct
13 Correct 1110 ms 352644 KB Output is correct
14 Correct 1022 ms 353080 KB Output is correct
15 Correct 1553 ms 375820 KB Output is correct
16 Correct 2047 ms 390880 KB Output is correct
17 Correct 1430 ms 379044 KB Output is correct
18 Correct 1129 ms 349148 KB Output is correct
19 Correct 1334 ms 374468 KB Output is correct
20 Correct 1360 ms 374408 KB Output is correct
21 Correct 1305 ms 388960 KB Output is correct
22 Correct 856 ms 343900 KB Output is correct
23 Correct 59 ms 120496 KB Output is correct
24 Correct 63 ms 120572 KB Output is correct
25 Correct 64 ms 120780 KB Output is correct
26 Correct 72 ms 120812 KB Output is correct
27 Correct 63 ms 120832 KB Output is correct
28 Correct 61 ms 120776 KB Output is correct
29 Correct 60 ms 120732 KB Output is correct
30 Correct 61 ms 120836 KB Output is correct
31 Correct 63 ms 120824 KB Output is correct
32 Correct 69 ms 120812 KB Output is correct
33 Correct 60 ms 120812 KB Output is correct
34 Correct 60 ms 120688 KB Output is correct
35 Correct 62 ms 120648 KB Output is correct
36 Correct 64 ms 120540 KB Output is correct
37 Correct 61 ms 120468 KB Output is correct
38 Correct 61 ms 120744 KB Output is correct
39 Correct 62 ms 120780 KB Output is correct
40 Correct 61 ms 120856 KB Output is correct
41 Correct 67 ms 120460 KB Output is correct
42 Correct 64 ms 120848 KB Output is correct
43 Correct 61 ms 120860 KB Output is correct
44 Correct 61 ms 120792 KB Output is correct
45 Correct 60 ms 120568 KB Output is correct
46 Correct 68 ms 120652 KB Output is correct
47 Correct 66 ms 120808 KB Output is correct
48 Correct 60 ms 120776 KB Output is correct
49 Correct 69 ms 120780 KB Output is correct
50 Correct 61 ms 120520 KB Output is correct
51 Correct 65 ms 120844 KB Output is correct
52 Correct 64 ms 120804 KB Output is correct
53 Correct 62 ms 120832 KB Output is correct
54 Correct 63 ms 120904 KB Output is correct
55 Correct 64 ms 120876 KB Output is correct
56 Correct 73 ms 120800 KB Output is correct
57 Correct 63 ms 120780 KB Output is correct
58 Correct 58 ms 120768 KB Output is correct
59 Correct 59 ms 120892 KB Output is correct
60 Correct 393 ms 130992 KB Output is correct
61 Correct 1281 ms 338996 KB Output is correct
62 Correct 261 ms 132720 KB Output is correct
63 Correct 157 ms 128624 KB Output is correct
64 Correct 132 ms 129920 KB Output is correct
65 Correct 307 ms 132716 KB Output is correct
66 Correct 73 ms 121984 KB Output is correct
67 Correct 317 ms 237684 KB Output is correct
68 Correct 398 ms 237956 KB Output is correct
69 Correct 886 ms 331252 KB Output is correct
70 Correct 946 ms 331184 KB Output is correct
71 Correct 517 ms 282224 KB Output is correct
72 Correct 469 ms 282192 KB Output is correct
73 Correct 605 ms 313788 KB Output is correct
74 Correct 606 ms 314636 KB Output is correct
75 Correct 86 ms 128380 KB Output is correct
76 Correct 307 ms 227676 KB Output is correct
77 Correct 57 ms 120504 KB Output is correct
78 Correct 60 ms 120564 KB Output is correct
79 Correct 69 ms 120516 KB Output is correct
80 Correct 59 ms 120516 KB Output is correct
81 Correct 66 ms 120568 KB Output is correct
82 Correct 58 ms 120628 KB Output is correct
83 Correct 60 ms 120720 KB Output is correct
84 Correct 60 ms 120592 KB Output is correct
85 Correct 67 ms 120548 KB Output is correct
86 Correct 65 ms 120672 KB Output is correct
87 Correct 57 ms 120464 KB Output is correct
88 Correct 61 ms 120780 KB Output is correct
89 Correct 90 ms 120764 KB Output is correct
90 Correct 122 ms 120652 KB Output is correct
91 Correct 97 ms 120776 KB Output is correct
92 Correct 279 ms 212672 KB Output is correct
93 Correct 499 ms 226120 KB Output is correct
94 Correct 701 ms 242872 KB Output is correct
95 Correct 305 ms 214876 KB Output is correct
96 Correct 307 ms 213656 KB Output is correct
97 Correct 312 ms 214036 KB Output is correct
98 Correct 421 ms 222500 KB Output is correct
99 Correct 345 ms 220740 KB Output is correct
100 Correct 352 ms 220868 KB Output is correct
101 Correct 371 ms 221600 KB Output is correct
102 Correct 345 ms 221204 KB Output is correct
103 Correct 382 ms 206056 KB Output is correct
104 Correct 389 ms 214472 KB Output is correct
105 Correct 380 ms 208488 KB Output is correct
106 Correct 303 ms 202060 KB Output is correct
107 Correct 310 ms 203208 KB Output is correct
108 Correct 283 ms 200316 KB Output is correct
109 Correct 278 ms 203160 KB Output is correct
110 Correct 109 ms 121916 KB Output is correct
111 Correct 99 ms 121200 KB Output is correct
112 Correct 1355 ms 370768 KB Output is correct
113 Correct 1047 ms 337444 KB Output is correct
114 Correct 897 ms 290256 KB Output is correct
115 Correct 116 ms 183140 KB Output is correct
116 Correct 426 ms 231412 KB Output is correct
117 Correct 826 ms 261768 KB Output is correct
118 Correct 297 ms 225356 KB Output is correct
119 Correct 288 ms 225268 KB Output is correct
120 Correct 86 ms 130380 KB Output is correct
121 Correct 602 ms 246512 KB Output is correct
122 Correct 530 ms 242624 KB Output is correct
123 Correct 1200 ms 345420 KB Output is correct
124 Correct 1161 ms 345392 KB Output is correct
125 Correct 1325 ms 346512 KB Output is correct
126 Correct 2113 ms 377324 KB Output is correct
127 Correct 1378 ms 381628 KB Output is correct
128 Correct 1274 ms 382732 KB Output is correct
129 Correct 1215 ms 357768 KB Output is correct
130 Correct 1253 ms 377724 KB Output is correct