Codeforces 731C. Socks Unicom Block

Source: Internet
Author: User

C. SocksTime limit per test: 2 secondsMemory limit per test: MegabytesInput Standard InputOutput 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 has prepared a lot of food, left some money and washed all Arseniy ' s clothes.

Ten minutes before her leave she realized that it would being also useful to prepare instruction of which particular clothes To wear on all of the days she she'll 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 he mother had to do is to write down the Integers  L i  and  R i  for each of the days -the indices of socks to wear on the Day  i   (obv Iously,  l i  stands for the left foot And  R i  for the right). Each sock are painted in one of  k colors.

When mother already left Arseniy noticed, according to instruction he would wear the socks of different colors on some Days. Of course, that's 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 the c2>k colors.

Arseniy wants to repaint some of the socks in such a-the-a-, that's 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 being very busy these days he'll have no time to change the colors of any socks so he had to finalize the Colors now.

The new computer game Bota-3 is just realised and Arseniy can ' t wait to play it. What's the minimum number of socks that need their color to being changed in order to make it possible to follow mother ' s in Structions and wear the socks of the same color during each of mdays.

Input

The first line of input contains three integers n, m and k (2≤ n ≤ 0≤ m ≤200, 1≤ k ≤200)-the number of socks, the number of days and T He number of available colors respectively.

The second line Contain  n  integers  C 1,  C 2, ...,  C n   (1≤ C i k )  -current colors of Arseniy ' s socks.

Each of the following m lines contains II integers li and RI (1≤ l, rin, l iri)-indices of socks which Arseniy should wear during the i-th Day.

Output

Print One integer-the minimum number of socks that should has their colors changed in order to being able to obey the Inst Ructions and not make people laugh from watching the socks of different colors.

ExamplesInput
3 2 3
1 2 3
0 S
2 3
Output
2
Input
3 2 2
1 1 2
0 S
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 are no need to change any colors.

Topic Connection: Http://codeforces.com/contest/731/problem/C

Test Instructions: There are n socks, the color of each sock is c[i]. M days, wear the first Li,ri socks every day. Change the color of the socks before m days and ask at least how many socks you need to change in order to wear the same color socks every day.

idea: Unicom block. Ask how many unicom blocks. You can use the search, or you can use and check the set. The socks must be the same color in each of the unicom blocks. The color of the socks inside the Unicom block becomes the most frequently occurring color. Note: If you use only the cou array to represent the number of occurrences of the color in the Unicom block, you must initialize the array cou before each search, which will time out. Can add a set to indicate the current link inside the color of the block, if the color x has not appeared, then cou[x]=1, no person cou[x]++. Then put the color x into set. In this case, it is not necessary to initialize the cou array every time, because a single cou[x] can be initialized by set, so it is only necessary to initialize the set each time. Code:
#include <bits/stdc++.h>using namespacestd;Const intmaxn=3e5+ -;intC[maxn];vector<int>EDGE[MAXN];intSIGN[MAXN];Set<int>s;intCOU[MAXN];intCnt=0, max=0;voidDfsinti) {Sign[i]=1; if(S.count (c[i]) = =0) cou[c[i]]=1; Elsecou[c[i]]++;    S.insert (C[i]); CNT++; Max=Max (Max,cou[c[i]]);  for(intj=0; J<edge[i].size (); J + +)    {        if(sign[edge[i][j]]==0) DFS (Edge[i][j]); }}intMain () {inti,j,n,m,k; scanf ("%d%d%d",&n,&m,&k);  for(i=1; i<=n; i++) scanf ("%d",&C[i]);  for(i=1; i<=m; i++)    {        intu,v; scanf ("%d%d",&u,&v);        Edge[u].push_back (v);    Edge[v].push_back (U); }    intans=0;  for(i=1; i<=n;i++)    {        if(sign[i]!=0)Continue; CNT=max=0;        S.clear ();        DFS (i); Ans+=cnt-Max; } cout<<ans<<Endl; return 0;}
View Code

Codeforces 731C. Socks Unicom Block

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.