Submission #235279

# Submission time Handle Problem Language Result Execution time Memory
235279 2020-05-27T14:54:13 Z kshitij_sodani Capital City (JOI20_capital_city) C++17
100 / 100
1186 ms 68832 KB
#include <bits/stdc++.h>
using namespace std;
typedef int64_t llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
int n,k;
int aa,bb;
int it[200001];
set<int> adj[200001];
int si[200001];

vector<int> col[200001];
vector<int> col2[200001];

vector<int> rem;
vector<int> rem2;


int vis[200001];
int vis2[200001];
int par[200001];


int count2=0;
int cur=0;
int dfs(int no,int par=-1){
	si[no]=1;
	rem.pb(it[no]-1);
	rem2.pb(no);
	col[it[no]-1].pb(no);
	for(auto j:adj[no]){
		if(j==par){
			continue;
		}
		dfs(j,no);
		si[no]+=si[j];
	}
}
int find(int no,int par=-1){
	for(auto j:adj[no]){
		if(j==par){
			continue;
		}
		if(si[j]>si[cur]/2){
			return find(j,no);
		}
	}
	return no;
}
int dfs2(int no,int par2=-1){
	par[no]=par2;
	for(auto j:adj[no]){
		if(j==par2){
			continue;
		}
		dfs2(j,no);
	}
}
int ma;
int dec(int no){
	cur=no;
	dfs(cur);
	int cen=find(no);
	count2=0;
	dfs2(cen);
	queue<int> ss;
	ss.push(cen);
	int st=1;
	int ans=0;
	vis2[cen]=1;
	while(ss.size()){
		int x=ss.front();
		ss.pop();
		if(vis[it[x]-1]==0){
			vis[it[x]-1]=1;
			ans+=1;
			if(col[it[x]-1].size()!=col2[it[x]-1].size()){
				st=0;
				break;
			}
			for(auto j:col[it[x]-1]){
				if(j!=x){
					ss.push(j);
				}
			}
		}
		x=par[x];
		while(x!=-1){
			if(vis2[x]==1){
				break;
			}
			ss.push(x);
			vis2[x]=1;
			x=par[x];
		}
	}
	if(st==1){
		ma=min(ma,ans-1);
	}
	for(auto j:rem){
		col[j].clear();
		vis[j]=0;
	}
	for(auto j:rem2){
		vis2[j]=0;
	}
	rem2.clear();
	rem.clear();
	for(auto j:adj[cen]){
		adj[j].erase(cen);
	}
	for(auto j:adj[cen]){
		dec(j);
	}
}
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cin>>n>>k;
	for(int i=0;i<n-1;i++){
		cin>>aa>>bb;
		adj[aa-1].insert(bb-1);
		adj[bb-1].insert(aa-1);
	}
	for(int i=0;i<n;i++){
		cin>>it[i];
		col2[it[i]-1].pb(i);
	}
	ma=n-1;
	dec(0);
	cout<<ma<<endl;

	return 0;
}

Compilation message

capital_city.cpp: In function 'int dfs(int, int)':
capital_city.cpp:40:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
capital_city.cpp: In function 'int dfs2(int, int)':
capital_city.cpp:60:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
capital_city.cpp: In function 'int dec(int)':
capital_city.cpp:117:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^
# Verdict Execution time Memory Grader output
1 Correct 16 ms 19200 KB Output is correct
2 Correct 18 ms 19200 KB Output is correct
3 Correct 16 ms 19200 KB Output is correct
4 Correct 15 ms 19200 KB Output is correct
5 Correct 16 ms 19200 KB Output is correct
6 Correct 16 ms 19200 KB Output is correct
7 Correct 16 ms 19200 KB Output is correct
8 Correct 15 ms 19200 KB Output is correct
9 Correct 15 ms 19200 KB Output is correct
10 Correct 15 ms 19200 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 19200 KB Output is correct
2 Correct 18 ms 19200 KB Output is correct
3 Correct 16 ms 19200 KB Output is correct
4 Correct 15 ms 19200 KB Output is correct
5 Correct 16 ms 19200 KB Output is correct
6 Correct 16 ms 19200 KB Output is correct
7 Correct 16 ms 19200 KB Output is correct
8 Correct 15 ms 19200 KB Output is correct
9 Correct 15 ms 19200 KB Output is correct
10 Correct 15 ms 19200 KB Output is correct
11 Correct 19 ms 19456 KB Output is correct
12 Correct 19 ms 19456 KB Output is correct
13 Correct 19 ms 19456 KB Output is correct
14 Correct 19 ms 19456 KB Output is correct
15 Correct 22 ms 19468 KB Output is correct
16 Correct 19 ms 19456 KB Output is correct
17 Correct 18 ms 19584 KB Output is correct
18 Correct 18 ms 19584 KB Output is correct
19 Correct 18 ms 19584 KB Output is correct
20 Correct 18 ms 19584 KB Output is correct
21 Correct 18 ms 19584 KB Output is correct
22 Correct 19 ms 19632 KB Output is correct
23 Correct 21 ms 19580 KB Output is correct
24 Correct 20 ms 19584 KB Output is correct
25 Correct 18 ms 19584 KB Output is correct
26 Correct 19 ms 19584 KB Output is correct
27 Correct 21 ms 19584 KB Output is correct
28 Correct 19 ms 19584 KB Output is correct
29 Correct 19 ms 19584 KB Output is correct
30 Correct 19 ms 19584 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1055 ms 68204 KB Output is correct
2 Correct 382 ms 68832 KB Output is correct
3 Correct 1092 ms 68012 KB Output is correct
4 Correct 383 ms 68576 KB Output is correct
5 Correct 1034 ms 64732 KB Output is correct
6 Correct 372 ms 68700 KB Output is correct
7 Correct 1022 ms 64864 KB Output is correct
8 Correct 370 ms 67816 KB Output is correct
9 Correct 1063 ms 62812 KB Output is correct
10 Correct 1104 ms 60188 KB Output is correct
11 Correct 1160 ms 63068 KB Output is correct
12 Correct 1186 ms 65724 KB Output is correct
13 Correct 1111 ms 59820 KB Output is correct
14 Correct 1151 ms 66096 KB Output is correct
15 Correct 1157 ms 65828 KB Output is correct
16 Correct 1156 ms 60820 KB Output is correct
17 Correct 1143 ms 61492 KB Output is correct
18 Correct 1151 ms 61544 KB Output is correct
19 Correct 1128 ms 64780 KB Output is correct
20 Correct 1136 ms 66860 KB Output is correct
21 Correct 16 ms 19232 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 16 ms 19200 KB Output is correct
2 Correct 18 ms 19200 KB Output is correct
3 Correct 16 ms 19200 KB Output is correct
4 Correct 15 ms 19200 KB Output is correct
5 Correct 16 ms 19200 KB Output is correct
6 Correct 16 ms 19200 KB Output is correct
7 Correct 16 ms 19200 KB Output is correct
8 Correct 15 ms 19200 KB Output is correct
9 Correct 15 ms 19200 KB Output is correct
10 Correct 15 ms 19200 KB Output is correct
11 Correct 19 ms 19456 KB Output is correct
12 Correct 19 ms 19456 KB Output is correct
13 Correct 19 ms 19456 KB Output is correct
14 Correct 19 ms 19456 KB Output is correct
15 Correct 22 ms 19468 KB Output is correct
16 Correct 19 ms 19456 KB Output is correct
17 Correct 18 ms 19584 KB Output is correct
18 Correct 18 ms 19584 KB Output is correct
19 Correct 18 ms 19584 KB Output is correct
20 Correct 18 ms 19584 KB Output is correct
21 Correct 18 ms 19584 KB Output is correct
22 Correct 19 ms 19632 KB Output is correct
23 Correct 21 ms 19580 KB Output is correct
24 Correct 20 ms 19584 KB Output is correct
25 Correct 18 ms 19584 KB Output is correct
26 Correct 19 ms 19584 KB Output is correct
27 Correct 21 ms 19584 KB Output is correct
28 Correct 19 ms 19584 KB Output is correct
29 Correct 19 ms 19584 KB Output is correct
30 Correct 19 ms 19584 KB Output is correct
31 Correct 1055 ms 68204 KB Output is correct
32 Correct 382 ms 68832 KB Output is correct
33 Correct 1092 ms 68012 KB Output is correct
34 Correct 383 ms 68576 KB Output is correct
35 Correct 1034 ms 64732 KB Output is correct
36 Correct 372 ms 68700 KB Output is correct
37 Correct 1022 ms 64864 KB Output is correct
38 Correct 370 ms 67816 KB Output is correct
39 Correct 1063 ms 62812 KB Output is correct
40 Correct 1104 ms 60188 KB Output is correct
41 Correct 1160 ms 63068 KB Output is correct
42 Correct 1186 ms 65724 KB Output is correct
43 Correct 1111 ms 59820 KB Output is correct
44 Correct 1151 ms 66096 KB Output is correct
45 Correct 1157 ms 65828 KB Output is correct
46 Correct 1156 ms 60820 KB Output is correct
47 Correct 1143 ms 61492 KB Output is correct
48 Correct 1151 ms 61544 KB Output is correct
49 Correct 1128 ms 64780 KB Output is correct
50 Correct 1136 ms 66860 KB Output is correct
51 Correct 16 ms 19232 KB Output is correct
52 Correct 842 ms 49824 KB Output is correct
53 Correct 927 ms 49860 KB Output is correct
54 Correct 868 ms 49816 KB Output is correct
55 Correct 887 ms 49824 KB Output is correct
56 Correct 898 ms 49856 KB Output is correct
57 Correct 903 ms 49836 KB Output is correct
58 Correct 903 ms 54512 KB Output is correct
59 Correct 884 ms 54748 KB Output is correct
60 Correct 1058 ms 54204 KB Output is correct
61 Correct 1153 ms 53804 KB Output is correct
62 Correct 398 ms 68568 KB Output is correct
63 Correct 398 ms 68704 KB Output is correct
64 Correct 392 ms 68064 KB Output is correct
65 Correct 386 ms 68684 KB Output is correct
66 Correct 766 ms 58868 KB Output is correct
67 Correct 784 ms 58724 KB Output is correct
68 Correct 794 ms 59004 KB Output is correct
69 Correct 808 ms 59136 KB Output is correct
70 Correct 807 ms 58980 KB Output is correct
71 Correct 810 ms 58984 KB Output is correct
72 Correct 800 ms 58896 KB Output is correct
73 Correct 799 ms 58236 KB Output is correct
74 Correct 794 ms 58984 KB Output is correct
75 Correct 832 ms 59048 KB Output is correct
76 Correct 980 ms 58752 KB Output is correct
77 Correct 876 ms 56928 KB Output is correct
78 Correct 1074 ms 61280 KB Output is correct
79 Correct 1064 ms 59224 KB Output is correct
80 Correct 1037 ms 66276 KB Output is correct
81 Correct 1060 ms 62944 KB Output is correct
82 Correct 1076 ms 63068 KB Output is correct
83 Correct 1041 ms 59608 KB Output is correct
84 Correct 1083 ms 65628 KB Output is correct
85 Correct 1099 ms 63992 KB Output is correct
86 Correct 1117 ms 59180 KB Output is correct
87 Correct 1106 ms 60924 KB Output is correct
88 Correct 1002 ms 62056 KB Output is correct
89 Correct 962 ms 57952 KB Output is correct
90 Correct 945 ms 57700 KB Output is correct
91 Correct 1028 ms 60312 KB Output is correct
92 Correct 1022 ms 58824 KB Output is correct
93 Correct 950 ms 58560 KB Output is correct
94 Correct 949 ms 57952 KB Output is correct
95 Correct 943 ms 59464 KB Output is correct
96 Correct 990 ms 58180 KB Output is correct
97 Correct 1002 ms 60248 KB Output is correct