Codeforces 731C Socks【Dfs+贪心】

186 篇文章 0 订阅

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 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 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种颜色,没天对应两个输入l,r,表示今天要穿的这双袜子的左右编号,必须需要两只袜子颜色相同。我们可以在最初的时候改变一些袜子的颜色,问最少改变多少只袜子的颜色就能使得满足上述条件。


思路:


1、如果有这样一种情况:

1 2

2 3

3 4

明显1 2 需要同一颜色,2 3需要同一颜色,3 4也需要同一颜色,那么我们不难想到,那么我们在最初改变袜子颜色的时候,就一定需要将这四只袜子变成同一颜色。

那么我们对应的m个输入,我们将其转化成图论建边。然后O(n)的Dfs预处理出每只袜子对应的集合编号。


2、然后我们将这N只袜子对应处理成g个集合。同时我们设定vector<int >mp【20040】;其中mp【i】表示集合i中的袜子编号的集合。然后我们再O(n)处理出每个集合中出现次数最多的颜色,对应我们需要将这个集合中所有的袜子都变成这个颜色,明显这样贪心是最优的,也是需要改变袜子颜色操作次数最少的方案。


3、那么按照上述过程处理,总时间复杂度:O(N);明显在时限内。


Ac代码:

#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
struct node
{
    int from;
    int to;
    int next;
}e[2000500];
int n,m,k,cont,g;
int vis[200050];
int contz[200050];
int color[200050];
int head[200050];
int a[200050];
vector<int >mp[200040];
void add(int from,int to)
{
    e[cont].to=to;
    e[cont].next=head[from];
    head[from]=cont++;
}
void Dfs(int u)
{
    color[u]=g;
    vis[u]=1;
    for(int i=head[u];i!=-1;i=e[i].next)
    {
        int v=e[i].to;
        if(vis[v]==0)
        {
            Dfs(v);
        }
    }
}
void Slove()
{
    g=0;
    memset(color,0,sizeof(color));
    memset(vis,0,sizeof(vis));
    for(int i=1;i<=n;i++)
    {
        if(vis[i]==0)
        {
            g++;
            Dfs(i);
        }
    }
    for(int i=1;i<=n;i++)
    {
        mp[color[i]].push_back(i);
    }
    int output=0;
    for(int i=1;i<=g;i++)
    {
        int maxn=0;
        for(int j=0;j<mp[i].size();j++)
        {
            int v=mp[i][j];
            contz[a[v]]++;
            if(contz[a[v]]>maxn)maxn=contz[a[v]];
        }
        output+=mp[i].size()-maxn;
        for(int j=0;j<mp[i].size();j++)
        {
            int v=mp[i][j];
            contz[a[v]]--;
        }
    }
    printf("%d\n",output);
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        cont=0;
        memset(head,-1,sizeof(head));
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
        }
        for(int i=0;i<m;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
            add(x,y);
            add(y,x);
        }
        Slove();
    }
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值