답안 #261170

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
261170 2020-08-11T13:14:04 Z Saboon 수도 (JOI20_capital_city) C++14
41 / 100
1466 ms 67008 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;

int n, m;
vector<int> t[maxn], cit[maxn];
int sz[maxn], st[maxn], ft[maxn], h[maxn], par[maxn][18], up[maxn], Time = 0, inPlace[maxn];
int Grand[maxn], col[maxn];

int lca(int v, int u){
	if (h[v] < h[u])
		swap(v, u);
	for (int i = 17; i >= 0; i--)
		if (h[v] - (1 << i) >= h[u])
			v = par[v][i];
	if (v == u)
		return v;
	for (int i = 17; i >= 0; i--)
		if (par[v][i] != par[u][i])
			v = par[v][i], u = par[u][i];
	return par[v][0];
}

int dis(int v, int u){
	return h[v] + h[u] - 2*h[lca(v,u)];
}

int seg[4*maxn], lazy[4*maxn];

void shift(int,int,int);

int get(int id, int L, int R, int l, int r){
	if (r <= L or R <= l or seg[id] == 0)
		return -1;
	if (L+1 == R)
		return L;
	shift(id, L, R);
	int mid = (L + R) >> 1;
	int ret = get(2*id+0, L, mid, l, r);
	if (ret != -1)
		return ret;
	return get(2*id+1, mid, R, l, r);
}

void change(int id, int L, int R, int l, int r, int val){
	if (r <= L or R <= l)
		return;
	if (l <= L and R <= r){
		seg[id] = val;
		lazy[id] = val;
		return;
	}
	shift(id, L, R);
	int mid = (L + R) >> 1;
	change(2*id+0, L, mid, l, r, val);
	change(2*id+1, mid, R, l, r, val);
	seg[id] = max(seg[2*id+0], seg[2*id+1]);
}

void shift(int id, int L, int R){
	if (lazy[id] == -1)
		return;
	int mid = (L + R) >> 1;
	change(2*id+0, L, mid, L, mid, lazy[id]);
	change(2*id+1, mid, R, mid, R, lazy[id]);
	lazy[id] = -1;
}

int cmpCount, distinctColor[maxn];
vector<int> cmp[maxn], topol;
bool visited[maxn];

void dfs(int c, bool w){
	visited[c] = 1;
	for (auto it : cit[c]){
		int v = it;
		change(1, 0, n, st[v], st[v]+1, 0);
		while (v != -1 and h[v] > h[Grand[c]]){
			int l = (h[up[v]] >= h[Grand[c]] ? st[up[v]] : st[Grand[c]]), r = st[v]+1;
			int idx = get(1, 0, n, l, r);
			if (idx == -1){
				v = par[up[v]][0];
				continue;
			}
			change(1, 0, n, idx, idx+1, 0);
			if (!visited[col[inPlace[idx]]])
				dfs(col[inPlace[idx]], w);
		}
	}
	if (w == 0)
		topol.push_back(c);
	else{
		for (auto v : cit[c])
			cmp[cmpCount].push_back(v);
		distinctColor[cmpCount] ++;
	}
}

void SCC(){
	change(1, 0, n, 0, n, 1);
	for (int i = 1; i <= m; i++)
		if (!visited[i])
			dfs(i, 0);
	change(1, 0, n, 0, n, 1);
	memset(visited, 0, sizeof visited);
	for (auto i : topol)
		if (!visited[i])
			dfs(i, 1), cmpCount ++;
}

void dfs1(int v, int p = -1){
	par[v][0] = p;
	for (int i = 1; par[v][i-1] != -1 and i < 18; i++)
		par[v][i] = par[par[v][i-1]][i-1];
	st[v] = Time, inPlace[Time++] = v;
	bool bigChild = true;
	for (auto u : t[v]){
		h[u] = h[v] + 1;
		if (bigChild)
			up[u] = up[v];
		else
			up[u] = u;
		bigChild = false;
		dfs1(u, v);
	}
}

int dfssz(int v, int par = -1){
	for (int i = 0; i < t[v].size(); i++)
		if (t[v][i] == par)
			t[v].erase(t[v].begin() + i);
	sz[v] = 1;
	for (auto u : t[v])
		sz[v] += dfssz(u, v);
	sort(t[v].begin(), t[v].end(), [](int a, int b){return sz[a] > sz[b];});
	return sz[v];
}

int main(){
	ios_base::sync_with_stdio(false);
	cin >> n >> m;
	for (int i = 0; i < n-1; i++){
		int v, u;
		cin >> v >> u;
		t[v].push_back(u);
		t[u].push_back(v);
	}
	dfssz(1);
	memset(par, -1, sizeof par);
	up[1] = 1;
	dfs1(1);
	for (int i = 1; i <= n; i++){
		int c;
		cin >> c;
		col[i] = c;
		cit[c].push_back(i);
	}
	for (int i = 1; i <= m; i++){
		sort(cit[i].begin(), cit[i].end(), [](int a, int b){return st[a] < st[b];});
		Grand[i] = cit[i][0];
		for (auto v : cit[i])
			Grand[i] = lca(Grand[i], v);
	}
	SCC();
	int answer = m-1;
	for (int i = 0; i < cmpCount; i++){
		sort(cmp[i].begin(), cmp[i].end(), [](int a, int b){return st[a] < st[b];});
		int cnt = 0;
		for (int j = 0; j < cmp[i].size(); j++){
			int v = cmp[i][j];
			if (j+1 < cmp[i].size())
				cnt += dis(v, cmp[i][j+1]);
			else
				cnt += dis(v, cmp[i][0]);
		}
		cnt = (cnt+2) / 2;
		if (cnt == cmp[i].size())
			answer = min(answer, distinctColor[i]-1);
	}
	cout << answer << endl;
}

Compilation message

capital_city.cpp: In function 'int dfssz(int, int)':
capital_city.cpp:130:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
  for (int i = 0; i < t[v].size(); i++)
                  ~~^~~~~~~~~~~~~
capital_city.cpp: In function 'int main()':
capital_city.cpp:170:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 0; j < cmp[i].size(); j++){
                   ~~^~~~~~~~~~~~~~~
capital_city.cpp:172:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    if (j+1 < cmp[i].size())
        ~~~~^~~~~~~~~~~~~~~
capital_city.cpp:178:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if (cnt == cmp[i].size())
       ~~~~^~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 28800 KB Output is correct
2 Correct 19 ms 28800 KB Output is correct
3 Correct 19 ms 28800 KB Output is correct
4 Correct 21 ms 28800 KB Output is correct
5 Correct 20 ms 28800 KB Output is correct
6 Correct 19 ms 28800 KB Output is correct
7 Correct 19 ms 28800 KB Output is correct
8 Correct 22 ms 28800 KB Output is correct
9 Correct 20 ms 28792 KB Output is correct
10 Correct 21 ms 28800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 28800 KB Output is correct
2 Correct 19 ms 28800 KB Output is correct
3 Correct 19 ms 28800 KB Output is correct
4 Correct 21 ms 28800 KB Output is correct
5 Correct 20 ms 28800 KB Output is correct
6 Correct 19 ms 28800 KB Output is correct
7 Correct 19 ms 28800 KB Output is correct
8 Correct 22 ms 28800 KB Output is correct
9 Correct 20 ms 28792 KB Output is correct
10 Correct 21 ms 28800 KB Output is correct
11 Correct 24 ms 28928 KB Output is correct
12 Correct 28 ms 28928 KB Output is correct
13 Correct 28 ms 28920 KB Output is correct
14 Correct 25 ms 28928 KB Output is correct
15 Correct 23 ms 29056 KB Output is correct
16 Correct 27 ms 29056 KB Output is correct
17 Correct 23 ms 29184 KB Output is correct
18 Correct 29 ms 29056 KB Output is correct
19 Correct 23 ms 29056 KB Output is correct
20 Correct 23 ms 29056 KB Output is correct
21 Correct 26 ms 29056 KB Output is correct
22 Correct 22 ms 29184 KB Output is correct
23 Correct 24 ms 29056 KB Output is correct
24 Correct 23 ms 29056 KB Output is correct
25 Correct 23 ms 29056 KB Output is correct
26 Correct 25 ms 29056 KB Output is correct
27 Correct 23 ms 29056 KB Output is correct
28 Correct 23 ms 29056 KB Output is correct
29 Correct 24 ms 29048 KB Output is correct
30 Correct 24 ms 29056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1032 ms 66440 KB Output is correct
2 Correct 434 ms 67008 KB Output is correct
3 Correct 984 ms 66148 KB Output is correct
4 Correct 440 ms 66932 KB Output is correct
5 Correct 858 ms 62960 KB Output is correct
6 Correct 411 ms 66800 KB Output is correct
7 Correct 959 ms 63216 KB Output is correct
8 Correct 418 ms 66160 KB Output is correct
9 Correct 1043 ms 59104 KB Output is correct
10 Correct 935 ms 56588 KB Output is correct
11 Correct 903 ms 59504 KB Output is correct
12 Correct 902 ms 62100 KB Output is correct
13 Correct 966 ms 56260 KB Output is correct
14 Correct 1025 ms 62408 KB Output is correct
15 Correct 1080 ms 61984 KB Output is correct
16 Correct 988 ms 57032 KB Output is correct
17 Correct 1166 ms 57488 KB Output is correct
18 Correct 1039 ms 57804 KB Output is correct
19 Correct 985 ms 60944 KB Output is correct
20 Correct 922 ms 63252 KB Output is correct
21 Correct 20 ms 28800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 28800 KB Output is correct
2 Correct 19 ms 28800 KB Output is correct
3 Correct 19 ms 28800 KB Output is correct
4 Correct 21 ms 28800 KB Output is correct
5 Correct 20 ms 28800 KB Output is correct
6 Correct 19 ms 28800 KB Output is correct
7 Correct 19 ms 28800 KB Output is correct
8 Correct 22 ms 28800 KB Output is correct
9 Correct 20 ms 28792 KB Output is correct
10 Correct 21 ms 28800 KB Output is correct
11 Correct 24 ms 28928 KB Output is correct
12 Correct 28 ms 28928 KB Output is correct
13 Correct 28 ms 28920 KB Output is correct
14 Correct 25 ms 28928 KB Output is correct
15 Correct 23 ms 29056 KB Output is correct
16 Correct 27 ms 29056 KB Output is correct
17 Correct 23 ms 29184 KB Output is correct
18 Correct 29 ms 29056 KB Output is correct
19 Correct 23 ms 29056 KB Output is correct
20 Correct 23 ms 29056 KB Output is correct
21 Correct 26 ms 29056 KB Output is correct
22 Correct 22 ms 29184 KB Output is correct
23 Correct 24 ms 29056 KB Output is correct
24 Correct 23 ms 29056 KB Output is correct
25 Correct 23 ms 29056 KB Output is correct
26 Correct 25 ms 29056 KB Output is correct
27 Correct 23 ms 29056 KB Output is correct
28 Correct 23 ms 29056 KB Output is correct
29 Correct 24 ms 29048 KB Output is correct
30 Correct 24 ms 29056 KB Output is correct
31 Correct 1032 ms 66440 KB Output is correct
32 Correct 434 ms 67008 KB Output is correct
33 Correct 984 ms 66148 KB Output is correct
34 Correct 440 ms 66932 KB Output is correct
35 Correct 858 ms 62960 KB Output is correct
36 Correct 411 ms 66800 KB Output is correct
37 Correct 959 ms 63216 KB Output is correct
38 Correct 418 ms 66160 KB Output is correct
39 Correct 1043 ms 59104 KB Output is correct
40 Correct 935 ms 56588 KB Output is correct
41 Correct 903 ms 59504 KB Output is correct
42 Correct 902 ms 62100 KB Output is correct
43 Correct 966 ms 56260 KB Output is correct
44 Correct 1025 ms 62408 KB Output is correct
45 Correct 1080 ms 61984 KB Output is correct
46 Correct 988 ms 57032 KB Output is correct
47 Correct 1166 ms 57488 KB Output is correct
48 Correct 1039 ms 57804 KB Output is correct
49 Correct 985 ms 60944 KB Output is correct
50 Correct 922 ms 63252 KB Output is correct
51 Correct 20 ms 28800 KB Output is correct
52 Correct 1449 ms 48412 KB Output is correct
53 Correct 1466 ms 48440 KB Output is correct
54 Incorrect 1425 ms 48388 KB Output isn't correct
55 Halted 0 ms 0 KB -