Codeforces 731c:socks (and collection)

Source: Internet
Author: User

Http://codeforces.com/problemset/problem/731/C

Test instructions: There are n socks, M days, k colors, each socks have a color, and then give M days, there are two socks per day, each sock may be different colors, ask to let the daily socks are the same color, to re-dye the minimum number of socks.

Idea: And check the merger, the same day socks merged together, and then formed a CNT collection, each set is independent, so the sorting, find out the socks in each set of the same color of the most is which color, and then the other does not belong to this color is dyed this color, so that the number of re-dyeing socks is the least. Then the answer to each set adds up to the final result. Also afraid that this too violent will time out, fortunately No.

1#include <cstdio>2#include <algorithm>3#include <iostream>4#include <cstring>5#include <string>6#include <cmath>7#include <queue>8#include <vector>9#include <map>Ten#include <Set> One using namespacestd; A #defineINF 0x3f3f3f3f - #defineN 200010 -typedefLong LongLL; the intFa[n], col[n], root[n], vis[n]; -vector<int>Vec[n]; -  - intFind (intx) { +     if(x = = Fa[x])returnx; -     returnFA[X] =Find (fa[x]); + } A  at voidMerge (intXinty) { -x = Find (x), y =Find (y); -     if(x = = y)return ; -FA[X] =y; - } -  in intMain () { -     intN, M, K; toCIN >> n >> M >>K; +      for(inti =1; I <= N; i++) { -Fa[i] =i; thescanf"%d", &col[i]); *     } $      for(inti =0; I < m; i++) {Panax Notoginseng         intu, v; -scanf"%d%d", &u, &v); theVis[u] = Vis[v] =1; + Merge (U, v); A     } the     intCNT =0; +      for(inti =1; I <= N; i++) { -         if(Vis[i]) { $             if(Fa[i] = = i) root[++cnt] =i; $ Vec[find (i)].push_back (Col[i]); -         } -     } the     intAns =0; -      for(inti =1; I <= CNT; i++)Wuyi sort (Vec[root[i]].begin (), Vec[root[i]].end ()); the      for(inti =1; I <= CNT; i++) { -         intnow =1, MA =0; Wu          for(intj =1; J < Vec[root[i]].size (); J + +) { -             if(Vec[root[i]][j]! = vec[root[i]][j-1]) { AboutMA = Max (MA, now); now =1; $}Elsenow++; -         } -Ma =Max (now, MA); -Ans + = vec[root[i]].size ()-Ma; A     } +printf"%d\n", ans); the     return 0; -}

Codeforces 731c:socks (and collection)

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.