답안 #555838

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
555838 2022-05-01T16:29:20 Z Koosha_mv Jail (JOI22_jail) C++14
0 / 100
6 ms 9684 KB
#include <bits/stdc++.h>
using namespace std;
#define dbgv(v) cout<<#v<<" = "; f(i,0,v.size()) cout<<v[i]<<" "; cout<<endl
#define dbga(a,x,y) cout<<#a<<" = "; f(i,x,y) cout<<a[i]<<" "; cout<<endl
#define erorp(x) cout<<#x<<"={"<<x.F<<" , "<<x.S<<"}"<<endl
#define eror(x) cout<<#x<<'='<<(x)<<endl
#define f_(i,a,b) for(int i=a;i>=b;i--)
#define f(i,a,b) for(int i=a;i<b;i++)
#define nb(x) __builtin_popcount(x)
#define all(v) v.begin(),v.end()
#define bit(n,k) (((n)>>(k))&1)
#define Add(x,y) x=(x+y)%mod
#define maxm(a,b) a=max(a,b)
#define minm(a,b) a=min(a,b)
#define lst(x) x[x.size()-1]
#define sz(x) int(x.size())
#define mp make_pair
#define ll long long
#define pb push_back
#define S second
#define F first

const int N=2e5+99;

int n,m,ans,a[N],b[N],h[N],vis[N],par[N];
vector<int> g[N];
vector<pair<int,int>> vec[N];

void dfs1(int u,int p){
	for(auto v : g[u]){
		if(v==p) continue ;
		par[v]=u;
		h[v]=h[u]+1;
		dfs1(v,u);
	}
}
void dfs2(int u){
	vis[u]=1;
	for(auto v : g[u]){
		if(vis[v]==1) ans=0;
		if(vis[v]) continue ;
		dfs2(v);
	}
	vis[u]=2;
}
void Main(){
	cin>>n;
	f(i,1,n){
		int u,v;
		cin>>u>>v;
		g[u].pb(v);
		g[v].pb(u);
	}	
	cin>>m;
	f(i,1,m+1){
		cin>>a[i]>>b[i];
		vec[a[i]].pb({i,0});
		vec[b[i]].pb({i,1});
	}
	dfs1(1,1);
	f(i,1,n+1) g[i].clear();
	f(i,1,m+1){
		//eror(i);
		int u=a[i],v=b[i];
		while(u!=v){
			//cout<<u<<" "<<v<<endl;
			if(h[u]<h[v]) swap(u,v);
			//cout<<u<<" -> "<<par[u]<<endl;
			u=par[u];
			
			if(u!=v){
				for(auto p : vec[u]){
					if(p.S==1){
						//cout<<i<<" -> "<<p.F<<endl;;
						g[i].pb(p.F);
					}
					else{
						//cout<<p.F<<" --> "<<i<<endl;
						g[p.F].pb(i);
					}
				}
			}
		}
	}
	ans=1;
	f(i,1,m+1){
		if(vis[i]) continue;
		dfs2(i);
	}
	if(ans) cout<<"Yes"<<'\n';
	else cout<<"No"<<'\n';
}

int32_t main(){
	ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int tc;
	cin>>tc;
	while(tc--){
		Main();
		f(i,1,max(n,m)+1) g[i].clear(),vec[i].clear(),vis[i]=0;
	}
}
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Incorrect 5 ms 9684 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Incorrect 5 ms 9684 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Incorrect 5 ms 9684 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Incorrect 5 ms 9684 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Incorrect 5 ms 9684 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 6 ms 9684 KB Output is correct
2 Incorrect 5 ms 9684 KB Output isn't correct
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Incorrect 5 ms 9684 KB Output isn't correct
4 Halted 0 ms 0 KB -