답안 #555874

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
555874 2022-05-01T18:30:51 Z Koosha_mv Jail (JOI22_jail) C++14
49 / 100
5000 ms 34616 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,Time,a[N],b[N],t[N],h[N],st[N],ft[N],sz[N],up[N],vis[N],par[N],seg0[N];
vector<int> topo,g[N],seg1[N];

void Dfs(int );

void dfs1(int u,int p){
	f(i,0,g[u].size()) if(g[u][i]==p) g[u].erase(g[u].begin()+i);
	sz[u]=1;
	par[u]=p;
	for(auto v : g[u]){
		h[v]=h[u]+1;
		dfs1(v,u);
		sz[u]+=sz[v];
	}
	sort(all(g[u]),[&](int u,int v){ return sz[u]>sz[v]; });
	
}
void dfs2(int u){
	st[u]=Time++;
	for(auto v : g[u]) up[v]=(v==g[u][0] ? up[u] : v),dfs2(v);
	ft[u]=Time;
	//cout<<u<<" : "<<st[u]<<" "<<ft[u]<<" "<<up[u]<<endl;
}
int pet(int l,int r){
	int ans=0;
	for(l+=n,r+=n;l<r;l>>=1,r>>=1){
		if(l&1) ans+=seg0[l++];
		if(r&1) ans+=seg0[--r];
	}
	return ans;
}
int Get(int u,int v){
	int ans=0;
	while(up[u]!=up[v]){
		if(h[up[u]]<h[up[v]]) swap(u,v);
		ans+=pet(st[up[u]],st[u]+1);
		u=par[up[u]];
	}
	if(h[u]>h[v]) swap(u,v);
	ans+=pet(st[u],st[v]+1);
	return ans;
}
void add(int l,int r,int id){
	for(l+=n,r+=n;l<r;l>>=1,r>>=1){
		if(l&1){
			//cout<<l<<" -> "<<id<<endl;
			seg1[l++].pb(id);
		}
		if(r&1){
			seg1[--r].pb(id);
			//cout<<r<<" -> "<<id<<endl;
		}
	}
}
void add0(int x,int val){
	for(x+=n;x>0;x>>=1) seg0[x]+=val;
}
void add1(int u,int v,int id){
	while(up[u]!=up[v]){
		if(h[up[u]]<h[up[v]]) swap(u,v);
		add(st[up[u]],st[u]+1,id);
		u=par[up[u]];
	}
	if(h[u]>h[v]) swap(u,v);
	add(st[u],st[v]+1,id);
}
void pert(int id){
	if(seg0[id]==0) return ;
	if(n<=id){
		Dfs(t[id-n]);
		return ;
	}
	pert(id<<1);
	pert(id<<1|1);
}
void get2(int l,int r){
	for(l+=n,r+=n;l<r;l>>=1,r>>=1){
		if(l&1) pert(l++);
		if(r&1) pert(--r);
	}
}
void get1(int u,int v){
	while(up[u]!=up[v]){
		if(h[up[u]]<h[up[v]]) swap(u,v);
		get2(st[up[u]],st[u]+1);
		u=par[up[u]];
	}
	if(h[u]>h[v]) swap(u,v);
	get2(st[u],st[v]+1);
}
void Dfs(int u){
	if(vis[u]) return ;
	vis[u]=1;
	for(int x=st[a[u]]+n;x>0;x>>=1){
		//cout<<u<<" -------- "<<x<<endl;
		while(seg1[x].size()){
			int v=seg1[x].back();
			seg1[x].pop_back();
			Dfs(v);
		}
	}
	get1(a[u],b[u]);
	topo.pb(u);
}
void Main(){
	cin>>n;
	f(i,1,n){
		int u,v;
		cin>>u>>v;
		g[u].pb(v);
		g[v].pb(u);
	}
	up[1]=1;
	dfs1(1,1);
	dfs2(1);	
	cin>>m;
	f(i,1,m+1){
		cin>>a[i]>>b[i];
		t[st[b[i]]]=i;
		add0(st[b[i]],1);
		add1(a[i],b[i],i);
	}
	f(i,1,m+1){
		//cout<<i<<" : "<<vis[i]<<endl;
		Dfs(i);
	}
	reverse(all(topo));
	//dbgv(topo);
	fill(seg0,seg0+(n<<1),0);
	f(i,1,m+1) add0(st[a[i]],1);
	for(auto x : topo){
		add0(st[a[x]],-1);
		if(Get(a[x],b[x])){
			//eror(x);
			cout<<"No"<<'\n';
			return ;
		}
		add0(st[b[x]],1);
	}
	cout<<"Yes"<<'\n';
}

int32_t main(){
	ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int tc;
	cin>>tc;
	while(tc--){
		Main();
		Time=0;
		int mx=2*max(n,m)+10;
		topo.clear();
		f(i,0,mx) g[i].clear(),seg1[i].clear(),t[i]=vis[i]=seg0[i]=0;
	}
}

Compilation message

jail.cpp: In function 'void dfs1(int, int)':
jail.cpp:8:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 | #define f(i,a,b) for(int i=a;i<b;i++)
......
   31 |  f(i,0,g[u].size()) if(g[u][i]==p) g[u].erase(g[u].begin()+i);
      |    ~~~~~~~~~~~~~~~             
jail.cpp:31:2: note: in expansion of macro 'f'
   31 |  f(i,0,g[u].size()) if(g[u][i]==p) g[u].erase(g[u].begin()+i);
      |  ^
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 7 ms 9684 KB Output is correct
4 Correct 13 ms 9684 KB Output is correct
5 Correct 23 ms 9684 KB Output is correct
6 Correct 7 ms 9812 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Correct 7 ms 9812 KB Output is correct
9 Correct 46 ms 11052 KB Output is correct
10 Correct 59 ms 28232 KB Output is correct
11 Correct 11 ms 9796 KB Output is correct
12 Correct 51 ms 9804 KB Output is correct
13 Correct 109 ms 32624 KB Output is correct
14 Correct 127 ms 34616 KB Output is correct
15 Execution timed out 5088 ms 33864 KB Time limit exceeded
16 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 7 ms 9812 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 8 ms 9684 KB Output is correct
6 Correct 9 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 6 ms 9684 KB Output is correct
12 Correct 6 ms 9808 KB Output is correct
13 Correct 6 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 7 ms 9812 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 8 ms 9684 KB Output is correct
6 Correct 9 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 6 ms 9684 KB Output is correct
12 Correct 6 ms 9808 KB Output is correct
13 Correct 6 ms 9684 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 7 ms 9812 KB Output is correct
17 Correct 6 ms 9684 KB Output is correct
18 Correct 8 ms 9812 KB Output is correct
19 Correct 5 ms 9692 KB Output is correct
20 Correct 6 ms 9812 KB Output is correct
21 Correct 6 ms 9812 KB Output is correct
22 Correct 6 ms 9760 KB Output is correct
23 Correct 6 ms 9728 KB Output is correct
24 Correct 5 ms 9684 KB Output is correct
25 Correct 6 ms 9752 KB Output is correct
26 Correct 6 ms 9684 KB Output is correct
27 Correct 6 ms 9812 KB Output is correct
28 Correct 5 ms 9684 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 7 ms 9812 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 8 ms 9684 KB Output is correct
6 Correct 9 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 6 ms 9684 KB Output is correct
12 Correct 6 ms 9808 KB Output is correct
13 Correct 6 ms 9684 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 7 ms 9812 KB Output is correct
17 Correct 6 ms 9684 KB Output is correct
18 Correct 8 ms 9812 KB Output is correct
19 Correct 5 ms 9692 KB Output is correct
20 Correct 6 ms 9812 KB Output is correct
21 Correct 6 ms 9812 KB Output is correct
22 Correct 6 ms 9760 KB Output is correct
23 Correct 6 ms 9728 KB Output is correct
24 Correct 5 ms 9684 KB Output is correct
25 Correct 6 ms 9752 KB Output is correct
26 Correct 6 ms 9684 KB Output is correct
27 Correct 6 ms 9812 KB Output is correct
28 Correct 5 ms 9684 KB Output is correct
29 Correct 7 ms 9812 KB Output is correct
30 Correct 6 ms 9812 KB Output is correct
31 Correct 7 ms 9812 KB Output is correct
32 Correct 7 ms 9812 KB Output is correct
33 Correct 6 ms 9812 KB Output is correct
34 Correct 7 ms 9812 KB Output is correct
35 Correct 7 ms 9812 KB Output is correct
36 Correct 6 ms 9828 KB Output is correct
37 Correct 8 ms 9812 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9684 KB Output is correct
2 Correct 6 ms 9684 KB Output is correct
3 Correct 7 ms 9812 KB Output is correct
4 Correct 6 ms 9684 KB Output is correct
5 Correct 8 ms 9684 KB Output is correct
6 Correct 9 ms 9684 KB Output is correct
7 Correct 6 ms 9684 KB Output is correct
8 Correct 6 ms 9684 KB Output is correct
9 Correct 6 ms 9684 KB Output is correct
10 Correct 6 ms 9684 KB Output is correct
11 Correct 6 ms 9684 KB Output is correct
12 Correct 6 ms 9808 KB Output is correct
13 Correct 6 ms 9684 KB Output is correct
14 Correct 5 ms 9684 KB Output is correct
15 Correct 5 ms 9684 KB Output is correct
16 Correct 7 ms 9812 KB Output is correct
17 Correct 6 ms 9684 KB Output is correct
18 Correct 8 ms 9812 KB Output is correct
19 Correct 5 ms 9692 KB Output is correct
20 Correct 6 ms 9812 KB Output is correct
21 Correct 6 ms 9812 KB Output is correct
22 Correct 6 ms 9760 KB Output is correct
23 Correct 6 ms 9728 KB Output is correct
24 Correct 5 ms 9684 KB Output is correct
25 Correct 6 ms 9752 KB Output is correct
26 Correct 6 ms 9684 KB Output is correct
27 Correct 6 ms 9812 KB Output is correct
28 Correct 5 ms 9684 KB Output is correct
29 Correct 7 ms 9812 KB Output is correct
30 Correct 6 ms 9812 KB Output is correct
31 Correct 7 ms 9812 KB Output is correct
32 Correct 7 ms 9812 KB Output is correct
33 Correct 6 ms 9812 KB Output is correct
34 Correct 7 ms 9812 KB Output is correct
35 Correct 7 ms 9812 KB Output is correct
36 Correct 6 ms 9828 KB Output is correct
37 Correct 8 ms 9812 KB Output is correct
38 Correct 45 ms 11008 KB Output is correct
39 Correct 48 ms 28256 KB Output is correct
40 Correct 60 ms 10880 KB Output is correct
41 Correct 41 ms 10708 KB Output is correct
42 Correct 37 ms 11032 KB Output is correct
43 Correct 29 ms 10408 KB Output is correct
44 Correct 15 ms 10032 KB Output is correct
45 Correct 65 ms 18968 KB Output is correct
46 Incorrect 71 ms 18952 KB Output isn't correct
47 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 5 ms 9784 KB Output is correct
2 Correct 5 ms 9684 KB Output is correct
3 Correct 6 ms 9684 KB Output is correct
4 Correct 5 ms 9684 KB Output is correct
5 Correct 10 ms 9684 KB Output is correct
6 Correct 6 ms 9684 KB Output is correct
7 Correct 5 ms 9812 KB Output is correct
8 Correct 5 ms 9704 KB Output is correct
9 Correct 5 ms 9660 KB Output is correct
10 Correct 5 ms 9684 KB Output is correct
11 Correct 5 ms 9784 KB Output is correct
12 Correct 7 ms 9812 KB Output is correct
13 Correct 27 ms 9812 KB Output is correct
14 Correct 40 ms 9824 KB Output is correct
15 Correct 32 ms 9812 KB Output is correct
16 Execution timed out 5087 ms 19964 KB Time limit exceeded
17 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 Correct 7 ms 9684 KB Output is correct
4 Correct 13 ms 9684 KB Output is correct
5 Correct 23 ms 9684 KB Output is correct
6 Correct 7 ms 9812 KB Output is correct
7 Correct 6 ms 9812 KB Output is correct
8 Correct 7 ms 9812 KB Output is correct
9 Correct 46 ms 11052 KB Output is correct
10 Correct 59 ms 28232 KB Output is correct
11 Correct 11 ms 9796 KB Output is correct
12 Correct 51 ms 9804 KB Output is correct
13 Correct 109 ms 32624 KB Output is correct
14 Correct 127 ms 34616 KB Output is correct
15 Execution timed out 5088 ms 33864 KB Time limit exceeded
16 Halted 0 ms 0 KB -