题意:给定一个图,问你每次删除一条边后有几个连通块。

析:水题,就是并查集的运用,倒着推。

代码如下:

#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
using namespace std ;
typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1e4 + 5;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
int p[maxn];
int e1[maxn*10], e2[maxn*10];
int Find(int x){ return x == p[x] ? x : p[x] = Find(p[x]); }
int ans[maxn*10]; int main(){
while(scanf("%d %d", &n, &m) == 2){
for(int i = 0; i < n; ++i) p[i] = i; for(int i = 0; i < m; ++i)
scanf("%d %d", &e1[i], &e2[i]); int cnt = n;
for(int i = m-1; i >= 0; --i){
ans[i] = cnt;
int x = Find(e1[i]);
int y = Find(e2[i]);
if(x != y){
p[y] = x;
--cnt;
}
} for(int i = 0; i < m; ++i)
printf("%d\n", ans[i]);
}
return 0;
}

HDU 4496 D-City (并查集)的更多相关文章

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

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

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

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

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

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

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

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

  5. hdu 4496 其实还是并查集

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

  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 3635 Dragon Balls(并查集)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  9. hdu 2480 贪心+简单并查集

    Steal the Treasure Time Limit: 10000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

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

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

随机推荐

  1. 如何在不同编程语言中获取现在的Unix时间戳(Unix timestamp)?

    Java time JavaScript Math.round(new Date().getTime()/1000) 之所以除以1000是因为getTime()返回数值的单位是毫秒 Microsoft ...

  2. (转)FirstResponder 释放问题

    FirstResponder 释放问题 转自:http://www.cnblogs.com/smileEvday/archive/2012/03/18/2405190.html View的FirstR ...

  3. struts2自定义声明校验器

    1 //新建一个validators.xml在src根资源下,会覆盖default.xml的validators,所以你懂得 //接着,若使用声明式校验,则要把配置文件xxxAction-valida ...

  4. matplotlib 中文问题

    matplotlib的缺省配置文件中所使用的字体无法正确显示中文.为了让图表能正确显示中文,主要有三种设置中文的方法: (1)直接读取指定的字体文件.这种方法的依赖性最小,缺点是要指定字体文件的路径. ...

  5. 【英语】Bingo口语笔记(29) - Run系列

  6. JavaScript对象 + Browser 对象 + HTML DOM 对象

    JavaScript 对象 Array Boolean Date Math Number String RegExp Global Browser 对象 Window Navigator Screen ...

  7. SpringMVC注解@initbinder解决类型转换问题

    在使用SpringMVC的时候,经常会遇到表单中的日期字符串和JavaBean的Date类型的转换,而SpringMVC默认不支持这个格式的转换,所以需要手动配置,自定义数据的绑定才能解决这个问题.在 ...

  8. Datawindow.net 子数据窗口出错

    devexpress通过标签页面打开用户控件,数据窗口嵌入用户控件中.当点击数据窗口含下拉数据窗口字段时出现如下错误: 未处理 System.NullReferenceException Messag ...

  9. MYSQL中delete删除多表数据

    MYSQL中delete删除多表数据 DELETE删除多表数据,怎样才能同时删除多个关联表的数据呢?这里做了深入的解释: 1. delete from t1 where 条件 2.delete t1 ...

  10. C# winform打印总结 z

    http://blog.csdn.net/jing_xin/article/details/41444063 针对BEIYANG收据打印机 BTP-R580测试通过. 操作说明:http://www. ...