HDU4496(并查集)
D-City
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 3665    Accepted Submission(s): 1306
Problem Description
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
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
Sample Input
Sample Output
//2016.8.9
#include<iostream>
#include<cstdio>
#include<stack> using namespace std; int fa[], arr[][], cnt; int getf(int i)
{
if(fa[i]==i)return i;
return fa[i] = getf(fa[i]);
} int init(int n)
{
for(int i = ; i < n; i++)
fa[i] = i;
} int Merge(int a, int b)
{
int af = getf(a);
int bf = getf(b);
if(af!=bf){
fa[bf] = af;
cnt--;
}
} int main()
{
int n, m, u, v;
while(cin>>n>>m)
{
init(n);
for(int i = ; i < m; i++)
{
scanf("%d%d", &arr[i][], &arr[i][]);
}
stack<int> s;
cnt = n;
for(int i = m-; i >= ; i--)
{
s.push(cnt);
Merge(arr[i][], arr[i][]);
}
while(!s.empty())
{
int ans = s.top();
cout<<ans<<endl;
s.pop();
}
} return ;
}
HDU4496(并查集)的更多相关文章
- hdu4496并查集的删边操作
		题意: 给你一个图,问你删除一些边后还有几个连通快.. 思路: 典型的并查集删边操作,并查集的删边就是先把不删除的边并查集一边(本题没有不删除的边),然后逆序吧所有要删除的边以 ... 
- HDU4496 D-City【基础并查集】
		Problem Description Luxer is a really bad guy. He destroys everything he met. One day Luxer went to ... 
- BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]
		4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ... 
- 关押罪犯 and 食物链(并查集)
		题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ... 
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
		图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ... 
- bzoj1854--并查集
		这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ... 
- [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)
		Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ... 
- [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)
		Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ... 
- 【BZOJ-3673&3674】可持久化并查集       可持久化线段树 + 并查集
		3673: 可持久化并查集 by zky Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 1878 Solved: 846[Submit][Status ... 
随机推荐
- 2016"百度之星" - 资格赛(Astar Round1) Problem D
			排个序,map直接搞. #include <stdio.h> #include <math.h> #include<cstring> #include<cma ... 
- Mysql update语句赋值嵌套与在表列中数据后面增加数据
			1.Mysql update语句赋值嵌套select 点击(此处)折叠或打开 update a set col=(select col from a where id='5') where id&g ... 
- 11.10document对象练习
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ... 
- Android开发之回调函数
			写在前面,最近对回掉函数有了更深刻的认识,现在记录如下.由于在家看不到底层代码,在公司不能访问外网,现在只能凭靠记忆写这篇博文了,写错的地方还希望大神们指出来 其实给组件设置监听就是最典型的回掉函数的 ... 
- Eclipse 打开js文件时出现 Could not open the editor...
			选择 window-->General-->Editors-->File Associations -->(在右边上面的框中选择jsp或者你打开的文件类型)-->(然后在 ... 
- Victor 串口 VCL 控件 - 简单实用, 功能强大的 C++ Builder 串口控件!
			源:Victor 串口 VCL 控件 - 简单实用, 功能强大的 C++ Builder 串口控件! 2014年02月06日发布控件的重要更新版本: Victor 串口控件 1.5.0.2 版本 (包 ... 
- 《SpringMVC数据绑定入门》笔记
			基本类型 最好使用封装类型 简单多数据&多层级对象 简单多数据 单个对象,直接使用属性名=值即可 多层级对象 属性.属性=值即可 同属性多对象 WebDataBinder只在当前类中生效,不是 ... 
- List学习笔记
			List 特点:1.有序.2.可重复. ArrayList: 底层是数组,数组是有下标的. 会自动扩容,底层默认初始化容量是10,扩大之后的容量预设是原来容量的一半(jdk 1.8).以前好像是原容量 ... 
- 分页。php 引用代码
			<?php /** file: page.class.php 完美分页类 Page */ class Page { private $total; //数据表中总记录数 private $lis ... 
- BootStrap TreeView使用示例
			<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ... 
