hdu 4496 D-City(并查集)
Luxer is a really bad guy. He destroys everything he met.
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.
First line of the input contains two integers N and M.
Then following M lines each containing space-separated integers u and v, which denotes an D-line.
Constraints:
< N <=
< M <=
<= u, v < N.
Output M lines, the ith line is the answer after deleting the first i edges in the input.
The graph given in sample input is a complete graph, that each pair of vertex has an edge connecting them, so there's only 1 connected block at first. The first 3 lines of output are 1s because after deleting the first 3 edges of the graph, all vertexes still connected together. But after deleting the first 4 edges of the graph, vertex 1 will be disconnected with other vertex, and it became an independent connected block. Continue deleting edges the disconnected blocks increased and finally it will became the number of vertex, so the last output should always be N.
题目的意思是: 一开始有个图(n个点 m条边),然后从第0条边开始删除,(0~m条边依次删除),然后每删除一条边统计依次有多少个连通块。
做法是: 直接从最后一条边开始枚举,依次往前面添加合并边,统计连通块即可。具体看代码,应该看得懂。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<math.h>
#include<algorithm>
#include<queue>
#include<set>
#include<bitset>
#include<map>
#include<vector>
#include<stdlib.h>
#include <stack>
using namespace std;
#define PI acos(-1.0)
#define max(a,b) (a) > (b) ? (a) : (b)
#define min(a,b) (a) < (b) ? (a) : (b)
#define ll long long
#define eps 1e-10
#define MOD 1000000007
#define N 10006
#define M 100006
#define inf 1e12
int n,m;
int fa[N],ans[M];
int a[M],b[M];
void init(){
for(int i=;i<N;i++){
fa[i]=i;
}
}
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
}
bool merge(int x,int y){
int root1=find(x);
int root2=find(y);
if(root1==root2) return false;
fa[root1]=root2;
return true;
}
int main()
{
while(scanf("%d%d",&n,&m)==){
init();
for(int i=;i<m;i++){
scanf("%d%d",&a[i],&b[i]);
}
ans[m-]=n;
int k=m-;
for(int i=m-;i>=;i--){
if(merge(a[i],b[i])){
ans[k-]=ans[k]-;
}else{
ans[k-]=ans[k];
}
k--;
}
for(int i=;i<m;i++){
printf("%d\n",ans[i]);
} } return ;
}
hdu 4496 D-City(并查集)的更多相关文章
- HDU 4496 D-City (并查集)
题意:有n个城市,m条路,首先m条路都连上,接着输出m行,第i行代表删除前i行的得到的连通块个数 题解:正难则反,我们反向考虑使用并查集添边.首先每个点都没有相连,接着倒着来边添加边计算,当两个点父节 ...
- HDU 4496 D-City —— (并查集的应用)
给出n个点和m条边,一条一条地删除边,问每次删除以后有多少个联通块. 分析:其实就是并查集的应用,只是前一阵子一直做图论思路一直囿于tarjan了..方法就是,记录每一条边,然后从最后一条边开始不断的 ...
- HDU 4496 D-City (并查集,水题)
D-City Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Subm ...
- hdu 4496 其实还是并查集
Problem Description Luxer is a really bad guy. He destroys everything he met. One day Luxer went to ...
- HDU 1811 拓扑排序 并查集
有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...
- hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)
hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...
- hdu 3635 Dragon Balls(并查集)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 2480 贪心+简单并查集
Steal the Treasure Time Limit: 10000/6000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- <hdu - 1232> 畅通工程 并查集问题 (注意中的细节)
本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232 结题思路:因为题目是汉语的,那我就不解释题意了,要求的是最少建设的道路,我们可以用并查集来做这 ...
随机推荐
- LeeCode-Rotate Array
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array ...
- ZOJ3574(归并排序求逆数对)
Under Attack II Time Limit: 5 Seconds Memory Limit: 65536 KB Because of the sucessfully calcula ...
- poj 2411 新写法
别以为我在刷水题.... 今天做了场srm,500pt想到了是dp但是无从下手,但是看了rng_58的神代码后顿觉海阔天空啊(盯着看了一个下午),相比于一年前的写法,真的是不忍直视啊, TC真是个好地 ...
- LaTex 下编译后不能显示中文,或者中文乱码
在 Sublime Text 中编辑以下文件并保存(第一行的注释很重要),按下 Cammand + B 编译: %!TEX program = xelatex \documentclass[UTF8] ...
- OpenCV MFC 模块间通信
1. 新建MFC项目 点击完成. 2. 添加按钮 在"工具箱"中找到"Button"控件,添加至界面: 2. 配置opencv, 添加colordetecto ...
- IOS反地理编码取得城市名称
// 获取当前所在的城市名 CLGeocoder *reverseGeocoder=[[CLGeocoder alloc] init]; [reverseGeocoder reverseGeocode ...
- AutoResetEvent与ManualResetEvent区别
本文来自:http://www.360doc.com/content/10/1126/10/3267996_72536817.shtml 在.Net多线程编程中,AutoResetEvent和Manu ...
- 在Swift中使用遗留的C API
Swift的类型系统的设计目的在于简化我们的生活,为此它强制用户遵守严格的代码规范来达到这一点.毫无疑问这是一件大好事,它鼓励程序员们编写 更好更正确的代码.然而,当Swift与历史遗留的代码库.特别 ...
- C# Socket学习笔记二
小记:昨天咱们已经了解了Socket的通信原理,可是点对点的一次通信并不是我们想要的,那么今天那我们就继续学习异步通信,简单来说就是服务器端和客户端可以进行多次 互发信息的通信而不用担心通道会关闭.在 ...
- mvc 笔记
@{ ViewBag.Title = "主页"; } <div> 这里就是渲染Body啦.~~不需要写神马<asp:Content />,其实因为Rende ...