HDU4496_D-City(并查集删边/逆向)
D-City
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 1315    Accepted Submission(s): 496
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.
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.
5 10
0 1
1 2
1 3
1 4
0 2
2 3
0 4
0 3
3 4
2 4
1
1
1
2
2
2
2
3
4
5HintThe 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.
#include <iostream>
#include <cstdio>
#include <cstring>
#define M 100000+10
#define N 10000+10
using namespace std;
int n,m;
struct node
{
int u,v;
} edge[M];
int B[N],ans[M];
int fine(int x)
{
if(B[x]!=x)
B[x]=fine(B[x]);
return B[x];
}
int main()
{
int i;
while(~scanf("%d%d",&n,&m))
{
memset(ans,0,sizeof(ans));
memset(edge,0,sizeof(edge));
for(i=0;i<n;i++)
B[i]=i;
for(i=0; i<m; i++)
{
scanf("%d%d",&edge[i].u,&edge[i].v);
}
int sum=n;
for(i=m-1;i>=0;i--)
{
ans[i]=sum;
int xx=fine(edge[i].u);
int yy=fine(edge[i].v);
if(xx!=yy)
{
sum--;
B[xx]=yy;
}
}
for(i=0;i<m;i++)
printf("%d\n",ans[i]);
}
return 0;
}
HDU4496_D-City(并查集删边/逆向)的更多相关文章
- ZOJ 3261 - Connections in Galaxy War ,并查集删边
		In order to strengthen the defense ability, many stars in galaxy allied together and built many bidi ... 
- HDU——2473Junk-Mail Filter(并查集删点)
		Junk-Mail Filter Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ... 
- UVA 11987 Almost Union-Find (并查集+删边)
		开始给你n个集合,m种操作,初始集合:{1}, {2}, {3}, … , {n} 操作有三种: 1 xx1 yy1 : 合并xx1与yy1两个集合 2 xx1 yy1 :将xx1元素分离出来合到yy ... 
- HDU 2473 Junk-Mail Filter(并查集+删点,设立虚父节点/找个代理)
		题意:有N封邮件, 然后又两种操作,如果是M X Y , 表示X和Y是相同的邮件.如果是S X,那么表示对X的判断是错误的,X是不属于X当前所在的那个集合,要把X分离出来,让X变成单独的一个.最后问集 ... 
- hdu 4496 并查集 逆向 并查集删边
		貌似某大犇说过 正难则反,,, 题目说要对这张图进行删边,然后判断联通块的个数,那么就可以先把所有边都删掉,之后从后往前加边,若加的边两端点不在同一个联通块中, 那么此时联通快个数少一,否则不变 #i ... 
- nyoj_1022:合纵连横(并查集删点)
		题目链接 参考链接 只附代码好了 #include<bits/stdc++.h> using namespace std; ; int a[N],b[N],vis[N]; int n,m, ... 
- ZOJ - 3261 Connections in Galaxy War(并查集删边)
		https://cn.vjudge.net/problem/ZOJ-3261 题意 银河系各大星球之间有不同的能量值, 并且他们之间互相有通道连接起来,可以用来传递信息,这样一旦有星球被怪兽攻击,便可 ... 
- nyoj 1022:合纵连横(并查集删点)
		题目链接 参考链接 只附代码好了 #include<bits/stdc++.h> using namespace std; ; int a[N],b[N],vis[N]; int n,m, ... 
- Connections in Galaxy War ZOJ - 3261  离线操作+逆序并查集    并查集删边
		#include<iostream> #include<cstring> #include<stdio.h> #include<map> #includ ... 
随机推荐
- 为iPhone6设计自适应布局(一)
			译者的话:本文是自适应布局的巩固篇,所以对布局约束的添加操作步骤等没有详细的说明.如果看着吃力的话请先移步Swift自适应布局(Adaptive Layout)教程. Apple从iOS6加入了Aut ... 
- FileZilla 安装配置参考
			http://www.admin10000.com/document/72.html 解决 NAT issue https://wiki.filezilla-project.org/Network_C ... 
- visual studio 一直显示正在准备解决方案
			首先重启电脑,无法解决的情况下执行以下步骤: Kill Visual Studio Open Visual Studio without loading a solution Disable Ankh ... 
- table超过30个字段如何处理呢? bootstrap
			样式: @media (max-width: 768px) { .table-supplier { width: 100%; height: 100%; margin-bottom: 12.75px; ... 
- OpenGL ES 2.0 向量
			访问向量中的某个分量<向量名>.<分量名> 将一个向量看作位置时,可以使用x.y.z.w4个分量名,其分别代表X轴.Y轴.Z轴.向量的模. 将一个向量看作颜色时,可以使用r.g ... 
- Oracle11g R2学习系列 之八高级数据类型
			所谓的高级数据类型,就是大数据类型,即BCNB(助记词:BC牛逼)+XML数据类型. B:blob,用来存储可变长度的二进制数据. C:clob,主要用来存储可变长度的字符型数据,也就是其他数据库中提 ... 
- css 完美替换图片
			1.css替换简单图标的展示方法 ;display:inline-block;position:absolute;left:11px;top:10px;border-right:6px solid t ... 
- AFNetworking 官方文档
			AFNetworking Version Minimum iOS Target Minimum OS X Target Notes 2.x iOS 6 OS X 10.8 Xcode 5 is req ... 
- zoj 3772 Calculate the Function
			http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235 这道题需要构造矩阵:F(X)=F(X-1)+F(X-2)*A(X)转化为 ... 
- 【转】android 兼容性测试 CTS 测试过程(实践测试验证通过)
			原文网址:http://blog.csdn.net/jianguo_liao19840726/article/details/7222814 写这个博客的时候是为了记忆,建议大家还是看官方的说明,官方 ... 
