HDU 4496 D-City (并查集)
来源:互联网 发布:运营淘宝店 编辑:程序博客网 时间:2024/05/07 03:30
题意:有n个城市,m条路,首先m条路都连上,接着输出m行,第i行代表删除前i行的得到的连通块个数
题解:正难则反,我们反向考虑使用并查集添边。首先每个点都没有相连,接着倒着来边添加边计算,当两个点父节点相同时连通块不变,否则-1
#include<set>#include<map>#include<queue>#include<stack>#include<cmath>#include<vector>#include<string>#include<cstdio>#include<cstring>#include<stdlib.h>#include<iostream>#include<algorithm>using namespace std;#define eps 1E-8/*注意可能会有输出-0.000*/#define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型#define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化#define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0#define mul(a,b) (a<<b)#define dir(a,b) (a>>b)typedef long long ll;typedef unsigned long long ull;const int Inf=1<<28;const double Pi=acos(-1.0);const int Mod=1e9+7;const int Max=10010;int fat[Max];struct node{ int xx1,yy1;}lin[Max<<4];int ans[Max<<4];void Init(int n){ for(int i=0;i<=n;i++) fat[i]=i; return;}int Find(int x){ if(x==fat[x]) return fat[x]; return fat[x]=Find(fat[x]);}int Union(int x,int y){ int x1=Find(x); int y1=Find(y); if(x1==y1) return 0; fat[x1]=y1; return 1;}int main(){ int n,m; while(~scanf("%d %d",&n,&m)) { Init(n); for(int i=0;i<m;i++) scanf("%d %d",&lin[i].xx1,&lin[i].yy1); int num=n; for(int i=m-1;i>=0;i--)//离线倒序 { ans[i]=num; num-=Union(lin[i].xx1,lin[i].yy1); } for(int i=0;i<m;i++) printf("%d\n",ans[i]); } return 0;}
0 0
- HDU 4496 D-City(并查集)
- hdu 4496 D-City 并查集
- HDU 4496 D-CITY(并查集)
- 【并查集】-HDU-4496-D-City
- HDU 4496 D-City(并查集)
- hdu 4496 D-City(并查集)
- 【HDU】4496 D-City 并查集
- hdu 4496 D-City 并查集
- hdu 4496 D-City(并查集)
- 并查集--HDU - 4496 D-City
- hdu 4496 D-City 并查集
- hdu 4496 D-City (并查集)
- hdu 4496 D-City(并查集)
- HDU 4496 D-City (并查集)
- hdu 4496 D-City (并查集)
- HDU 4496 D-City(并查集)
- hdu 4496 D-City(并查集逆向思维)
- HDU-4496 D-City (并查集)
- 【读书笔记《Android游戏编程之从零开始》】19.游戏开发基础(游戏音乐与音效)
- 2016北京石油展完美落幕 劲达IBM智能电池组亮相展会
- php计算时间差/两个时间日期相隔的天数,时,分,秒
- 获取git的标签
- Correlation Filter in Visual Tracking系列一:Visual Object Tracking using Adaptive Correlation Filters 论
- HDU 4496 D-City (并查集)
- 苹果IAP开发中的那些坑和掉单问题
- 腾讯2017年实习生编程题目(第二题)----2.算法基础-字符移位 小Q最近遇到了一个难题:把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,且不能申请额外的空间。
- Spring应用上下文配置:java编程配置
- 箭头发射后根据重力来决定箭头的朝向旋转方向
- jQuery HTML相关的基础知识梳理
- Egret深入了解屏幕适配
- POJ 3259_SPFA_判断是否含负权环
- Android opencv 图片处理