Submission #261196

# Submission time Handle Problem Language Result Execution time Memory
261196 2020-08-11T14:03:06 Z Saboon Capital City (JOI20_capital_city) C++14
100 / 100
1570 ms 70772 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], 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);
	}
	int answer = m-1;
	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);
		int cnt = 0;
		for (int j = 0; j < cit[i].size(); j++){
			int v = cit[i][j];
			if (j+1 < cit[i].size())
				cnt += dis(v, cit[i][j+1]);
			else
				cnt += dis(v, cit[i][0]);
		}
		cnt = (cnt+2) / 2;
		if (cnt == cit[i].size())
			answer = 0;
	}
	SCC();
	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:166:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 0; j < cit[i].size(); j++){
                   ~~^~~~~~~~~~~~~~~
capital_city.cpp:168:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    if (j+1 < cit[i].size())
        ~~~~^~~~~~~~~~~~~~~
capital_city.cpp:174:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if (cnt == cit[i].size())
       ~~~~^~~~~~~~~~~~~~~~
capital_city.cpp:181:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   for (int j = 0; j < cmp[i].size(); j++){
                   ~~^~~~~~~~~~~~~~~
capital_city.cpp:183:12: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
    if (j+1 < cmp[i].size())
        ~~~~^~~~~~~~~~~~~~~
capital_city.cpp:189:11: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
   if (cnt == cmp[i].size())
       ~~~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 22 ms 28800 KB Output is correct
2 Correct 20 ms 28800 KB Output is correct
3 Correct 21 ms 28800 KB Output is correct
4 Correct 20 ms 28800 KB Output is correct
5 Correct 21 ms 28800 KB Output is correct
6 Correct 21 ms 28800 KB Output is correct
7 Correct 20 ms 28800 KB Output is correct
8 Correct 21 ms 28800 KB Output is correct
9 Correct 21 ms 28728 KB Output is correct
10 Correct 20 ms 28800 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 28800 KB Output is correct
2 Correct 20 ms 28800 KB Output is correct
3 Correct 21 ms 28800 KB Output is correct
4 Correct 20 ms 28800 KB Output is correct
5 Correct 21 ms 28800 KB Output is correct
6 Correct 21 ms 28800 KB Output is correct
7 Correct 20 ms 28800 KB Output is correct
8 Correct 21 ms 28800 KB Output is correct
9 Correct 21 ms 28728 KB Output is correct
10 Correct 20 ms 28800 KB Output is correct
11 Correct 24 ms 28928 KB Output is correct
12 Correct 23 ms 29056 KB Output is correct
13 Correct 25 ms 28928 KB Output is correct
14 Correct 22 ms 28928 KB Output is correct
15 Correct 26 ms 28980 KB Output is correct
16 Correct 21 ms 29056 KB Output is correct
17 Correct 24 ms 29056 KB Output is correct
18 Correct 23 ms 29056 KB Output is correct
19 Correct 26 ms 29048 KB Output is correct
20 Correct 23 ms 29056 KB Output is correct
21 Correct 24 ms 29056 KB Output is correct
22 Correct 23 ms 29184 KB Output is correct
23 Correct 22 ms 29052 KB Output is correct
24 Correct 22 ms 29056 KB Output is correct
25 Correct 22 ms 29056 KB Output is correct
26 Correct 24 ms 29184 KB Output is correct
27 Correct 24 ms 29048 KB Output is correct
28 Correct 24 ms 29056 KB Output is correct
29 Correct 22 ms 29056 KB Output is correct
30 Correct 24 ms 29068 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 988 ms 66420 KB Output is correct
2 Correct 441 ms 70644 KB Output is correct
3 Correct 948 ms 69876 KB Output is correct
4 Correct 435 ms 70644 KB Output is correct
5 Correct 975 ms 66668 KB Output is correct
6 Correct 432 ms 70532 KB Output is correct
7 Correct 941 ms 66928 KB Output is correct
8 Correct 426 ms 69744 KB Output is correct
9 Correct 951 ms 62516 KB Output is correct
10 Correct 896 ms 59976 KB Output is correct
11 Correct 956 ms 63272 KB Output is correct
12 Correct 947 ms 65680 KB Output is correct
13 Correct 970 ms 59632 KB Output is correct
14 Correct 951 ms 65968 KB Output is correct
15 Correct 1036 ms 65880 KB Output is correct
16 Correct 1052 ms 60872 KB Output is correct
17 Correct 1053 ms 61328 KB Output is correct
18 Correct 975 ms 61360 KB Output is correct
19 Correct 1026 ms 64500 KB Output is correct
20 Correct 1025 ms 66992 KB Output is correct
21 Correct 20 ms 28796 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 22 ms 28800 KB Output is correct
2 Correct 20 ms 28800 KB Output is correct
3 Correct 21 ms 28800 KB Output is correct
4 Correct 20 ms 28800 KB Output is correct
5 Correct 21 ms 28800 KB Output is correct
6 Correct 21 ms 28800 KB Output is correct
7 Correct 20 ms 28800 KB Output is correct
8 Correct 21 ms 28800 KB Output is correct
9 Correct 21 ms 28728 KB Output is correct
10 Correct 20 ms 28800 KB Output is correct
11 Correct 24 ms 28928 KB Output is correct
12 Correct 23 ms 29056 KB Output is correct
13 Correct 25 ms 28928 KB Output is correct
14 Correct 22 ms 28928 KB Output is correct
15 Correct 26 ms 28980 KB Output is correct
16 Correct 21 ms 29056 KB Output is correct
17 Correct 24 ms 29056 KB Output is correct
18 Correct 23 ms 29056 KB Output is correct
19 Correct 26 ms 29048 KB Output is correct
20 Correct 23 ms 29056 KB Output is correct
21 Correct 24 ms 29056 KB Output is correct
22 Correct 23 ms 29184 KB Output is correct
23 Correct 22 ms 29052 KB Output is correct
24 Correct 22 ms 29056 KB Output is correct
25 Correct 22 ms 29056 KB Output is correct
26 Correct 24 ms 29184 KB Output is correct
27 Correct 24 ms 29048 KB Output is correct
28 Correct 24 ms 29056 KB Output is correct
29 Correct 22 ms 29056 KB Output is correct
30 Correct 24 ms 29068 KB Output is correct
31 Correct 988 ms 66420 KB Output is correct
32 Correct 441 ms 70644 KB Output is correct
33 Correct 948 ms 69876 KB Output is correct
34 Correct 435 ms 70644 KB Output is correct
35 Correct 975 ms 66668 KB Output is correct
36 Correct 432 ms 70532 KB Output is correct
37 Correct 941 ms 66928 KB Output is correct
38 Correct 426 ms 69744 KB Output is correct
39 Correct 951 ms 62516 KB Output is correct
40 Correct 896 ms 59976 KB Output is correct
41 Correct 956 ms 63272 KB Output is correct
42 Correct 947 ms 65680 KB Output is correct
43 Correct 970 ms 59632 KB Output is correct
44 Correct 951 ms 65968 KB Output is correct
45 Correct 1036 ms 65880 KB Output is correct
46 Correct 1052 ms 60872 KB Output is correct
47 Correct 1053 ms 61328 KB Output is correct
48 Correct 975 ms 61360 KB Output is correct
49 Correct 1026 ms 64500 KB Output is correct
50 Correct 1025 ms 66992 KB Output is correct
51 Correct 20 ms 28796 KB Output is correct
52 Correct 1422 ms 51980 KB Output is correct
53 Correct 1457 ms 52080 KB Output is correct
54 Correct 1397 ms 52096 KB Output is correct
55 Correct 1428 ms 51824 KB Output is correct
56 Correct 1492 ms 51916 KB Output is correct
57 Correct 1570 ms 52012 KB Output is correct
58 Correct 922 ms 55568 KB Output is correct
59 Correct 910 ms 56276 KB Output is correct
60 Correct 1111 ms 56164 KB Output is correct
61 Correct 1134 ms 55480 KB Output is correct
62 Correct 429 ms 70772 KB Output is correct
63 Correct 427 ms 70644 KB Output is correct
64 Correct 429 ms 69872 KB Output is correct
65 Correct 429 ms 70512 KB Output is correct
66 Correct 897 ms 60400 KB Output is correct
67 Correct 892 ms 60148 KB Output is correct
68 Correct 899 ms 60368 KB Output is correct
69 Correct 781 ms 60400 KB Output is correct
70 Correct 847 ms 60272 KB Output is correct
71 Correct 855 ms 60256 KB Output is correct
72 Correct 800 ms 60144 KB Output is correct
73 Correct 826 ms 59504 KB Output is correct
74 Correct 818 ms 60268 KB Output is correct
75 Correct 812 ms 60216 KB Output is correct
76 Correct 879 ms 60616 KB Output is correct
77 Correct 858 ms 58864 KB Output is correct
78 Correct 955 ms 61172 KB Output is correct
79 Correct 1025 ms 59248 KB Output is correct
80 Correct 1027 ms 66544 KB Output is correct
81 Correct 1044 ms 62700 KB Output is correct
82 Correct 944 ms 62832 KB Output is correct
83 Correct 951 ms 59440 KB Output is correct
84 Correct 960 ms 65476 KB Output is correct
85 Correct 937 ms 63964 KB Output is correct
86 Correct 941 ms 59184 KB Output is correct
87 Correct 987 ms 60784 KB Output is correct
88 Correct 1176 ms 62320 KB Output is correct
89 Correct 1078 ms 58608 KB Output is correct
90 Correct 971 ms 58156 KB Output is correct
91 Correct 959 ms 60728 KB Output is correct
92 Correct 1104 ms 58988 KB Output is correct
93 Correct 948 ms 58860 KB Output is correct
94 Correct 947 ms 58440 KB Output is correct
95 Correct 944 ms 59760 KB Output is correct
96 Correct 956 ms 58544 KB Output is correct
97 Correct 972 ms 60400 KB Output is correct