답안 #873074

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
873074 2023-11-14T12:06:43 Z serifefedartar 수도 (JOI20_capital_city) C++17
100 / 100
467 ms 41816 KB
#include <bits/stdc++.h>
using namespace std;

#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 1e9+9;
const ll LOGN = 20; 
const ll MAXN = 2e5 + 100;

vector<vector<int>> graph;
int city[MAXN], marked[MAXN], sz[MAXN], vis[MAXN], cVis[MAXN];
vector<int> seen, v[MAXN];
queue<int> q;

int get_sz(int node, int parent) {
	sz[node] = 1;
	for (auto u : graph[node]) {
		if (u == parent || marked[u])
			continue;
		sz[node] += get_sz(u, node);
	}
	return sz[node];
}

int find_centro(int node, int parent, int n) {
	for (auto u : graph[node]) {
		if (u != parent && !marked[u] && sz[u] * 2 >= n)
			return find_centro(u, node, n);
	}
	return node;
}

int par[MAXN], last[MAXN];
void dfs(int node, int parent, int c) {
	last[node] = c;
	vis[node] = false;
	seen.push_back(city[node]);
	for (auto u : graph[node]) {
		if (parent != u && !marked[u]) {
			par[u] = node;
			dfs(u, node, c);
		}
	}
}

int ans;
int cnt;
void decompose(int node) {
	int n = get_sz(node, node);
	int centro = find_centro(node, node, n);
	marked[centro] = true;
	last[centro] = centro; 

	q = queue<int>();
	seen.clear();
	seen.push_back(city[centro]); 
	for (auto u : graph[centro]) {
		if (!marked[u]) {
			par[u] = centro; 
			dfs(u, centro, centro);
		}
	}

	for (auto u : seen)
		cVis[u] = false;

	cnt = 0;
	q.push(city[centro]);
	bool control = true;
	while (!q.empty()) {
		int c = q.front();
		q.pop();

		if (cVis[c])
			continue;
		cVis[c] = true;
		cnt++;

		for (auto u : v[c]) {
			if (last[u] != centro) {
				control = false;
				break;
			}

			int now = u;
			while (now != centro) {
				if (vis[now])
					break;
				vis[now] = true; 
				q.push(city[now]);
				now = par[now];
			}
		}
	}
	if (control)
		ans = min(ans, cnt - 1);

	for (auto u : graph[centro]) {
		if (!marked[u])
			decompose(u);
	}
}

void dfs3(int node, int parent) {
	v[city[node]].push_back(node);
	for (auto u : graph[node]) {
		if (u == parent)
			continue;
		dfs3(u, node);
	}	
}

int main() {
	fast
	int N, K, A, B;
	cin >> N >> K;
	ans = N;

	graph = vector<vector<int>>(N+1, vector<int>());
	for (int i = 1; i < N; i++) {
		cin >> A >> B;
		graph[A].push_back(B);
		graph[B].push_back(A);
	}
	for (int i = 1; i <= N; i++)
		cin >> city[i];
	dfs3(1, 1);
	decompose(1);
	cout << ans << "\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8536 KB Output is correct
3 Correct 1 ms 8536 KB Output is correct
4 Correct 2 ms 8676 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 1 ms 8792 KB Output is correct
8 Correct 1 ms 8696 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 2 ms 8536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8536 KB Output is correct
3 Correct 1 ms 8536 KB Output is correct
4 Correct 2 ms 8676 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 1 ms 8792 KB Output is correct
8 Correct 1 ms 8696 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 2 ms 8536 KB Output is correct
11 Correct 4 ms 8796 KB Output is correct
12 Correct 3 ms 8796 KB Output is correct
13 Correct 3 ms 8792 KB Output is correct
14 Correct 3 ms 8796 KB Output is correct
15 Correct 2 ms 8796 KB Output is correct
16 Correct 3 ms 8796 KB Output is correct
17 Correct 2 ms 8796 KB Output is correct
18 Correct 2 ms 8936 KB Output is correct
19 Correct 2 ms 8796 KB Output is correct
20 Correct 2 ms 8932 KB Output is correct
21 Correct 2 ms 8796 KB Output is correct
22 Correct 2 ms 8796 KB Output is correct
23 Correct 3 ms 8804 KB Output is correct
24 Correct 2 ms 8796 KB Output is correct
25 Correct 3 ms 8796 KB Output is correct
26 Correct 3 ms 8796 KB Output is correct
27 Correct 3 ms 9048 KB Output is correct
28 Correct 3 ms 8796 KB Output is correct
29 Correct 3 ms 8796 KB Output is correct
30 Correct 3 ms 8784 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 284 ms 41364 KB Output is correct
2 Correct 138 ms 41684 KB Output is correct
3 Correct 296 ms 41164 KB Output is correct
4 Correct 138 ms 41612 KB Output is correct
5 Correct 331 ms 38696 KB Output is correct
6 Correct 151 ms 41696 KB Output is correct
7 Correct 359 ms 38820 KB Output is correct
8 Correct 149 ms 41460 KB Output is correct
9 Correct 447 ms 37416 KB Output is correct
10 Correct 427 ms 35688 KB Output is correct
11 Correct 393 ms 38032 KB Output is correct
12 Correct 391 ms 39916 KB Output is correct
13 Correct 363 ms 35268 KB Output is correct
14 Correct 424 ms 40156 KB Output is correct
15 Correct 381 ms 40168 KB Output is correct
16 Correct 375 ms 35916 KB Output is correct
17 Correct 362 ms 36604 KB Output is correct
18 Correct 467 ms 36540 KB Output is correct
19 Correct 365 ms 39112 KB Output is correct
20 Correct 365 ms 40980 KB Output is correct
21 Correct 1 ms 8536 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 8540 KB Output is correct
2 Correct 1 ms 8536 KB Output is correct
3 Correct 1 ms 8536 KB Output is correct
4 Correct 2 ms 8676 KB Output is correct
5 Correct 2 ms 8540 KB Output is correct
6 Correct 2 ms 8540 KB Output is correct
7 Correct 1 ms 8792 KB Output is correct
8 Correct 1 ms 8696 KB Output is correct
9 Correct 1 ms 8540 KB Output is correct
10 Correct 2 ms 8536 KB Output is correct
11 Correct 4 ms 8796 KB Output is correct
12 Correct 3 ms 8796 KB Output is correct
13 Correct 3 ms 8792 KB Output is correct
14 Correct 3 ms 8796 KB Output is correct
15 Correct 2 ms 8796 KB Output is correct
16 Correct 3 ms 8796 KB Output is correct
17 Correct 2 ms 8796 KB Output is correct
18 Correct 2 ms 8936 KB Output is correct
19 Correct 2 ms 8796 KB Output is correct
20 Correct 2 ms 8932 KB Output is correct
21 Correct 2 ms 8796 KB Output is correct
22 Correct 2 ms 8796 KB Output is correct
23 Correct 3 ms 8804 KB Output is correct
24 Correct 2 ms 8796 KB Output is correct
25 Correct 3 ms 8796 KB Output is correct
26 Correct 3 ms 8796 KB Output is correct
27 Correct 3 ms 9048 KB Output is correct
28 Correct 3 ms 8796 KB Output is correct
29 Correct 3 ms 8796 KB Output is correct
30 Correct 3 ms 8784 KB Output is correct
31 Correct 284 ms 41364 KB Output is correct
32 Correct 138 ms 41684 KB Output is correct
33 Correct 296 ms 41164 KB Output is correct
34 Correct 138 ms 41612 KB Output is correct
35 Correct 331 ms 38696 KB Output is correct
36 Correct 151 ms 41696 KB Output is correct
37 Correct 359 ms 38820 KB Output is correct
38 Correct 149 ms 41460 KB Output is correct
39 Correct 447 ms 37416 KB Output is correct
40 Correct 427 ms 35688 KB Output is correct
41 Correct 393 ms 38032 KB Output is correct
42 Correct 391 ms 39916 KB Output is correct
43 Correct 363 ms 35268 KB Output is correct
44 Correct 424 ms 40156 KB Output is correct
45 Correct 381 ms 40168 KB Output is correct
46 Correct 375 ms 35916 KB Output is correct
47 Correct 362 ms 36604 KB Output is correct
48 Correct 467 ms 36540 KB Output is correct
49 Correct 365 ms 39112 KB Output is correct
50 Correct 365 ms 40980 KB Output is correct
51 Correct 1 ms 8536 KB Output is correct
52 Correct 277 ms 28068 KB Output is correct
53 Correct 269 ms 28104 KB Output is correct
54 Correct 247 ms 28100 KB Output is correct
55 Correct 280 ms 28076 KB Output is correct
56 Correct 259 ms 28104 KB Output is correct
57 Correct 242 ms 27992 KB Output is correct
58 Correct 202 ms 30664 KB Output is correct
59 Correct 209 ms 31180 KB Output is correct
60 Correct 266 ms 30660 KB Output is correct
61 Correct 280 ms 30152 KB Output is correct
62 Correct 146 ms 41676 KB Output is correct
63 Correct 137 ms 41816 KB Output is correct
64 Correct 137 ms 41416 KB Output is correct
65 Correct 142 ms 41672 KB Output is correct
66 Correct 189 ms 35008 KB Output is correct
67 Correct 182 ms 34784 KB Output is correct
68 Correct 231 ms 35016 KB Output is correct
69 Correct 198 ms 35104 KB Output is correct
70 Correct 194 ms 35044 KB Output is correct
71 Correct 179 ms 35020 KB Output is correct
72 Correct 173 ms 34868 KB Output is correct
73 Correct 185 ms 34312 KB Output is correct
74 Correct 186 ms 35308 KB Output is correct
75 Correct 185 ms 35020 KB Output is correct
76 Correct 218 ms 33740 KB Output is correct
77 Correct 245 ms 32280 KB Output is correct
78 Correct 363 ms 36368 KB Output is correct
79 Correct 374 ms 34760 KB Output is correct
80 Correct 345 ms 40400 KB Output is correct
81 Correct 370 ms 37576 KB Output is correct
82 Correct 364 ms 37832 KB Output is correct
83 Correct 334 ms 35024 KB Output is correct
84 Correct 398 ms 39952 KB Output is correct
85 Correct 361 ms 38648 KB Output is correct
86 Correct 335 ms 34704 KB Output is correct
87 Correct 358 ms 36292 KB Output is correct
88 Correct 296 ms 37060 KB Output is correct
89 Correct 359 ms 33916 KB Output is correct
90 Correct 293 ms 33816 KB Output is correct
91 Correct 281 ms 35668 KB Output is correct
92 Correct 310 ms 34604 KB Output is correct
93 Correct 303 ms 34140 KB Output is correct
94 Correct 306 ms 34016 KB Output is correct
95 Correct 282 ms 35012 KB Output is correct
96 Correct 309 ms 33992 KB Output is correct
97 Correct 293 ms 35532 KB Output is correct