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. [NOIP 2017]棋盘

    题目描述 有一个 m×m 的棋盘,棋盘上每一个格子可能是红色.黄色或没有任何颜色的.你现在要从棋盘的最左上角走到棋盘的最右下角. 任何一个时刻,你所站在的位置必须是有颜色的(不能是无色的), 你只能向 ...

  2. 【套题】qbxt国庆刷题班D2

    D2 今天的题感觉还是好妙的 T1 传送门 Description 现在有一张\(n\)个节点\(m\)条边的无向连通图\(G=(V,E)\),满足这张图中不存在长度大于等于3的环且图中没有重边和自环 ...

  3. 中国MOOC_面向对象程序设计——Java语言_第2周 对象交互_秒计时的数字时钟

    第2周编程题 查看帮助 返回   第2周编程题,在课程所给的时钟程序的基础上修改 依照学术诚信条款,我保证此作业是本人独立完成的. 温馨提示: 1.本次作业属于Online Judge题目,提交后由系 ...

  4. Ubuntu安装CUDA9.0 + cuDNN

    本篇文章是基于安装CUDA 9.0的经验写,CUDA9.0目前支持Ubuntu16.04和Ubuntu17.04两个版本,如下图所示(最下面的安装方式我们选择第一个,即runfile方式): 下载链接 ...

  5. ICM Technex 2017 and Codeforces Round #400 (Div. 1 + Div. 2, combined) A map B贪心 C思路前缀

    A. A Serial Killer time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  6. [codeforces/edu30]总结(E)

    链接:http://codeforces.com/contest/873/ A题: 贪心,把最大的k个数变成x即可. B题: 从左向右枚举右端点,维护balance的最长长度.任意一个子串可以看做两个 ...

  7. Mybatis中传入List条件

    传入一个map的参数,map里有一个tenantIds的List,在xml里先判断这个List的size是否大于o,然后通过foreach 构造一个in后面括号里的元素,具体的xml如下: <i ...

  8. Python爬虫学习笔记之微信宫格验证码的识别(存在问题)

    本节我们将介绍新浪微博宫格验证码的识别.微博宫格验证码是一种新型交互式验证码,每个宫格之间会有一条 指示连线,指示了应该的滑动轨迹.我们要按照滑动轨迹依次从起始宫格滑动到终止宫格,才可以完成验证,如 ...

  9. JavaScript知识递归实现数组中指定后代元素的查找

    描述:要求将下面的数据类型中每个子孙后代id放入一个数组并返回 var arr = [ {"id":28,"text":"公司信息", &q ...

  10. 关于MyBatis一些小错误,元素内容必须由格式正确的字符数据或标记组成.

    今天在Mapper.xml文件写查询语句报了个奇怪的错误 Caused by: org.apache.ibatis.builder.BuilderException: Error creating d ...