hdu 4496 D-City(并查集)
2015-11-25 18:12
295 查看
[align=left]Problem Description[/align]
[align=left]Input[/align]
[align=left]Output[/align]
[align=left]Sample Input[/align]
[align=left]Sample Output[/align]
Hint
[align=left]Source[/align]
2013 ACM-ICPC吉林通化全国邀请赛——题目重现
题目的意思是: 一开始有个图(n个点 m条边),然后从第0条边开始删除,(0~m条边依次删除),然后每删除一条边统计依次有多少个连通块。
做法是: 直接从最后一条边开始枚举,依次往前面添加合并边,统计连通块即可。具体看代码,应该看得懂。
View Code
Luxer is a really bad guy. He destroys everything he met. One day Luxer went to D-city. D-city has N D-points and M D-lines. Each D-line connects exactly two D-points. Luxer will destroy all the D-lines. The mayor of D-city wants to know how many connected blocks of D-city left after Luxer destroying the first K D-lines in the input. Two points are in the same connected blocks if and only if they connect to each other directly or indirectly.
[align=left]Input[/align]
First line of the input contains two integers N and M. Then following M lines each containing 2 space-separated integers u and v, which denotes an D-line. Constraints: 0 < N <= 10000 0 < M <= 100000 0 <= u, v < N.
[align=left]Output[/align]
Output M lines, the ith line is the answer after deleting the first i edges in the input.
[align=left]Sample Input[/align]
5 10 0 1 1 2 1 3 1 4 0 2 2 3 0 4 0 3 3 4 2 4
[align=left]Sample Output[/align]
1 1 1 2 2 2 2 3 4 5
Hint
The graph given in sample input is a complete graph, that each pair of vertex has an edge connecting them, so there's only 1 connected block at first. The first 3 lines of output are 1s because after deleting the first 3 edges of the graph, all vertexes still connected together. But after deleting the first 4 edges of the graph, vertex 1 will be disconnected with other vertex, and it became an independent connected block. Continue deleting edges the disconnected blocks increased and finally it will became the number of vertex, so the last output should always be N.
[align=left]Source[/align]
2013 ACM-ICPC吉林通化全国邀请赛——题目重现
题目的意思是: 一开始有个图(n个点 m条边),然后从第0条边开始删除,(0~m条边依次删除),然后每删除一条边统计依次有多少个连通块。
做法是: 直接从最后一条边开始枚举,依次往前面添加合并边,统计连通块即可。具体看代码,应该看得懂。
#pragma comment(linker, "/STACK:1024000000,1024000000") #include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<math.h> #include<algorithm> #include<queue> #include<set> #include<bitset> #include<map> #include<vector> #include<stdlib.h> #include <stack> using namespace std; #define PI acos(-1.0) #define max(a,b) (a) > (b) ? (a) : (b) #define min(a,b) (a) < (b) ? (a) : (b) #define ll long long #define eps 1e-10 #define MOD 1000000007 #define N 10006 #define M 100006 #define inf 1e12 int n,m; int fa ,ans[M]; int a[M],b[M]; void init(){ for(int i=0;i<N;i++){ fa[i]=i; } } int find(int x){ return fa[x]==x?x:fa[x]=find(fa[x]); } bool merge(int x,int y){ int root1=find(x); int root2=find(y); if(root1==root2) return false; fa[root1]=root2; return true; } int main() { while(scanf("%d%d",&n,&m)==2){ init(); for(int i=0;i<m;i++){ scanf("%d%d",&a[i],&b[i]); } ans[m-1]=n; int k=m-1; for(int i=m-1;i>=0;i--){ if(merge(a[i],b[i])){ ans[k-1]=ans[k]-1; }else{ ans[k-1]=ans[k]; } k--; } for(int i=0;i<m;i++){ printf("%d\n",ans[i]); } } return 0; }
View Code
相关文章推荐
- 黑马程序员——泛型
- Web前端
- -lt -gt -ge -le -eq的意义
- [leetcode] Best Time to Buy and Sell Stock
- 一个标准的ECharts代码
- 黑马程序员——Java基础——集合(四)
- 《Constellations》
- 《Disk Storage》
- org.apache.hadoop.hdfs.protocol.AlreadyBeingCreatedException
- Android中的Fragment之间如何通信
- 秒杀系统数据同步(保证不多卖)
- mfc dll
- springMVC获取file,几种转换
- java多线程
- Clicker
- 針對新版OWASP十大網頁應用弱點逐一查驗
- libpcap在centos安装
- jquery基础
- ECharts切换主题
- java基础<八>-------多态