Submission #555890

# Submission time Handle Problem Language Result Execution time Memory
555890 2022-05-01T18:40:13 Z Koosha_mv Jail (JOI22_jail) C++14
100 / 100
645 ms 62588 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=5e5+99,lg=40;

int n,m,ans,Time,a[N],b[N],t[N],h[N],st[N],ft[N],sz[N],up[N],cnt[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){
			seg1[l++].pb(id);
		}
		if(r&1){
			seg1[--r].pb(id);
		}
	}
}
void add0(int x,int val){
	for(x+=n;x>0;x>>=1) seg0[x]+=val;
}
void add1(int u,int v,int id){
	int cnt=0;
	while(up[u]!=up[v]){
		cnt++;
		if(h[up[u]]<h[up[v]]) swap(u,v);
		add(st[up[u]],st[u]+1,id);
		u=par[up[u]];
	}
	if(cnt>lg) assert(0);
	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){
	cnt[u]++;
	if(cnt[u]>2*n) assert(0);
	if(vis[u]) return ;
	add0(st[b[u]],-1);
	vis[u]=1;
	for(int x=st[a[u]]+n;x>0;x>>=1){
		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){
		Dfs(i);
	}
	reverse(all(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])){
			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]=cnt[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);
      |  ^
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23840 KB Output is correct
2 Correct 12 ms 23892 KB Output is correct
3 Correct 12 ms 23892 KB Output is correct
4 Correct 19 ms 23892 KB Output is correct
5 Correct 30 ms 23856 KB Output is correct
6 Correct 12 ms 23892 KB Output is correct
7 Correct 13 ms 23892 KB Output is correct
8 Correct 12 ms 23932 KB Output is correct
9 Correct 39 ms 25216 KB Output is correct
10 Correct 53 ms 43664 KB Output is correct
11 Correct 21 ms 23908 KB Output is correct
12 Correct 50 ms 23920 KB Output is correct
13 Correct 90 ms 48500 KB Output is correct
14 Correct 98 ms 50944 KB Output is correct
15 Correct 158 ms 50888 KB Output is correct
16 Correct 348 ms 59804 KB Output is correct
17 Correct 105 ms 52912 KB Output is correct
18 Correct 100 ms 53532 KB Output is correct
19 Correct 108 ms 52556 KB Output is correct
20 Correct 109 ms 52572 KB Output is correct
21 Correct 107 ms 52912 KB Output is correct
22 Correct 78 ms 50244 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23892 KB Output is correct
2 Correct 11 ms 23892 KB Output is correct
3 Correct 14 ms 23876 KB Output is correct
4 Correct 13 ms 23928 KB Output is correct
5 Correct 13 ms 23892 KB Output is correct
6 Correct 13 ms 23892 KB Output is correct
7 Correct 13 ms 23892 KB Output is correct
8 Correct 13 ms 23892 KB Output is correct
9 Correct 13 ms 23892 KB Output is correct
10 Correct 14 ms 23852 KB Output is correct
11 Correct 13 ms 23872 KB Output is correct
12 Correct 11 ms 23892 KB Output is correct
13 Correct 12 ms 23832 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23892 KB Output is correct
2 Correct 11 ms 23892 KB Output is correct
3 Correct 14 ms 23876 KB Output is correct
4 Correct 13 ms 23928 KB Output is correct
5 Correct 13 ms 23892 KB Output is correct
6 Correct 13 ms 23892 KB Output is correct
7 Correct 13 ms 23892 KB Output is correct
8 Correct 13 ms 23892 KB Output is correct
9 Correct 13 ms 23892 KB Output is correct
10 Correct 14 ms 23852 KB Output is correct
11 Correct 13 ms 23872 KB Output is correct
12 Correct 11 ms 23892 KB Output is correct
13 Correct 12 ms 23832 KB Output is correct
14 Correct 12 ms 23896 KB Output is correct
15 Correct 11 ms 23816 KB Output is correct
16 Correct 13 ms 23940 KB Output is correct
17 Correct 12 ms 23892 KB Output is correct
18 Correct 15 ms 23904 KB Output is correct
19 Correct 12 ms 23892 KB Output is correct
20 Correct 13 ms 23868 KB Output is correct
21 Correct 12 ms 23924 KB Output is correct
22 Correct 14 ms 23948 KB Output is correct
23 Correct 12 ms 23892 KB Output is correct
24 Correct 13 ms 23788 KB Output is correct
25 Correct 17 ms 23932 KB Output is correct
26 Correct 14 ms 23912 KB Output is correct
27 Correct 15 ms 23892 KB Output is correct
28 Correct 12 ms 23820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23892 KB Output is correct
2 Correct 11 ms 23892 KB Output is correct
3 Correct 14 ms 23876 KB Output is correct
4 Correct 13 ms 23928 KB Output is correct
5 Correct 13 ms 23892 KB Output is correct
6 Correct 13 ms 23892 KB Output is correct
7 Correct 13 ms 23892 KB Output is correct
8 Correct 13 ms 23892 KB Output is correct
9 Correct 13 ms 23892 KB Output is correct
10 Correct 14 ms 23852 KB Output is correct
11 Correct 13 ms 23872 KB Output is correct
12 Correct 11 ms 23892 KB Output is correct
13 Correct 12 ms 23832 KB Output is correct
14 Correct 12 ms 23896 KB Output is correct
15 Correct 11 ms 23816 KB Output is correct
16 Correct 13 ms 23940 KB Output is correct
17 Correct 12 ms 23892 KB Output is correct
18 Correct 15 ms 23904 KB Output is correct
19 Correct 12 ms 23892 KB Output is correct
20 Correct 13 ms 23868 KB Output is correct
21 Correct 12 ms 23924 KB Output is correct
22 Correct 14 ms 23948 KB Output is correct
23 Correct 12 ms 23892 KB Output is correct
24 Correct 13 ms 23788 KB Output is correct
25 Correct 17 ms 23932 KB Output is correct
26 Correct 14 ms 23912 KB Output is correct
27 Correct 15 ms 23892 KB Output is correct
28 Correct 12 ms 23820 KB Output is correct
29 Correct 14 ms 23896 KB Output is correct
30 Correct 13 ms 23892 KB Output is correct
31 Correct 14 ms 23892 KB Output is correct
32 Correct 12 ms 23944 KB Output is correct
33 Correct 12 ms 23892 KB Output is correct
34 Correct 16 ms 23952 KB Output is correct
35 Correct 15 ms 23900 KB Output is correct
36 Correct 15 ms 23892 KB Output is correct
37 Correct 16 ms 23920 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23892 KB Output is correct
2 Correct 11 ms 23892 KB Output is correct
3 Correct 14 ms 23876 KB Output is correct
4 Correct 13 ms 23928 KB Output is correct
5 Correct 13 ms 23892 KB Output is correct
6 Correct 13 ms 23892 KB Output is correct
7 Correct 13 ms 23892 KB Output is correct
8 Correct 13 ms 23892 KB Output is correct
9 Correct 13 ms 23892 KB Output is correct
10 Correct 14 ms 23852 KB Output is correct
11 Correct 13 ms 23872 KB Output is correct
12 Correct 11 ms 23892 KB Output is correct
13 Correct 12 ms 23832 KB Output is correct
14 Correct 12 ms 23896 KB Output is correct
15 Correct 11 ms 23816 KB Output is correct
16 Correct 13 ms 23940 KB Output is correct
17 Correct 12 ms 23892 KB Output is correct
18 Correct 15 ms 23904 KB Output is correct
19 Correct 12 ms 23892 KB Output is correct
20 Correct 13 ms 23868 KB Output is correct
21 Correct 12 ms 23924 KB Output is correct
22 Correct 14 ms 23948 KB Output is correct
23 Correct 12 ms 23892 KB Output is correct
24 Correct 13 ms 23788 KB Output is correct
25 Correct 17 ms 23932 KB Output is correct
26 Correct 14 ms 23912 KB Output is correct
27 Correct 15 ms 23892 KB Output is correct
28 Correct 12 ms 23820 KB Output is correct
29 Correct 14 ms 23896 KB Output is correct
30 Correct 13 ms 23892 KB Output is correct
31 Correct 14 ms 23892 KB Output is correct
32 Correct 12 ms 23944 KB Output is correct
33 Correct 12 ms 23892 KB Output is correct
34 Correct 16 ms 23952 KB Output is correct
35 Correct 15 ms 23900 KB Output is correct
36 Correct 15 ms 23892 KB Output is correct
37 Correct 16 ms 23920 KB Output is correct
38 Correct 54 ms 25136 KB Output is correct
39 Correct 54 ms 43632 KB Output is correct
40 Correct 45 ms 25072 KB Output is correct
41 Correct 47 ms 24932 KB Output is correct
42 Correct 42 ms 25284 KB Output is correct
43 Correct 37 ms 24624 KB Output is correct
44 Correct 23 ms 24148 KB Output is correct
45 Correct 86 ms 34440 KB Output is correct
46 Correct 83 ms 34420 KB Output is correct
47 Correct 54 ms 39076 KB Output is correct
48 Correct 54 ms 39008 KB Output is correct
49 Correct 62 ms 34788 KB Output is correct
50 Correct 61 ms 34608 KB Output is correct
51 Correct 49 ms 35752 KB Output is correct
52 Correct 51 ms 35732 KB Output is correct
53 Correct 21 ms 24788 KB Output is correct
54 Correct 92 ms 34380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 15 ms 23860 KB Output is correct
2 Correct 13 ms 23892 KB Output is correct
3 Correct 12 ms 23892 KB Output is correct
4 Correct 12 ms 23840 KB Output is correct
5 Correct 19 ms 23892 KB Output is correct
6 Correct 13 ms 23892 KB Output is correct
7 Correct 12 ms 23844 KB Output is correct
8 Correct 12 ms 23776 KB Output is correct
9 Correct 12 ms 23892 KB Output is correct
10 Correct 13 ms 23912 KB Output is correct
11 Correct 14 ms 23892 KB Output is correct
12 Correct 14 ms 23904 KB Output is correct
13 Correct 35 ms 23848 KB Output is correct
14 Correct 48 ms 23920 KB Output is correct
15 Correct 50 ms 23928 KB Output is correct
16 Correct 108 ms 35960 KB Output is correct
17 Correct 224 ms 43904 KB Output is correct
18 Correct 441 ms 55564 KB Output is correct
19 Correct 141 ms 37620 KB Output is correct
20 Correct 141 ms 37640 KB Output is correct
21 Correct 132 ms 37564 KB Output is correct
22 Correct 178 ms 42828 KB Output is correct
23 Correct 147 ms 41880 KB Output is correct
24 Correct 158 ms 41968 KB Output is correct
25 Correct 160 ms 42100 KB Output is correct
26 Correct 190 ms 42036 KB Output is correct
27 Correct 212 ms 42812 KB Output is correct
28 Correct 222 ms 49764 KB Output is correct
29 Correct 199 ms 44792 KB Output is correct
30 Correct 217 ms 48452 KB Output is correct
31 Correct 155 ms 50596 KB Output is correct
32 Correct 145 ms 41672 KB Output is correct
33 Correct 151 ms 44100 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 12 ms 23840 KB Output is correct
2 Correct 12 ms 23892 KB Output is correct
3 Correct 12 ms 23892 KB Output is correct
4 Correct 19 ms 23892 KB Output is correct
5 Correct 30 ms 23856 KB Output is correct
6 Correct 12 ms 23892 KB Output is correct
7 Correct 13 ms 23892 KB Output is correct
8 Correct 12 ms 23932 KB Output is correct
9 Correct 39 ms 25216 KB Output is correct
10 Correct 53 ms 43664 KB Output is correct
11 Correct 21 ms 23908 KB Output is correct
12 Correct 50 ms 23920 KB Output is correct
13 Correct 90 ms 48500 KB Output is correct
14 Correct 98 ms 50944 KB Output is correct
15 Correct 158 ms 50888 KB Output is correct
16 Correct 348 ms 59804 KB Output is correct
17 Correct 105 ms 52912 KB Output is correct
18 Correct 100 ms 53532 KB Output is correct
19 Correct 108 ms 52556 KB Output is correct
20 Correct 109 ms 52572 KB Output is correct
21 Correct 107 ms 52912 KB Output is correct
22 Correct 78 ms 50244 KB Output is correct
23 Correct 12 ms 23892 KB Output is correct
24 Correct 11 ms 23892 KB Output is correct
25 Correct 14 ms 23876 KB Output is correct
26 Correct 13 ms 23928 KB Output is correct
27 Correct 13 ms 23892 KB Output is correct
28 Correct 13 ms 23892 KB Output is correct
29 Correct 13 ms 23892 KB Output is correct
30 Correct 13 ms 23892 KB Output is correct
31 Correct 13 ms 23892 KB Output is correct
32 Correct 14 ms 23852 KB Output is correct
33 Correct 13 ms 23872 KB Output is correct
34 Correct 11 ms 23892 KB Output is correct
35 Correct 12 ms 23832 KB Output is correct
36 Correct 12 ms 23896 KB Output is correct
37 Correct 11 ms 23816 KB Output is correct
38 Correct 13 ms 23940 KB Output is correct
39 Correct 12 ms 23892 KB Output is correct
40 Correct 15 ms 23904 KB Output is correct
41 Correct 12 ms 23892 KB Output is correct
42 Correct 13 ms 23868 KB Output is correct
43 Correct 12 ms 23924 KB Output is correct
44 Correct 14 ms 23948 KB Output is correct
45 Correct 12 ms 23892 KB Output is correct
46 Correct 13 ms 23788 KB Output is correct
47 Correct 17 ms 23932 KB Output is correct
48 Correct 14 ms 23912 KB Output is correct
49 Correct 15 ms 23892 KB Output is correct
50 Correct 12 ms 23820 KB Output is correct
51 Correct 14 ms 23896 KB Output is correct
52 Correct 13 ms 23892 KB Output is correct
53 Correct 14 ms 23892 KB Output is correct
54 Correct 12 ms 23944 KB Output is correct
55 Correct 12 ms 23892 KB Output is correct
56 Correct 16 ms 23952 KB Output is correct
57 Correct 15 ms 23900 KB Output is correct
58 Correct 15 ms 23892 KB Output is correct
59 Correct 16 ms 23920 KB Output is correct
60 Correct 54 ms 25136 KB Output is correct
61 Correct 54 ms 43632 KB Output is correct
62 Correct 45 ms 25072 KB Output is correct
63 Correct 47 ms 24932 KB Output is correct
64 Correct 42 ms 25284 KB Output is correct
65 Correct 37 ms 24624 KB Output is correct
66 Correct 23 ms 24148 KB Output is correct
67 Correct 86 ms 34440 KB Output is correct
68 Correct 83 ms 34420 KB Output is correct
69 Correct 54 ms 39076 KB Output is correct
70 Correct 54 ms 39008 KB Output is correct
71 Correct 62 ms 34788 KB Output is correct
72 Correct 61 ms 34608 KB Output is correct
73 Correct 49 ms 35752 KB Output is correct
74 Correct 51 ms 35732 KB Output is correct
75 Correct 21 ms 24788 KB Output is correct
76 Correct 92 ms 34380 KB Output is correct
77 Correct 15 ms 23860 KB Output is correct
78 Correct 13 ms 23892 KB Output is correct
79 Correct 12 ms 23892 KB Output is correct
80 Correct 12 ms 23840 KB Output is correct
81 Correct 19 ms 23892 KB Output is correct
82 Correct 13 ms 23892 KB Output is correct
83 Correct 12 ms 23844 KB Output is correct
84 Correct 12 ms 23776 KB Output is correct
85 Correct 12 ms 23892 KB Output is correct
86 Correct 13 ms 23912 KB Output is correct
87 Correct 14 ms 23892 KB Output is correct
88 Correct 14 ms 23904 KB Output is correct
89 Correct 35 ms 23848 KB Output is correct
90 Correct 48 ms 23920 KB Output is correct
91 Correct 50 ms 23928 KB Output is correct
92 Correct 108 ms 35960 KB Output is correct
93 Correct 224 ms 43904 KB Output is correct
94 Correct 441 ms 55564 KB Output is correct
95 Correct 141 ms 37620 KB Output is correct
96 Correct 141 ms 37640 KB Output is correct
97 Correct 132 ms 37564 KB Output is correct
98 Correct 178 ms 42828 KB Output is correct
99 Correct 147 ms 41880 KB Output is correct
100 Correct 158 ms 41968 KB Output is correct
101 Correct 160 ms 42100 KB Output is correct
102 Correct 190 ms 42036 KB Output is correct
103 Correct 212 ms 42812 KB Output is correct
104 Correct 222 ms 49764 KB Output is correct
105 Correct 199 ms 44792 KB Output is correct
106 Correct 217 ms 48452 KB Output is correct
107 Correct 155 ms 50596 KB Output is correct
108 Correct 145 ms 41672 KB Output is correct
109 Correct 151 ms 44100 KB Output is correct
110 Correct 54 ms 24968 KB Output is correct
111 Correct 32 ms 24424 KB Output is correct
112 Correct 233 ms 50468 KB Output is correct
113 Correct 135 ms 44588 KB Output is correct
114 Correct 170 ms 47560 KB Output is correct
115 Correct 53 ms 35884 KB Output is correct
116 Correct 214 ms 41480 KB Output is correct
117 Correct 645 ms 62588 KB Output is correct
118 Correct 112 ms 35928 KB Output is correct
119 Correct 133 ms 35724 KB Output is correct
120 Correct 22 ms 25292 KB Output is correct
121 Correct 239 ms 42184 KB Output is correct
122 Correct 280 ms 42000 KB Output is correct
123 Correct 132 ms 46744 KB Output is correct
124 Correct 144 ms 46640 KB Output is correct
125 Correct 140 ms 47112 KB Output is correct
126 Correct 451 ms 61464 KB Output is correct
127 Correct 212 ms 53288 KB Output is correct
128 Correct 143 ms 54304 KB Output is correct
129 Correct 148 ms 50328 KB Output is correct
130 Correct 180 ms 50444 KB Output is correct