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 ...
随机推荐
- Effictive Java学习笔记1:创建和销毁对象
建议1:考虑用静态工厂方法代替构造器 理由:1)静态方法有名字啊,更容易懂和理解.构造方法重载容易让人混淆,并不是好主意 2)静态工厂方法可以不必每次调用时都创建一个新对象,而公共构造函数每次调用都会 ...
- Entity Framework性能影响因素分析
1.对象管理机制-复杂为更好的管理模型对象,EF提供了一套内部管理机制和跟踪对象的状态,保存对象一致性,使用方便,但是性能有所降低. 2.执行机制-高度封装在EF中,所有的查询表达式都会经过语法分析. ...
- 利用 gnuplot_i 在你的 c 程序中调用 GNUPLOT
这是一篇非常早曾经写的小文章,最初发表于我的搜狐博客(2008-09-23 22:55).由于自从转移到这里后,sohu 博客就不再维护了,所以把这篇文章也一起挪了过来. GNUPLOT 是一款功能强 ...
- LeetCode 246. Strobogrammatic Number (可颠倒数字) $
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- Codeforces Round #250 (Div. 1) D. The Child and Sequence 线段树 区间求和+点修改+区间取模
D. The Child and Sequence At the children's day, the child came to Picks's house, and messed his h ...
- 如何理解Apache License, Version 2.0(整理)
如何理解Apache License, Version 2.0(整理) 问题: 最近看到apache发布了2.0版本的License.而且微软也以此发布了部分源代码.我对OpenSource不是特熟, ...
- Mysql的简单使用(二)
接上文Mysql的简单使用(一) 字段参数以“(字段名1 数据类型1,字段名2 数据类型2,......)”的形式构建. 关于mysql常用的数据类型,一下是比较常用的几种,想查阅比较详细的资料可以自 ...
- 洛谷 p1625
高精度 我以为这题必有高论,怎么想也想不出来,没想到竟是如此粗鄙做法. 我们写一个高精度模拟一下,然后枚举约数看是否能约分,由于我不会高精度除法,就抄了一发 其实这种两项之比和项数有关的数列是不能推通 ...
- C# Pen绘制虚线(System.Drawing.Pen与System.Windows.Media.Pen)
一.绘制虚线的方法 GDI绘制,使用的是System.Drawing.Pen Pen pen = new Pen(Color.Red, 1); pen.DashStyle = S ...
- E20170705-hm
bubble n. 泡,水泡; 冒泡,起泡; 泡影,妄想; (欲表达的) 一点感情;