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

AC代码

#include <cstdio>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <cstring>
#include <utility>
#include <string>
#include <iostream>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
#pragma comment(linker, "/STACK:1024000000,1024000000")
#define eps 1e-10
#define inf 0x3f3f3f3f
#define PI pair<int, int>
typedef long long LL;
const int maxn = 10000 + 5;
int p[maxn], vis[maxn];
PI a[maxn*10];

int find(int x) {
	return p[x] == x ? x : p[x] = find(p[x]);
}

bool unionset(int x, int y) {
	int rx = find(x), ry = find(y);
	if(rx != ry) {
		p[rx] = ry;
		return true;
	}
	return false;
}

int main() {
	int n, m;
	while(scanf("%d%d", &n, &m) == 2) {
		for(int i = 0; i < n; ++i) p[i] = i;
		int u, v;
		for(int i = 0; i < m; ++i) {
			scanf("%d%d", &u, &v);
			a[i] = make_pair(u, v);
		}
		stack<int>ans;
		for(int i = m-1; i >= 0; --i) {
			ans.push(n);
			if(unionset(a[i].first, a[i].second)) --n;
		}
		while(!ans.empty()) {
			printf("%d\n", ans.top());
			ans.pop();
		}
	}
	return 0;
} 

如有不当之处欢迎指出!

HDU - 4496 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_4496_逆向并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=4496 逆向并查集,先读取,然后从后向前join每次保存答案即可. #include<iostream> ...

  7. ZOJ 3261 Connections in Galaxy War(逆向并查集)

    参考链接: http://www.cppblog.com/yuan1028/archive/2011/02/13/139990.html http://blog.csdn.net/roney_win/ ...

  8. HDU 1811 拓扑排序 并查集

    有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...

  9. zoj 3261 逆向并查集+离线处理

    题意:给出一些点,每个点有权值,然后有一些边,相连.无向的.然后有一些操作 链接:点我 query a.表示从a出发的能到达的所有点权值最大的点的编号(相同取编号最小,而且权值要比自己大) desto ...

随机推荐

  1. HTML中padding和margin的区别和用法

     margin(外边距) 定义:margin是用来隔开元素与元素的间距,发生在元素本身的外部,margin用于布局分开元素使元素与元素互不相干. 提示:margin: top right bottom ...

  2. sqlserver存储过程及mybatis调用——待续

    创建带输入参数存储过程 use yanantestgoif exists (select * from sys.objects where name='yanan')drop procedure ya ...

  3. Linux 中su和sudo命令的几个注意点

    1 su与su - 的区别 1.1命令说明 su对应是是no-login shell的方式进行账号登陆,命令行的变量配置还是切换账号前的变量. su-对应的是login shell的方式进行账号登陆, ...

  4. 怎样查看MYSQL数据库的端口号

    show variables like '%port%';

  5. zabbix-proxy搭建

    环境: 因为公司需要监控远程客户机,但server端无法主动连接agent端,客户端可以连接公司ip 公司有固定ip,可以开放某个端口给zabbixserver,客户机agent端可以主动通过外网连接 ...

  6. sqlserver数据库使用技巧(一)--限制数据库的大小

    如何限制数据库的大小? 随着数据库的使用,他占用的空间会越来越大,为了便于资源的合理分配和管理,我们可以限制其最大的大小,这个建议只在测试环境使用 具体操作如下: 打开sqlserver数据库管理工具 ...

  7. Akamai CDN

    Akamai CDN中的几个重要组件 mapping system 调度系统(映射client到edge cluster,进而到edge server) edge server platform 边缘 ...

  8. @RequestMapping注解

    Spring MVC中用于参数绑定的注解有很多,都在org.springframework.web.bind.annotation包中,根据它们处理的request的不同内容部分可以分为四类(主要讲解 ...

  9. Directx3d绘制包围体并控制光照效果

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  10. 安装php扩展phpredis

    下载phpredis-master.tar.gz下载地址:http://pan.baidu.com/s/1i37R8TB 解包tar zxvf phpredis-master.tar.gzcd php ...