Submission #551837

# Submission time Handle Problem Language Result Execution time Memory
551837 2022-04-21T17:12:22 Z kshitij_sodani Jail (JOI22_jail) C++14
0 / 100
262 ms 419588 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define a first
#define b second
#define pb push_back
#define endl '\n'
const llo mod=1e9+7;

int q;
int ee;
vector<int> adj[200001];
int cc[200001];
int dd[200001];
int vis[200001];
vector<int> ss;
vector<int> tt;
vector<int> pre[200001];
map<int,int> ind[200001];
int ind2[200001];
int ind3[200001];
vector<int> adj2[200001*40];
int vis2[200001*40];
int vis3[200001*40];
void dfs(int no,int par2=-1){
	ss.pb(no);
	if(ee==no){
		tt=ss;
	}
	for(auto j:adj[no]){
		if(j!=par2){
			dfs(j,no);
		}
	}
	ss.pop_back();
}
int stt=1;
void dfs2(int no){
	vis2[no]=1;
	vis3[no]=1;

	for(auto j:adj2[no]){
		assert(j!=-1);
		
		if(vis3[j]==1){
			stt=0;
		}
		if(vis2[j]==0){
			dfs2(j);
		}
	}
	vis3[no]=0;
}
int par[120001][20];
int pp[120001][20];
int pp2[120001][20];

int up[120001];
int lev[120001];
void dfs3(int no,int par2=-1,int xx=-1,int levv=0){
	par[no][0]=par2;
	up[no]=xx;
	lev[no]=levv;
	if(ind2[no]>=0 or ind3[no]>=0){
		xx=no;
	}
	for(auto j:adj[no]){
		
		if(j!=par2){

			dfs3(j,no,xx,levv+1);
		}
	}
}
int lca(int aa,int bb){
	if(lev[bb]<lev[aa]){
		swap(aa,bb);
	}
	//cout<<lev[bb]<<",,"<<lev[aa]<<endl;
	int dif=lev[bb]-lev[aa];
	//cout<<dif<<",,"<<endl;
	for(int j=19;j>=0;j--){
		if((1<<j)&dif){
			bb=par[bb][j];
		}
	}
/*	cout<<bb<<",,"<<par[bb][0]<<endl;
	cout<<dif<<",,"<<endl;
	cout<<aa<<",,"<<bb<<endl;*/
	if(aa==bb){
		return aa;
	}
	for(int j=19;j>=0;j--){
		if(par[aa][j]!=par[bb][j]){
			aa=par[aa][j];
			bb=par[bb][j];
		}
	}
	return par[aa][0];
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>q;
	while(q--){
		int n;
		cin>>n;
		for(int i=0;i<n;i++){
			adj[i].clear();
		}
		for(int i=0;i<n*50;i++){
			adj2[i].clear();
		}
		for(int i=0;i<n-1;i++){
			int aa,bb;
			cin>>aa>>bb;
			aa--;
			bb--;
			adj[aa].pb(bb);
			adj[bb].pb(aa);
		}
		int m;
		cin>>m;
		stt=1;
		for(int i=0;i<n;i++){
			ind2[i]=-1;
			ind3[i]=-1;
		}
		for(int i=0;i<m;i++){
			cin>>cc[i]>>dd[i];
			ind[i].clear();
			cc[i]--;
			dd[i]--;
			ind2[dd[i]]=i;
			ind3[cc[i]]=i;
			ee=dd[i];
		/*	dfs(cc[i]);
			pre[i]=tt;
			for(auto j:tt){
				ind[i][j]++;
			}*/
			
		}

		dfs3(0);

		for(int j=1;j<20;j++){
			for(int i=0;i<n;i++){
				if(par[i][j-1]==-1){
					par[i][j]=-1;
				}
				else{
					par[i][j]=par[par[i][j-1]][j-1];
				}
			}

		}
		int ind6=m;
		for(int j=0;j<20;j++){
			for(int i=0;i<n;i++){
				if(par[i][j]==-1){
					pp[i][j]=-1;
					continue;
				}
				ind6++;
				pp[i][j]=ind6;
				if(j>0){
					adj2[pp[i][j]].pb(pp[i][j-1]);
					adj2[pp[i][j]].pb(pp[par[i][j-1]][j-1]);
				}
			}
		}
		for(int j=0;j<20;j++){
			for(int i=0;i<n;i++){
				if(par[i][j]==-1){
					pp2[i][j]=-1;
					continue;
				}
				ind6++;
				pp2[i][j]=ind6;
				if(j>0){
					adj2[pp2[i][j-1]].pb(pp2[i][j]);
					adj2[pp2[par[i][j-1]][j-1]].pb(pp2[i][j]);
				}
			}
		}
		for(int i=0;i<n;i++){
			if(ind2[i]>=0){
				adj2[pp[i][0]].pb(ind2[i]);
			}
			if(ind3[i]>=0){
				adj2[ind3[i]].pb(pp2[i][0]);
			}
		}

	/*	for(int i=0;i<ind6;i++){
			adj2[i].clear();
		}*/
		for(int i=0;i<m;i++){
			int x=lca(cc[i],dd[i]);

		///	cout<<cc[i]<<":"<<dd[i]<<":"<<x<<endl;
			int j=par[cc[i]][0];

			if(x!=cc[i] and lev[j]>lev[x]){
				int dif=lev[j]-lev[x];
				for(int jj=19;jj>=0;jj--){
					if((1<<jj)&dif){
						adj2[i].pb(pp[j][jj]);
						adj2[pp2[j][jj]].pb(i);
						j=par[j][jj];
					}
				}
			}
			j=par[dd[i]][0];
			if(x!=dd[i] and lev[j]>lev[x]){
				int dif=lev[j]-lev[x];
				for(int jj=19;jj>=0;jj--){
					if((1<<jj)&dif){
						adj2[i].pb(pp[j][jj]);
						adj2[pp2[j][jj]].pb(i);
						j=par[j][jj];
					}
				}
			}
			vector<int> tte;
			tte.pb(cc[i]);
			tte.pb(dd[i]);
			tte.pb(x);
			for(auto j:tte){
				if(ind3[j]>=0 and ind3[j]!=i){
					adj2[i].pb(ind3[j]);
					//cout<<i<<":"<<ind3[j]<<endl;
				}
				if(ind2[j]>=0 and ind2[j]!=i){
					adj2[ind2[j]].pb(i);
					//cout<<ind2[j]<<":"<<i<<endl;
				}
			}
			/*while(j!=-1){
				if(lev[j]<lev[x]){
					break;
				}
				if(ind3[j]>=0 and ind3[j]!=i){
					adj2[i].pb(ind3[j]);
					//cout<<i<<":"<<ind3[j]<<endl;
				}
				if(ind2[j]>=0 and ind2[j]!=i){
					adj2[ind2[j]].pb(i);
					//cout<<ind2[j]<<":"<<i<<endl;
				}
				j=up[j];
			}
			j=dd[i];
			while(j!=-1){
				if(lev[j]<lev[x]){
					break;
				}
				if(ind3[j]>=0 and ind3[j]!=i){
					adj2[i].pb(ind3[j]);
					//cout<<i<<":"<<ind3[j]<<endl;
				}
				if(ind2[j]>=0 and ind2[j]!=i){
					adj2[ind2[j]].pb(i);
					//cout<<ind2[j]<<":"<<i<<endl;
				}
				j=up[j];
			}*/
			//cout<<endl;
		/*	for(auto j:pre[i]){
				if(ind3[j]>=0 and ind3[j]!=i){
					adj2[i].pb(ind3[j]);
					//cout<<i<<":"<<ind3[j]<<endl;
				}
				if(ind2[j]>=0 and ind2[j]!=i){
					adj2[ind2[j]].pb(i);
				//	cout<<ind2[j]<<":"<<i<<endl;
				}
			}*/
		}
		for(int i=0;i<ind6;i++){
			vis2[i]=0;
		}
		for(int i=0;i<ind6;i++){
			/*for(auto j:adj2[i]){
				cout<<i<<","<<j<<endl;
			}*/
			if(vis2[i]==0){
				dfs2(i);
			}
		}
		if(stt==0){
			cout<<"No"<<endl;
		}
		else{
			cout<<"Yes"<<endl;
		}


	}






	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 102 ms 206924 KB Output is correct
2 Runtime error 262 ms 419588 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 111 ms 207028 KB Output is correct
2 Runtime error 256 ms 419560 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 111 ms 207028 KB Output is correct
2 Runtime error 256 ms 419560 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 111 ms 207028 KB Output is correct
2 Runtime error 256 ms 419560 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 111 ms 207028 KB Output is correct
2 Runtime error 256 ms 419560 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 117 ms 207028 KB Output is correct
2 Correct 112 ms 207016 KB Output is correct
3 Runtime error 244 ms 419564 KB Execution killed with signal 6
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 102 ms 206924 KB Output is correct
2 Runtime error 262 ms 419588 KB Execution killed with signal 6
3 Halted 0 ms 0 KB -