Submission #546537

# Submission time Handle Problem Language Result Execution time Memory
546537 2022-04-07T18:38:43 Z inksamurai Jail (JOI22_jail) C++17
100 / 100
2373 ms 457344 KB
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,x,n) for(int i=x;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3x2WzQf ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
 
void slv(){
	int n;
	cin>>n;
	
	vec(vi) adj(n);
	// bool pok=0;
	rep(i,n-1){
		int u,v;
		cin>>u>>v;
		u-=1,v-=1;
		// pok=pok or (u!=v-1);
		adj[u].pb(v);
		adj[v].pb(u);
	}

	// assert(pok);
	
	int m;
	cin>>m;
	vec(pii) rbts;
	rep(i,m){
		int s,t;
		cin>>s>>t;
		s-=1,t-=1;
		rbts.pb({s,t});
	}

	// fill depth
	vi parent(n,0),depth(n,0),tin(n,0),tout(n,0);
	int tmer=0;
	{
		auto dfs=[&](auto self,int v,int par)->void{
			tin[v]=tmer++;
			for(auto u:adj[v]){
				if(u==par) continue;
				parent[u]=v;
				depth[u]=depth[v]+1;
				self(self,u,v);
			}
			tout[v]=tmer++;
		};
		dfs(dfs,0,-1);
	}

	// fill sparse table
	vec(vi) spr(18,vi(n)),ids,iids;
	ids=iids=spr;
	vi d0(n),d1(n);
	{
		rep(j,18){
			rep(v,n){
				spr[j][v]=!j?parent[v]:spr[j-1][spr[j-1][v]];
			}
		}
		int num=0;
		rep(v,n){
			d0[v]=num++;
		}
		rep(v,n){
			d1[v]=num++;
		}
		rep(t,2){
			rep(j,18){
				rep(v,n){
					if(!t) ids[j][v]=num++;
					else iids[j][v]=num++;
				}
			}
		}
		// print(num);
	}

	// add corressponding edges to it's ranges
	vec(vi) gph(50*n+m);
	rep(v,n){
		int up=spr[0][v];
		gph[d0[up]].pb(ids[0][v]);
		gph[d0[v]].pb(ids[0][v]);
		gph[iids[0][v]].pb(d1[up]);
		gph[iids[0][v]].pb(d1[v]);
	}
	rep(j,17){
		rep(v,n){
			int up=ids[j+1][v]; // next level node
			gph[ids[j][v]].pb(up);
			int par=ids[j][spr[j][v]]; // vertex after jump
			gph[par].pb(up);
			// if(v==2 and j==0) print(ids[j][v],ids[j][spr[j][v]],up);

			int iup=iids[j+1][v]; // next level inversed node
			gph[iup].pb(iids[j][v]);
			par=iids[j][spr[j][v]]; // vertex after jump
			gph[iup].pb(par);
		}
	}

	// debug junk
	{
		// // 0 1 2 3 4 5 6 7 
		// // 8 9 10 11 12 13 14 15 
		// // 16 17 18 19 20 21 22 23 

		// rep(j,2){
		// 	rep(v,n){
		// 		if(j==1){
		// 			cout<<ids[j][v]<<"\n";
		// 			for(auto u:gph[ids[j][v]]){
		// 				cout<<u<<" ";
		// 			}
		// 			cout<<"\n";
		// 		}
		// 		// cout<<ids[j][v]<<" ";
		// 	}

		// 	cout<<"\n";
		// }
	}

	auto ancestor=[&](int u,int v)->bool{
		return tin[u]<=tin[v] and tout[v]<=tout[u];
	};

	auto lca=[&](int s,int t)->int{
		if(ancestor(s,t)) return s;
		if(ancestor(t,s)) return t;
		int u=t;
		per(j,17){
			if(!ancestor(spr[j][u],s)){
				u=spr[j][u];
			}
		}
		return spr[0][u];
	};

	rep(i,m){
		auto [s,t]=rbts[i];
		int now_id=50*n+i;
		gph[now_id].pb(d0[s]);
		gph[d1[t]].pb(now_id);
	}

	auto add_edge=[&](int v,int up,int id){
		gph[d0[v]].pb(id);
		gph[id].pb(d1[v]);
		per(j,17){
			int u=spr[j][v];
			if(depth[u]>=depth[up]){
				gph[ids[j][v]].pb(id);
				gph[id].pb(iids[j][v]);
				v=u;
			}
		}
	};

	// for(auto u:gph[1]){
	// 	print(u);
	// }

	vi ss(n,-1),tt(n,-1);
	rep(i,m){
		auto [s,t]=rbts[i];
		ss[s]=50*n+i;
		tt[t]=50*n+i;
	}

	rep(i,m){
		auto [s,t]=rbts[i];
		int anc=lca(s,t);
		int now_id=50*n+i;
		if(tt[s]!=-1){
			// print(tt[s]);
			gph[now_id].pb(tt[s]);
		}
		if(ss[t]!=-1){
			// print(ss[t]);
			gph[ss[t]].pb(now_id);
		}
		if(ancestor(s,t) or ancestor(t,s)){
			if(ancestor(t,s)) swap(s,t);
			// print("here");
			t=spr[0][t];
			int id=now_id,up=s;
			int v=t;
			if(s!=t){
				// print(t);
				// print(d0[t],id);
				// print(id,d1[t]);
				gph[d0[t]].pb(id);
				gph[id].pb(d1[t]);
			}
			per(j,17){
				int u=spr[j][v];
				if(depth[u]>depth[up]){
					// print(v,u,j,ids[j][v],id);
					// print(v,u,s,t,j,ids[j+1][v],id);
					gph[ids[j][v]].pb(id);
					gph[id].pb(iids[j][v]);
					v=u;
				}
			}
		}else{
			// print("here");
			if(depth[s]-depth[anc]>=1){
				s=spr[0][s];
				add_edge(s,anc,now_id);
			}
			if(depth[t]-depth[anc]>=1){
				t=spr[0][t];
				add_edge(t,anc,now_id);
			}
		}
	}

	auto has_cyc=[&]()->bool{
		vi usd(50*n+m,0),usd1(50*n+m,0);
		bool cyc=0;
		auto dfs=[&](auto self,int v)->void{
			usd[v]=1;
			usd1[v]=1;
			// print(v);
			for(auto u:gph[v]){
				if(usd1[u]){
					// print(u,v);
					cyc=1;
				}
				if(!usd[u]){
					self(self,u);
				}
			}
			usd1[v]=0;
		};
		// dfs(dfs,0);
		rep(v,50*n+m){
			if(!usd[v]){
				dfs(dfs,v);
			}
		}
		return cyc;
	};

	int res=has_cyc();
	// print(res);
	print(res?"No":"Yes");
}
 
signed main(){
_3x2WzQf;
	int t;
	cin>>t;
	rep(cs,t){
		slv();
	}
//	
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 196 ms 596 KB Output is correct
5 Correct 414 ms 700 KB Output is correct
6 Correct 24 ms 1108 KB Output is correct
7 Correct 19 ms 1068 KB Output is correct
8 Correct 22 ms 1112 KB Output is correct
9 Correct 586 ms 18864 KB Output is correct
10 Correct 676 ms 366948 KB Output is correct
11 Correct 79 ms 340 KB Output is correct
12 Correct 451 ms 776 KB Output is correct
13 Correct 711 ms 374212 KB Output is correct
14 Correct 845 ms 374180 KB Output is correct
15 Correct 1619 ms 401864 KB Output is correct
16 Correct 2360 ms 457344 KB Output is correct
17 Correct 693 ms 377608 KB Output is correct
18 Correct 716 ms 378928 KB Output is correct
19 Correct 701 ms 377664 KB Output is correct
20 Correct 690 ms 377664 KB Output is correct
21 Correct 1020 ms 401076 KB Output is correct
22 Correct 714 ms 372976 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 26 ms 1108 KB Output is correct
4 Correct 19 ms 1096 KB Output is correct
5 Correct 18 ms 1096 KB Output is correct
6 Correct 20 ms 1068 KB Output is correct
7 Correct 17 ms 1068 KB Output is correct
8 Correct 20 ms 1092 KB Output is correct
9 Correct 19 ms 1068 KB Output is correct
10 Correct 18 ms 1096 KB Output is correct
11 Correct 16 ms 1096 KB Output is correct
12 Correct 9 ms 1068 KB Output is correct
13 Correct 10 ms 1068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 26 ms 1108 KB Output is correct
4 Correct 19 ms 1096 KB Output is correct
5 Correct 18 ms 1096 KB Output is correct
6 Correct 20 ms 1068 KB Output is correct
7 Correct 17 ms 1068 KB Output is correct
8 Correct 20 ms 1092 KB Output is correct
9 Correct 19 ms 1068 KB Output is correct
10 Correct 18 ms 1096 KB Output is correct
11 Correct 16 ms 1096 KB Output is correct
12 Correct 9 ms 1068 KB Output is correct
13 Correct 10 ms 1068 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 17 ms 1068 KB Output is correct
17 Correct 19 ms 1128 KB Output is correct
18 Correct 19 ms 1068 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 18 ms 1108 KB Output is correct
21 Correct 18 ms 1104 KB Output is correct
22 Correct 17 ms 1108 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 724 KB Output is correct
25 Correct 20 ms 1088 KB Output is correct
26 Correct 7 ms 1108 KB Output is correct
27 Correct 19 ms 1108 KB Output is correct
28 Correct 3 ms 404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 26 ms 1108 KB Output is correct
4 Correct 19 ms 1096 KB Output is correct
5 Correct 18 ms 1096 KB Output is correct
6 Correct 20 ms 1068 KB Output is correct
7 Correct 17 ms 1068 KB Output is correct
8 Correct 20 ms 1092 KB Output is correct
9 Correct 19 ms 1068 KB Output is correct
10 Correct 18 ms 1096 KB Output is correct
11 Correct 16 ms 1096 KB Output is correct
12 Correct 9 ms 1068 KB Output is correct
13 Correct 10 ms 1068 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 17 ms 1068 KB Output is correct
17 Correct 19 ms 1128 KB Output is correct
18 Correct 19 ms 1068 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 18 ms 1108 KB Output is correct
21 Correct 18 ms 1104 KB Output is correct
22 Correct 17 ms 1108 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 724 KB Output is correct
25 Correct 20 ms 1088 KB Output is correct
26 Correct 7 ms 1108 KB Output is correct
27 Correct 19 ms 1108 KB Output is correct
28 Correct 3 ms 404 KB Output is correct
29 Correct 19 ms 1116 KB Output is correct
30 Correct 20 ms 1152 KB Output is correct
31 Correct 19 ms 1324 KB Output is correct
32 Correct 23 ms 1124 KB Output is correct
33 Correct 19 ms 1104 KB Output is correct
34 Correct 19 ms 996 KB Output is correct
35 Correct 20 ms 1068 KB Output is correct
36 Correct 13 ms 984 KB Output is correct
37 Correct 11 ms 964 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 0 ms 212 KB Output is correct
3 Correct 26 ms 1108 KB Output is correct
4 Correct 19 ms 1096 KB Output is correct
5 Correct 18 ms 1096 KB Output is correct
6 Correct 20 ms 1068 KB Output is correct
7 Correct 17 ms 1068 KB Output is correct
8 Correct 20 ms 1092 KB Output is correct
9 Correct 19 ms 1068 KB Output is correct
10 Correct 18 ms 1096 KB Output is correct
11 Correct 16 ms 1096 KB Output is correct
12 Correct 9 ms 1068 KB Output is correct
13 Correct 10 ms 1068 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 17 ms 1068 KB Output is correct
17 Correct 19 ms 1128 KB Output is correct
18 Correct 19 ms 1068 KB Output is correct
19 Correct 2 ms 340 KB Output is correct
20 Correct 18 ms 1108 KB Output is correct
21 Correct 18 ms 1104 KB Output is correct
22 Correct 17 ms 1108 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 2 ms 724 KB Output is correct
25 Correct 20 ms 1088 KB Output is correct
26 Correct 7 ms 1108 KB Output is correct
27 Correct 19 ms 1108 KB Output is correct
28 Correct 3 ms 404 KB Output is correct
29 Correct 19 ms 1116 KB Output is correct
30 Correct 20 ms 1152 KB Output is correct
31 Correct 19 ms 1324 KB Output is correct
32 Correct 23 ms 1124 KB Output is correct
33 Correct 19 ms 1104 KB Output is correct
34 Correct 19 ms 996 KB Output is correct
35 Correct 20 ms 1068 KB Output is correct
36 Correct 13 ms 984 KB Output is correct
37 Correct 11 ms 964 KB Output is correct
38 Correct 549 ms 18880 KB Output is correct
39 Correct 666 ms 366956 KB Output is correct
40 Correct 657 ms 19208 KB Output is correct
41 Correct 626 ms 19088 KB Output is correct
42 Correct 504 ms 19340 KB Output is correct
43 Correct 525 ms 18700 KB Output is correct
44 Correct 118 ms 3724 KB Output is correct
45 Correct 761 ms 366708 KB Output is correct
46 Correct 727 ms 366644 KB Output is correct
47 Correct 837 ms 364064 KB Output is correct
48 Correct 816 ms 364052 KB Output is correct
49 Correct 670 ms 367400 KB Output is correct
50 Correct 682 ms 367352 KB Output is correct
51 Correct 723 ms 366644 KB Output is correct
52 Correct 741 ms 366756 KB Output is correct
53 Correct 127 ms 25480 KB Output is correct
54 Correct 791 ms 367404 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 0 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 87 ms 392 KB Output is correct
6 Correct 9 ms 1068 KB Output is correct
7 Correct 10 ms 1068 KB Output is correct
8 Correct 2 ms 340 KB Output is correct
9 Correct 2 ms 316 KB Output is correct
10 Correct 2 ms 596 KB Output is correct
11 Correct 2 ms 396 KB Output is correct
12 Correct 15 ms 976 KB Output is correct
13 Correct 290 ms 828 KB Output is correct
14 Correct 468 ms 716 KB Output is correct
15 Correct 417 ms 740 KB Output is correct
16 Correct 977 ms 372016 KB Output is correct
17 Correct 1607 ms 390256 KB Output is correct
18 Correct 2219 ms 449372 KB Output is correct
19 Correct 859 ms 372200 KB Output is correct
20 Correct 1069 ms 376356 KB Output is correct
21 Correct 999 ms 376388 KB Output is correct
22 Correct 1404 ms 402596 KB Output is correct
23 Correct 1341 ms 398944 KB Output is correct
24 Correct 1403 ms 399092 KB Output is correct
25 Correct 1327 ms 399280 KB Output is correct
26 Correct 1314 ms 399092 KB Output is correct
27 Correct 880 ms 388212 KB Output is correct
28 Correct 906 ms 391932 KB Output is correct
29 Correct 857 ms 389308 KB Output is correct
30 Correct 941 ms 376660 KB Output is correct
31 Correct 1071 ms 379284 KB Output is correct
32 Correct 1105 ms 378116 KB Output is correct
33 Correct 1318 ms 381968 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 0 ms 212 KB Output is correct
4 Correct 196 ms 596 KB Output is correct
5 Correct 414 ms 700 KB Output is correct
6 Correct 24 ms 1108 KB Output is correct
7 Correct 19 ms 1068 KB Output is correct
8 Correct 22 ms 1112 KB Output is correct
9 Correct 586 ms 18864 KB Output is correct
10 Correct 676 ms 366948 KB Output is correct
11 Correct 79 ms 340 KB Output is correct
12 Correct 451 ms 776 KB Output is correct
13 Correct 711 ms 374212 KB Output is correct
14 Correct 845 ms 374180 KB Output is correct
15 Correct 1619 ms 401864 KB Output is correct
16 Correct 2360 ms 457344 KB Output is correct
17 Correct 693 ms 377608 KB Output is correct
18 Correct 716 ms 378928 KB Output is correct
19 Correct 701 ms 377664 KB Output is correct
20 Correct 690 ms 377664 KB Output is correct
21 Correct 1020 ms 401076 KB Output is correct
22 Correct 714 ms 372976 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 26 ms 1108 KB Output is correct
26 Correct 19 ms 1096 KB Output is correct
27 Correct 18 ms 1096 KB Output is correct
28 Correct 20 ms 1068 KB Output is correct
29 Correct 17 ms 1068 KB Output is correct
30 Correct 20 ms 1092 KB Output is correct
31 Correct 19 ms 1068 KB Output is correct
32 Correct 18 ms 1096 KB Output is correct
33 Correct 16 ms 1096 KB Output is correct
34 Correct 9 ms 1068 KB Output is correct
35 Correct 10 ms 1068 KB Output is correct
36 Correct 0 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 17 ms 1068 KB Output is correct
39 Correct 19 ms 1128 KB Output is correct
40 Correct 19 ms 1068 KB Output is correct
41 Correct 2 ms 340 KB Output is correct
42 Correct 18 ms 1108 KB Output is correct
43 Correct 18 ms 1104 KB Output is correct
44 Correct 17 ms 1108 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 2 ms 724 KB Output is correct
47 Correct 20 ms 1088 KB Output is correct
48 Correct 7 ms 1108 KB Output is correct
49 Correct 19 ms 1108 KB Output is correct
50 Correct 3 ms 404 KB Output is correct
51 Correct 19 ms 1116 KB Output is correct
52 Correct 20 ms 1152 KB Output is correct
53 Correct 19 ms 1324 KB Output is correct
54 Correct 23 ms 1124 KB Output is correct
55 Correct 19 ms 1104 KB Output is correct
56 Correct 19 ms 996 KB Output is correct
57 Correct 20 ms 1068 KB Output is correct
58 Correct 13 ms 984 KB Output is correct
59 Correct 11 ms 964 KB Output is correct
60 Correct 549 ms 18880 KB Output is correct
61 Correct 666 ms 366956 KB Output is correct
62 Correct 657 ms 19208 KB Output is correct
63 Correct 626 ms 19088 KB Output is correct
64 Correct 504 ms 19340 KB Output is correct
65 Correct 525 ms 18700 KB Output is correct
66 Correct 118 ms 3724 KB Output is correct
67 Correct 761 ms 366708 KB Output is correct
68 Correct 727 ms 366644 KB Output is correct
69 Correct 837 ms 364064 KB Output is correct
70 Correct 816 ms 364052 KB Output is correct
71 Correct 670 ms 367400 KB Output is correct
72 Correct 682 ms 367352 KB Output is correct
73 Correct 723 ms 366644 KB Output is correct
74 Correct 741 ms 366756 KB Output is correct
75 Correct 127 ms 25480 KB Output is correct
76 Correct 791 ms 367404 KB Output is correct
77 Correct 0 ms 340 KB Output is correct
78 Correct 1 ms 340 KB Output is correct
79 Correct 1 ms 340 KB Output is correct
80 Correct 1 ms 212 KB Output is correct
81 Correct 87 ms 392 KB Output is correct
82 Correct 9 ms 1068 KB Output is correct
83 Correct 10 ms 1068 KB Output is correct
84 Correct 2 ms 340 KB Output is correct
85 Correct 2 ms 316 KB Output is correct
86 Correct 2 ms 596 KB Output is correct
87 Correct 2 ms 396 KB Output is correct
88 Correct 15 ms 976 KB Output is correct
89 Correct 290 ms 828 KB Output is correct
90 Correct 468 ms 716 KB Output is correct
91 Correct 417 ms 740 KB Output is correct
92 Correct 977 ms 372016 KB Output is correct
93 Correct 1607 ms 390256 KB Output is correct
94 Correct 2219 ms 449372 KB Output is correct
95 Correct 859 ms 372200 KB Output is correct
96 Correct 1069 ms 376356 KB Output is correct
97 Correct 999 ms 376388 KB Output is correct
98 Correct 1404 ms 402596 KB Output is correct
99 Correct 1341 ms 398944 KB Output is correct
100 Correct 1403 ms 399092 KB Output is correct
101 Correct 1327 ms 399280 KB Output is correct
102 Correct 1314 ms 399092 KB Output is correct
103 Correct 880 ms 388212 KB Output is correct
104 Correct 906 ms 391932 KB Output is correct
105 Correct 857 ms 389308 KB Output is correct
106 Correct 941 ms 376660 KB Output is correct
107 Correct 1071 ms 379284 KB Output is correct
108 Correct 1105 ms 378116 KB Output is correct
109 Correct 1318 ms 381968 KB Output is correct
110 Correct 469 ms 1164 KB Output is correct
111 Correct 324 ms 820 KB Output is correct
112 Correct 1812 ms 406616 KB Output is correct
113 Correct 1104 ms 369600 KB Output is correct
114 Correct 1378 ms 418376 KB Output is correct
115 Correct 526 ms 370700 KB Output is correct
116 Correct 848 ms 375720 KB Output is correct
117 Correct 1913 ms 411044 KB Output is correct
118 Correct 761 ms 367536 KB Output is correct
119 Correct 713 ms 367476 KB Output is correct
120 Correct 68 ms 31716 KB Output is correct
121 Correct 1140 ms 377668 KB Output is correct
122 Correct 1108 ms 377700 KB Output is correct
123 Correct 1189 ms 370184 KB Output is correct
124 Correct 1236 ms 370360 KB Output is correct
125 Correct 1262 ms 370980 KB Output is correct
126 Correct 2373 ms 447268 KB Output is correct
127 Correct 1393 ms 429672 KB Output is correct
128 Correct 1082 ms 423732 KB Output is correct
129 Correct 1067 ms 420636 KB Output is correct
130 Correct 1174 ms 426900 KB Output is correct