codeforces_731C_[dfs][并查集]
2 seconds
256 megabytes
standard input
standard output
Arseniy is already grown-up and independent. His mother decided to leave him alone for m days and left on a vacation. She have prepared a lot of food, left some money and washed all Arseniy's clothes.
Ten minutes before her leave she realized that it would be also useful to prepare instruction of which particular clothes to wear on each of the days she will be absent. Arseniy's family is a bit weird so all the clothes is enumerated. For example, each of Arseniy's n socks is assigned a unique integer from 1 to n. Thus, the only thing his mother had to do was to write down two integers li and ri for each of the days — the indices of socks to wear on the day i(obviously, li stands for the left foot and ri for the right). Each sock is painted in one of k colors.
When mother already left Arseniy noticed that according to instruction he would wear the socks of different colors on some days. Of course, that is a terrible mistake cause by a rush. Arseniy is a smart boy, and, by some magical coincidence, he posses k jars with the paint — one for each of k colors.
Arseniy wants to repaint some of the socks in such a way, that for each of m days he can follow the mother's instructions and wear the socks of the same color. As he is going to be very busy these days he will have no time to change the colors of any socks so he has to finalize the colors now.
The new computer game Bota-3 was just realised and Arseniy can't wait to play it. What is the minimum number of socks that need their color to be changed in order to make it possible to follow mother's instructions and wear the socks of the same color during each of m days.
The first line of input contains three integers n, m and k (2 ≤ n ≤ 200 000, 0 ≤ m ≤ 200 000, 1 ≤ k ≤ 200 000) — the number of socks, the number of days and the number of available colors respectively.
The second line contain n integers c1, c2, ..., cn (1 ≤ ci ≤ k) — current colors of Arseniy's socks.
Each of the following m lines contains two integers li and ri (1 ≤ li, ri ≤ n, li ≠ ri) — indices of socks which Arseniy should wear during the i-th day.
Print one integer — the minimum number of socks that should have their colors changed in order to be able to obey the instructions and not make people laugh from watching the socks of different colors.
3 2 3
1 2 3
1 2
2 3
2
3 2 2
1 1 2
1 2
2 1
0
In the first sample, Arseniy can repaint the first and the third socks to the second color.
In the second sample, there is no need to change any colors.
这道题比赛的时候理解错了。。。
题意:n双袜子,m天,每天穿一双袜子,在已知这m天每天穿的袜子的条件下,问至少改变多少次袜子的颜色,在改过颜色后,每天穿的两只袜子是同种颜色。。
思路:将可能在同一天穿的两只袜子放入一个集合,可以发现改变颜色后最终这个集合中的袜子的颜色是一样的(传递的关系)。所以,在每个集合中将袜子的颜色改为集合中最多的颜色即可。
dfs搜索,搜连通分支中最多的颜色:
#include<cstdio>
#include<set>
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
#define N 200005 int sock[N];
int father[N];
int vis[N];
vector<int> v[N];
map<int ,int>mm;
int cnt=; void dfs(int x)
{
if(vis[x]==)
return;
vis[x]=;
mm[sock[x]]++;
cnt=max(cnt,mm[sock[x]]);
for(int i=;i<v[x].size();i++)
dfs(v[x][i]);
} int main()
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
for(int i=; i<=n; i++)
scanf("%d",&sock[i]);
for(int i=; i<m; i++)
{
int l,r;
scanf("%d%d",&l,&r);
v[l].push_back(r);
v[r].push_back(l);
}
int tmp=;
for(int i=; i<=n; i++)
{
if(vis[i]==)
{
cnt=;
mm.clear();
dfs(i);
tmp+=cnt;
}
}
printf("%d\n",n-tmp);
return ;
}
并查集:
#include<cstdio>
#include<set>
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
#define N 200005 int sock[N];
int father[N];
int vis[N];
vector<int> v[N]; int Find(int x)
{
if(father[x]!=x)
father[x]=Find(father[x]);
return father[x];
} void Merge(int x,int y)
{
int fx=Find(x);
int fy=Find(y);
if(fx!=fy)
father[fx]=fy;
} int main()
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
for(int i=; i<=n; i++)
{
scanf("%d",&sock[i]);
father[i]=i;
}
for(int i=; i<m; i++)
{
int l,r;
scanf("%d%d",&l,&r);
if(l!=r)
Merge(l,r);
}
for(int i=; i<=n; i++)
{
int fi=Find(i);
v[fi].push_back(sock[i]);
}
int res=;
for(int i=; i<=n; i++)
{ //cout<<mm[1]<<endl;
if(v[i].size()<=)
continue;
map<int ,int>mm;
int maxn=,se=;
for(int j=; j<v[i].size(); j++)
{
mm[v[i][j]]++;
maxn=max(maxn,mm[v[i][j]]);
se++;
}
res+=se-maxn;
}
printf("%d\n",res);
return ;
}
codeforces_731C_[dfs][并查集]的更多相关文章
- 分珠(dfs+并查集)
1140 分珠 时间限制:500MS 内存限制:65536K提交次数:24 通过次数:18 题型: 编程题 语言: G++;GCC Description 如下图所示,有若干珠子,每颗珠子重量不 ...
- Codeforces 1027D Mouse Hunt (强连通缩点 || DFS+并查集)
<题目链接> 题目大意: 有n个房间,每个房间都会有一只老鼠.处于第i个房间的老鼠可以逃窜到第ai个房间中.现在要清理掉所有的老鼠,而在第i个房间中防止老鼠夹的花费是ci,问你消灭掉所有老 ...
- CodeForces - 455C Civilization (dfs+并查集)
http://codeforces.com/problemset/problem/455/C 题意 n个结点的森林,初始有m条边,现在有两种操作,1.查询x所在联通块的最长路径并输出:2.将结点x和y ...
- PAT甲题题解-1021. Deepest Root (25)-dfs+并查集
dfs求最大层数并查集求连通个数 #include <iostream> #include <cstdio> #include <algorithm> #inclu ...
- hdu 1198 Farm Irrigation(深搜dfs || 并查集)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm ...
- 1021. Deepest Root (25)——DFS+并查集
http://pat.zju.edu.cn/contests/pat-a-practise/1021 无环连通图也可以视为一棵树,选定图中任意一点作为根,如果这时候整个树的深度最大,则称其为 deep ...
- CF 115 A 【求树最大深度/DFS/并查集】
CF A. Party time limit per test3 seconds memory limit per test256 megabytes inputstandard input outp ...
- 51nod1307(暴力树剖/二分&dfs/并查集)
题目链接: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1307 题意: 中文题诶~ 思路: 解法1:暴力树剖 用一个数 ...
- ZOJ 3811 / 2014 牡丹江赛区网络赛 C. Untrusted Patrol bfs/dfs/并查集
Untrusted Patrol Time Limit: 3 Seconds Memory Limit: 65536 KB ...
随机推荐
- 洛谷 P3063 [USACO12DEC]牛奶的路由Milk Routing
P3063 [USACO12DEC]牛奶的路由Milk Routing 题目背景 征求翻译.如果你能提供翻译或者题意简述,请直接发讨论,感谢你的贡献. 题目描述 Farmer John's farm ...
- gn3 --iou
http://www.wyzc.com/Course/Course/learnAction/id/14049/center/0#lesson/400847 http://www.mamicode.co ...
- Java---15---单例设计模式:---饿汉式和懒汉式
概念: 单例模式确保某个类仅仅有一个实例.并且自行实例化并向整个系统提供这个实例. 单例模式有下面特点: 1.单例类仅仅能有一个实例 2.单例类必须自己给自己创建自己的唯一实例 3.单例类必须给全 ...
- css高级:font-size
body{ font:62.5%/1.6em "Lucida Grande",Verdana,Geneva,Helvetica,Arial,sansserif; }//font-s ...
- P2453 [SDOI2006]最短距离 dp
自己想出来了!这个dp比较简单,而且转移也很简单,很自然,直接上代码就行了. 题干: 一种EDIT字母编辑器,它的功能是可以通过不同的变换操作可以把一个源串X [l..m]变换为新的目标串y[1..n ...
- SpringMVC+MyBaties关于上传(跟新)图片的问题
/** * 方法名:uploadPhoto * 描 述:TODO(上传图片) * 作 者:池彦龙 * 时 间:2017/03/30 * 返回类型: * 参 数: * 异 常: */ Controlle ...
- IDEA Spark Streaming Flume数据源 --解决无法转化为实际输入数据,及中文乱码(Scala)
需要三步: 1.shell:往 1234 端口写数据 nc localhost 1234 2.shell: 启动flume服务 cd /usr/local2/flume/bin ./flume-ng ...
- 66.extjs 里对getvalue() 和getRawValue()
转自:https://blog.csdn.net/u014236541/article/details/49663589?locationNum=8
- Eclipse自定义HTML5,JSP模板
原文:http://blog.csdn.net/xz2585458279/article/details/78833893 我们知道在MyEclipse里面编写的html和jsp模板并不符合html5 ...
- E20170630-ts
displacement n. 取代,替代; 免职,停职; [船] 排水量; [化] 置换;