CodeForces 731C. Socks(DFS)

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 form 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 from1 to n. Thus, the only thing his mother had to do was to write down two integersli andri for each of the days — the indices of socks to wear on the dayi (obviously, li stands for the left foot andri for the right). Each sock is painted in one ofk 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 possesk 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 integersli andri (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.

题意:Arseniy有n双袜子,编号1~n,颜色是ci。现在他妈妈为他安排了m天内的左右脚袜子的搭配,然而他并不喜欢穿颜色不一样的袜子,他有k种颜色的颜料颜色编号1~k,现在求他至少改变多少双袜子,才能使得他既能按照他妈妈的要求搭配而且使得每次的袜子颜色一样。

思路:用袜子编号建图,然后选图里颜色出现次数最多的一种,用图的结点数减去该颜色出现次数就是最少改变的次数。因为可能有多个图,所以每次要初始化颜色出现次数,

然后就用memset超时了,改成map.clear就过了,新技能get。

#include <bits/stdc++.h>
using namespace std;
#define maxn 200010
int head[maxn*2], c[maxn], tot;
bool vis[maxn];
int num, maxx;
set<int> s;
map<int, int> cnt;
struct node{
    int to, next;
}edge[maxn*2];
void add(int u, int v){
    edge[tot].to = v;
    edge[tot].next = head[u];
    head[u] = tot++;
}
void dfs(int now){
    cnt[c[now]]++;
    if(cnt[c[now]]>maxx) maxx = cnt[c[now]];
    vis[now] = 1;
    num++;
    for(int i = head[now];i != -1;i = edge[i].next){
        int son = edge[i].to;
        if(vis[son]) continue;
        dfs(son);
    }
}
int main()
{
    int n, m, k, i, u, v;
    scanf("%d %d %d", &n, &m, &k);
    memset(head, -1, sizeof head);
    tot = 1;
    for(i = 1;i <= n;i++) scanf("%d", &c[i]);
    for(i = 0;i < m;i++){
        scanf("%d %d", &u, &v);
        add(u, v);
        add(v, u);
        s.insert(u);
        s.insert(v);
    }
    int res = 0;
    set<int>::iterator it;
    for(it = s.begin();it != s.end();it++){
        num = 0, maxx = 0;
        if(!vis[*it]) dfs(*it);
        res += (num-maxx);
        cnt.clear();
    }
    printf("%d", res);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值