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

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

代码如下:

#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. Volley : "参数param:{ inoutNo:inoutNo ,whcode:’’}

    private void fuzzySearch() { mRequestQueue = Volley.newRequestQueue(getActivity()); String str = Sha ...

  2. 【转】很有用但鲜有人知的 Linux 命令

    Linux命令行吸引了大多数Linux爱好者.一个正常的Linux用户一般掌握大约50-60个命令来处理每日的任务.Linux命令和它们的转换对于Linux用户.Shell脚本程序员和管理员来说是最有 ...

  3. HDU 5366 The mook jong (简单DP)

    题意:ZJiaQ希望把木人桩摆在自家的那个由1*1的地砖铺成的1*n的院子里.由于ZJiaQ是个强迫症,所以他要把一个木人桩正好摆在一个地砖上,由于木人桩手比较长,所以两个木人桩之间地砖必须大于等于两 ...

  4. 【英语】Bingo口语笔记(39) - Get系列

  5. TextView及其子类

    1.TextView控件(TextView是EditView.Button等类的父类) <1>android:id   给当前控件定义了一个唯一标识符 <2>android:l ...

  6. PHP的cURL库:抓取网页,POST数据及其他,HTTP认证 抓取数据

    From : http://developer.51cto.com/art/200904/121739.htm 下面是一个小例程: ﹤?php// 初始化一个 cURL 对象$curl = curl_ ...

  7. 解析AFNetWorking 网络框架(二)

    转:http://blog.csdn.net/andy_jiangbin/article/details/17114989 接着前面写. 本帖先讲AFURLConnectionOperation,它是 ...

  8. C++模板实例掌握

    前段时间重新学习C++,主要看C++编程思想和C++设计新思维.对模版的使用有了更进一层的了解,特总结如下: 下面列出了模版的常用情况:  << '\n';} //参考:http://ww ...

  9. 顺序表的基本操作(C)

    在顺序存储结构实现基本操作:初始化.创建.插入.删除.查找.遍历.逆置.合并运算. 运行示例: 请输入线性表La的长度: 请输入线性表La中的元素(共5个) *** 此时线性表La中的元素 *** * ...

  10. Ubuntu/CentOS使用BIND配置DNS服务器

    ------ubuntu server 12---------- 1.安装bind9 sudo apt-get -y install bind9 bind9utils 2.配置  /etc/bind/ ...