Submission #218430

# Submission time Handle Problem Language Result Execution time Memory
218430 2020-04-02T07:19:51 Z GioChkhaidze Capital City (JOI20_capital_city) C++14
100 / 100
876 ms 35056 KB
#include <bits/stdc++.h>
#define pb push_back
using namespace std;

const int N=2e5+5;
int n,k,ans=1e9;
bool f[N],fix[N],lvl[N];
int c[N],P[N],Cf[N];
vector < int > v[N],s[N],h;

int Dfs(int x,int p,int sz,int &center) {
	int tot=1;
	Cf[c[x]]++;
	h.pb(x);
	for (int i=0; i<v[x].size(); i++) {
		int to=v[x][i];
		if (!lvl[to] && to!=p) 
			tot+=Dfs(to,x,sz,center); 
	}
	if (center==-1 && (2*tot>=sz || !p)) center=x;
	return tot;
}

void Go(int x,int p) {
	P[x]=p;
	for (int i=0; i<v[x].size(); i++) {
		int to=v[x][i];
		if (!lvl[to] && to!=p) Go(to,x);
	}
}

void Build(int x,int sz) {
	int center=-1;
	Dfs(x,0,sz,center);
	Go(center,0);
	queue < int > q;
	lvl[center]=1;
	q.push(center);
	int cn=0;
	while (!q.empty()) {
		int x=q.front();
		q.pop();
		if (fix[x]) continue;
		fix[x]=1;
		if (s[c[x]].size()!=Cf[c[x]]) {
			cn=1e9;
			while (!q.empty()) 
				q.pop();
			break;
		}
		if (!f[c[x]]) {
			cn++;
			f[c[x]]=1;
			for (int i=0; i<s[c[x]].size(); i++) 
				q.push(s[c[x]][i]);
		}
		if (P[x])
			q.push(P[x]);
	}

	for (int i=0; i<h.size(); i++) {
		int x=h[i];
		Cf[c[x]]=f[c[x]]=fix[x]=0;
	}
	h.clear();
	ans=min(ans,cn);
	for (int i=0; i<v[center].size(); i++) {
		int to=v[center][i];
		if (!lvl[to]) Build(to,sz/2);
	} 
}

main () {
	cin>>n>>k;
	
	for (int i=1; i<n; i++) {
		int a,b;
		cin>>a>>b;
		v[a].pb(b);
		v[b].pb(a);
	}
	
	for (int i=1; i<=n; i++) {
		cin>>c[i];
		s[c[i]].pb(i);
	}

	Build(1,n);
	--ans;
	cout<<ans<<endl;
}

Compilation message

capital_city.cpp: In function 'int Dfs(int, int, int, int&)':
capital_city.cpp:15:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<v[x].size(); i++) {
                ~^~~~~~~~~~~~
capital_city.cpp: In function 'void Go(int, int)':
capital_city.cpp:26:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<v[x].size(); i++) {
                ~^~~~~~~~~~~~
capital_city.cpp: In function 'void Build(int, int)':
capital_city.cpp:45:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if (s[c[x]].size()!=Cf[c[x]]) {
       ~~~~~~~~~~~~~~^~~~~~~~~~
capital_city.cpp:54:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    for (int i=0; i<s[c[x]].size(); i++) 
                  ~^~~~~~~~~~~~~~~
capital_city.cpp:61:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<h.size(); i++) {
                ~^~~~~~~~~
capital_city.cpp:67:17: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i=0; i<v[center].size(); i++) {
                ~^~~~~~~~~~~~~~~~~
capital_city.cpp: At global scope:
capital_city.cpp:73:7: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
 main () {
       ^
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 12 ms 9728 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 9 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 12 ms 9728 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 9 ms 9728 KB Output is correct
11 Correct 13 ms 9856 KB Output is correct
12 Correct 13 ms 9856 KB Output is correct
13 Correct 14 ms 9856 KB Output is correct
14 Correct 14 ms 9856 KB Output is correct
15 Correct 13 ms 9856 KB Output is correct
16 Correct 14 ms 9856 KB Output is correct
17 Correct 13 ms 9984 KB Output is correct
18 Correct 14 ms 9856 KB Output is correct
19 Correct 13 ms 9984 KB Output is correct
20 Correct 13 ms 9984 KB Output is correct
21 Correct 13 ms 9856 KB Output is correct
22 Correct 13 ms 9984 KB Output is correct
23 Correct 14 ms 9856 KB Output is correct
24 Correct 13 ms 9856 KB Output is correct
25 Correct 13 ms 9984 KB Output is correct
26 Correct 14 ms 9984 KB Output is correct
27 Correct 14 ms 9984 KB Output is correct
28 Correct 13 ms 9984 KB Output is correct
29 Correct 14 ms 9856 KB Output is correct
30 Correct 13 ms 9856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 737 ms 34536 KB Output is correct
2 Correct 443 ms 35048 KB Output is correct
3 Correct 741 ms 34540 KB Output is correct
4 Correct 435 ms 34920 KB Output is correct
5 Correct 745 ms 31852 KB Output is correct
6 Correct 451 ms 35020 KB Output is correct
7 Correct 765 ms 32240 KB Output is correct
8 Correct 435 ms 34668 KB Output is correct
9 Correct 820 ms 30568 KB Output is correct
10 Correct 824 ms 28652 KB Output is correct
11 Correct 820 ms 30908 KB Output is correct
12 Correct 826 ms 32908 KB Output is correct
13 Correct 835 ms 28268 KB Output is correct
14 Correct 834 ms 33192 KB Output is correct
15 Correct 834 ms 32948 KB Output is correct
16 Correct 843 ms 29040 KB Output is correct
17 Correct 850 ms 29420 KB Output is correct
18 Correct 831 ms 29544 KB Output is correct
19 Correct 829 ms 31980 KB Output is correct
20 Correct 848 ms 33924 KB Output is correct
21 Correct 10 ms 9728 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 10 ms 9728 KB Output is correct
3 Correct 10 ms 9728 KB Output is correct
4 Correct 10 ms 9728 KB Output is correct
5 Correct 10 ms 9728 KB Output is correct
6 Correct 12 ms 9728 KB Output is correct
7 Correct 10 ms 9728 KB Output is correct
8 Correct 10 ms 9728 KB Output is correct
9 Correct 10 ms 9728 KB Output is correct
10 Correct 9 ms 9728 KB Output is correct
11 Correct 13 ms 9856 KB Output is correct
12 Correct 13 ms 9856 KB Output is correct
13 Correct 14 ms 9856 KB Output is correct
14 Correct 14 ms 9856 KB Output is correct
15 Correct 13 ms 9856 KB Output is correct
16 Correct 14 ms 9856 KB Output is correct
17 Correct 13 ms 9984 KB Output is correct
18 Correct 14 ms 9856 KB Output is correct
19 Correct 13 ms 9984 KB Output is correct
20 Correct 13 ms 9984 KB Output is correct
21 Correct 13 ms 9856 KB Output is correct
22 Correct 13 ms 9984 KB Output is correct
23 Correct 14 ms 9856 KB Output is correct
24 Correct 13 ms 9856 KB Output is correct
25 Correct 13 ms 9984 KB Output is correct
26 Correct 14 ms 9984 KB Output is correct
27 Correct 14 ms 9984 KB Output is correct
28 Correct 13 ms 9984 KB Output is correct
29 Correct 14 ms 9856 KB Output is correct
30 Correct 13 ms 9856 KB Output is correct
31 Correct 737 ms 34536 KB Output is correct
32 Correct 443 ms 35048 KB Output is correct
33 Correct 741 ms 34540 KB Output is correct
34 Correct 435 ms 34920 KB Output is correct
35 Correct 745 ms 31852 KB Output is correct
36 Correct 451 ms 35020 KB Output is correct
37 Correct 765 ms 32240 KB Output is correct
38 Correct 435 ms 34668 KB Output is correct
39 Correct 820 ms 30568 KB Output is correct
40 Correct 824 ms 28652 KB Output is correct
41 Correct 820 ms 30908 KB Output is correct
42 Correct 826 ms 32908 KB Output is correct
43 Correct 835 ms 28268 KB Output is correct
44 Correct 834 ms 33192 KB Output is correct
45 Correct 834 ms 32948 KB Output is correct
46 Correct 843 ms 29040 KB Output is correct
47 Correct 850 ms 29420 KB Output is correct
48 Correct 831 ms 29544 KB Output is correct
49 Correct 829 ms 31980 KB Output is correct
50 Correct 848 ms 33924 KB Output is correct
51 Correct 10 ms 9728 KB Output is correct
52 Correct 746 ms 21228 KB Output is correct
53 Correct 800 ms 21356 KB Output is correct
54 Correct 836 ms 21328 KB Output is correct
55 Correct 767 ms 21224 KB Output is correct
56 Correct 772 ms 21372 KB Output is correct
57 Correct 782 ms 21352 KB Output is correct
58 Correct 710 ms 23964 KB Output is correct
59 Correct 677 ms 24164 KB Output is correct
60 Correct 797 ms 23916 KB Output is correct
61 Correct 833 ms 23440 KB Output is correct
62 Correct 447 ms 35044 KB Output is correct
63 Correct 456 ms 35048 KB Output is correct
64 Correct 455 ms 34672 KB Output is correct
65 Correct 444 ms 35056 KB Output is correct
66 Correct 561 ms 28044 KB Output is correct
67 Correct 554 ms 27964 KB Output is correct
68 Correct 563 ms 28076 KB Output is correct
69 Correct 563 ms 28076 KB Output is correct
70 Correct 562 ms 28076 KB Output is correct
71 Correct 563 ms 28196 KB Output is correct
72 Correct 564 ms 28072 KB Output is correct
73 Correct 571 ms 27520 KB Output is correct
74 Correct 582 ms 28076 KB Output is correct
75 Correct 547 ms 28072 KB Output is correct
76 Correct 784 ms 27112 KB Output is correct
77 Correct 807 ms 25780 KB Output is correct
78 Correct 834 ms 29300 KB Output is correct
79 Correct 869 ms 27628 KB Output is correct
80 Correct 836 ms 33644 KB Output is correct
81 Correct 845 ms 30700 KB Output is correct
82 Correct 874 ms 30700 KB Output is correct
83 Correct 873 ms 28012 KB Output is correct
84 Correct 876 ms 32800 KB Output is correct
85 Correct 839 ms 31536 KB Output is correct
86 Correct 874 ms 27756 KB Output is correct
87 Correct 840 ms 29172 KB Output is correct
88 Correct 788 ms 29928 KB Output is correct
89 Correct 779 ms 26864 KB Output is correct
90 Correct 823 ms 26732 KB Output is correct
91 Correct 786 ms 28652 KB Output is correct
92 Correct 765 ms 27480 KB Output is correct
93 Correct 760 ms 27376 KB Output is correct
94 Correct 775 ms 26860 KB Output is correct
95 Correct 762 ms 28012 KB Output is correct
96 Correct 766 ms 27024 KB Output is correct
97 Correct 779 ms 28672 KB Output is correct