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. javaWeb项目部署到阿里云服务器

    1.云服务器中配置了jdk+eclipse+tomcat 2.将项目导出War到安装的Tomcat的webapps文件夹中: 右击项目->Export->WarFile->Desti ...

  2. 用了pm2之后,如何让console.log('..')出现在控制台

    使用  pm2 log 0 可以查看.当然这个 0 应该是应用的 id

  3. 猎豹网校C++ Primer学习笔记

    1.头文件(15th课) 大型项目开发,要有很多头文件.只能写声明,不能定义(类定义和常量定义可以). 自己新建头文件(类定义,外部变量声明,函数声明).源文件包含对应的头文件. 头文件里写类的声明, ...

  4. 关于Android studio下V4包 KeyEventCompat 类找不到问题

    V4包 KeyEventCompat 类找不到问题   本文链接:https://blog.csdn.net/shanshan_1117/article/details/84344557 今天我把su ...

  5. Java 数组元素逆序Reverse的三种方式

    Java 数组元素逆序Reverse的三种方式   本文链接:https://blog.csdn.net/xHibiki/article/details/82930521 题目 代码实现 说明 int ...

  6. Flex 布局的最简单表单

    http://www.ruanyifeng.com/blog/2018/10/flexbox-form.html https://www.cnblogs.com/grt322/p/8531882.ht ...

  7. SSM配置基于注解AOP

    pom.xml <dependency> <groupId>org.springframework</groupId> <artifactId>spri ...

  8. (十)java虚拟机性能监控工具

    一. jps(Java Virtual Machine Process Status Tool) jps主要用来输出JVM中运行的进程状态信息.语法格式如下: 命令行参数选项说明如下: 1.1 案例 ...

  9. 微信支付 composer 方法 --- 实测有效

    <h1 align="center">Pay</h1> <p align="center"> <a href=&quo ...

  10. react 生命周期函数的一些心得体会

    一.理论 组件本质上是状态机,输入确定,输出一定确定 生命周期的三个阶段,三者时间是不固定的,只是在逻辑上的分类: 二.初始化阶段: getDefaultProps:获取实例的默认属性(即使没有生成实 ...