hdu 4496(并查集逆向添边)
D-City
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 2933 Accepted Submission(s): 1038
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.
0 1
1 2
1 3
1 4
0 2
2 3
0 4
0 3
3 4
2 4
1
1
2
2
2
2
3
4
5
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.
#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(并查集逆向添边)的更多相关文章
- hdu 4496 并查集 逆向 并查集删边
貌似某大犇说过 正难则反,,, 题目说要对这张图进行删边,然后判断联通块的个数,那么就可以先把所有边都删掉,之后从后往前加边,若加的边两端点不在同一个联通块中, 那么此时联通快个数少一,否则不变 #i ...
- HDU 4496 并查集 逆向思维
给你n个点m条边,保证已经是个连通图,问每次按顺序去掉给定的一条边,当前的连通块数量. 与其正过来思考当前这边会不会是桥,不如倒过来在n个点即n个连通块下建图,检查其连通性,就能知道个数了 /** @ ...
- hdu 4496(并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4496. 思路:简单并查集应用,从后往前算就可以了. #include<iostream> ...
- hdu 4514 并查集+树形dp
湫湫系列故事——设计风景线 Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- HDU 3926 并查集 图同构简单判断 STL
给出两个图,问你是不是同构的... 直接通过并查集建图,暴力用SET判断下子节点个数就行了. /** @Date : 2017-09-22 16:13:42 * @FileName: HDU 3926 ...
- HDU 1232 并查集/dfs
原题: http://acm.hdu.edu.cn/showproblem.php?pid=1232 我的第一道并查集题目,刚刚学会,我是照着<啊哈算法>这本书学会的,感觉非常通俗易懂,另 ...
- HDU 2860 并查集
http://acm.hdu.edu.cn/showproblem.php?pid=2860 n个旅,k个兵,m条指令 AP 让战斗力为x的加入y旅 MG x旅y旅合并为x旅 GT 报告x旅的战斗力 ...
- hdu 1198 (并查集 or dfs) Farm Irrigation
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1198 有题目图11种土地块,块中的绿色线条为土地块中修好的水渠,现在一片土地由上述的各种土地块组成,需要浇 ...
- hdu 1598 (并查集加贪心) 速度与激情
题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1598 一道带有贪心思想的并查集 所以说像二分,贪心这类基础的要掌握的很扎实才行. 用结构体数组储存公 ...
随机推荐
- 牛客328B Rabbit的工作(1)
传送门:https://ac.nowcoder.com/acm/contest/328/B 题意:给你n和k,和一个长度为n的字符串,字符串中的0表示休息,1表示工作,连续工作会损耗连续的体力值,从1 ...
- bzoj 1811: [Ioi2005]mea 贪心,乱搞
[Ioi2005]mea Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 690 Solved: 257[Submit][Status][Discuss ...
- zTab layui多标签页组件
zTab zTab是一个layui多标签页插件,仿照了layuiAdmin的iframe版Tab实现 当前版本v1.0 码云地址:https://gitee.com/sushengbuyu/zTab ...
- java 课后作业
杨辉三角 组合数 public class YH { public static void main(String agrs[]) { int a[5][5],i,j; for(i = 0;i < ...
- 用pip命令安装Python第三方库
一.准备工作 1. 安装pip (1)下载 pip下载地址:https://pypi.python.org/pypi/pip#downloads (2)安装 下载后解压,控制台下进入解压后的目录,运行 ...
- uva 12325 Zombie's Treasure Chest
https://vjudge.net/problem/UVA-12325 题意: 一个箱子,体积为N 两种宝物,体积为S1.S2,价值为V1.V2,数量无限 最多装多少价值的宝物 数据范围:2^32 ...
- [洛谷P1404] 平均数
洛谷题目链接:平均数 题目描述 给一个长度为n的数列,我们需要找出该数列的一个子串,使得子串平均数最大化,并且子串长度>=m. 输入输出格式 输入格式: N+1行, 第一行两个整数n和m 接下来 ...
- [Luogu 3973] TJOI2015 线性代数
[Luogu 3973] TJOI2015 线性代数 这竟然是一道最小割模型. 据说是最大权闭合子图. 先把矩阵式子推出来. 然后,套路建模就好. #include <algorithm> ...
- Ubuntu12.04 安装LAMP及phpmyadmin
1.安装 Apache apt-get install apache2 2.安装 PHP5 apt-get install php5 libapache2-mod-php5 3.安装 MySQL ap ...
- UnknownHostException
1.查看Centos版本号,不同版本修改的方式可能不一样 cat /etc/issue 查看版本 2.通过hostname命令查看当前主机名 hostname 3.编辑network文件修改hostn ...