Codeforces Round #376 (Div. 2) C dfs+并查集
来源:互联网 发布:必应词典网络连接超时 编辑:程序博客网 时间:2024/05/10 16:09
题目传送门: http://codeforces.com/contest/731/problem/C
题意:有k有颜色,n天,m只袜子(n,m,k<=200000),每只袜子都有一个颜色,每天穿两只袜子a,b,问最少要改变多少只袜子的颜色才能使他每天都穿同种颜色的袜子
思路:先用邻接表建图,然后dfs找联通图,记录联通图中节点个数s和联通图中使用颜色最多的哪个颜色的个数c,然后s-c就是该联通图中需要改变的颜色数,最后把所有联通图的加起来就是答案
需要注意的是,当记录颜色时,数组用完要再dfs一遍清除或memset m长度的该数组,不要直接全部mem,会T
代码如下:
#include <iostream>#include <algorithm>#include <cstring>#include <stdio.h>#include <string>#include <cmath>#include <queue>#include <set>#include <map>#include <vector>using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define ll long long#define ull unsigned long long#define mem(n,v) memset(n,v,sizeof(n))#define MAX 200005#define MAXN 1000005#define PI 3.1415926#define E 2.718281828459#define opnin freopen("text.in.txt","r",stdin)#define opnout freopen("text.out.txt","w",stdout)#define clsin fclose(stdin)#define clsout fclose(stdout)#define haha1 cout << "haha1"<< endl#define haha2 cout << "haha2"<< endl#define haha3 cout << "haha3"<< endlconst int INF = 0x3f3f3f3f;const ll INFF = 0x3f3f3f3f3f3f3f3f;const double pi = 3.141592653589793;const double inf = 1e18;const double eps = 1e-8;const ll mod = 1e9+7;const ull mx = 133333331;struct e{ int next; int to;}edges[MAXN];int head[MAXN];int val[MAXN];int vis[MAXN];int color_num[MAX];int cnt;int n,m;void init(){ mem(edges,0); mem(val,0); mem(vis,0); mem(head,-1); mem(color_num,0); cnt = n = m = 0;}void addedge(int u,int v){ edges[cnt].to = v; edges[cnt].next = head[u]; head[u] = cnt++;}void getmap(int m){ int x,y; for(int i=1;i<=m;i++){ scanf("%d %d",&x,&y); addedge(x,y); addedge(y,x); }}int Size,num;void dfs(int u){ Size++; color_num[val[u]]++; num = max(num,color_num[val[u]]); for(int i=head[u];i!=-1;i=edges[i].next){ int v = edges[i].to; if(!vis[v]){ vis[v] = 1; dfs(v); } }}void dfs1(int u){ color_num[val[u]] = 0; for(int i=head[u];i!=-1;i=edges[i].next){ int v = edges[i].to; if(vis[v] == 1){ vis[v] = 2; dfs1(v); } }}int main(){ init(); int k; cin >> n >> m >>k; for(int i=1;i<=n;i++) scanf("%d",&val[i]); getmap(m); int sum = 0; for(int i=1;i<=n;i++){ num = Size = 0; if(!vis[i]){ vis[i] = 1; dfs(i); sum += (Size - num); vis[i] = 2; dfs1(i);// cout << Size << ' ' << num << ' ' << sum << endl; } } cout << sum << endl;}
0 0
- Codeforces Round #376 (Div. 2) C dfs+并查集
- Codeforces Round #376 (Div. 2) C 并查集
- Codeforces Round #385 (Div. 2) C. Hongcow Builds A Nation 并查集+贪心+组合学、图论、dfs
- Codeforces Round #376 (Div. 2) C. Socks && 731 C 详解(并查集)
- Codeforces Round #376 (Div. 2)C(DFS)
- Codeforces Round #376 (Div. 2) C. Socks (并查集维护)
- Codeforces Round #376 (Div. 2) C. Socks 并查集+贪心、图论
- Codeforces Round #376 (Div. 2) C. Socks —— 并查集 + 贪心
- Codeforces Round #266 (Div. 2) E codeforces466e(dfs序+并查集)
- Codeforces Round #266 (Div. 2)-E. Information Graph(dfs+并查集)
- Codeforces Round #250 (Div. 2)(B,C,D并查集)
- Codeforces Round #170 (Div. 2)---C. Learning Languages(并查集)
- Codeforces Round #385 (Div. 2) -- C. Hongcow Builds A Nation(并查集)
- Codeforces Round #385 (Div. 2)C. Hongcow Builds A Nation【并查集+贪心】好题~
- Codeforces Round #393 (Div. 2)-C. Pavel and barbecue(并查集)
- Codeforces Round #395 (Div. 2) -- C. Timofey and a tree(并查集+缩点)
- Codeforces Round #423 (Div. 2) C String Reconstruction 并查集
- Codeforces Round #433 (Div. 2, based on Olympiad of Metropolises) C. Planning(并查集)
- Cesium AnimationViewModel API
- 微软windows10 IOT支持PI3之后,树莓派3更是将获得Android官方原生支持
- go结构体的使用
- post和get的区别?
- phpCI框架 如何去掉默认index.php
- Codeforces Round #376 (Div. 2) C dfs+并查集
- setBackgroundResource()与setImageBitmap()以及setBackground()的使用
- 非常有用的Java程序片段
- 优雅的获取TextView用户点击的链接
- 【shader】体积光影
- liunx多终端history命令实时同步
- 从代码看一个程序员的笔力 -- 您所未注意的一些事
- 电脑上的显示输出接口你都认识么
- selenium + PhantomJS 操作