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. git补充(命令)转自https://github.com/Wasdns/github-example-repo

    在使用命令行进行提交时,通常使用git commit -m '注释信息'来填写commit注释信息,但是-m参数适合单行注释,对于多行的commit注释来说是不合适的.这里推荐使用git commit ...

  2. SSH 三大框架整合

    Spring整合web项目 在Servlet当中直接加载配置文件,获取对象 存在问题 每次请求都会创建一个Spring的工厂,这样浪费服务器资源,应该一个项目只有一个Spring的工厂. 在服务器启动 ...

  3. eclipse导入maven项目有红叉及pom.xml出错的问题的解决

    导入我们的项目的时候总会出现很多红叉,看着很难受,其实可以解决的(本人使用方法一解决)解决方法:1.先build project,然后右键项目->maven->update project ...

  4. OpenJudge计算概论-大象喝水

    /*========================================================= 大象喝水 总时间限制: 1000ms 内存限制: 65536kB 描述 一只大象 ...

  5. throws和throw的使用

    throws 用在方法定义上 后面跟一个或者多个异常名称 如果是多个异常名称,之间使用","隔开 , 表达的意思是给该方法添加一个或者多个异常声明; 告诉调用者该方法可能会出现问题 ...

  6. pytorch加载数据的方法-没弄,打算弄

    参考:https://www.jianshu.com/p/aee6a3d72014 # 网络,netg为生成器,netd为判别器 netg, netd = NetG(opt), NetD(opt) # ...

  7. python argparse例子实践

    python 解析命令行读取参数,在多个文件或者不同语言协同的项目中,python脚本经常需要从命令行直接读取参数. 万能的python自带了sys.arg.argparse.optparse模块等, ...

  8. .NET CORE添加引用包

    一 .添加服务端引用 1.直接右键添加项目引用,添加后再csproj文件中如下②,也可以直接用这种格式写入csproj文件中 2.直接右键添加DLL引用,添加后再csproj文件中如下③,也可以直接用 ...

  9. Spring Boot学习笔记——Spring Boot与MyBatis的集成(项目示例)

    1.准备数据库环境 # 创建数据库 CREATE DATABASE IF NOT EXISTS zifeiydb DEFAULT CHARSET utf8 COLLATE utf8_general_c ...

  10. 【leetcode_easy】598. Range Addition II

    problem 598. Range Addition II 题意: 第一感觉就是最小的行和列的乘积即是最后结果. class Solution { public: int maxCount(int ...