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 2 space-separated integers u and v, which denotes an D-line. 

Constraints: 

0 < N <= 10000 

0 < M <= 100000 

0 <= u, v < N.

Output

Output M lines, the ith line is the answer after deleting the first i edges in the input.

Sample Input

5 10 0 1 1 2 1 3 1 4 0 2 2 3 0 4 0 3 3 4 2 4

Sample Output

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.

题意:这个小家伙每次删除一条边(初始时每对节点间都有边相连),问每次删除一条边后有几个联通块。

思路:比较基础的并查集应用,不明白的可以看这篇博客并查集 ,并查集是相连在一起,这题需要反着来,不断加边,用数组记录下联通块的数量,最后输出即可。

#include<cstdio>
#include<cstring>
#include <iostream>
#include<algorithm>
using namespace std;
const int N=10005;
const int M=100005;
int pre[N],r[N],s[M];
int n,m;
struct node
{
int x,y;
}a[M];
void init(int n)
{
for(int i = 0;i < n;++i)
{
pre[i]=i;
r[i]=0;
}
} int findpre(int x)
{
if(pre[x] == x)
return x;
return pre[x] = findpre(pre[x]);
} void join(int x,int y)
{
x = findpre(x);
y = findpre(y);
if(x == y)
return;
if(r[x] < r[y])
pre[x] = y;
else
{
pre[y] = x;
if(r[x] == r[y])
r[x]++;
}
} bool same(int x,int y)
{
return findpre(x) == findpre(y);
} int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i = 0; i < m; ++i)
scanf("%d%d",&a[i].x,&a[i].y);
init(n);
int ans = n;
for(int i = m-1; i >= 0; --i)
{
s[i] = ans;
if(!same(a[i].x,a[i].y))
--ans;
join(a[i].x,a[i].y);
}
for(int i = 0; i < m; ++i)
printf("%d\n",s[i]);
}
return 0;
}

HDU4496 D-City【基础并查集】的更多相关文章

  1. hdu 1829 基础并查集,查同性恋

    A Bug's Life Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  2. poj-2236 Wireless Network &&poj-1611 The Suspects && poj-2524 Ubiquitous Religions (基础并查集)

    http://poj.org/problem?id=2236 由于发生了地震,有关组织组把一圈电脑一个无线网,但是由于余震的破坏,所有的电脑都被损坏,随着电脑一个个被修好,无线网也逐步恢复工作,但是由 ...

  3. AOJ 2170 Marked Ancestor (基础并查集)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=45522 给定一棵树的n个节点,每个节点标号在1到n之间,1是树的根节点,有如 ...

  4. poj2236 基础并查集

    题目链接:http://poj.org/problem?id=2236 题目大意:城市网络由n台电脑组成,因地震全部瘫痪,现在进行修复,规定距离小于等于d的电脑修复之后是可以直接相连 进行若干操作,O ...

  5. 基础并查集poj2236

    An earthquake takes place in Southeast Asia. The ACM (Asia Cooperated Medical team) have set up a wi ...

  6. HDU - 4496 City 逆向并查集

    思路:逆向并查集,逆向加入每一条边即可.在获取联通块数量的时候,直接判断新加入的边是否合并了两个集合,如果合并了说明联通块会减少一个,否则不变. AC代码 #include <cstdio> ...

  7. CodeForces - 827A:String Reconstruction (基础并查集)

    Ivan had string s consisting of small English letters. However, his friend Julia decided to make fun ...

  8. hdu1325 Is It A Tree? 基础并查集

    #include <stdio.h> #include <string.h> ], g[]; int find(int x) //并查集的查找,找到共同的父亲 { if (f[ ...

  9. HDU1213How Many Tables(基础并查集)

    HDU1213How Many Tables Problem Description Today is Ignatius' birthday. He invites a lot of friends. ...

随机推荐

  1. Android开发之接收系统广播消息

    BroadcastReceiver除了接收用户所发送的广播消息之外.另一个重要的用途:接收系统广播. 假设应用须要在系统特定时刻运行某些操作,就能够通过监听系统广播来实现.Android的大量系统事件 ...

  2. javascript学习---BOM

    1.top是顶级的框架,也就是浏览器窗口. 2.window.close()只能关闭window.open()打开的窗口. 3.firefox不支持修改状态栏,firefox3后强制始终在弹出窗口中显 ...

  3. CentOS常用基础命令大全

    这篇文章主要介绍了CentOS常用基础命令大全,学习centos的朋友需要掌握的知识,需要的朋友可以参考下 1.关机 (系统的关机.重启以及登出 ) 的命令shutdown -h now 关闭系统(1 ...

  4. 【USACO07FEB】 Cow Relays

    [题目链接]              点击打开链接 [算法]            朴素算法,就是跑N-1遍floyd            而满分算法就是通过矩阵快速幂加速这个过程 [代码]   ...

  5. 6 WPF控件

    WPF控件分类: 内容控件 标题内容控件 文本控件 列表控件 基于范围的控件 日期控件 控件类 控件是与用户交互的元素.控件可以获得焦点,能接受键盘或鼠标的输入. 所有控件的基类是System.Win ...

  6. [noi.ac_D1T2]sort

    https://www.zybuluo.com/ysner/note/1289967 题面 定义"翻转排序":每次操作均为把区间\([l,r]\)中所有数倒过来,即\(swap(a ...

  7. 14_传智播客iOS视频教程_OC的数据类型

    对比一下OC和C差别,首先第一个是数据类型. C语言的数据类型分哪几类?C语言有哪些数据类型? 基本数据类型当然还包括int的一些修饰符.像short.long.long long.unsigned. ...

  8. PCB DotNetCore Swagger生成WebAPI文档配置方法

    在.net framework框架下可以使用WebApiTestClientWebApi生成WebAPI接口文档与方便接口测试用,而在DotnetCore却没有找到这个工具了,baidu查找一下发现有 ...

  9. [Swift通天遁地]一、超级工具-(13)使用PKHUD制作各种动态提示窗口

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  10. GG_Model 类库与数据库表对应建立实体类

    3.4.GG_Model 类库与数据库表对应建立实体类 我这里不教大家写代码,直接用TT模板自动生成,省去写代码的麻烦. A. 三个文件MysqlDbhelper.ttinclude .mysqlMa ...