D-City

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 3838    Accepted Submission(s): 1379

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.

 
题目大意:逆行并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496
 
递归代码:
#include <iostream>
#include <cstdio>
using namespace std;
const
int MAX=1e4+;
const
int MAX1=1e5+;
int
f[MAX];
int
sum[MAX1];
struct
ss
{

int
a,b;
}
s[MAX1];
int
find(int n)
{

if
(n!=f[n])
f[n]=find(f[n]);
return
f[n];
}

int
main()
{

int
n,m;
while
(cin>>n>>m)
{

for
(int i=;i<n;i++)
f[i]=i;
for
(int i=;i<m;i++)
scanf("%d%d",&s[i].a,&s[i].b);
sum[m]=n;
for
(int i=m-;i>=;i--)
{
int a1=find(s[i].a);
int
a2=find(s[i].b);
if
(a1!=a2)
{
f[a1]=a2;sum[i]=sum[i+]-;}
else

sum[i]=sum[i+];
}

for
(int i=;i<=m;i++)
cout<<sum[i]<<endl;
}
}
递推代码:
#include <iostream>
#include <cstdio>
using namespace std;
const
int MAX=1e4+;
const
int MAX1=1e5+;
int
f[MAX];
int
sum[MAX1];
struct
ss
{

int
a,b;
}
s[MAX1];
int
find(int n)
{

int
res=n;
while
(n!=f[n])
n=f[n];
int
j;
while
(res!=n)
{

j=f[res];
f[res]=n;
res=j;
}

return
n;
}

int
main()
{

int
n,m;
while
(cin>>n>>m)
{

for
(int i=;i<n;i++)
f[i]=i;
for
(int i=;i<m;i++)
scanf("%d%d",&s[i].a,&s[i].b);
sum[m]=n;
for
(int i=m-;i>=;i--)
{
int a1=find(s[i].a);
int
a2=find(s[i].b);
if
(a1!=a2)
{
f[a1]=a2;find(a1);sum[i]=sum[i+]-;}
else

sum[i]=sum[i+];
}

for
(int i=;i<=m;i++)
cout<<sum[i]<<endl;
}
}
 

hdu-4496-D-City的更多相关文章

  1. HDU 4496 并查集 逆向思维

    给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...

  2. HDU - 4496 City 逆向并查集

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

  3. HDU 4496 D-City(逆向并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=4496 题意: 给出n个顶点m条边的图,每次选择一条边删去,求每次删边后的连通块个数. 思路: 离线处理删边,从后 ...

  4. hdu 4496(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...

  5. 线段树 扫描线 L - Atlantis HDU - 1542 M - City Horizon POJ - 3277 N - Paint the Wall HDU - 1543

    学习博客推荐——线段树+扫描线(有关扫描线的理解) 我觉得要注意的几点 1 我的模板线段树的叶子节点存的都是 x[L]~x[L+1] 2 如果没有必要这个lazy 标志是可以不下传的 也就省了一个pu ...

  6. HDU 4849-Wow! Such City!(最短路)

    Wow! Such City! Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Other ...

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

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

  8. HDU 4496 D-City(并查集,逆思维)

    题目 熟能生巧...常做这类题,就不会忘记他的思路了... //可以反过来用并查集,还是逐个加边,但是反过来输出...我是白痴.....又没想到 //G++能过,C++却wa,这个也好奇怪呀... # ...

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

    题意:给定一个图,问你每次删除一条边后有几个连通块. 析:水题,就是并查集的运用,倒着推. 代码如下: #include <cstdio> #include <string> ...

  10. hdu 4496 (并差集)

    题意:给出一个图,m条边,输出删除前i条边后该图的联通块的个数. 思路:刚开始想着是不是联通问题,后来看明白题意后知道,如果从最后一条边添加的话,答案就会出来了,就是并差集的操作. #include& ...

随机推荐

  1. Atitit 马尔可夫过程(Markov process) hmm隐马尔科夫。 马尔可夫链,的原理attilax总结

    Atitit 马尔可夫过程(Markov process) hmm隐马尔科夫. 马尔可夫链,的原理attilax总结 1. 马尔可夫过程1 1.1. 马尔科夫的应用 生成一篇"看起来像文章的 ...

  2. 投资人谈VR色变,VR好戏却刚刚开始

    去年下半年,资本圈谈O2O色变,以至于创业者们都不敢说自己做O2O:到了今年下半年,资本圈却成为了谈VR色变--在中国的互联网科技创业中,资本市场已经成为了创业的一种风向标.资本走向哪里,创业者就走向 ...

  3. 每天一个linux命令(12):more命令

    more命令,功能类似 cat ,cat命令是整个文件的内容从上到下显示在屏幕上. more会以一页一页的显示方便使用者逐页阅读,而最基本的指令就是按空白键(space)就往下一页显示,按 b 键就会 ...

  4. Liferay7 BPM门户开发之43: Gradle依赖管理

    进入liferay v7.0,官方推荐使用Gradle进行依赖管理和发布,所以必须知道Gradle的用法,网上资料很多,不赘述 只写依赖管理的分类 一般用外部仓库依赖,也可以用本地文件依赖(依赖本地j ...

  5. ROC曲线与AUC值

    本文根据以下文章整理而成,链接: (1)http://blog.csdn.net/ice110956/article/details/20288239 (2)http://blog.csdn.net/ ...

  6. 【资源】C++学习资料 - 逆天整理 - 精华无密版【最新】

    再失效就太无语了,链接都是多份的~~—————————————————基础——————————————C++环境搭建(全套)http://pan.baidu.com/s/1o6y0smY链接:http ...

  7. 有jQuery背景,该如何用AngularJS编程思想?

    "我可以熟练使用jQuery进行客户端应用的开发,但是现在我希望开始使用Angular.js.哪位能描述一下这个过程中必要的模式变化吗?希望您的答案能够围绕下面这些具体的问题: 1. 我如何 ...

  8. C#编写简单的聊天程序

    这是一篇基于Socket进行网络编程的入门文章,我对于网络编程的学习并不够深入,这篇文章是对于自己知识的一个巩固,同时希望能为初学的朋友提供一点参考.文章大体分为四个部分:程序的分析与设计.C#网络编 ...

  9. 云计算之路-阿里云上:Web服务器遭遇奇怪的“黑色30秒”问题

    今天下午访问高峰的时候,主站的Web服务器出现奇怪的问题,开始是2台8核8G的云服务器(ECS),后来又加了1台8核8G的云服务器,问题依旧. 而且3台服务器特地使用了不同的配置:1台是禁用了虚拟内存 ...

  10. Tridiv:基于 Web 的 CSS 编辑器,创建炫丽 3D 图形

    Tridiv 是一个基于 Web 的编辑器,使用 CSS 创建 3D 形状.它提供了一个传统的四个面板的操作界面,给出了从每个平面的视图,以及一个预览窗格中示出的最终的效果.使用 Tridiv 可以创 ...