D-City

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

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
思路:并查集应用,处理是倒过来处理。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int father[], a[], b[], block[];
int find(int x)
{
return x == father[x] ? x : father[x] = find(father[x]);
} void unit(int x, int y)
{
int a = find(x);
int b = find(y);
father[a] = father[b];
return ;
} int main(int argc, char const *argv[])
{
int n, m;
//freopen("in.c", "r", stdin);
while(~scanf("%d%d", &n, &m))
{
memset(block, , sizeof(block));
for(int i = ; i < n; i ++)
father[i] = i;
for(int i = ; i < m; i ++)
scanf("%d%d", &a[i], &b[i]);
for(int i = m-; i >= ; i --)
{
block[i] = n;
if(find(a[i]) != find(b[i]))
{
n--;
unit(a[i], b[i]);
}
}
for(int i = ;i < m; i ++)
printf("%d\n", block[i]);
}
return ;
}

HDU -- 4496的更多相关文章

  1. HDU 4496 并查集 逆向思维

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

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

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

  3. hdu 4496(并查集)

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

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

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

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

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

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

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

  7. hdu 4496 (并差集)

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

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

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

  9. HDU - 4496 City 逆向并查集

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

随机推荐

  1. [配置文件] C#修改App.config,Web.config文件帮助类,ConfigHelper (转载)

    点击下载 ConfigHelper-sufei.rar 主要功能如下 .根据Key取Value值 .根据Key修改Value .添加新的Key ,Value键值对 .根据Key删除项 /// < ...

  2. C#当中的多线程_任务并行库(上)

    复习: 第三章内容中我们提到了三种异步编程模型,这里简单复习一下,分别如下 1.APM(异步编程模式):形如Beginxxx,Endxxx. 2.EAP(基于事件的异步编程模式):这个我们在.net中 ...

  3. eclipse和cygwin搭建C++环境的修正版本

    最近嫌弃切换系统麻烦.用了cygwin作为netbeans下C++的环境继续学习.我学的很渣,就不卖弄了. 网络上有很多这样的文章.经过对比和实验发现一个深坑.教程部分还是跟其他的一样,重点部分红字做 ...

  4. Gtest打桩函数

    假设Client的定义如下 class Client { ...... public: virtual bool GetData(std::string& data); ...... }; 我 ...

  5. php 用于检测是PC还是手机访问

    <?php$ua = strtolower($_SERVER['HTTP_USER_AGENT']);$uachar = "/(iphone|android|phone|mobile| ...

  6. React Native:使用 JavaScript 构建原生应用 详细剖析

    数月前,Facebook 对外宣布了正在开发的 React Native 框架,这个框架允许你使用 JavaScript 开发原生的 iOS 应用——就在今天,Beta 版的仓库释出了! 基于 Pho ...

  7. AS3.0声明静态属性和静态方法

    静态属性的变量声明要加static,static var 名称:属性类型=值 静态属性的常量声明要加static或者const,并在声明时就要赋值.static||const var 名称:属性类型= ...

  8. 国内国外最好的java开发论坛及站点 [转]

    国内: www.chinajavaworld.com -论坛人很多,高手也多,不过好像都在潜水    www.cn-java.com -也很不错,文章很好,但是就是商业性浓了点.    www.chi ...

  9. PHPCMS实现文章置顶功能的方法

    我个人喜欢把PHPCMS当作博客来用,而作为一个博客,怎能少了文章置顶功能呢?其中用PHPCMS实现置顶功能非常简单,无非是修改下推荐位的名称为置顶,然后在文章列表中推送需要置顶的文章罢了. 不过博客 ...

  10. WPF 分页控件 WPF 多线程 BackgroundWorker

    WPF 分页控件 WPF 多线程 BackgroundWorker 大家好,好久没有发表一篇像样的博客了,最近的开发实在头疼,很多东西无从下口,需求没完没了,更要命的是公司的开发从来不走正规流程啊, ...