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)