#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pl;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tt;
#define all(a) a.begin(), a.end()
#define filter(a) a.erase(unique(all(a)), a.end())
const int mn = 2e5 + 5;
const int LOG = 18;
int go[mn][LOG], num[mn], depth[mn], clr[mn], sz[mn], timeDfs, n, k;
vector<int> nodeList[mn], adj[mn];
namespace SCC {
const int NODE = 3e6 + 8e5 + 6;
int num[NODE], low[NODE], gr[NODE], ans[NODE], timeDfs, counter = 0;
vector<int> graph[NODE];
bool del[NODE];
stack<int> st;
void dfs (int u, int p) {
num[u] = low[u] = ++timeDfs;
st.push(u);
for (int v : graph[u]) {
if (del[v]) continue;
if (!num[v]) {
dfs(v, u);
low[u] = min(low[u], low[v]);
}
else low[u] = min(low[u], num[v]);
}
if (low[u] == num[u]) {
int cur = 0, v = 0, flag = 1;
counter++;
do {
v = st.top(); st.pop();
del[v] = 1, cur += (v > LOG * n), gr[v] = counter;
for (int oth : graph[v])
if (gr[oth] && gr[oth] != counter) flag = 0;
if (!v) flag = 0;
} while (v != u);
ans[counter] = (flag ? cur : INT_MAX);
}
}
int compress() {
for (int i = 1; i <= LOG * n + k; i++)
if (!num[i]) dfs(i, i);
int res = INT_MAX;
for (int i = 1; i <= counter; i++) res = min(res, ans[i]);
return res;
}
};
int node (int layer, int u) {
return layer * n + u;
}
void addEdge (int a, int b) {
SCC::graph[a].push_back(b);
}
void dfs (int u, int p, int d) {
go[u][0] = p, depth[u] = d, num[u] = ++timeDfs, sz[u] = 1;
if (p) addEdge(node(0, u), node(LOG, clr[p]));
else addEdge(node(0, u), 0);
for (int i = 1; i < LOG; i++) {
go[u][i] = go[go[u][i - 1]][i - 1];
addEdge(node(i, u), node(i - 1, u));
if (go[u][i - 1]) addEdge(node(i, u), node(i - 1, go[u][i - 1]));
}
for (int v : adj[u]) {
if (v == p) continue;
dfs(v, u, d + 1);
sz[u] += sz[v];
}
}
int goUp (int a, int k) {
for (int i = 0; i < LOG; i++)
if (k & (1 << i)) a = go[a][i];
return a;
}
int lca (int a, int b) {
if (depth[a] < depth[b]) swap(a, b);
a = goUp(a, depth[a] - depth[b]);
if (a == b) return a;
for (int i = LOG - 1; i >= 0; i--)
if (go[a][i] != go[b][i]) a = go[a][i], b = go[b][i];
return go[a][0];
}
void connectPath (int source, int a, int p) {
//cout << "connecting " << source << " " << a << " " << p << "\n";
addEdge(source, node(LOG, clr[a]));
for (int i = LOG - 1; i >= 0; i--) {
if (depth[go[a][i]] < depth[p]) continue;
addEdge(source, node(i, a));
a = go[a][i];
}
}
bool cmp (int a, int b) {
return num[a] < num[b];
}
bool isParent (int p, int u) {
return num[p] <= num[u] && num[u] < num[p] + sz[p];
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n >> k;
for (int i = 1; i < n; i++) {
int a, b; cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
for (int i = 1; i <= n; i++) {
cin >> clr[i];
nodeList[clr[i]].push_back(i);
}
dfs(1, 0, 1);
for (int color = 1; color <= k; color++) {
if (nodeList[color].empty()) continue;
sort(all(nodeList[color]), cmp);
int curSz = nodeList[color].size();
for (int i = 1; i < curSz; i++)
nodeList[color].push_back(lca(nodeList[color][i - 1], nodeList[color][i]));
sort(all(nodeList[color]), cmp);
filter(nodeList[color]);
stack<int> st({nodeList[color][0]});
for (int i = 1; i < nodeList[color].size(); i++) {
while (st.size() && !isParent(st.top(), nodeList[color][i])) st.pop();
if (st.size())
connectPath(node(LOG, color), nodeList[color][i], st.top());
st.push(nodeList[color][i]);
}
}
cout << SCC::compress() - 1;
return 0;
}
Compilation message
capital_city.cpp: In function 'int main()':
capital_city.cpp:148:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
148 | for (int i = 1; i < nodeList[color].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
99164 KB |
Output is correct |
2 |
Correct |
42 ms |
98912 KB |
Output is correct |
3 |
Correct |
43 ms |
99152 KB |
Output is correct |
4 |
Correct |
43 ms |
99020 KB |
Output is correct |
5 |
Correct |
41 ms |
98912 KB |
Output is correct |
6 |
Correct |
42 ms |
99144 KB |
Output is correct |
7 |
Correct |
44 ms |
99048 KB |
Output is correct |
8 |
Correct |
41 ms |
99152 KB |
Output is correct |
9 |
Correct |
42 ms |
99156 KB |
Output is correct |
10 |
Correct |
42 ms |
98896 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
99164 KB |
Output is correct |
2 |
Correct |
42 ms |
98912 KB |
Output is correct |
3 |
Correct |
43 ms |
99152 KB |
Output is correct |
4 |
Correct |
43 ms |
99020 KB |
Output is correct |
5 |
Correct |
41 ms |
98912 KB |
Output is correct |
6 |
Correct |
42 ms |
99144 KB |
Output is correct |
7 |
Correct |
44 ms |
99048 KB |
Output is correct |
8 |
Correct |
41 ms |
99152 KB |
Output is correct |
9 |
Correct |
42 ms |
99156 KB |
Output is correct |
10 |
Correct |
42 ms |
98896 KB |
Output is correct |
11 |
Correct |
46 ms |
101096 KB |
Output is correct |
12 |
Correct |
45 ms |
101244 KB |
Output is correct |
13 |
Correct |
49 ms |
101196 KB |
Output is correct |
14 |
Correct |
51 ms |
101068 KB |
Output is correct |
15 |
Correct |
52 ms |
101200 KB |
Output is correct |
16 |
Correct |
54 ms |
101204 KB |
Output is correct |
17 |
Correct |
52 ms |
101092 KB |
Output is correct |
18 |
Correct |
51 ms |
101204 KB |
Output is correct |
19 |
Correct |
51 ms |
101084 KB |
Output is correct |
20 |
Correct |
50 ms |
101208 KB |
Output is correct |
21 |
Correct |
49 ms |
101204 KB |
Output is correct |
22 |
Correct |
52 ms |
101968 KB |
Output is correct |
23 |
Correct |
49 ms |
101452 KB |
Output is correct |
24 |
Correct |
50 ms |
101200 KB |
Output is correct |
25 |
Correct |
48 ms |
101204 KB |
Output is correct |
26 |
Correct |
50 ms |
101208 KB |
Output is correct |
27 |
Correct |
51 ms |
101200 KB |
Output is correct |
28 |
Correct |
54 ms |
101200 KB |
Output is correct |
29 |
Correct |
52 ms |
101200 KB |
Output is correct |
30 |
Correct |
50 ms |
101200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1571 ms |
441944 KB |
Output is correct |
2 |
Correct |
726 ms |
455912 KB |
Output is correct |
3 |
Correct |
1567 ms |
388212 KB |
Output is correct |
4 |
Correct |
720 ms |
456120 KB |
Output is correct |
5 |
Correct |
1527 ms |
399264 KB |
Output is correct |
6 |
Correct |
812 ms |
447056 KB |
Output is correct |
7 |
Correct |
1444 ms |
361128 KB |
Output is correct |
8 |
Correct |
695 ms |
426944 KB |
Output is correct |
9 |
Correct |
1623 ms |
321940 KB |
Output is correct |
10 |
Correct |
1598 ms |
318096 KB |
Output is correct |
11 |
Correct |
1572 ms |
322208 KB |
Output is correct |
12 |
Correct |
1621 ms |
326208 KB |
Output is correct |
13 |
Correct |
1573 ms |
317492 KB |
Output is correct |
14 |
Correct |
1590 ms |
327500 KB |
Output is correct |
15 |
Correct |
1690 ms |
326992 KB |
Output is correct |
16 |
Correct |
1662 ms |
318804 KB |
Output is correct |
17 |
Correct |
1550 ms |
319148 KB |
Output is correct |
18 |
Correct |
1168 ms |
319348 KB |
Output is correct |
19 |
Correct |
1512 ms |
325204 KB |
Output is correct |
20 |
Correct |
1118 ms |
328516 KB |
Output is correct |
21 |
Correct |
47 ms |
98920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
99164 KB |
Output is correct |
2 |
Correct |
42 ms |
98912 KB |
Output is correct |
3 |
Correct |
43 ms |
99152 KB |
Output is correct |
4 |
Correct |
43 ms |
99020 KB |
Output is correct |
5 |
Correct |
41 ms |
98912 KB |
Output is correct |
6 |
Correct |
42 ms |
99144 KB |
Output is correct |
7 |
Correct |
44 ms |
99048 KB |
Output is correct |
8 |
Correct |
41 ms |
99152 KB |
Output is correct |
9 |
Correct |
42 ms |
99156 KB |
Output is correct |
10 |
Correct |
42 ms |
98896 KB |
Output is correct |
11 |
Correct |
46 ms |
101096 KB |
Output is correct |
12 |
Correct |
45 ms |
101244 KB |
Output is correct |
13 |
Correct |
49 ms |
101196 KB |
Output is correct |
14 |
Correct |
51 ms |
101068 KB |
Output is correct |
15 |
Correct |
52 ms |
101200 KB |
Output is correct |
16 |
Correct |
54 ms |
101204 KB |
Output is correct |
17 |
Correct |
52 ms |
101092 KB |
Output is correct |
18 |
Correct |
51 ms |
101204 KB |
Output is correct |
19 |
Correct |
51 ms |
101084 KB |
Output is correct |
20 |
Correct |
50 ms |
101208 KB |
Output is correct |
21 |
Correct |
49 ms |
101204 KB |
Output is correct |
22 |
Correct |
52 ms |
101968 KB |
Output is correct |
23 |
Correct |
49 ms |
101452 KB |
Output is correct |
24 |
Correct |
50 ms |
101200 KB |
Output is correct |
25 |
Correct |
48 ms |
101204 KB |
Output is correct |
26 |
Correct |
50 ms |
101208 KB |
Output is correct |
27 |
Correct |
51 ms |
101200 KB |
Output is correct |
28 |
Correct |
54 ms |
101200 KB |
Output is correct |
29 |
Correct |
52 ms |
101200 KB |
Output is correct |
30 |
Correct |
50 ms |
101200 KB |
Output is correct |
31 |
Correct |
1571 ms |
441944 KB |
Output is correct |
32 |
Correct |
726 ms |
455912 KB |
Output is correct |
33 |
Correct |
1567 ms |
388212 KB |
Output is correct |
34 |
Correct |
720 ms |
456120 KB |
Output is correct |
35 |
Correct |
1527 ms |
399264 KB |
Output is correct |
36 |
Correct |
812 ms |
447056 KB |
Output is correct |
37 |
Correct |
1444 ms |
361128 KB |
Output is correct |
38 |
Correct |
695 ms |
426944 KB |
Output is correct |
39 |
Correct |
1623 ms |
321940 KB |
Output is correct |
40 |
Correct |
1598 ms |
318096 KB |
Output is correct |
41 |
Correct |
1572 ms |
322208 KB |
Output is correct |
42 |
Correct |
1621 ms |
326208 KB |
Output is correct |
43 |
Correct |
1573 ms |
317492 KB |
Output is correct |
44 |
Correct |
1590 ms |
327500 KB |
Output is correct |
45 |
Correct |
1690 ms |
326992 KB |
Output is correct |
46 |
Correct |
1662 ms |
318804 KB |
Output is correct |
47 |
Correct |
1550 ms |
319148 KB |
Output is correct |
48 |
Correct |
1168 ms |
319348 KB |
Output is correct |
49 |
Correct |
1512 ms |
325204 KB |
Output is correct |
50 |
Correct |
1118 ms |
328516 KB |
Output is correct |
51 |
Correct |
47 ms |
98920 KB |
Output is correct |
52 |
Correct |
1501 ms |
312660 KB |
Output is correct |
53 |
Correct |
1548 ms |
313172 KB |
Output is correct |
54 |
Correct |
1507 ms |
313168 KB |
Output is correct |
55 |
Correct |
1501 ms |
313172 KB |
Output is correct |
56 |
Correct |
1570 ms |
312956 KB |
Output is correct |
57 |
Correct |
1504 ms |
312988 KB |
Output is correct |
58 |
Correct |
1129 ms |
311232 KB |
Output is correct |
59 |
Correct |
1155 ms |
312252 KB |
Output is correct |
60 |
Correct |
1157 ms |
312596 KB |
Output is correct |
61 |
Correct |
1153 ms |
312244 KB |
Output is correct |
62 |
Correct |
726 ms |
455932 KB |
Output is correct |
63 |
Correct |
743 ms |
455960 KB |
Output is correct |
64 |
Correct |
697 ms |
428104 KB |
Output is correct |
65 |
Correct |
883 ms |
453392 KB |
Output is correct |
66 |
Correct |
1644 ms |
318372 KB |
Output is correct |
67 |
Correct |
1498 ms |
318296 KB |
Output is correct |
68 |
Correct |
1373 ms |
318408 KB |
Output is correct |
69 |
Correct |
1639 ms |
318544 KB |
Output is correct |
70 |
Correct |
1424 ms |
318396 KB |
Output is correct |
71 |
Correct |
1546 ms |
318372 KB |
Output is correct |
72 |
Correct |
1449 ms |
318284 KB |
Output is correct |
73 |
Correct |
1474 ms |
318788 KB |
Output is correct |
74 |
Correct |
1463 ms |
318412 KB |
Output is correct |
75 |
Correct |
1496 ms |
318360 KB |
Output is correct |
76 |
Correct |
1074 ms |
315516 KB |
Output is correct |
77 |
Correct |
1071 ms |
314896 KB |
Output is correct |
78 |
Correct |
1630 ms |
318960 KB |
Output is correct |
79 |
Correct |
1640 ms |
316112 KB |
Output is correct |
80 |
Correct |
1626 ms |
327936 KB |
Output is correct |
81 |
Correct |
1600 ms |
321824 KB |
Output is correct |
82 |
Correct |
1631 ms |
322412 KB |
Output is correct |
83 |
Correct |
1644 ms |
316332 KB |
Output is correct |
84 |
Correct |
1641 ms |
326484 KB |
Output is correct |
85 |
Correct |
1621 ms |
324156 KB |
Output is correct |
86 |
Correct |
1505 ms |
316472 KB |
Output is correct |
87 |
Correct |
1648 ms |
318956 KB |
Output is correct |
88 |
Correct |
1748 ms |
321568 KB |
Output is correct |
89 |
Correct |
1563 ms |
315492 KB |
Output is correct |
90 |
Correct |
1572 ms |
315456 KB |
Output is correct |
91 |
Correct |
1565 ms |
319132 KB |
Output is correct |
92 |
Correct |
1585 ms |
316756 KB |
Output is correct |
93 |
Correct |
1551 ms |
316432 KB |
Output is correct |
94 |
Correct |
1537 ms |
315404 KB |
Output is correct |
95 |
Correct |
1558 ms |
317836 KB |
Output is correct |
96 |
Correct |
1583 ms |
315096 KB |
Output is correct |
97 |
Correct |
1578 ms |
319176 KB |
Output is correct |