zoukankan      html  css  js  c++  java
  • 2017ecjtu-summer training #4 CodeForces 731C

    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 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 ≤ 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只袜子的,最多k个颜色,m天,每天都要穿某两只袜子,不能让某一天穿不同颜色的袜子,问至少改变多少只袜子的颜色。 
    思路:把要在同一天穿的袜子用并查集放到一起,然后找出最多的那种颜色,size-max即为这堆袜子至少要改的次数。把每堆要改变的次数都加起来就是答案。

    AC代码

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <vector>
    #include <stack>
    #include <map>
    using namespace std;
    #define maxn 200010
    int pre[maxn];
    int c[maxn];
    int num[maxn];
    vector<int> f[maxn];
    long long ans=0;
    int find(int x)
    {
         if(x!=pre[x])
         pre[x]=find(pre[x]);
         return pre[x];
    }
    void mix(int x,int y)
    {
         int x1=find(x),y1=find(y);
         if(x1!=y1)
         pre[x1]=y1;
    }
    int main()
    {
         int n,m,k;
         int i,j;
         cin>>n>>m>>k;
         for(i=1;i<=n;i++)
         {
            cin>>c[i];
             pre[i]=i;
         }
        int a,b;
        for(i=0;i<m;i++)
        {
           cin>>a>>b;
           mix(a,b);
        }
        int xu=0;
        for(i=1; i<=n; i++)
        {
            if(pre[i]==i)
                num[i]=++xu;
        }
        for(i=1; i<=n; i++)
        {
            f[num[find(i)]].push_back(c[i]);
        }
        for(i=1; i<=xu; i++)
        {
            int ma=0;
            int len=f[i].size();
            map<int,int>x;
            for(j=0; j<len; j++)
            {
                x[f[i][j]]++;
                ma=max(ma,x[f[i][j]]);
            }
            ans+=(len-ma);
        }
        cout<<ans<<endl;
        return 0;
    }
  • 相关阅读:
    N皇后问题(回溯递归思想)
    链表大合集(二) 栈和队列的实现
    我是林荫
    蒟蒻林荫小复习——莫队
    神奇脑洞题解——[HAOI2011]Problem b
    蒟蒻林荫小复习——莫比乌斯反演
    蒟蒻林荫小复习——关于有限制区间元素查询的一些解法
    蒟蒻林荫小复习——K短路的A*解法
    蒟蒻林荫小复习——2-SAT的染色法求最小答案序列
    蒟蒻林荫小复习——克鲁斯卡尔重构树
  • 原文地址:https://www.cnblogs.com/stranger-/p/7152195.html
Copyright © 2011-2022 走看看