codeforces 731C(DFS)
来源:互联网 发布:冥府之路 知乎 编辑:程序博客网 时间:2024/05/17 02:55
题目链接:http://codeforces.com/contest/731/problem/C
题意:有n只袜子(1~n),k种颜色(1~k),在m天中,左脚穿下标为l,右脚穿下标为r的袜子,问最少修改几只袜子的颜色,可以使每天穿的袜子左右两只都同颜色。
好恶心的袜子,一会儿看成改袜子的颜色,一会儿看成改l,r的颜色,一会下标看混......不过,菜是原罪=_=
思路:先建图,在每个连通分支中,把所有袜子的颜色都改成出现颜色次数最多的那个颜色,即该分支节点个数 - 最多出现次数
#include<bits/stdc++.h>using namespace std;const int N = 2e5 + 10;int c[N];bool vis[N];vector <int> G[N];map <int,int> num;int cur,maxm;void dfs(int x){ if(vis[x]) return; cur++; maxm = max(maxm,++num[c[x]]); vis[x] = 1; for(int i = 0; i < G[x].size(); i++) dfs(G[x][i]);}int main(){ int n,m,k,ans = 0; scanf("%d %d %d",&n,&m,&k); for(int i = 1; i <= n; i++) scanf("%d",c+i); for(int i = 1; i <= m; i++) { int l,r; scanf("%d %d",&l,&r); G[l].push_back(r); G[r].push_back(l); } for(int i = 1; i <= n; i++) { cur = maxm = 0; dfs(i); ans += cur - maxm; num.clear(); } printf("%d\n",ans); return 0;}
0 0
- codeforces 731C(DFS)
- CodeForces 731C. Socks(DFS)
- 【codeforces 731 C Socks】+ DFS
- codeforces 767C (dfs)
- Codeforces 731C Socks【Dfs+贪心】
- Codeforces 116C - Party(dfs)
- CodeForces 430C Xor-tree(DFS)
- CodeForces 616 C. The Labyrinth(dfs)
- CodeForces 616C The Labyrinth(DFS)
- CodeForces 686C Robbers' watch (dfs)
- Codeforces 767C Garland (dfs)
- Codeforces 744C【DFS】
- Codeforces 369C DFS
- codeforces 850C dfs
- [DFS] Codeforces 638C
- codeforces 767 c Garland(dfs)
- codeforces 767C Garland 【dfs】
- codeforces /782/problem/C [dfs]
- 与MOOC有关的期刊
- sql where having
- 时钟相关概念
- NFS简介和配置 一
- 解决Error:Execution failed for task ':app:transformClassesWithJarMergingForDebug'.
- codeforces 731C(DFS)
- 已经装了MySQL-python,但仍报错: No module named MySQLdb
- https 详解
- Ubuntu中关闭桌面
- Xcode中iOS DeviceSupport 文件过大
- 发行你的数字货币
- Linux中select()函数分析
- [LeetCode]--389. Find the Difference
- linux系统下的OPENCV编译