#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 = 4e6 + 6;
int num[NODE], low[NODE], gr[NODE], ans[NODE], timeDfs, counter = 0;
vector<int> graph[NODE], nodeList[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;
counter++;
do {
v = st.top(); st.pop();
del[v] = 1, cur += (v > LOG * n);
gr[v] = counter, nodeList[counter].push_back(v);
} while (v != u);
ans[counter] = cur;
}
}
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++) {
bool flag = 1;
for (int u : nodeList[i]) {
if (!u) flag = 0;
for (int v : graph[u])
if (i != gr[v]) flag = 0;
}
if (flag) 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) {
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:153:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
153 | for (int i = 1; i < nodeList[color].size(); i++) {
| ~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
197712 KB |
Output is correct |
2 |
Correct |
85 ms |
197712 KB |
Output is correct |
3 |
Correct |
89 ms |
197716 KB |
Output is correct |
4 |
Correct |
90 ms |
197724 KB |
Output is correct |
5 |
Correct |
90 ms |
197712 KB |
Output is correct |
6 |
Correct |
85 ms |
197700 KB |
Output is correct |
7 |
Correct |
89 ms |
197648 KB |
Output is correct |
8 |
Correct |
90 ms |
197712 KB |
Output is correct |
9 |
Correct |
91 ms |
197732 KB |
Output is correct |
10 |
Correct |
93 ms |
197712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
197712 KB |
Output is correct |
2 |
Correct |
85 ms |
197712 KB |
Output is correct |
3 |
Correct |
89 ms |
197716 KB |
Output is correct |
4 |
Correct |
90 ms |
197724 KB |
Output is correct |
5 |
Correct |
90 ms |
197712 KB |
Output is correct |
6 |
Correct |
85 ms |
197700 KB |
Output is correct |
7 |
Correct |
89 ms |
197648 KB |
Output is correct |
8 |
Correct |
90 ms |
197712 KB |
Output is correct |
9 |
Correct |
91 ms |
197732 KB |
Output is correct |
10 |
Correct |
93 ms |
197712 KB |
Output is correct |
11 |
Correct |
96 ms |
200808 KB |
Output is correct |
12 |
Correct |
93 ms |
200788 KB |
Output is correct |
13 |
Correct |
100 ms |
200784 KB |
Output is correct |
14 |
Correct |
95 ms |
200784 KB |
Output is correct |
15 |
Correct |
99 ms |
200936 KB |
Output is correct |
16 |
Correct |
96 ms |
200788 KB |
Output is correct |
17 |
Correct |
102 ms |
201044 KB |
Output is correct |
18 |
Correct |
93 ms |
201044 KB |
Output is correct |
19 |
Correct |
96 ms |
201044 KB |
Output is correct |
20 |
Correct |
92 ms |
201044 KB |
Output is correct |
21 |
Correct |
89 ms |
200896 KB |
Output is correct |
22 |
Correct |
91 ms |
201808 KB |
Output is correct |
23 |
Correct |
95 ms |
201292 KB |
Output is correct |
24 |
Correct |
91 ms |
200832 KB |
Output is correct |
25 |
Correct |
98 ms |
200804 KB |
Output is correct |
26 |
Correct |
88 ms |
201004 KB |
Output is correct |
27 |
Correct |
92 ms |
200728 KB |
Output is correct |
28 |
Correct |
97 ms |
200784 KB |
Output is correct |
29 |
Correct |
96 ms |
200788 KB |
Output is correct |
30 |
Correct |
96 ms |
200784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
801 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
197712 KB |
Output is correct |
2 |
Correct |
85 ms |
197712 KB |
Output is correct |
3 |
Correct |
89 ms |
197716 KB |
Output is correct |
4 |
Correct |
90 ms |
197724 KB |
Output is correct |
5 |
Correct |
90 ms |
197712 KB |
Output is correct |
6 |
Correct |
85 ms |
197700 KB |
Output is correct |
7 |
Correct |
89 ms |
197648 KB |
Output is correct |
8 |
Correct |
90 ms |
197712 KB |
Output is correct |
9 |
Correct |
91 ms |
197732 KB |
Output is correct |
10 |
Correct |
93 ms |
197712 KB |
Output is correct |
11 |
Correct |
96 ms |
200808 KB |
Output is correct |
12 |
Correct |
93 ms |
200788 KB |
Output is correct |
13 |
Correct |
100 ms |
200784 KB |
Output is correct |
14 |
Correct |
95 ms |
200784 KB |
Output is correct |
15 |
Correct |
99 ms |
200936 KB |
Output is correct |
16 |
Correct |
96 ms |
200788 KB |
Output is correct |
17 |
Correct |
102 ms |
201044 KB |
Output is correct |
18 |
Correct |
93 ms |
201044 KB |
Output is correct |
19 |
Correct |
96 ms |
201044 KB |
Output is correct |
20 |
Correct |
92 ms |
201044 KB |
Output is correct |
21 |
Correct |
89 ms |
200896 KB |
Output is correct |
22 |
Correct |
91 ms |
201808 KB |
Output is correct |
23 |
Correct |
95 ms |
201292 KB |
Output is correct |
24 |
Correct |
91 ms |
200832 KB |
Output is correct |
25 |
Correct |
98 ms |
200804 KB |
Output is correct |
26 |
Correct |
88 ms |
201004 KB |
Output is correct |
27 |
Correct |
92 ms |
200728 KB |
Output is correct |
28 |
Correct |
97 ms |
200784 KB |
Output is correct |
29 |
Correct |
96 ms |
200788 KB |
Output is correct |
30 |
Correct |
96 ms |
200784 KB |
Output is correct |
31 |
Runtime error |
801 ms |
524288 KB |
Execution killed with signal 9 |
32 |
Halted |
0 ms |
0 KB |
- |