Submission #1104981

# Submission time Handle Problem Language Result Execution time Memory
1104981 2024-10-25T03:26:37 Z hiensumi Capital City (JOI20_capital_city) C++14
100 / 100
484 ms 74940 KB
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
 
using namespace std;

typedef pair<int,int>P;
#define SZ 500005
#define CNUM 500005
#define INF 1000000000

int n,k,col[SZ];
vector<int>edge[SZ];
vector<int>vertex[CNUM];
bool rem[SZ];
int sub_size[SZ];
int ans = 1000000000;
bool check_col[CNUM];
int up[SZ];
vector<int>cur_vertex, cur_list[CNUM];

int make(int v,int u){
	int cnt = 1;
	up[v] = u;
	check_col[col[v]] = 0;
	cur_vertex.push_back(v);
	cur_list[col[v]].clear();
	for(int i=0;i<edge[v].size();i++){
		int to = edge[v][i];
		if(rem[to] || to == u) continue;
		cnt += make(to,v);
	}
	return sub_size[v] = cnt;
}
P search(int v,int u,int cur_size){
	P ret = P(1000000000,-1);
	int sum=1,mx=0;
	for(int i=0;i<edge[v].size();i++){
		int to = edge[v][i];
		if(rem[to] || to == u) continue;
		ret = min(ret,search(to,v,cur_size));
		mx = max(mx,sub_size[to]);
		sum += sub_size[to];
	}
	mx = max(mx,cur_size-sum);
	ret = min(ret,P(mx,v));
	return ret;
}
void sol(int v){
	make(v,-1);
	cur_vertex.clear();
	int cent = search(v,-1,sub_size[v]).second;
	make(cent,-1);
	rem[cent] = true;
	int sz = cur_vertex.size();
	for(int i=0;i<sz;i++) cur_list[col[cur_vertex[i]]].push_back(cur_vertex[i]);
	queue<int>que;
	que.push(col[cent]);
	check_col[col[cent]] = 1;
	int use = 0;
	while(!que.empty()){
		int c = que.front(); que.pop();
		use++;
		if(cur_list[c].size() != vertex[c].size()){
			use = INF;
			break;
		}
		for(int i=0;i<cur_list[c].size();i++){
			int look = cur_list[c][i];
			if(up[look] == -1) continue;
			int nw = col[up[look]];
			if(!check_col[nw]){
				que.push(nw);
				check_col[nw] = 1;
			}
		}
	}
	ans = min(ans,use);
	for(int i=0;i<edge[cent].size();i++){
		if(!rem[edge[cent][i]]) sol(edge[cent][i]);
	}
	rem[cent] = false;
}
int main(){
	scanf("%d %d",&n,&k);
	for(int i=1;i<n;i++){
		int a,b; scanf("%d%d",&a,&b);
		edge[a].push_back(b);
		edge[b].push_back(a);
	}
	for(int i=1;i<=n;i++){
		scanf("%d",&col[i]);
		vertex[col[i]].push_back(i);
	}
	sol(1);
	assert(ans <= n);
	printf("%d\n",ans-1);
	return 0;
}

Compilation message

capital_city.cpp: In function 'int make(int, int)':
capital_city.cpp:32:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |  for(int i=0;i<edge[v].size();i++){
      |              ~^~~~~~~~~~~~~~~
capital_city.cpp: In function 'P search(int, int, int)':
capital_city.cpp:42:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   42 |  for(int i=0;i<edge[v].size();i++){
      |              ~^~~~~~~~~~~~~~~
capital_city.cpp: In function 'void sol(int)':
capital_city.cpp:72:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   72 |   for(int i=0;i<cur_list[c].size();i++){
      |               ~^~~~~~~~~~~~~~~~~~~
capital_city.cpp:83:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   83 |  for(int i=0;i<edge[cent].size();i++){
      |              ~^~~~~~~~~~~~~~~~~~
capital_city.cpp: In function 'int main()':
capital_city.cpp:89:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   89 |  scanf("%d %d",&n,&k);
      |  ~~~~~^~~~~~~~~~~~~~~
capital_city.cpp:91:17: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   91 |   int a,b; scanf("%d%d",&a,&b);
      |            ~~~~~^~~~~~~~~~~~~~
capital_city.cpp:96:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   96 |   scanf("%d",&col[i]);
      |   ~~~~~^~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 7 ms 39504 KB Output is correct
2 Correct 7 ms 39536 KB Output is correct
3 Correct 7 ms 39504 KB Output is correct
4 Correct 8 ms 39504 KB Output is correct
5 Correct 7 ms 39504 KB Output is correct
6 Correct 7 ms 39504 KB Output is correct
7 Correct 8 ms 39504 KB Output is correct
8 Correct 8 ms 39672 KB Output is correct
9 Correct 6 ms 39504 KB Output is correct
10 Correct 7 ms 41552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 39504 KB Output is correct
2 Correct 7 ms 39536 KB Output is correct
3 Correct 7 ms 39504 KB Output is correct
4 Correct 8 ms 39504 KB Output is correct
5 Correct 7 ms 39504 KB Output is correct
6 Correct 7 ms 39504 KB Output is correct
7 Correct 8 ms 39504 KB Output is correct
8 Correct 8 ms 39672 KB Output is correct
9 Correct 6 ms 39504 KB Output is correct
10 Correct 7 ms 41552 KB Output is correct
11 Correct 9 ms 39760 KB Output is correct
12 Correct 8 ms 39928 KB Output is correct
13 Correct 8 ms 39504 KB Output is correct
14 Correct 9 ms 39760 KB Output is correct
15 Correct 10 ms 39868 KB Output is correct
16 Correct 10 ms 39760 KB Output is correct
17 Correct 8 ms 39856 KB Output is correct
18 Correct 9 ms 39616 KB Output is correct
19 Correct 8 ms 39760 KB Output is correct
20 Correct 8 ms 39612 KB Output is correct
21 Correct 11 ms 39852 KB Output is correct
22 Correct 8 ms 39760 KB Output is correct
23 Correct 9 ms 39760 KB Output is correct
24 Correct 8 ms 39760 KB Output is correct
25 Correct 8 ms 39760 KB Output is correct
26 Correct 8 ms 39688 KB Output is correct
27 Correct 9 ms 39760 KB Output is correct
28 Correct 8 ms 39760 KB Output is correct
29 Correct 8 ms 39616 KB Output is correct
30 Correct 11 ms 39760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 389 ms 72656 KB Output is correct
2 Correct 227 ms 74824 KB Output is correct
3 Correct 431 ms 73916 KB Output is correct
4 Correct 216 ms 74796 KB Output is correct
5 Correct 426 ms 70856 KB Output is correct
6 Correct 213 ms 74784 KB Output is correct
7 Correct 372 ms 71036 KB Output is correct
8 Correct 211 ms 73900 KB Output is correct
9 Correct 465 ms 68448 KB Output is correct
10 Correct 438 ms 65980 KB Output is correct
11 Correct 438 ms 69052 KB Output is correct
12 Correct 453 ms 71536 KB Output is correct
13 Correct 471 ms 65468 KB Output is correct
14 Correct 464 ms 71936 KB Output is correct
15 Correct 417 ms 71676 KB Output is correct
16 Correct 424 ms 66440 KB Output is correct
17 Correct 414 ms 67260 KB Output is correct
18 Correct 407 ms 67340 KB Output is correct
19 Correct 384 ms 70588 KB Output is correct
20 Correct 394 ms 72892 KB Output is correct
21 Correct 9 ms 41552 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 39504 KB Output is correct
2 Correct 7 ms 39536 KB Output is correct
3 Correct 7 ms 39504 KB Output is correct
4 Correct 8 ms 39504 KB Output is correct
5 Correct 7 ms 39504 KB Output is correct
6 Correct 7 ms 39504 KB Output is correct
7 Correct 8 ms 39504 KB Output is correct
8 Correct 8 ms 39672 KB Output is correct
9 Correct 6 ms 39504 KB Output is correct
10 Correct 7 ms 41552 KB Output is correct
11 Correct 9 ms 39760 KB Output is correct
12 Correct 8 ms 39928 KB Output is correct
13 Correct 8 ms 39504 KB Output is correct
14 Correct 9 ms 39760 KB Output is correct
15 Correct 10 ms 39868 KB Output is correct
16 Correct 10 ms 39760 KB Output is correct
17 Correct 8 ms 39856 KB Output is correct
18 Correct 9 ms 39616 KB Output is correct
19 Correct 8 ms 39760 KB Output is correct
20 Correct 8 ms 39612 KB Output is correct
21 Correct 11 ms 39852 KB Output is correct
22 Correct 8 ms 39760 KB Output is correct
23 Correct 9 ms 39760 KB Output is correct
24 Correct 8 ms 39760 KB Output is correct
25 Correct 8 ms 39760 KB Output is correct
26 Correct 8 ms 39688 KB Output is correct
27 Correct 9 ms 39760 KB Output is correct
28 Correct 8 ms 39760 KB Output is correct
29 Correct 8 ms 39616 KB Output is correct
30 Correct 11 ms 39760 KB Output is correct
31 Correct 389 ms 72656 KB Output is correct
32 Correct 227 ms 74824 KB Output is correct
33 Correct 431 ms 73916 KB Output is correct
34 Correct 216 ms 74796 KB Output is correct
35 Correct 426 ms 70856 KB Output is correct
36 Correct 213 ms 74784 KB Output is correct
37 Correct 372 ms 71036 KB Output is correct
38 Correct 211 ms 73900 KB Output is correct
39 Correct 465 ms 68448 KB Output is correct
40 Correct 438 ms 65980 KB Output is correct
41 Correct 438 ms 69052 KB Output is correct
42 Correct 453 ms 71536 KB Output is correct
43 Correct 471 ms 65468 KB Output is correct
44 Correct 464 ms 71936 KB Output is correct
45 Correct 417 ms 71676 KB Output is correct
46 Correct 424 ms 66440 KB Output is correct
47 Correct 414 ms 67260 KB Output is correct
48 Correct 407 ms 67340 KB Output is correct
49 Correct 384 ms 70588 KB Output is correct
50 Correct 394 ms 72892 KB Output is correct
51 Correct 9 ms 41552 KB Output is correct
52 Correct 307 ms 53848 KB Output is correct
53 Correct 284 ms 53948 KB Output is correct
54 Correct 275 ms 56000 KB Output is correct
55 Correct 312 ms 53948 KB Output is correct
56 Correct 314 ms 54048 KB Output is correct
57 Correct 305 ms 53812 KB Output is correct
58 Correct 310 ms 60096 KB Output is correct
59 Correct 323 ms 60860 KB Output is correct
60 Correct 405 ms 60604 KB Output is correct
61 Correct 365 ms 60160 KB Output is correct
62 Correct 216 ms 74820 KB Output is correct
63 Correct 225 ms 74940 KB Output is correct
64 Correct 213 ms 74280 KB Output is correct
65 Correct 217 ms 74692 KB Output is correct
66 Correct 244 ms 65404 KB Output is correct
67 Correct 258 ms 65448 KB Output is correct
68 Correct 224 ms 65404 KB Output is correct
69 Correct 245 ms 65404 KB Output is correct
70 Correct 280 ms 65416 KB Output is correct
71 Correct 266 ms 65404 KB Output is correct
72 Correct 235 ms 65400 KB Output is correct
73 Correct 264 ms 64496 KB Output is correct
74 Correct 212 ms 65536 KB Output is correct
75 Correct 205 ms 65504 KB Output is correct
76 Correct 314 ms 64956 KB Output is correct
77 Correct 327 ms 61116 KB Output is correct
78 Correct 415 ms 67264 KB Output is correct
79 Correct 369 ms 65272 KB Output is correct
80 Correct 413 ms 72060 KB Output is correct
81 Correct 438 ms 68552 KB Output is correct
82 Correct 484 ms 68800 KB Output is correct
83 Correct 420 ms 65468 KB Output is correct
84 Correct 444 ms 71356 KB Output is correct
85 Correct 465 ms 69820 KB Output is correct
86 Correct 422 ms 64956 KB Output is correct
87 Correct 418 ms 66752 KB Output is correct
88 Correct 378 ms 68028 KB Output is correct
89 Correct 428 ms 63932 KB Output is correct
90 Correct 430 ms 63744 KB Output is correct
91 Correct 432 ms 66236 KB Output is correct
92 Correct 388 ms 64768 KB Output is correct
93 Correct 350 ms 64588 KB Output is correct
94 Correct 365 ms 63840 KB Output is correct
95 Correct 374 ms 65224 KB Output is correct
96 Correct 396 ms 63968 KB Output is correct
97 Correct 343 ms 65980 KB Output is correct