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.

 逆向思维的重要性!
#include<cstdio>
#include<iostream>
#include<string.h>
#include<stack>
#define maxn 10005
using namespace std;
int
pre[maxn];
int
num;
void
init()
{

for
(int i=;i<maxn;i++) pre[i]=i;
}

int
find(int x)
{

if
(pre[x]==x) return x;
else return
pre[x]=find(pre[x]);
}

void
merge(int x,int y)
{

x=find(x);
y=find(y);
if
(x!=y)
{

num--;
pre[y]=x;
}
}

int
main()
{

cin.sync_with_stdio(false);
int
n,m;
while
(cin>>n>>m)
{

stack<int> fuck,fuck2;
int
ret[m];
while
(m--)
{

int
x,y;
cin>>x>>y;
fuck.push(x),fuck.push(y);
}

init();
num=n;
int
rett=;
while
(!fuck.empty())
{

int
yy=fuck.top();
fuck.pop();
int
xx=fuck.top();
fuck.pop();
ret[rett++]=num;
merge(xx,yy);
}

for
(int i=rett-;i>=;i--) cout<<ret[i]<<endl;
}

return
;
}

hdu 4496 其实还是并查集的更多相关文章

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

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

  2. HDU - 4496 City 逆向并查集

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

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

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

  4. hdu 4496 D-City(并查集)

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

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

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

  6. HDU 1811 拓扑排序 并查集

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

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

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

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

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

  9. HDU 5441 Travel(并查集+统计节点个数)

    http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给出一个图,每条边有一个距离,现在有多个询问,每个询问有一个距离值d,对于每一个询问,计算出有多少点 ...

随机推荐

  1. Oracle 存储过程—为数传递变量

    oracle 存储过程的基本语法create or replace procedure proc1( p_para1 varchar2, p_para2 out varchar2, p_para3 i ...

  2. MIPS 指令集(共31条)

    MIPS 指令集(共31条) MIPS 指令集(共31条) 助记符 指令格式 示例 示例含义 操作及其解释 Bit # 31..26 25..21 20..16 15..11 10..6 5..0 R ...

  3. Golang基础笔记

    <基础> Go语言中的3个关键字用于标准的错误处理流程: defer,panic,recover. 定义一个名为f 的匿名函数: Go 不支持继承和重载. Go的goroutine概念:使 ...

  4. LC 375. Guess Number Higher or Lower II

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

  5. iOS8.0如何使用Touch ID来做验证

    对于Objective-C而言,只要几行代码即可搞定. 比如: #import <LocalAuthentication/LocalAuthentication.h> - (void)vi ...

  6. 用户Ip地址和百度地图api接口获取用户地理位置(经纬度坐标,城市)

    <?php   //获取用户ip(外网ip 服务器上可以获取用户外网Ip 本机ip地址只能获取127.0.0.1) function getip(){     if(!empty($_SERVE ...

  7. Civil 3D百度云地址

    Civil 3D 2018百度云地址 https://pan.baidu.com/s/1edeVhG Civil 3D 2019注册机百度云地址 链接: https://pan.baidu.com/s ...

  8. 使用Lock对象实现同步效果

    Lock是一个接口,为了使用一个Lock对象,需要用到   Lock lock = new ReentrantLock();   与 synchronized (someObject) 类似的,loc ...

  9. ireport如何拼接sql?

    ireport如何拼接sql   ireport如何拼接sql? 解决方法: 1.ireport的sql select * from emp as e $P!{whereSQL}; 2.java代码 ...

  10. python 优雅的解析 jsonp

    一段 jsonp 格式数据 mtopjsonpweexcb1({"api":"mtop.taobao.idle.recycle.nextspunav.get", ...