Submission #217079

# Submission time Handle Problem Language Result Execution time Memory
217079 2020-03-28T22:48:27 Z Pajaraja Capital City (JOI20_capital_city) C++17
100 / 100
729 ms 34296 KB
#include <bits/stdc++.h>
#define MAXN 200007
using namespace std;
int c[MAXN],ccnt[MAXN],p[MAXN],sz,ctr,rez=1000000000,br,n,k;
bool vi[MAXN],vc[MAXN],fas,vb[MAXN];
vector<int> g[MAXN],b[MAXN];
queue<int> q;
void dfssize(int s,int f)
{
	sz++; vi[s]=false; vb[c[s]]=false; ccnt[c[s]]=0;
	for(int i=0;i<g[s].size();i++) if(g[s][i]!=f && !vc[g[s][i]]) dfssize(g[s][i],s);
}
int fndctr(int s,int f)
{
	int sum=1;
	bool mb=true;
	for(int i=0;i<g[s].size();i++)  if(g[s][i]!=f && !vc[g[s][i]])
	{
		int x=fndctr(g[s][i],s);
		if(x>sz/2) mb=false;
		else sum+=x;
	}
	if(sz-sum>sz/2) mb=false;
	if(mb) ctr=s;
	return sum;
}
void dfsctr(int s,int f)
{
	ccnt[c[s]]++;
	p[s]=f;
	for(int i=0;i<g[s].size();i++) if(g[s][i]!=f && !vc[g[s][i]]) dfsctr(g[s][i],s);
}
void dodajboju(int co)
{
	if(vb[co] || fas) return;
	if(ccnt[co]!=b[co].size()) {fas=true; return;}
	vb[co]=true; br++;
	for(int i=0;i<b[co].size();i++) q.push(b[co][i]);
}
void dizanje(int s)
{
	if(vi[s] || fas) return;
	vi[s]=true;
	dodajboju(c[s]);
	dizanje(p[s]);
}
void centrodecomp(int s)
{
	sz=0; dfssize(s,s); fndctr(s,s);
	int cent=ctr;
	dfsctr(cent,cent); br=0; vc[cent]=true; fas=false;
	vi[cent]=true; dodajboju(c[cent]);
	while(!fas && !q.empty())
	{
		int u=q.front();
		dizanje(u);
		q.pop();
	}
	while(!q.empty()) q.pop();
	if(!fas) rez=min(rez,br);
	for(int i=0;i<g[cent].size();i++) if(!vc[g[cent][i]]) centrodecomp(g[cent][i]);
}
int main()
{
	scanf("%d%d",&n,&k);
	for(int i=0;i<n-1;i++)
	{
		int t1,t2;
		scanf("%d%d",&t1,&t2);
		g[t1].push_back(t2);
		g[t2].push_back(t1);
	}
	for(int i=1;i<=n;i++) scanf("%d",&c[i]);
	for(int i=1;i<=n;i++) b[c[i]].push_back(i);
	centrodecomp(1);
	printf("%d",rez-1);
}

Compilation message

capital_city.cpp: In function 'void dfssize(int, int)':
capital_city.cpp:11:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<g[s].size();i++) if(g[s][i]!=f && !vc[g[s][i]]) dfssize(g[s][i],s);
              ~^~~~~~~~~~~~
capital_city.cpp: In function 'int fndctr(int, int)':
capital_city.cpp:17:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<g[s].size();i++)  if(g[s][i]!=f && !vc[g[s][i]])
              ~^~~~~~~~~~~~
capital_city.cpp: In function 'void dfsctr(int, int)':
capital_city.cpp:31:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<g[s].size();i++) if(g[s][i]!=f && !vc[g[s][i]]) dfsctr(g[s][i],s);
              ~^~~~~~~~~~~~
capital_city.cpp: In function 'void dodajboju(int)':
capital_city.cpp:36:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  if(ccnt[co]!=b[co].size()) {fas=true; return;}
     ~~~~~~~~^~~~~~~~~~~~~~
capital_city.cpp:38:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<b[co].size();i++) q.push(b[co][i]);
              ~^~~~~~~~~~~~~
capital_city.cpp: In function 'void centrodecomp(int)':
capital_city.cpp:61:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for(int i=0;i<g[cent].size();i++) if(!vc[g[cent][i]]) centrodecomp(g[cent][i]);
              ~^~~~~~~~~~~~~~~
capital_city.cpp: In function 'int main()':
capital_city.cpp:65:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d",&n,&k);
  ~~~~~^~~~~~~~~~~~~~
capital_city.cpp:69:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d%d",&t1,&t2);
   ~~~~~^~~~~~~~~~~~~~~~
capital_city.cpp:73:29: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for(int i=1;i<=n;i++) scanf("%d",&c[i]);
                        ~~~~~^~~~~~~~~~~~
# 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 10 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 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 10 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 10 ms 9728 KB Output is correct
11 Correct 12 ms 9856 KB Output is correct
12 Correct 13 ms 9856 KB Output is correct
13 Correct 12 ms 9856 KB Output is correct
14 Correct 12 ms 9856 KB Output is correct
15 Correct 15 ms 9984 KB Output is correct
16 Correct 12 ms 9856 KB Output is correct
17 Correct 12 ms 9984 KB Output is correct
18 Correct 11 ms 9856 KB Output is correct
19 Correct 11 ms 9856 KB Output is correct
20 Correct 11 ms 9984 KB Output is correct
21 Correct 12 ms 9856 KB Output is correct
22 Correct 12 ms 9984 KB Output is correct
23 Correct 12 ms 9984 KB Output is correct
24 Correct 12 ms 9856 KB Output is correct
25 Correct 12 ms 9856 KB Output is correct
26 Correct 12 ms 9984 KB Output is correct
27 Correct 12 ms 9984 KB Output is correct
28 Correct 12 ms 9856 KB Output is correct
29 Correct 12 ms 9856 KB Output is correct
30 Correct 12 ms 9856 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 572 ms 33632 KB Output is correct
2 Correct 247 ms 34168 KB Output is correct
3 Correct 570 ms 33376 KB Output is correct
4 Correct 243 ms 34168 KB Output is correct
5 Correct 571 ms 31224 KB Output is correct
6 Correct 253 ms 34296 KB Output is correct
7 Correct 608 ms 31456 KB Output is correct
8 Correct 252 ms 33764 KB Output is correct
9 Correct 709 ms 29880 KB Output is correct
10 Correct 692 ms 27864 KB Output is correct
11 Correct 703 ms 30272 KB Output is correct
12 Correct 718 ms 32280 KB Output is correct
13 Correct 718 ms 27432 KB Output is correct
14 Correct 714 ms 32452 KB Output is correct
15 Correct 716 ms 32320 KB Output is correct
16 Correct 697 ms 28212 KB Output is correct
17 Correct 715 ms 28896 KB Output is correct
18 Correct 709 ms 29052 KB Output is correct
19 Correct 711 ms 31540 KB Output is correct
20 Correct 705 ms 33296 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 10 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 10 ms 9728 KB Output is correct
11 Correct 12 ms 9856 KB Output is correct
12 Correct 13 ms 9856 KB Output is correct
13 Correct 12 ms 9856 KB Output is correct
14 Correct 12 ms 9856 KB Output is correct
15 Correct 15 ms 9984 KB Output is correct
16 Correct 12 ms 9856 KB Output is correct
17 Correct 12 ms 9984 KB Output is correct
18 Correct 11 ms 9856 KB Output is correct
19 Correct 11 ms 9856 KB Output is correct
20 Correct 11 ms 9984 KB Output is correct
21 Correct 12 ms 9856 KB Output is correct
22 Correct 12 ms 9984 KB Output is correct
23 Correct 12 ms 9984 KB Output is correct
24 Correct 12 ms 9856 KB Output is correct
25 Correct 12 ms 9856 KB Output is correct
26 Correct 12 ms 9984 KB Output is correct
27 Correct 12 ms 9984 KB Output is correct
28 Correct 12 ms 9856 KB Output is correct
29 Correct 12 ms 9856 KB Output is correct
30 Correct 12 ms 9856 KB Output is correct
31 Correct 572 ms 33632 KB Output is correct
32 Correct 247 ms 34168 KB Output is correct
33 Correct 570 ms 33376 KB Output is correct
34 Correct 243 ms 34168 KB Output is correct
35 Correct 571 ms 31224 KB Output is correct
36 Correct 253 ms 34296 KB Output is correct
37 Correct 608 ms 31456 KB Output is correct
38 Correct 252 ms 33764 KB Output is correct
39 Correct 709 ms 29880 KB Output is correct
40 Correct 692 ms 27864 KB Output is correct
41 Correct 703 ms 30272 KB Output is correct
42 Correct 718 ms 32280 KB Output is correct
43 Correct 718 ms 27432 KB Output is correct
44 Correct 714 ms 32452 KB Output is correct
45 Correct 716 ms 32320 KB Output is correct
46 Correct 697 ms 28212 KB Output is correct
47 Correct 715 ms 28896 KB Output is correct
48 Correct 709 ms 29052 KB Output is correct
49 Correct 711 ms 31540 KB Output is correct
50 Correct 705 ms 33296 KB Output is correct
51 Correct 10 ms 9728 KB Output is correct
52 Correct 504 ms 20412 KB Output is correct
53 Correct 573 ms 20668 KB Output is correct
54 Correct 558 ms 20408 KB Output is correct
55 Correct 558 ms 20408 KB Output is correct
56 Correct 567 ms 20664 KB Output is correct
57 Correct 558 ms 20536 KB Output is correct
58 Correct 509 ms 23160 KB Output is correct
59 Correct 488 ms 23392 KB Output is correct
60 Correct 620 ms 22904 KB Output is correct
61 Correct 672 ms 22648 KB Output is correct
62 Correct 237 ms 34040 KB Output is correct
63 Correct 242 ms 34040 KB Output is correct
64 Correct 245 ms 33916 KB Output is correct
65 Correct 270 ms 34016 KB Output is correct
66 Correct 360 ms 27248 KB Output is correct
67 Correct 356 ms 27120 KB Output is correct
68 Correct 357 ms 27248 KB Output is correct
69 Correct 377 ms 27252 KB Output is correct
70 Correct 370 ms 27252 KB Output is correct
71 Correct 366 ms 27260 KB Output is correct
72 Correct 360 ms 27248 KB Output is correct
73 Correct 364 ms 26620 KB Output is correct
74 Correct 357 ms 27224 KB Output is correct
75 Correct 378 ms 27376 KB Output is correct
76 Correct 625 ms 26156 KB Output is correct
77 Correct 628 ms 24696 KB Output is correct
78 Correct 729 ms 28740 KB Output is correct
79 Correct 721 ms 27120 KB Output is correct
80 Correct 714 ms 32820 KB Output is correct
81 Correct 695 ms 29944 KB Output is correct
82 Correct 708 ms 30012 KB Output is correct
83 Correct 722 ms 27400 KB Output is correct
84 Correct 724 ms 32188 KB Output is correct
85 Correct 717 ms 30892 KB Output is correct
86 Correct 711 ms 27032 KB Output is correct
87 Correct 697 ms 28420 KB Output is correct
88 Correct 609 ms 29324 KB Output is correct
89 Correct 619 ms 26060 KB Output is correct
90 Correct 617 ms 26104 KB Output is correct
91 Correct 617 ms 27852 KB Output is correct
92 Correct 627 ms 26708 KB Output is correct
93 Correct 622 ms 26616 KB Output is correct
94 Correct 610 ms 25980 KB Output is correct
95 Correct 614 ms 27264 KB Output is correct
96 Correct 624 ms 26232 KB Output is correct
97 Correct 624 ms 27768 KB Output is correct