Problem Description
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.
Input
First line of the input contains two integers N and M.
Then following M lines each containing space-separated integers u and v, which denotes an D-line.
Constraints:
< N <=
< M <=
<= u, v < N.
 
Output
Output M lines, the ith line is the answer after deleting the first i edges in the input.
Sample Input

 
Sample Output

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.
 
Source
 

题目的意思是: 一开始有个图(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[N],ans[M];
int a[M],b[M];
void init(){
for(int i=;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)==){
init();
for(int i=;i<m;i++){
scanf("%d%d",&a[i],&b[i]);
}
ans[m-]=n;
int k=m-;
for(int i=m-;i>=;i--){
if(merge(a[i],b[i])){
ans[k-]=ans[k]-;
}else{
ans[k-]=ans[k];
}
k--;
}
for(int i=;i<m;i++){
printf("%d\n",ans[i]);
} } return ;
}

hdu 4496 D-City(并查集)的更多相关文章

  1. HDU 4496 D-City (并查集)

    题意:有n个城市,m条路,首先m条路都连上,接着输出m行,第i行代表删除前i行的得到的连通块个数 题解:正难则反,我们反向考虑使用并查集添边.首先每个点都没有相连,接着倒着来边添加边计算,当两个点父节 ...

  2. HDU 4496 D-City —— (并查集的应用)

    给出n个点和m条边,一条一条地删除边,问每次删除以后有多少个联通块. 分析:其实就是并查集的应用,只是前一阵子一直做图论思路一直囿于tarjan了..方法就是,记录每一条边,然后从最后一条边开始不断的 ...

  3. HDU 4496 D-City (并查集,水题)

    D-City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  4. hdu 4496 其实还是并查集

    Problem Description Luxer is a really bad guy. He destroys everything he met. One day Luxer went to ...

  5. HDU 1811 拓扑排序 并查集

    有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...

  6. hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)

    hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...

  7. hdu 3635 Dragon Balls(并查集)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  8. hdu 2480 贪心+简单并查集

    Steal the Treasure Time Limit: 10000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  9. <hdu - 1232> 畅通工程 并查集问题 (注意中的细节)

    本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232  结题思路:因为题目是汉语的,那我就不解释题意了,要求的是最少建设的道路,我们可以用并查集来做这 ...

随机推荐

  1. 关于 require的缓存

    有两个文件 a.js内容如下: var add = require("./t.js").add; var add2 = require("./t.js").ad ...

  2. 设置MySQL数据表主键

    设置MySQL数据表主键: 使用“primary key”关键字创建主键数据列.被设置为主键列不允许出现重复的值,很多情况下与“auto_increment”递增数字相结合.如下SQL语句所示: My ...

  3. 查看mysql apache php nginx的编译参数

    查看mysql编译参数: cat /usr/local/mysql/bin/mysqlbug|grep configure 查看apache编译参数: cat /usr/local/apache2/b ...

  4. Echarts动态数据显示

    自己慢慢摸索出来的,留着以后可能会用到 一.先引入jquery,再引入echarts.js 二.配置容器 三.配置路径和数据选项等 <script type="text/javascr ...

  5. hdu2317Nasty Hacks

    Problem Description You are the CEO of Nasty Hacks Inc., a company that creates small pieces of mali ...

  6. C与C++

    有幸在某网站发现这篇文章,读罢,觉得蛮有道理,发来大家一起共勉之! 总是被同学们问到,如何学习C和C++才不茫然,才不是乱学,想了一下,这里给出一个总的回复. 一家之言,欢迎拍砖哈. 1.可以考虑先学 ...

  7. mybatis使用order by注意

    直接用动态参数生成,不会排序: <if test="orderColumn!=null and orderColumn != ''"> ORDER BY #{order ...

  8. Android 使用网络ADB调试.

    前提: android Phone和PC在同一局域网内. android Phone 有虚拟终端(CM系统集成了ADB网络调试,比较赞.); 1.在android phone虚拟终端输入 stop a ...

  9. AngularJS:何时应该使用Directive、Controller、Service?【新手必看】

    (这篇文章你们一定要看,尤其初学的人,好吗亲?) 大漠穷秋 译 AngularJS是一款非常强大的前端MVC框架.同时,它也引入了相当多的概念,这些概念我们可能不是太熟悉.(译者注:老外真谦虚,我大天 ...

  10. ecos3.0编译 if_lancepci.c:528: 错误: 赋值运算的左操作数必须是左值

    /home/xin/ecos3/ecos-3.0/packages/devs/eth/amd/lancepci/v3_0/src/if_lancepci.c:528: 错误: 赋值运算的左操作数必须是 ...