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. 如何用好 github 中的 watch、star、fork

    http://www.jianshu.com/p/6c366b53ea41 https://www.zhihu.com/question/20431718 在每个 github 项目的右上角,都有三个 ...

  2. ACM1258邻接表

    教训:使用邻接表的时候一定要把邻接表的结构组定义的足够大,不能仅仅等于节点的个数,因为线段的数量往往远超过节点的数量. 这个题目是拓扑排序练习,提高下理解. #include<iostream& ...

  3. python---异步IO(asyncio)协程

    简单了解 在py3中内置了asyncio模块.其编程模型就是一个消息循环. 模块查看: from .base_events import * from .coroutines import * #协程 ...

  4. Mybatis(4) 映射文件-参数处理

    参数处理: 单参数处理: mybatis 不会做任何特殊处理. #{key} : key 可以写任何字段取出参数值. 测试方法: mapper接口: mapper.xml: 控制台: 多参数处理: m ...

  5. 解决Sourcetree 每次拉取提交都需要输入密码

    问题产生背景 客户端领导决定使用http方式拉取和push代码,所以无法使用之前的ssh方式做免密处理 解决办法 方法1:在.git目录中有个config目录,在路径前配置下用户名和密码即可,如下所示 ...

  6. jsp05 指令与动作

    JSP7个动作指令如下 : jsp:forward: 执行页面转向,将请求的处理转发到下一个页面. jsp:param: 用于传递参数,必须与其他支持参数曲标签一起使用. jsp:include: 用 ...

  7. Why to Not Not Start a Startup

    我花了周六,周日两天的时间,把这篇长文给阅读完了.很受益,改变了我的很多认知,也给我开拓了视野. 转载: Want to start a startup? Get funded by Y Combin ...

  8. Http跨域时候预检没通过的几种原因

    网上大多数涉及的原因(直接复制粘帖): CORS把HTTP请求分成两类,不同类别按不同的策略进行跨域资源共享协商. 1. 简单跨域请求. 当HTTP请求出现以下两种情况时,浏览器认为是简单跨域请求: ...

  9. 作为一名前端开发工程师,你必须掌握的WEB模板引擎:Handlebars

    作为一名前端开发工程师,你必须掌握的WEB模板引擎:Handlebars 一.为什么需要使用模板引擎? 关于为什么要使用模板引擎,按照我常对学生说的一句话就是:不用重复造轮子..   简单来说,模板最 ...

  10. Vue前端开发规范(山东数漫江湖)

    一.强制 1. 组件名为多个单词 组件名应该始终是多个单词的,根组件 App 除外. 正例: export default { name: 'TodoItem', // ... } 反例: expor ...