acgo题库
  • 首页
  • 题库
  • 学习
  • 天梯
  • 备赛

    竞赛

    • CSP-J/S
    • 蓝桥杯

    考级

    • GESP
    • CPA
    • 电子学会考级
  • 资讯
  • 竞赛
  • 讨论
  • 团队
  • 商城
登录
注册
题目详情提交记录(0)
  • 题解

    #include <bits/stdc++.h> using namespace std; int main() { int n, m, k; scanf("%d%d%d", &n, &m, &k); vector<int> c(n + 1); for (int i = 1; i <= n; i++) scanf("%d", &c[i]); vector<int> fa(n + 1), sz(n + 1, 1); iota(fa.begin(), fa.end(), 0); function<int(int)> find = [&](int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }; auto un = [&](int a, int b) { a = find(a); b = find(b); if (a != b) { if (sz[a] < sz[b]) swap(a, b); fa[b] = a; sz[a] += sz[b]; } }; for (int i = 0; i < m; i++) { int l, r; scanf("%d%d", &l, &r); un(l, r); } // 每个根:颜色计数 unordered_map<int, unordered_map<int,int>> mp; for (int i = 1; i <= n; i++) mp[find(i)][c[i]]++; long long ans = 0; for (auto &pr : mp) { int total = 0, mx = 0; for (auto &p : pr.second) { total += p.second; mx = max(mx, p.second); } ans += total - mx; } printf("%lld\n", ans); return 0; }

    userId_undefined
    未知
    模拟·模拟练习生倔强青铜冒泡宗师→排序元老
    0阅读
    0回复
    0点赞
暂无数据

提交答案之后,这里将显示提交结果~

首页