Codeforces Round #376 (Div. 2)C(DFS)
来源:互联网 发布:捕鱼刷分软件 编辑:程序博客网 时间:2024/05/22 04:27
C. Socks
time limit per test:2 seconds
memory limit per test:256 megabytes
input:standard input
output: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.
Input
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.
Output
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.
Examples
Input
3 2 3
1 2 3
1 2
2 3
Output
2
Input
3 2 2
1 1 2
1 2
2 1
Output
0
Note
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天,k个颜色(貌似没软用)刚开始每只袜子为ai色,每天穿编号为l和r的袜子,要每天穿的必须同一颜色,问你开始最少的染色次数使得其满足要求。
题解:我们合并要穿在一起的袜子,则最小花费就合并后袜子的数量-其中颜色最多的。dfs一下即可。
代码:
#include <bits/stdc++.h>using namespace std;const int N=2e6+10;vector<int> a[N];int vis[N];int col[N];int q[N];int cnt[N];int sz,mx,n,k,m,x,y;void dfs(int x){ q[sz++]=x; cnt[col[x]]++; if(cnt[col[x]]>mx) mx=cnt[col[x]]; vis[x]=1; int up=a[x].size(); for(int i=0;i<up;i++) { int to=a[x][i]; if(!vis[to]) dfs(to); }}int solo(int x){ sz=0; mx=0; dfs(x); for(int i=0;i<sz;i++) cnt[col[q[i]]]--; return sz-mx;}int main(){ scanf("%d%d%d",&n,&m,&k); for(int i=1;i<=n;i++) { scanf("%d",&col[i]); } while(m--) { scanf("%d%d",&x,&y); a[x].push_back(y); a[y].push_back(x); } int ans=0; for(int i=1;i<=n;i++) { if(!vis[i]) ans+=solo(i); } printf("%d\n",ans); return 0;}
- Codeforces Round #376 (Div. 2)C(DFS)
- Codeforces Round #301 C (Div. 2) 【dfs】
- Codeforces Round #359 (Div. 2) C DFS
- Codeforces Round #321 (Div. 2) C DFS
- Codeforces Round #376 (Div. 2) C dfs+并查集
- Codeforces Round #256 (Div. 2) C. Painting Fence (DFS)
- Codeforces Round #321 (Div. 2)(C)模拟,DFS
- Codeforces Round #359 (Div. 2) C. Robbers' watch (DFS)
- Codeforces Round #360 (Div. 2) C DFS判断二分图
- Codeforces Round #374 (Div. 2) C. Journey(dfs+dp)
- Codeforces Round #398 (Div. 2) -- C. Garland (dfs)
- Codeforces Round #398(Div. 2)C. Garland【Dfs+思维】
- Codeforces Round #245 (Div. 2) C. Xor-tree DFS
- Codeforces Round #308 (Div. 2) C. Vanya and Scales dfs
- Codeforces Round #321 (Div. 2) C. Kefa and Park dfs
- Codeforces Round #398 (Div. 2) C. Garland —— DFS
- Codeforces Round #197 (Div. 2) C(dfs)
- Codeforces Round #428 (Div. 2) C. Journey(dfs deep
- genymotion下载360桌面之后安装的时候 提示此应用与您的手机不兼容
- 公开课分享《如何快速有效地编写一份商业计划书》
- CSDN-markdown编辑器
- 使用spring-loaded实现应用热部署
- CentOS/RedHat install Tensorflow in HPC
- Codeforces Round #376 (Div. 2)C(DFS)
- 过滤器的常见应用
- LoRa开发6:接收数据帧
- 用十条命令在一分钟内检查Linux服务器性能
- 浅谈javascript的Array.prototype.slice.call
- codeforces-16A-A. Flag (C && 简单模拟 && 暴力农夫山泉)
- linux export命令
- Leetcode - Remove Nth Node From End of List
- JS中的valueOf方法