CF

来源:互联网 发布:白鹿原 冷先生 知乎 编辑:程序博客网 时间:2024/05/19 22:02

1.题目描述:

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 kcolors.

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 nm and k (2 ≤ n ≤ 200 0000 ≤ m ≤ 200 0001 ≤ k ≤ 200 000) — the number of socks, the number of days and the number of available colors respectively.

The second line contain n integers c1c2, ..., 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 ≤ nli ≠ 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 31 2 31 22 3
output
2
input
3 2 21 1 21 22 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.


2.题意概述:

小明的妈妈给小明留下了n只袜子,给你一个大小为n的颜色序列c
代表第i只袜子的颜色,小明的妈妈在以后的m天要求小明每天穿编号为l[i],r[i]所组成的一双袜子
小明觉得如果颜色不一样的话很丢人。。想一次把袜子的颜色全部改好。。使得。。既按照妈妈的指令去做
又能使每天穿的一双袜子颜色相同,问,最少改变几只袜子的颜色(能满足条件)

3.解题思路:

如果我们深入这个题的话。。会观察得到一个性质。。小明每天穿的两只袜子颜色必然相同

那么这就相当于建立了一种联系,即给题目中的变量建立了联系,于是我们如何来维护这个联系呢

第一我们可以把他们都放到一个集合里面。。(我们找集合里面出现次数最多的颜色就好了)这个得实现方法,标记每个点的所属集合。。。vis数组

还可以用并查集(不过这个题没有查询操作不必强行并查集),还可以直接连边(这样连边连在一起的一颗森林,他们的颜色必定相同)

你用并查集可以非常容易地把那些点放到一个集合里去。但是如果开二维数组,肯定会MLE,这里一个技巧就是用map的映射关系

4.AC代码:

#include <stdio.h>#include <vector>#include <map>#include <algorithm>#define maxn 200200using  namespace  std;int pa[maxn], col[maxn], num[maxn];vector<int>v[maxn];map<int, int> mp;void init(int n){for (int i = 1; i <= n; i++)pa[i] = i;}int findset(int x) {return pa[x] == x ? x : pa[x] = findset(pa[x]);}void unionset(int x, int y) {x = findset(x);y = findset(y);if (x != y)pa[y] = x;}int  main() {int n, m, k, cnt = 0, ans = 0;scanf("%d%d%d", &n, &m, &k);init(n);for (int i = 1; i <= n; i++)scanf("%d", &col[i]);for (int i = 1; i <= m; i++){int l, r;scanf("%d%d", &l, &r);unionset(l, r);}for (int i = 1; i <= n; i++)if (pa[i] == i)num[i] = ++cnt; for (int i = 1; i <= n; i++)v[num[findset(i)]].push_back(col[i]); for (int i = 1; i <= cnt; i++) {int sz = v[i].size();int mx = 0;mp.clear();for (int j = 0; j <= sz - 1; j++){mp[v[i][j]] ++;mx = max(mx, mp[v[i][j]]);}ans += (sz - mx);}printf("%d\n", ans);return 0;}

0 0