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

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. linux_磁盘体系

    未曾习艺先学礼,未曾学武先习德 当今磁盘都是温室磁盘,原理是一样的,高速转动的的盘,磁头做径向运动 当今磁盘的发展趋势: 体积更小.速度更快.容量更大.使用更安全 速度更快: 主轴转速: 10000/ ...

  2. 译-BMC Remedy Action Request System权限控制概述

    原文链接:Access control overview 说明: BMC Remedy Action Request System是BMC ITSM产品平台,简称AR 或者Remedy,可实现基于IT ...

  3. python---进程与线程

    进程和线程 什么是线程(thread)什么是进程 线程:操作系统能够进行运算调度的最小单位.它被包含在进程中,是进程中的实际运作单位.是一串指令的集合 一个线程指的是进程中一个单一顺序的控制流,一个进 ...

  4. JSP -java service pages

    jsp的运行机制: 1客户端(请求jsp页面)------->JSP页面(转换为java源文件)------->Selvet文件(.java) (编译成相应的字节码文件也就是后缀名是.cl ...

  5. Codeforce A. Fair Game

    A. Fair Game time limit per test 1 second memory limit per test 256 megabytes input standard input o ...

  6. Centos6.5离线安装lsb_release

    参考 http://www.linuxfromscratch.org/blfs/view/systemd/postlfs/lsb-release.html首先在其他电脑下载lsb_release源码地 ...

  7. bzoj 1930: [Shoi2003]pacman 吃豆豆 [费用流]

    1930: [Shoi2003]pacman 吃豆豆 题意:两个PACMAN吃豆豆.一开始的时候,PACMAN都在坐标原点的左下方,豆豆都在右上方.PACMAN走到豆豆处就会吃掉它.PACMAN行走的 ...

  8. I/O多路转接模型

    body, table{font-family: 微软雅黑; font-size: 13.5pt} table{border-collapse: collapse; border: solid gra ...

  9. windows 下编译 OpenSSL1.0.2l 版

    1.需要的软件工具: microsoft visual studio2013(或2010以后其他版本) Perl 软件, 版本为strawberry - perl - 5.26.0.1 - 64bit ...

  10. 炸金花的JS实现从0开始之 -------现在什么都不会(1)

    新年结束了.回想起来唯一留下乐趣的就是在家和朋友玩玩炸金花. 遂有此文. 对不起,我这时候还没有思路. 让我捋一捋. ... ... 捋一捋啊... ... 好了.今天先这样吧: (1)先整理出所有的 ...