답안 #667166

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
667166 2022-11-30T15:54:17 Z Kahou 수도 (JOI20_capital_city) C++14
11 / 100
1264 ms 524288 KB
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define endl '\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int N = 2e5 + 50;
int n, k, c[N], sub[N], sz[N];
bool flg[N];

map<int, int> mp[N];
vector<int> adj[N];
vector<int> adjP[N], adjM[N];
int id[N], scz[N];
vector<int> vc;
bool vis[N];

void dfs(int u, int p) {
	for (int v:adj[u]) {
		if (v == p) continue;

		dfs(v, u);

		for (pii x:mp[v]) {
			if (x.S < sz[x.F] && x.F != c[u]) {
				adjP[x.F].push_back(c[u]);
				adjM[c[u]].push_back(x.F);
			}
			mp[u][x.F] += x.S;
		}
	}
	mp[u][c[u]]++;
}

void dfs2(int u) {
	vis[u] = 1;
	for (int v:adjP[u]) {
		if (vis[v]) continue;
		dfs2(v);
	}
	vc.push_back(u);
}
void sfd(int u) {
	scz[id[u]]++;
	for (int v:adjM[u]) {
		if (id[v]) continue;
		id[v] = id[u];
		sfd(v);
	}
}
void solve() {
	cin >> n >> k;
	for (int i = 1; i <= n-1; i++) {
		int u, v;
		cin >> u >> v;
		adj[u].push_back(v);
		adj[v].push_back(u);
	}
	for (int u = 1; u <= n; u++) {
		cin >> c[u];
		sz[c[u]]++;
	}
	dfs(1, 0);
	for (int u = 1; u <= k; u++) {
		sort(adjP[u].begin(), adjP[u].end());
		adjP[u].resize(unique(adjP[u].begin(), adjP[u].end())-adjP[u].begin());
		sort(adjM[u].begin(), adjM[u].end());
		adjM[u].resize(unique(adjM[u].begin(), adjM[u].end())-adjM[u].begin());
	}
	for (int u = 1; u <= k; u++) {
		if (!vis[u]) dfs2(u);
	}
	reverse(vc.begin(), vc.end());
	for (int u:vc) {
		if (!id[u]) {
			flg[u] = 1;
			id[u] = u;
			sfd(u);
		}
	}
	int ans = 1e9;
	for (int u:vc) {
		for (int v:adjP[u]) {
			flg[id[u]] = flg[id[u]]&&(id[v] == id[u]);
		}
	}
	for (int u:vc) {
		if (flg[id[u]]) ans = min(ans, scz[id[u]]);
	}
	cout << ans-1 << endl;
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	solve();
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23828 KB Output is correct
2 Correct 12 ms 23836 KB Output is correct
3 Correct 11 ms 23836 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23836 KB Output is correct
6 Correct 13 ms 23832 KB Output is correct
7 Correct 13 ms 23828 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 12 ms 23832 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23828 KB Output is correct
2 Correct 12 ms 23836 KB Output is correct
3 Correct 11 ms 23836 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23836 KB Output is correct
6 Correct 13 ms 23832 KB Output is correct
7 Correct 13 ms 23828 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 12 ms 23832 KB Output is correct
11 Correct 16 ms 24532 KB Output is correct
12 Correct 15 ms 24488 KB Output is correct
13 Correct 15 ms 24532 KB Output is correct
14 Correct 19 ms 24544 KB Output is correct
15 Correct 48 ms 36692 KB Output is correct
16 Correct 43 ms 32840 KB Output is correct
17 Correct 59 ms 39616 KB Output is correct
18 Correct 58 ms 38144 KB Output is correct
19 Correct 61 ms 39140 KB Output is correct
20 Correct 89 ms 49800 KB Output is correct
21 Correct 59 ms 38468 KB Output is correct
22 Correct 285 ms 105276 KB Output is correct
23 Correct 217 ms 77500 KB Output is correct
24 Correct 48 ms 35660 KB Output is correct
25 Correct 51 ms 38756 KB Output is correct
26 Correct 64 ms 43376 KB Output is correct
27 Correct 53 ms 39244 KB Output is correct
28 Correct 46 ms 36300 KB Output is correct
29 Correct 45 ms 34964 KB Output is correct
30 Correct 44 ms 35404 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Runtime error 1264 ms 524288 KB Execution killed with signal 9
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 23828 KB Output is correct
2 Correct 12 ms 23836 KB Output is correct
3 Correct 11 ms 23836 KB Output is correct
4 Correct 12 ms 23764 KB Output is correct
5 Correct 12 ms 23836 KB Output is correct
6 Correct 13 ms 23832 KB Output is correct
7 Correct 13 ms 23828 KB Output is correct
8 Correct 12 ms 23764 KB Output is correct
9 Correct 12 ms 23764 KB Output is correct
10 Correct 12 ms 23832 KB Output is correct
11 Correct 16 ms 24532 KB Output is correct
12 Correct 15 ms 24488 KB Output is correct
13 Correct 15 ms 24532 KB Output is correct
14 Correct 19 ms 24544 KB Output is correct
15 Correct 48 ms 36692 KB Output is correct
16 Correct 43 ms 32840 KB Output is correct
17 Correct 59 ms 39616 KB Output is correct
18 Correct 58 ms 38144 KB Output is correct
19 Correct 61 ms 39140 KB Output is correct
20 Correct 89 ms 49800 KB Output is correct
21 Correct 59 ms 38468 KB Output is correct
22 Correct 285 ms 105276 KB Output is correct
23 Correct 217 ms 77500 KB Output is correct
24 Correct 48 ms 35660 KB Output is correct
25 Correct 51 ms 38756 KB Output is correct
26 Correct 64 ms 43376 KB Output is correct
27 Correct 53 ms 39244 KB Output is correct
28 Correct 46 ms 36300 KB Output is correct
29 Correct 45 ms 34964 KB Output is correct
30 Correct 44 ms 35404 KB Output is correct
31 Runtime error 1264 ms 524288 KB Execution killed with signal 9
32 Halted 0 ms 0 KB -