CodeForces 731C Socks 并查集
2016-10-28 22:05
330 查看
//http://codeforces.com/problemset/problem/731/C #include<bits/stdc++.h> using namespace std; const int MAX=2e5+5; int N,M,K,Color[MAX],pre[MAX],num[MAX]; vector<int> G[MAX]; int Find(int x) { int r=x; while(r!=pre[r]) r=pre[r]; int i=x,j; while(pre[i]!=r) { j=pre[i]; pre[i]=r; i=j; } return r; } void mix(int x,int y) { int fx=Find(x),fy=Find(y); if(fx!=fy) pre[fy]=fx; } int main() { ios::sync_with_stdio(false); cin>>N>>M>>K; for (int i=1; i<=N; i++) { cin>>Color[i]; pre[i]=i; } for (int i=1,a,b; i<=M; i++) { cin>>a>>b; mix(a,b); } int Ans=0,total=0; for (int i=1;i<=N;i++) if (pre[i]==i) num[i]=++total; for (int i=1;i<=N;i++) G[num[Find(i)]].push_back(Color[i]); for (int i=1;i<=total;i++) { int temp=0; map<int,int> x; for (int j=0;j<(int)G[i].size();j++) x[G[i][j]]=x[G[i][j]]+1,temp=max(temp,x[G[i][j]]); Ans+=G[i].size()-temp; } cout<<Ans; return 0; }
相关文章推荐
- CodeForces - 731C Socks(并查集)(贪心)
- CodeForces 731C Socks (DFS或并查集)
- CodeForces - 731C Socks(并查集)(贪心)
- CodeForces - 731C Socks(并查集)(贪心)
- codeforces 731c Socks 并查集
- CodeForces 731C-Socks(联通图 并查集)
- CodeForces - 731C Socks(并查集)(贪心)
- Codeforces 731C Socks 并查集
- CodeForces - 731C Socks(并查集,贪心)
- CodeForces - 731C Socks(并查集)(贪心)
- CodeForces 731C - Socks(并查集)
- CodeForces - 731C Socks(并查集)(贪心)
- CodeForces - 731C Socks(并查集)(贪心)
- CodeForces - 731C Socks(并查集)(贪心)
- CodeForces - 731C Socks(并查集)(贪心)
- CodeForces - 731C Socks(并查集)(贪心)
- 【并查集+贪心】 CodeForces - 731C - Socks
- CodeForces - 731C Socks(并查集)(贪心)
- Codeforces 731C Socks(并查集)
- CodeForces - 731C Socks (并查集)