D-City

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2933    Accepted Submission(s): 1038

Problem Description
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.
 
Input
First line of the input contains two integers N and M.
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
Output M lines, the ith line is the answer after deleting the first i edges in the input.
 
Sample Input
5 10
0 1
1 2
1 3
1 4
0 2
2 3
0 4
0 3
3 4
2 4
 
Sample Output
1
1
1
2
2
2
2
3
4
5

Hint

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个点(0-(n-1)) m条边,每次删除一条边,然后求每次剩下的连通分量
题解:先将m条边输入,然后反向添边(m-1->0)得到结果

#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std; const int N = ;
const int M =;
int father[N]; int _find(int x){
if(x==father[x]) return x;
return father[x]=_find(father[x]);
}
struct Edge{
int s,e;
}edge[M];
int a[M];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){ for(int i=;i<n;i++){
father[i] = i;
}
for(int i=;i<m;i++){
scanf("%d%d",&edge[i].s,&edge[i].e);
}
a[m-]=n; ///最开始都是独立的点
for(int i=m-;i>;i--){ ///从最后一条边开始添加
int x = _find(edge[i].s);
int y = _find(edge[i].e);
if(x!=y){
father[x] = y;
a[i-] = a[i]-;
}
else a[i-]=a[i];
}
for(int i=;i<m;i++){
printf("%d\n",a[i]);
}
}
return ;
}

hdu 4496(并查集逆向添边)的更多相关文章

  1. hdu 4496 并查集 逆向 并查集删边

    貌似某大犇说过 正难则反,,, 题目说要对这张图进行删边,然后判断联通块的个数,那么就可以先把所有边都删掉,之后从后往前加边,若加的边两端点不在同一个联通块中, 那么此时联通快个数少一,否则不变 #i ...

  2. HDU 4496 并查集 逆向思维

    给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...

  3. hdu 4496(并查集)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...

  4. hdu 4514 并查集+树形dp

    湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tot ...

  5. HDU 3926 并查集 图同构简单判断 STL

    给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...

  6. HDU 1232 并查集/dfs

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...

  7. HDU 2860 并查集

    http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...

  8. hdu 1198 (并查集 or dfs) Farm Irrigation

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...

  9. hdu 1598 (并查集加贪心) 速度与激情

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...

随机推荐

  1. 一些实用的JQuery代码片段收集

    本文将展示50个非常实用的JQuery代码片段,这些代码能够给你的JavaScript项目提供帮助.其中的一些代码段是从jQuery1.4.2才开始支持的做法,另一些则是真正有用的函数或方法,他们能够 ...

  2. POJ2349:Arctic Network(二分+最小生成树)

    Arctic Network Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 28311   Accepted: 8570 题 ...

  3. bzoj 4488 [Jsoi2015]最大公约数 结论+暴力

    [Jsoi2015]最大公约数 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 302  Solved: 169[Submit][Status][Dis ...

  4. JQuery学习六

    <JQuery cookie>插件 cookie是保存在浏览器上的内容,用户在这次浏览页面的时候向cookie中保存文本内容.下次再访问页面的时侯就可以取出来上次保存的内容.这样可以得到上 ...

  5. Spring Boot 打包部署

    一.打包成jar并部署 1.工程--右键选择运行配置: 在Goals中输入: org.apache.maven.plugins:maven-jar-plugin:.RELEASE:repackage ...

  6. linux 执行shell脚本的4种方法总结

    bash shell 脚本的方法有多种,假设我们编写好的shell脚本的文件名为hello.sh,文件位置在/data/shell目录中并已有执行权限. 方法一:切换到shell脚本所在的目录(此时, ...

  7. centos6.8安装并配置zimbra

    一.对域名设置MX记录 二.安装准备 1.关闭selinux vi /etc/selinux/config SELINUX=disabled 2.iptables防火墙端口设置 # iptables ...

  8. gradle web项目启动报错: java.lang.ClassNotFoundException: org.springframework.web.util.IntrospectorCleanupListener

    严重: Error configuring application listener of class org.springframework.web.util.IntrospectorCleanup ...

  9. springcloud(一):大话Spring Cloud(山东数漫江湖)

    研究了一段时间spring boot了准备向spirng cloud进发,公司架构和项目也全面拥抱了Spring Cloud.在使用了一段时间后发现Spring Cloud从技术架构上降低了对大型系统 ...

  10. js_判断当前url是否合法http(s)

    alert(checkURL('http:555')); //false function checkURL(URL) { var str = URL, Expression = /http(s)?: ...