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 ...
随机推荐
- jquery之提示信息
//生成优惠券并分发 function saveCouponAssign(){ //发行券种 var couponTypeId = $("#couponTypeId").combo ...
- CentOS 恢复 rm -rf * 误删数据(转)
一. 将磁盘分区挂载为只读 这一步很重要,并且在误删除文件后应尽快将磁盘挂载为只读.越早进行,恢复的成功机率就越大. 1. 查看被删除文件位于哪个分区 [root@localhost ~]# mo ...
- Asp.net MVC4 捆绑和压缩
减少加载JavaScript和css文件加载时间 减少http请求次数和数据大小 无需配置 允许自定义和扩展
- ajaxFileUpload 注意!
后台context.Response.ContentType = "text/html";
- C# 温故而知新:Stream篇(—)
C# 温故而知新:Stream篇(—) 目录: 什么是Stream? 什么是字节序列? Stream的构造函数 Stream的重要属性及方法 Stream的示例 Stream异步读写 Stream 和 ...
- BZOJ 1003 物流运输 (动态规划 SPFA 最短路)
1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 5590 Solved: 2293 [Submit][Stat ...
- C语言中的memset函数和数组指针
代码: #include <iostream> #include <cstring> using namespace std; int main(){ ] = {}; mems ...
- Qt将表格table保存为excel(odbc方式)
首先是保存excel的方法,可参照: http://dzmlmszp.blog.163.com/blog/static/179271962014819111812531/ ok,进入正题. 现在我有一 ...
- javascript第二课练习
<script type="text/javascript"> window.onload=function() //网页全部加载完后执行 { va ...
- 使用pip install 或者easy_install安装Python的各种包出现cc failed with exit status 1
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...