Submission #246082

# Submission time Handle Problem Language Result Execution time Memory
246082 2020-07-08T07:06:33 Z onjo0127 Capital City (JOI20_capital_city) C++11
100 / 100
1274 ms 35708 KB
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;

int C[200009], pr[200009], sb[200009], stt[200009], ans = INF;
vector<int> G[200009], S[200009];
bool chk[200009], use[200009];

void dfs(int x, int p) {
	pr[x] = p;
	sb[x] = 1;
	for(auto& it: G[x]) if(it != p && !chk[it]) {
		dfs(it, x);
		sb[x] += sb[it];
	}
}

int cent(int rt) {
	int x = rt, p = rt;
	while(1) {
		int mxi = -1;
		for(auto& it: G[x]) if(it != p && !chk[it]) {
			if(mxi == -1 || sb[mxi] < sb[it]) mxi = it;
		}
		if(mxi == -1 || sb[mxi]*2 <= sb[rt]) return x;
		p = x; x = mxi;
	}
}

void col(int x, int p, int c) {
	stt[x] = c;
	for(auto& it: G[x]) if(it != p && !chk[it]) col(it, x, c);
}

void go(int rt) {
	col(rt, rt, 2);
	dfs(rt, rt);
	int ctr = cent(rt);
	dfs(ctr, ctr);
	vector<int> U;
	queue<int> que; que.push(C[ctr]);
	use[C[ctr]] = 1; U.push_back(C[ctr]);
	while(que.size()) {
		int nc = que.front(); que.pop();
		for(auto& it: S[nc]) {
			int x = it;
			if(stt[x] == 0) goto abt;
			while(pr[x] != x) {
				if(stt[x] == 0) goto abt;
				if(stt[x] == 1) break;
				stt[x] = 1;
				if(!use[C[x]]) {
					use[C[x]] = 1;
					U.push_back(C[x]);
					que.push(C[x]);
				}
				x = pr[x];
			}
		}
	}
	ans = min(ans, (int)U.size() - 1);
	abt:
	for(auto& it: U) use[it] = 0;
	col(rt, rt, 0);
	chk[ctr] = 1;
	for(auto& it: G[ctr]) if(!chk[it]) go(it);
}

int main() {
	int N, K; scanf("%d%d", &N, &K);
	for(int i=0; i<N-1; i++) {
		int u, v; scanf("%d%d",&u,&v);
		G[u].push_back(v);
		G[v].push_back(u);
	}
	for(int i=1; i<=N; i++) {
		scanf("%d", &C[i]);
		S[C[i]].push_back(i);
	}
	go(1);
	printf("%d", ans);
	return 0;
}

Compilation message

capital_city.cpp: In function 'int main()':
capital_city.cpp:70:17: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  int N, K; scanf("%d%d", &N, &K);
            ~~~~~^~~~~~~~~~~~~~~~
capital_city.cpp:72:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int u, v; scanf("%d%d",&u,&v);
             ~~~~~^~~~~~~~~~~~~~
capital_city.cpp:77:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%d", &C[i]);
   ~~~~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 10 ms 9728 KB Output is correct
2 Correct 12 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 12 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 13 ms 9856 KB Output is correct
12 Correct 12 ms 9856 KB Output is correct
13 Correct 13 ms 9984 KB Output is correct
14 Correct 12 ms 9856 KB Output is correct
15 Correct 16 ms 9856 KB Output is correct
16 Correct 13 ms 9856 KB Output is correct
17 Correct 13 ms 9984 KB Output is correct
18 Correct 12 ms 9984 KB Output is correct
19 Correct 12 ms 9984 KB Output is correct
20 Correct 12 ms 9984 KB Output is correct
21 Correct 12 ms 9984 KB Output is correct
22 Correct 13 ms 9984 KB Output is correct
23 Correct 13 ms 9984 KB Output is correct
24 Correct 14 ms 9856 KB Output is correct
25 Correct 14 ms 9984 KB Output is correct
26 Correct 14 ms 10112 KB Output is correct
27 Correct 13 ms 9984 KB Output is correct
28 Correct 13 ms 9984 KB Output is correct
29 Correct 13 ms 9984 KB Output is correct
30 Correct 13 ms 9984 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 860 ms 35448 KB Output is correct
2 Correct 305 ms 35704 KB Output is correct
3 Correct 889 ms 35192 KB Output is correct
4 Correct 299 ms 35708 KB Output is correct
5 Correct 824 ms 33400 KB Output is correct
6 Correct 294 ms 35704 KB Output is correct
7 Correct 791 ms 33656 KB Output is correct
8 Correct 309 ms 35444 KB Output is correct
9 Correct 976 ms 32452 KB Output is correct
10 Correct 994 ms 31096 KB Output is correct
11 Correct 991 ms 32648 KB Output is correct
12 Correct 1274 ms 34296 KB Output is correct
13 Correct 954 ms 30712 KB Output is correct
14 Correct 942 ms 34452 KB Output is correct
15 Correct 952 ms 34488 KB Output is correct
16 Correct 945 ms 31124 KB Output is correct
17 Correct 949 ms 31608 KB Output is correct
18 Correct 965 ms 31800 KB Output is correct
19 Correct 933 ms 33656 KB Output is correct
20 Correct 983 ms 35064 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 12 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 13 ms 9856 KB Output is correct
12 Correct 12 ms 9856 KB Output is correct
13 Correct 13 ms 9984 KB Output is correct
14 Correct 12 ms 9856 KB Output is correct
15 Correct 16 ms 9856 KB Output is correct
16 Correct 13 ms 9856 KB Output is correct
17 Correct 13 ms 9984 KB Output is correct
18 Correct 12 ms 9984 KB Output is correct
19 Correct 12 ms 9984 KB Output is correct
20 Correct 12 ms 9984 KB Output is correct
21 Correct 12 ms 9984 KB Output is correct
22 Correct 13 ms 9984 KB Output is correct
23 Correct 13 ms 9984 KB Output is correct
24 Correct 14 ms 9856 KB Output is correct
25 Correct 14 ms 9984 KB Output is correct
26 Correct 14 ms 10112 KB Output is correct
27 Correct 13 ms 9984 KB Output is correct
28 Correct 13 ms 9984 KB Output is correct
29 Correct 13 ms 9984 KB Output is correct
30 Correct 13 ms 9984 KB Output is correct
31 Correct 860 ms 35448 KB Output is correct
32 Correct 305 ms 35704 KB Output is correct
33 Correct 889 ms 35192 KB Output is correct
34 Correct 299 ms 35708 KB Output is correct
35 Correct 824 ms 33400 KB Output is correct
36 Correct 294 ms 35704 KB Output is correct
37 Correct 791 ms 33656 KB Output is correct
38 Correct 309 ms 35444 KB Output is correct
39 Correct 976 ms 32452 KB Output is correct
40 Correct 994 ms 31096 KB Output is correct
41 Correct 991 ms 32648 KB Output is correct
42 Correct 1274 ms 34296 KB Output is correct
43 Correct 954 ms 30712 KB Output is correct
44 Correct 942 ms 34452 KB Output is correct
45 Correct 952 ms 34488 KB Output is correct
46 Correct 945 ms 31124 KB Output is correct
47 Correct 949 ms 31608 KB Output is correct
48 Correct 965 ms 31800 KB Output is correct
49 Correct 933 ms 33656 KB Output is correct
50 Correct 983 ms 35064 KB Output is correct
51 Correct 10 ms 9728 KB Output is correct
52 Correct 726 ms 24976 KB Output is correct
53 Correct 706 ms 24924 KB Output is correct
54 Correct 696 ms 24952 KB Output is correct
55 Correct 710 ms 24952 KB Output is correct
56 Correct 705 ms 24952 KB Output is correct
57 Correct 721 ms 24824 KB Output is correct
58 Correct 620 ms 27512 KB Output is correct
59 Correct 585 ms 27656 KB Output is correct
60 Correct 750 ms 27176 KB Output is correct
61 Correct 955 ms 27128 KB Output is correct
62 Correct 319 ms 35576 KB Output is correct
63 Correct 298 ms 35672 KB Output is correct
64 Correct 303 ms 35448 KB Output is correct
65 Correct 312 ms 35576 KB Output is correct
66 Correct 518 ms 30320 KB Output is correct
67 Correct 547 ms 30188 KB Output is correct
68 Correct 498 ms 30316 KB Output is correct
69 Correct 503 ms 30192 KB Output is correct
70 Correct 494 ms 30064 KB Output is correct
71 Correct 488 ms 30064 KB Output is correct
72 Correct 508 ms 30188 KB Output is correct
73 Correct 508 ms 29576 KB Output is correct
74 Correct 552 ms 30320 KB Output is correct
75 Correct 524 ms 30148 KB Output is correct
76 Correct 707 ms 29992 KB Output is correct
77 Correct 713 ms 29028 KB Output is correct
78 Correct 1008 ms 31512 KB Output is correct
79 Correct 939 ms 30300 KB Output is correct
80 Correct 1024 ms 34656 KB Output is correct
81 Correct 963 ms 32524 KB Output is correct
82 Correct 949 ms 32660 KB Output is correct
83 Correct 974 ms 30532 KB Output is correct
84 Correct 985 ms 34108 KB Output is correct
85 Correct 1012 ms 33064 KB Output is correct
86 Correct 1020 ms 30328 KB Output is correct
87 Correct 941 ms 31352 KB Output is correct
88 Correct 827 ms 31880 KB Output is correct
89 Correct 824 ms 29432 KB Output is correct
90 Correct 833 ms 29432 KB Output is correct
91 Correct 821 ms 30840 KB Output is correct
92 Correct 943 ms 30004 KB Output is correct
93 Correct 824 ms 29816 KB Output is correct
94 Correct 842 ms 29304 KB Output is correct
95 Correct 842 ms 30456 KB Output is correct
96 Correct 816 ms 29688 KB Output is correct
97 Correct 838 ms 30712 KB Output is correct