Submission #414155

# Submission time Handle Problem Language Result Execution time Memory
414155 2021-05-30T07:11:40 Z nathanlee726 Mergers (JOI19_mergers) C++14
100 / 100
1632 ms 217804 KB
//#include<i_am_noob_orz>
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;
#define ll long long
//#define int ll
#define ull unsigned long long
#define pii pair<int,int>
#define X first
#define Y second
#define mod ((ll)1e9+7)
#define pb push_back
#define mp make_pair
#define abs(x) ((x)>0?(x):(-(x)))
#define F(n) Fi(i,n)
#define Fi(i,n) Fl(i,0,n)
#define Fl(i,l,n) for(int i=l;i<n;i++)
#define memres(a) memset(a,0,sizeof(a))
#define all(a) a.begin(),a.end()
#define sz(a) ((int)a.size())
#define ceiling(a,b) (((a)+(b)-1)/(b))
#define endl '\n'
#define bit_count(x) __builtin_popcountll((x))
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define jimmy_is_kind false
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> rbtree;
 

//#define LOCAL
#ifdef LOCAL
#define bug(...) cerr<<"#"<<__LINE__<<' '<<#__VA_ARGS__<<"- ", _do(__VA_ARGS__)
template<typename T> void _do(T && x) {cerr<<x<<endl;}
template<typename T, typename ...S> void _do(T && x, S&&...y) {cerr<<x<<", "; _do(y...);}
#define IOS()
#else
#define IOS() ios_base::sync_with_stdio(0), cin.tie(0)
#define endl '\n'
#define bug(...)
#endif
 
int add(int a,int b){return(a+b>=mod?a+b-mod:a+b);}
int sub(int a,int b){return(a<b?a+mod-b:a-b);}
int po(int a,int b){
	if(b==0)return 1;
	if(b==1)return(a%mod);
	int tem=po(a,b/2);
	if(b&1)return(((tem*tem)%mod)*a)%mod;
	else return(tem*tem)%mod; 
}
int GCD(int a,int b){
	int x=0;
	int ra,rb;
	while(a&&b){
		if(((a&1)==0)&&((b&1)==0)){
			a>>=1,b>>=1,x++;
		}
		else if((a^b)&1){
			if(a&1)b>>=1;
			else a>>=1;
		}
		else{
			ra=abs(a-b),rb=min(a,b);
			a=ra,b=rb;
		}
	}
	return max(a,b)<<x;
}
int gcd(int a,int b){if(b==0)return a;return gcd(b,a%b);}
vector<int> g[500010],la[500010],gr[500010];
int di[500010],st[500010],pa[500010],fr[500010],si[500010],fa[500010],ct[500010],ind=0,l[500010],r[500010],ST[1000010][20],id[1000010],ct2[500010];
int find(int x){ 
	if(fr[x]!=x)fr[x]=find(fr[x]);
	return fr[x];
} 
void merge(int x,int y){
	x=find(x),y=find(y);
	if(x==y)return;
	if(si[x]>si[y])swap(x,y);
	fr[x]=y;
	si[y]+=si[x];
}
void dfs1(int p,int f,int d){
	id[ind]=p;
	di[p]=d;
	l[p]=ind++;
	r[p]=l[p];
	for(int u:g[p]){
		if(u==f)continue;
		dfs1(u,p,d+1);
		id[ind]=p;
		r[p]=ind++;
	}
}
void dfs(int p,int f,int d){
	fa[p]=f;
	pa[p]=d-la[st[p]].back();
	la[st[p]].pb(d);
	for(int u:g[p]){
		if(u==f)continue;
		dfs(u,p,d+1);
	}
	la[st[p]].pop_back(); 
}
void dfs2(int p,int f){
	for(int u:g[p]){
		if(u==f)continue;
		if(find(p)!=find(u))ct2[find(p)]++,ct2[find(u)]++;
		dfs2(u,p);
	}
} 
int LCA(int x,int y){
	int li=min(l[x],l[y]),ri=max(r[x],r[y]);
	int len=ri-li+1;
	int lg=__lg(len);
	return (di[ST[li][lg]]<di[ST[ri-(1<<lg)+1][lg]]?ST[li][lg]:ST[ri-(1<<lg)+1][lg]);
}
signed main(){
	IOS();
	//freopen("input.txt","r",stdin);
	int n,k;
	cin>>n>>k;
	F(n-1){
		int x,y;
		cin>>x>>y;
		--x,--y;
		g[x].pb(y);
		g[y].pb(x); 	 
	}
	F(n)cin>>st[i];
	if(n==1){
		cout<<"0"<<endl;
		return 0;
	}
	F(n){
		gr[st[i]].pb(i);
	}
	dfs1(0,-1,1);
	//assert(ind==(2*n)-1);
	F(2*n-1)ST[i][0]=id[i];
	Fi(j,19)for(int i=0;i+(1<<(j+1))-1<2*n-1;i++){
		ST[i][j+1]=(di[ST[i][j]]<di[ST[i+(1<<j)][j]]?ST[i][j]:ST[i+(1<<j)][j]);
	}
	F(k){
		int np=gr[i+1][0];
		Fi(j,sz(gr[i+1])-1){
			np=LCA(np,gr[i+1][j+1]);
		}	
		la[i+1].pb(di[np]);
	}
	dfs(0,-1,1);
	F(n)fr[i]=i,si[i]=1,ct[i]=sz(g[i]);
	F(k){
		if(sz(gr[i+1])>=2){
			Fi(j,sz(gr[i+1])-1){
				merge(gr[i+1][j],gr[i+1][j+1]);
			}
		}
	}
	queue<pii> q;
	F(n){
		if(sz(g[i])==1&&i!=0){
			q.push({i,pa[i]});
		}
	}
	while(!q.empty()){
		pii tmp=q.front();q.pop();
		if(tmp.Y>0)merge(tmp.X,fa[tmp.X]);
		tmp.X=fa[tmp.X];
		tmp.Y=max(pa[tmp.X],tmp.Y-1);
		if(tmp.X!=-1){
			ct[tmp.X]--;
			pa[tmp.X]=max(tmp.Y,pa[tmp.X]);
			if(ct[tmp.X]==1)q.push(tmp); 
		} 
	}
	dfs2(0,-1);
	int ans=0;
	F(n){
		if(ct2[i]==1)ans++;
	}
	bug(ans);
	ans=ceiling(ans,2);
	cout<<ans<<endl;
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 21 ms 35652 KB Output is correct
2 Correct 21 ms 35548 KB Output is correct
3 Correct 21 ms 35660 KB Output is correct
4 Correct 20 ms 35524 KB Output is correct
5 Correct 22 ms 35524 KB Output is correct
6 Correct 25 ms 35600 KB Output is correct
7 Correct 22 ms 35532 KB Output is correct
8 Correct 20 ms 35596 KB Output is correct
9 Correct 20 ms 35536 KB Output is correct
10 Correct 20 ms 35576 KB Output is correct
11 Correct 19 ms 35572 KB Output is correct
12 Correct 20 ms 35504 KB Output is correct
13 Correct 24 ms 35628 KB Output is correct
14 Correct 22 ms 35628 KB Output is correct
15 Correct 21 ms 35532 KB Output is correct
16 Correct 21 ms 35524 KB Output is correct
17 Correct 21 ms 35504 KB Output is correct
18 Correct 21 ms 35576 KB Output is correct
19 Correct 21 ms 35524 KB Output is correct
20 Correct 22 ms 35628 KB Output is correct
21 Correct 20 ms 35588 KB Output is correct
22 Correct 20 ms 35660 KB Output is correct
23 Correct 24 ms 35628 KB Output is correct
24 Correct 23 ms 35580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 35652 KB Output is correct
2 Correct 21 ms 35548 KB Output is correct
3 Correct 21 ms 35660 KB Output is correct
4 Correct 20 ms 35524 KB Output is correct
5 Correct 22 ms 35524 KB Output is correct
6 Correct 25 ms 35600 KB Output is correct
7 Correct 22 ms 35532 KB Output is correct
8 Correct 20 ms 35596 KB Output is correct
9 Correct 20 ms 35536 KB Output is correct
10 Correct 20 ms 35576 KB Output is correct
11 Correct 19 ms 35572 KB Output is correct
12 Correct 20 ms 35504 KB Output is correct
13 Correct 24 ms 35628 KB Output is correct
14 Correct 22 ms 35628 KB Output is correct
15 Correct 21 ms 35532 KB Output is correct
16 Correct 21 ms 35524 KB Output is correct
17 Correct 21 ms 35504 KB Output is correct
18 Correct 21 ms 35576 KB Output is correct
19 Correct 21 ms 35524 KB Output is correct
20 Correct 22 ms 35628 KB Output is correct
21 Correct 20 ms 35588 KB Output is correct
22 Correct 20 ms 35660 KB Output is correct
23 Correct 24 ms 35628 KB Output is correct
24 Correct 23 ms 35580 KB Output is correct
25 Correct 22 ms 35536 KB Output is correct
26 Correct 26 ms 36428 KB Output is correct
27 Correct 25 ms 36308 KB Output is correct
28 Correct 23 ms 36648 KB Output is correct
29 Correct 22 ms 36508 KB Output is correct
30 Correct 23 ms 36296 KB Output is correct
31 Correct 23 ms 35644 KB Output is correct
32 Correct 23 ms 36692 KB Output is correct
33 Correct 24 ms 35648 KB Output is correct
34 Correct 21 ms 36364 KB Output is correct
35 Correct 22 ms 36512 KB Output is correct
36 Correct 26 ms 36300 KB Output is correct
37 Correct 23 ms 36456 KB Output is correct
38 Correct 21 ms 35660 KB Output is correct
39 Correct 25 ms 36428 KB Output is correct
40 Correct 21 ms 36288 KB Output is correct
41 Correct 22 ms 36280 KB Output is correct
42 Correct 22 ms 36472 KB Output is correct
43 Correct 22 ms 36564 KB Output is correct
44 Correct 20 ms 35568 KB Output is correct
45 Correct 23 ms 36572 KB Output is correct
46 Correct 22 ms 36380 KB Output is correct
47 Correct 20 ms 35660 KB Output is correct
48 Correct 23 ms 36356 KB Output is correct
49 Correct 22 ms 36540 KB Output is correct
50 Correct 23 ms 36548 KB Output is correct
51 Correct 22 ms 36280 KB Output is correct
52 Correct 22 ms 36300 KB Output is correct
53 Correct 23 ms 36464 KB Output is correct
54 Correct 21 ms 36300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 35652 KB Output is correct
2 Correct 21 ms 35548 KB Output is correct
3 Correct 21 ms 35660 KB Output is correct
4 Correct 20 ms 35524 KB Output is correct
5 Correct 22 ms 35524 KB Output is correct
6 Correct 25 ms 35600 KB Output is correct
7 Correct 22 ms 35532 KB Output is correct
8 Correct 20 ms 35596 KB Output is correct
9 Correct 20 ms 35536 KB Output is correct
10 Correct 20 ms 35576 KB Output is correct
11 Correct 19 ms 35572 KB Output is correct
12 Correct 20 ms 35504 KB Output is correct
13 Correct 24 ms 35628 KB Output is correct
14 Correct 22 ms 35628 KB Output is correct
15 Correct 21 ms 35532 KB Output is correct
16 Correct 21 ms 35524 KB Output is correct
17 Correct 21 ms 35504 KB Output is correct
18 Correct 21 ms 35576 KB Output is correct
19 Correct 21 ms 35524 KB Output is correct
20 Correct 22 ms 35628 KB Output is correct
21 Correct 20 ms 35588 KB Output is correct
22 Correct 20 ms 35660 KB Output is correct
23 Correct 24 ms 35628 KB Output is correct
24 Correct 23 ms 35580 KB Output is correct
25 Correct 19 ms 35532 KB Output is correct
26 Correct 111 ms 60364 KB Output is correct
27 Correct 140 ms 59688 KB Output is correct
28 Correct 22 ms 36300 KB Output is correct
29 Correct 20 ms 35604 KB Output is correct
30 Correct 19 ms 35532 KB Output is correct
31 Correct 135 ms 59592 KB Output is correct
32 Correct 23 ms 36300 KB Output is correct
33 Correct 155 ms 66820 KB Output is correct
34 Correct 143 ms 59708 KB Output is correct
35 Correct 23 ms 36272 KB Output is correct
36 Correct 190 ms 60352 KB Output is correct
37 Correct 22 ms 36356 KB Output is correct
38 Correct 23 ms 36372 KB Output is correct
39 Correct 109 ms 60308 KB Output is correct
40 Correct 24 ms 36592 KB Output is correct
41 Correct 149 ms 59532 KB Output is correct
42 Correct 163 ms 61972 KB Output is correct
43 Correct 23 ms 35660 KB Output is correct
44 Correct 180 ms 67160 KB Output is correct
45 Correct 176 ms 63544 KB Output is correct
46 Correct 21 ms 36300 KB Output is correct
47 Correct 22 ms 36300 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 107 ms 60384 KB Output is correct
2 Correct 156 ms 66516 KB Output is correct
3 Correct 24 ms 36420 KB Output is correct
4 Correct 24 ms 36428 KB Output is correct
5 Correct 24 ms 35592 KB Output is correct
6 Correct 20 ms 35524 KB Output is correct
7 Correct 22 ms 36364 KB Output is correct
8 Correct 179 ms 62760 KB Output is correct
9 Correct 23 ms 36300 KB Output is correct
10 Correct 155 ms 59848 KB Output is correct
11 Correct 21 ms 35616 KB Output is correct
12 Correct 142 ms 59748 KB Output is correct
13 Correct 217 ms 62600 KB Output is correct
14 Correct 233 ms 65944 KB Output is correct
15 Correct 109 ms 60348 KB Output is correct
16 Correct 23 ms 36420 KB Output is correct
17 Correct 21 ms 35632 KB Output is correct
18 Correct 150 ms 65884 KB Output is correct
19 Correct 214 ms 69444 KB Output is correct
20 Correct 23 ms 36428 KB Output is correct
21 Correct 21 ms 35604 KB Output is correct
22 Correct 146 ms 63464 KB Output is correct
23 Correct 23 ms 36404 KB Output is correct
24 Correct 197 ms 60600 KB Output is correct
25 Correct 206 ms 68408 KB Output is correct
26 Correct 22 ms 36556 KB Output is correct
27 Correct 23 ms 36512 KB Output is correct
28 Correct 22 ms 36300 KB Output is correct
29 Correct 25 ms 36300 KB Output is correct
30 Correct 27 ms 36356 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 21 ms 35652 KB Output is correct
2 Correct 21 ms 35548 KB Output is correct
3 Correct 21 ms 35660 KB Output is correct
4 Correct 20 ms 35524 KB Output is correct
5 Correct 22 ms 35524 KB Output is correct
6 Correct 25 ms 35600 KB Output is correct
7 Correct 22 ms 35532 KB Output is correct
8 Correct 20 ms 35596 KB Output is correct
9 Correct 20 ms 35536 KB Output is correct
10 Correct 20 ms 35576 KB Output is correct
11 Correct 19 ms 35572 KB Output is correct
12 Correct 20 ms 35504 KB Output is correct
13 Correct 24 ms 35628 KB Output is correct
14 Correct 22 ms 35628 KB Output is correct
15 Correct 21 ms 35532 KB Output is correct
16 Correct 21 ms 35524 KB Output is correct
17 Correct 21 ms 35504 KB Output is correct
18 Correct 21 ms 35576 KB Output is correct
19 Correct 21 ms 35524 KB Output is correct
20 Correct 22 ms 35628 KB Output is correct
21 Correct 20 ms 35588 KB Output is correct
22 Correct 20 ms 35660 KB Output is correct
23 Correct 24 ms 35628 KB Output is correct
24 Correct 23 ms 35580 KB Output is correct
25 Correct 22 ms 35536 KB Output is correct
26 Correct 26 ms 36428 KB Output is correct
27 Correct 25 ms 36308 KB Output is correct
28 Correct 23 ms 36648 KB Output is correct
29 Correct 22 ms 36508 KB Output is correct
30 Correct 23 ms 36296 KB Output is correct
31 Correct 23 ms 35644 KB Output is correct
32 Correct 23 ms 36692 KB Output is correct
33 Correct 24 ms 35648 KB Output is correct
34 Correct 21 ms 36364 KB Output is correct
35 Correct 22 ms 36512 KB Output is correct
36 Correct 26 ms 36300 KB Output is correct
37 Correct 23 ms 36456 KB Output is correct
38 Correct 21 ms 35660 KB Output is correct
39 Correct 25 ms 36428 KB Output is correct
40 Correct 21 ms 36288 KB Output is correct
41 Correct 22 ms 36280 KB Output is correct
42 Correct 22 ms 36472 KB Output is correct
43 Correct 22 ms 36564 KB Output is correct
44 Correct 20 ms 35568 KB Output is correct
45 Correct 23 ms 36572 KB Output is correct
46 Correct 22 ms 36380 KB Output is correct
47 Correct 20 ms 35660 KB Output is correct
48 Correct 23 ms 36356 KB Output is correct
49 Correct 22 ms 36540 KB Output is correct
50 Correct 23 ms 36548 KB Output is correct
51 Correct 22 ms 36280 KB Output is correct
52 Correct 22 ms 36300 KB Output is correct
53 Correct 23 ms 36464 KB Output is correct
54 Correct 21 ms 36300 KB Output is correct
55 Correct 19 ms 35532 KB Output is correct
56 Correct 111 ms 60364 KB Output is correct
57 Correct 140 ms 59688 KB Output is correct
58 Correct 22 ms 36300 KB Output is correct
59 Correct 20 ms 35604 KB Output is correct
60 Correct 19 ms 35532 KB Output is correct
61 Correct 135 ms 59592 KB Output is correct
62 Correct 23 ms 36300 KB Output is correct
63 Correct 155 ms 66820 KB Output is correct
64 Correct 143 ms 59708 KB Output is correct
65 Correct 23 ms 36272 KB Output is correct
66 Correct 190 ms 60352 KB Output is correct
67 Correct 22 ms 36356 KB Output is correct
68 Correct 23 ms 36372 KB Output is correct
69 Correct 109 ms 60308 KB Output is correct
70 Correct 24 ms 36592 KB Output is correct
71 Correct 149 ms 59532 KB Output is correct
72 Correct 163 ms 61972 KB Output is correct
73 Correct 23 ms 35660 KB Output is correct
74 Correct 180 ms 67160 KB Output is correct
75 Correct 176 ms 63544 KB Output is correct
76 Correct 21 ms 36300 KB Output is correct
77 Correct 22 ms 36300 KB Output is correct
78 Correct 107 ms 60384 KB Output is correct
79 Correct 156 ms 66516 KB Output is correct
80 Correct 24 ms 36420 KB Output is correct
81 Correct 24 ms 36428 KB Output is correct
82 Correct 24 ms 35592 KB Output is correct
83 Correct 20 ms 35524 KB Output is correct
84 Correct 22 ms 36364 KB Output is correct
85 Correct 179 ms 62760 KB Output is correct
86 Correct 23 ms 36300 KB Output is correct
87 Correct 155 ms 59848 KB Output is correct
88 Correct 21 ms 35616 KB Output is correct
89 Correct 142 ms 59748 KB Output is correct
90 Correct 217 ms 62600 KB Output is correct
91 Correct 233 ms 65944 KB Output is correct
92 Correct 109 ms 60348 KB Output is correct
93 Correct 23 ms 36420 KB Output is correct
94 Correct 21 ms 35632 KB Output is correct
95 Correct 150 ms 65884 KB Output is correct
96 Correct 214 ms 69444 KB Output is correct
97 Correct 23 ms 36428 KB Output is correct
98 Correct 21 ms 35604 KB Output is correct
99 Correct 146 ms 63464 KB Output is correct
100 Correct 23 ms 36404 KB Output is correct
101 Correct 197 ms 60600 KB Output is correct
102 Correct 206 ms 68408 KB Output is correct
103 Correct 22 ms 36556 KB Output is correct
104 Correct 23 ms 36512 KB Output is correct
105 Correct 22 ms 36300 KB Output is correct
106 Correct 25 ms 36300 KB Output is correct
107 Correct 27 ms 36356 KB Output is correct
108 Correct 924 ms 181532 KB Output is correct
109 Correct 1157 ms 181152 KB Output is correct
110 Correct 1146 ms 191924 KB Output is correct
111 Correct 1632 ms 217804 KB Output is correct
112 Correct 1534 ms 210100 KB Output is correct
113 Correct 1060 ms 197272 KB Output is correct
114 Correct 985 ms 163292 KB Output is correct
115 Correct 951 ms 163104 KB Output is correct
116 Correct 1169 ms 172264 KB Output is correct
117 Correct 1356 ms 196768 KB Output is correct
118 Correct 1004 ms 162628 KB Output is correct
119 Correct 1414 ms 196928 KB Output is correct
120 Correct 1468 ms 210744 KB Output is correct
121 Correct 1404 ms 196924 KB Output is correct
122 Correct 1351 ms 184224 KB Output is correct
123 Correct 1045 ms 200392 KB Output is correct
124 Correct 1530 ms 191452 KB Output is correct