Submission #520817

# Submission time Handle Problem Language Result Execution time Memory
520817 2022-01-31T04:59:15 Z nathanlee726 Capital City (JOI20_capital_city) C++14
100 / 100
1392 ms 487228 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);}
 
int n,k,dep[200010],ord[200010],pa[200010][20],inde=0,ind,id=0,pt[200010][20],co[200010],cnt[4000010],ou[4000010];
vector<int> g[200010],rg[4000010],cl[200010];
 
void dfs(int p,int f,int d){
	dep[p]=d;
	ord[p]=inde++;
	pa[p][0]=f;
	for(int j=0;j<18;j++){if(pa[p][j]!=-1)pa[p][j+1]=pa[pa[p][j]][j];else pa[p][j+1]=-1;}
	if(p!=0){
		pt[p][0]=ind++;
		assert(ind<=5000000);
		rg[pt[p][0]].pb(p);
		rg[pt[p][0]].pb(pa[p][0]);
		for(int j=1;(1ll<<j)<=dep[p];j++){
			pt[p][j]=ind++;
			rg[pt[p][j]].pb(p);
			rg[pt[p][j]].pb(pt[p][j-1]);
			assert(pa[p][j-1]!=-1);
			rg[pt[p][j]].pb(pt[pa[p][j-1]][j-1]);
		}
	}
	for(int u:g[p]){
		if(u==f)continue;
		dfs(u,p,d+1);
	}
}
 
bool cmp(int x,int y){
	return ord[x]<ord[y];
}
 
void ad_ed(int p,int l){
	int tp=p;
	assert(l>=0);
	for(int i=18;i>=0;i--){
		if((1ll<<i)&l)rg[p].pb(pt[tp][i]),tp=pa[tp][i];
	}
}
 
void lca_ed(int x,int y){
	if(x==y)return;
	int rx=x,ry=y;
	int xd=dep[x],yd=dep[y];
	if(xd>yd)swap(x,y),swap(xd,yd),swap(rx,ry);
	int len=yd-xd;
	for(int i=19;i>=0;i--){
		if((1ll<<i)&len)y=pa[y][i];
	}
	int r;
	if(x==y)r=dep[x];
	else{
	for(int i=19;i>=0;i--){
		if(pa[x][i]==-1)continue;
		if(pa[x][i]!=pa[y][i])x=pa[x][i],y=pa[y][i];
	}
	r=pa[x][0];
	r=dep[r];
	}
	int xl=xd-r,yl=yd-r;
	bug(rx,xl,ry,yl);
	if(xl>0)ad_ed(rx,xl);
	if(yl>0)ad_ed(ry,yl);
}
 
vector<int> sta;
int dfn[4000010],low[4000010],ind1=0,gr[4000010];
bool inst[4000010]={0};
 
void tarjan(int p){
	dfn[p]=low[p]=++ind1;
	inst[p]=1;
	sta.pb(p);
	for(int u:rg[p]){
		if(dfn[u]==0){
			tarjan(u);
			low[p]=min(low[p],low[u]);
		}
		else if(inst[u]){
			low[p]=min(low[p],dfn[u]);
		}
	}
	if(low[p]==dfn[p]){
		id++;
		while(sta.back()!=p){
			inst[sta.back()]=0;
			gr[sta.back()]=id;
			bug(id,sta.back());
			sta.pop_back();
		}
		//assert(sta.back()==p);
		inst[p]=0;
		gr[p]=id;
		bug(id,p);
		sta.pop_back();
	}
}
 
signed main(){
	IOS();
	cin>>n>>k;
	F(n-1){
		int x,y;
		cin>>x>>y;
		//x=i+1,y=i+2;
		--x,--y;
		g[x].pb(y);
		g[y].pb(x);
	}
	ind=n;
	dfs(0,-1,0);
	F(n){
		int x;
		cin>>x;
		//x=0;
		--x;
		cl[x].pb(i);
		co[i]=x;
	}
	F(k){
		if(sz(cl[i])>=2){
			sort(all(cl[i]),cmp);
			cl[i].pb(cl[i][0]);
			Fi(j,sz(cl[i])-1){
				bug(cl[i][j],cl[i][j+1]);
				rg[cl[i][j]].pb(cl[i][j+1]);
				lca_ed(cl[i][j],cl[i][j+1]);
			}
		}
	}
	memres(dfn);
	memres(low);
	memres(cnt);
	F(ind){
		if(dfn[i]==0)tarjan(i);
	}
	//memres(ou);
	//return 0;
	F(ind){
		if(sz(rg[i]))for(int j:rg[i]){
			if(gr[i]!=gr[j]){
				ou[gr[i]]++;
			}
		}
	}
	F(k){
		if(sz(cl[i]))cnt[gr[cl[i][0]]]++;
	}
	/*F(ind){
		bug(i);
		for(int j:rg[i])cout<<j<<" ";
		cout<<endl;
	}*/
	bug(id);
	int ans=n;
	for(int i=1;i<=id;i++)bug(cnt[i],ou[i]);
	for(int i=1;i<=id;i++)if(cnt[i]!=0&&ou[i]==0)ans=min(ans,cnt[i]);
	cout<<ans-1<<endl;
	return 0;
}
/*
10 5
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
5 4 1 3 2 3 2 1 4 5
 
20 10
19 6
6 3
3 18
18 20
20 11
11 7
7 17
17 14
14 9
9 13
13 5
5 12
12 4
4 10
10 2
2 16
16 15
15 8
8 1
8
3
2
6
7
5
7
5
1
1
4
9
4
6
2
10
9
10
8
3
 
20 10
1 2
2 3
3 4
4 5
5 6
6 7
7 8
8 9
9 10
10 11
11 12
12 13
13 14
14 15
15 16
16 17
17 18
18 19
19 20
8 5 2 10 3 1 6 9 7 4 1 6 9 7 4 3 10 2 5 8
*/
# Verdict Execution time Memory Grader output
1 Correct 64 ms 150644 KB Output is correct
2 Correct 66 ms 150632 KB Output is correct
3 Correct 66 ms 150620 KB Output is correct
4 Correct 71 ms 150648 KB Output is correct
5 Correct 67 ms 150596 KB Output is correct
6 Correct 68 ms 150752 KB Output is correct
7 Correct 64 ms 150576 KB Output is correct
8 Correct 66 ms 150572 KB Output is correct
9 Correct 66 ms 150596 KB Output is correct
10 Correct 78 ms 150580 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 150644 KB Output is correct
2 Correct 66 ms 150632 KB Output is correct
3 Correct 66 ms 150620 KB Output is correct
4 Correct 71 ms 150648 KB Output is correct
5 Correct 67 ms 150596 KB Output is correct
6 Correct 68 ms 150752 KB Output is correct
7 Correct 64 ms 150576 KB Output is correct
8 Correct 66 ms 150572 KB Output is correct
9 Correct 66 ms 150596 KB Output is correct
10 Correct 78 ms 150580 KB Output is correct
11 Correct 69 ms 151580 KB Output is correct
12 Correct 70 ms 151620 KB Output is correct
13 Correct 67 ms 151620 KB Output is correct
14 Correct 71 ms 151616 KB Output is correct
15 Correct 85 ms 151868 KB Output is correct
16 Correct 77 ms 151648 KB Output is correct
17 Correct 69 ms 151972 KB Output is correct
18 Correct 68 ms 151732 KB Output is correct
19 Correct 69 ms 151764 KB Output is correct
20 Correct 70 ms 151972 KB Output is correct
21 Correct 70 ms 151760 KB Output is correct
22 Correct 72 ms 152812 KB Output is correct
23 Correct 73 ms 152156 KB Output is correct
24 Correct 72 ms 151816 KB Output is correct
25 Correct 75 ms 152136 KB Output is correct
26 Correct 70 ms 152108 KB Output is correct
27 Correct 71 ms 152080 KB Output is correct
28 Correct 73 ms 152020 KB Output is correct
29 Correct 78 ms 151940 KB Output is correct
30 Correct 72 ms 151956 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1392 ms 471912 KB Output is correct
2 Correct 895 ms 487040 KB Output is correct
3 Correct 1329 ms 468624 KB Output is correct
4 Correct 924 ms 487128 KB Output is correct
5 Correct 1314 ms 421220 KB Output is correct
6 Correct 920 ms 484780 KB Output is correct
7 Correct 1276 ms 440388 KB Output is correct
8 Correct 877 ms 461552 KB Output is correct
9 Correct 1067 ms 348364 KB Output is correct
10 Correct 1040 ms 345892 KB Output is correct
11 Correct 1107 ms 353304 KB Output is correct
12 Correct 1141 ms 353600 KB Output is correct
13 Correct 1025 ms 351516 KB Output is correct
14 Correct 1109 ms 354712 KB Output is correct
15 Correct 1088 ms 356476 KB Output is correct
16 Correct 1129 ms 352156 KB Output is correct
17 Correct 1048 ms 351716 KB Output is correct
18 Correct 1053 ms 351536 KB Output is correct
19 Correct 1216 ms 361024 KB Output is correct
20 Correct 1067 ms 358204 KB Output is correct
21 Correct 71 ms 150596 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 150644 KB Output is correct
2 Correct 66 ms 150632 KB Output is correct
3 Correct 66 ms 150620 KB Output is correct
4 Correct 71 ms 150648 KB Output is correct
5 Correct 67 ms 150596 KB Output is correct
6 Correct 68 ms 150752 KB Output is correct
7 Correct 64 ms 150576 KB Output is correct
8 Correct 66 ms 150572 KB Output is correct
9 Correct 66 ms 150596 KB Output is correct
10 Correct 78 ms 150580 KB Output is correct
11 Correct 69 ms 151580 KB Output is correct
12 Correct 70 ms 151620 KB Output is correct
13 Correct 67 ms 151620 KB Output is correct
14 Correct 71 ms 151616 KB Output is correct
15 Correct 85 ms 151868 KB Output is correct
16 Correct 77 ms 151648 KB Output is correct
17 Correct 69 ms 151972 KB Output is correct
18 Correct 68 ms 151732 KB Output is correct
19 Correct 69 ms 151764 KB Output is correct
20 Correct 70 ms 151972 KB Output is correct
21 Correct 70 ms 151760 KB Output is correct
22 Correct 72 ms 152812 KB Output is correct
23 Correct 73 ms 152156 KB Output is correct
24 Correct 72 ms 151816 KB Output is correct
25 Correct 75 ms 152136 KB Output is correct
26 Correct 70 ms 152108 KB Output is correct
27 Correct 71 ms 152080 KB Output is correct
28 Correct 73 ms 152020 KB Output is correct
29 Correct 78 ms 151940 KB Output is correct
30 Correct 72 ms 151956 KB Output is correct
31 Correct 1392 ms 471912 KB Output is correct
32 Correct 895 ms 487040 KB Output is correct
33 Correct 1329 ms 468624 KB Output is correct
34 Correct 924 ms 487128 KB Output is correct
35 Correct 1314 ms 421220 KB Output is correct
36 Correct 920 ms 484780 KB Output is correct
37 Correct 1276 ms 440388 KB Output is correct
38 Correct 877 ms 461552 KB Output is correct
39 Correct 1067 ms 348364 KB Output is correct
40 Correct 1040 ms 345892 KB Output is correct
41 Correct 1107 ms 353304 KB Output is correct
42 Correct 1141 ms 353600 KB Output is correct
43 Correct 1025 ms 351516 KB Output is correct
44 Correct 1109 ms 354712 KB Output is correct
45 Correct 1088 ms 356476 KB Output is correct
46 Correct 1129 ms 352156 KB Output is correct
47 Correct 1048 ms 351716 KB Output is correct
48 Correct 1053 ms 351536 KB Output is correct
49 Correct 1216 ms 361024 KB Output is correct
50 Correct 1067 ms 358204 KB Output is correct
51 Correct 71 ms 150596 KB Output is correct
52 Correct 654 ms 256740 KB Output is correct
53 Correct 701 ms 257936 KB Output is correct
54 Correct 656 ms 257764 KB Output is correct
55 Correct 673 ms 258528 KB Output is correct
56 Correct 656 ms 257392 KB Output is correct
57 Correct 668 ms 258168 KB Output is correct
58 Correct 988 ms 322216 KB Output is correct
59 Correct 1027 ms 325124 KB Output is correct
60 Correct 1091 ms 315800 KB Output is correct
61 Correct 1161 ms 315300 KB Output is correct
62 Correct 875 ms 487228 KB Output is correct
63 Correct 957 ms 487184 KB Output is correct
64 Correct 866 ms 465384 KB Output is correct
65 Correct 893 ms 484788 KB Output is correct
66 Correct 824 ms 305596 KB Output is correct
67 Correct 899 ms 326708 KB Output is correct
68 Correct 817 ms 305360 KB Output is correct
69 Correct 965 ms 335588 KB Output is correct
70 Correct 984 ms 335588 KB Output is correct
71 Correct 926 ms 305432 KB Output is correct
72 Correct 924 ms 300864 KB Output is correct
73 Correct 986 ms 340976 KB Output is correct
74 Correct 849 ms 300784 KB Output is correct
75 Correct 998 ms 335544 KB Output is correct
76 Correct 964 ms 341300 KB Output is correct
77 Correct 936 ms 327156 KB Output is correct
78 Correct 1028 ms 351544 KB Output is correct
79 Correct 1088 ms 350692 KB Output is correct
80 Correct 1093 ms 356444 KB Output is correct
81 Correct 1044 ms 350128 KB Output is correct
82 Correct 1064 ms 353048 KB Output is correct
83 Correct 1043 ms 351604 KB Output is correct
84 Correct 1104 ms 359620 KB Output is correct
85 Correct 1066 ms 353828 KB Output is correct
86 Correct 1037 ms 353852 KB Output is correct
87 Correct 1018 ms 349208 KB Output is correct
88 Correct 1276 ms 353968 KB Output is correct
89 Correct 1210 ms 339964 KB Output is correct
90 Correct 1219 ms 336688 KB Output is correct
91 Correct 1209 ms 339944 KB Output is correct
92 Correct 1259 ms 346268 KB Output is correct
93 Correct 1291 ms 338116 KB Output is correct
94 Correct 1188 ms 337072 KB Output is correct
95 Correct 1212 ms 342196 KB Output is correct
96 Correct 1186 ms 349604 KB Output is correct
97 Correct 1247 ms 341984 KB Output is correct