답안 #261168

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
261168 2020-08-11T13:09:50 Z Saboon 수도 (JOI20_capital_city) C++14
41 / 100
1606 ms 69236 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 (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 20 ms 28800 KB Output is correct
2 Correct 23 ms 28800 KB Output is correct
3 Correct 20 ms 28800 KB Output is correct
4 Correct 19 ms 28800 KB Output is correct
5 Correct 20 ms 28800 KB Output is correct
6 Correct 20 ms 28800 KB Output is correct
7 Correct 22 ms 28800 KB Output is correct
8 Correct 21 ms 28800 KB Output is correct
9 Correct 21 ms 28800 KB Output is correct
10 Correct 21 ms 28800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 28800 KB Output is correct
2 Correct 23 ms 28800 KB Output is correct
3 Correct 20 ms 28800 KB Output is correct
4 Correct 19 ms 28800 KB Output is correct
5 Correct 20 ms 28800 KB Output is correct
6 Correct 20 ms 28800 KB Output is correct
7 Correct 22 ms 28800 KB Output is correct
8 Correct 21 ms 28800 KB Output is correct
9 Correct 21 ms 28800 KB Output is correct
10 Correct 21 ms 28800 KB Output is correct
11 Correct 26 ms 29048 KB Output is correct
12 Correct 26 ms 29056 KB Output is correct
13 Correct 25 ms 29056 KB Output is correct
14 Correct 25 ms 29056 KB Output is correct
15 Correct 24 ms 29056 KB Output is correct
16 Correct 24 ms 29056 KB Output is correct
17 Correct 23 ms 29056 KB Output is correct
18 Correct 24 ms 29056 KB Output is correct
19 Correct 23 ms 29056 KB Output is correct
20 Correct 24 ms 29056 KB Output is correct
21 Correct 27 ms 29056 KB Output is correct
22 Correct 24 ms 29184 KB Output is correct
23 Correct 24 ms 29048 KB Output is correct
24 Correct 24 ms 29056 KB Output is correct
25 Correct 25 ms 29048 KB Output is correct
26 Correct 24 ms 29184 KB Output is correct
27 Correct 26 ms 29184 KB Output is correct
28 Correct 24 ms 29056 KB Output is correct
29 Correct 24 ms 29056 KB Output is correct
30 Correct 25 ms 29056 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1086 ms 68500 KB Output is correct
2 Correct 423 ms 69236 KB Output is correct
3 Correct 1102 ms 68428 KB Output is correct
4 Correct 474 ms 69236 KB Output is correct
5 Correct 974 ms 65220 KB Output is correct
6 Correct 455 ms 69160 KB Output is correct
7 Correct 1046 ms 65520 KB Output is correct
8 Correct 415 ms 68464 KB Output is correct
9 Correct 920 ms 61232 KB Output is correct
10 Correct 878 ms 58640 KB Output is correct
11 Correct 932 ms 61812 KB Output is correct
12 Correct 996 ms 64308 KB Output is correct
13 Correct 944 ms 58456 KB Output is correct
14 Correct 934 ms 64616 KB Output is correct
15 Correct 1072 ms 64264 KB Output is correct
16 Correct 1054 ms 59324 KB Output is correct
17 Correct 866 ms 59788 KB Output is correct
18 Correct 754 ms 60080 KB Output is correct
19 Correct 923 ms 63184 KB Output is correct
20 Correct 894 ms 65508 KB Output is correct
21 Correct 21 ms 28800 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 20 ms 28800 KB Output is correct
2 Correct 23 ms 28800 KB Output is correct
3 Correct 20 ms 28800 KB Output is correct
4 Correct 19 ms 28800 KB Output is correct
5 Correct 20 ms 28800 KB Output is correct
6 Correct 20 ms 28800 KB Output is correct
7 Correct 22 ms 28800 KB Output is correct
8 Correct 21 ms 28800 KB Output is correct
9 Correct 21 ms 28800 KB Output is correct
10 Correct 21 ms 28800 KB Output is correct
11 Correct 26 ms 29048 KB Output is correct
12 Correct 26 ms 29056 KB Output is correct
13 Correct 25 ms 29056 KB Output is correct
14 Correct 25 ms 29056 KB Output is correct
15 Correct 24 ms 29056 KB Output is correct
16 Correct 24 ms 29056 KB Output is correct
17 Correct 23 ms 29056 KB Output is correct
18 Correct 24 ms 29056 KB Output is correct
19 Correct 23 ms 29056 KB Output is correct
20 Correct 24 ms 29056 KB Output is correct
21 Correct 27 ms 29056 KB Output is correct
22 Correct 24 ms 29184 KB Output is correct
23 Correct 24 ms 29048 KB Output is correct
24 Correct 24 ms 29056 KB Output is correct
25 Correct 25 ms 29048 KB Output is correct
26 Correct 24 ms 29184 KB Output is correct
27 Correct 26 ms 29184 KB Output is correct
28 Correct 24 ms 29056 KB Output is correct
29 Correct 24 ms 29056 KB Output is correct
30 Correct 25 ms 29056 KB Output is correct
31 Correct 1086 ms 68500 KB Output is correct
32 Correct 423 ms 69236 KB Output is correct
33 Correct 1102 ms 68428 KB Output is correct
34 Correct 474 ms 69236 KB Output is correct
35 Correct 974 ms 65220 KB Output is correct
36 Correct 455 ms 69160 KB Output is correct
37 Correct 1046 ms 65520 KB Output is correct
38 Correct 415 ms 68464 KB Output is correct
39 Correct 920 ms 61232 KB Output is correct
40 Correct 878 ms 58640 KB Output is correct
41 Correct 932 ms 61812 KB Output is correct
42 Correct 996 ms 64308 KB Output is correct
43 Correct 944 ms 58456 KB Output is correct
44 Correct 934 ms 64616 KB Output is correct
45 Correct 1072 ms 64264 KB Output is correct
46 Correct 1054 ms 59324 KB Output is correct
47 Correct 866 ms 59788 KB Output is correct
48 Correct 754 ms 60080 KB Output is correct
49 Correct 923 ms 63184 KB Output is correct
50 Correct 894 ms 65508 KB Output is correct
51 Correct 21 ms 28800 KB Output is correct
52 Correct 1325 ms 50768 KB Output is correct
53 Correct 1315 ms 50812 KB Output is correct
54 Incorrect 1606 ms 50800 KB Output isn't correct
55 Halted 0 ms 0 KB -