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.

 逆向思维的重要性!
#include<cstdio>
#include<iostream>
#include<string.h>
#include<stack>
#define maxn 10005
using namespace std;
int
pre[maxn];
int
num;
void
init()
{

for
(int i=;i<maxn;i++) pre[i]=i;
}

int
find(int x)
{

if
(pre[x]==x) return x;
else return
pre[x]=find(pre[x]);
}

void
merge(int x,int y)
{

x=find(x);
y=find(y);
if
(x!=y)
{

num--;
pre[y]=x;
}
}

int
main()
{

cin.sync_with_stdio(false);
int
n,m;
while
(cin>>n>>m)
{

stack<int> fuck,fuck2;
int
ret[m];
while
(m--)
{

int
x,y;
cin>>x>>y;
fuck.push(x),fuck.push(y);
}

init();
num=n;
int
rett=;
while
(!fuck.empty())
{

int
yy=fuck.top();
fuck.pop();
int
xx=fuck.top();
fuck.pop();
ret[rett++]=num;
merge(xx,yy);
}

for
(int i=rett-;i>=;i--) cout<<ret[i]<<endl;
}

return
;
}

hdu 4496 其实还是并查集的更多相关文章

  1. HDU 4496 D-City (并查集)

    题意:有n个城市,m条路,首先m条路都连上,接着输出m行,第i行代表删除前i行的得到的连通块个数 题解:正难则反,我们反向考虑使用并查集添边.首先每个点都没有相连,接着倒着来边添加边计算,当两个点父节 ...

  2. HDU - 4496 City 逆向并查集

    思路:逆向并查集,逆向加入每一条边即可.在获取联通块数量的时候,直接判断新加入的边是否合并了两个集合,如果合并了说明联通块会减少一个,否则不变. AC代码 #include <cstdio> ...

  3. HDU 4496 D-City —— (并查集的应用)

    给出n个点和m条边,一条一条地删除边,问每次删除以后有多少个联通块. 分析:其实就是并查集的应用,只是前一阵子一直做图论思路一直囿于tarjan了..方法就是,记录每一条边,然后从最后一条边开始不断的 ...

  4. hdu 4496 D-City(并查集)

    Problem Description Luxer is a really bad guy. He destroys everything he met. One day Luxer went to ...

  5. HDU 4496 D-City (并查集,水题)

    D-City Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Subm ...

  6. HDU 1811 拓扑排序 并查集

    有n个成绩,给出m个分数间的相对大小关系,问是否合法,矛盾,不完全,其中即矛盾即不完全输出矛盾的. 相对大小的关系可以看成是一个指向的条件,如此一来很容易想到拓扑模型进行拓扑排序,每次检查当前入度为0 ...

  7. hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点)

    hdu 6200 mustedge mustedge(并查集+树状数组 或者 LCT 缩点) 题意: 给一张无向连通图,有两种操作 1 u v 加一条边(u,v) 2 u v 计算u到v路径上桥的个数 ...

  8. <hdu - 1232> 畅通工程 并查集问题 (注意中的细节)

    本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1232  结题思路:因为题目是汉语的,那我就不解释题意了,要求的是最少建设的道路,我们可以用并查集来做这 ...

  9. HDU 5441 Travel(并查集+统计节点个数)

    http://acm.hdu.edu.cn/showproblem.php?pid=5441 题意:给出一个图,每条边有一个距离,现在有多个询问,每个询问有一个距离值d,对于每一个询问,计算出有多少点 ...

随机推荐

  1. [RK3399] /bin/sh: 1: lz4c: not found

    CPU:RK3399 系统:Android 8.1 第一次在 RK3399 编译 Android 8.1 的系统,编译内核过程中报错如下: /bin/sh: : lz4c: not found mak ...

  2. new HttpClient().PostAsync封装参数

    var data = Encoding.UTF8.GetBytes("{ \"y\": 5, \"x\": 3}"); var conten ...

  3. sqlServer sa账号被锁定

    alter login sa with password = '123'  unlock, check_policy = off, check_expiration = off    一切搞定.. 1 ...

  4. opencv 检测人脸、人眼

    This tutorial code’s is shown lines below. You can also download it from here . The second version ( ...

  5. Matlab基础:关于图像的基本操作

    -- %% 学习目标:学习关于图像的基本操作 %% 通过抖动来增强图像的的色彩对比度 clear all; close all; I = imread('cameraman.tif');%读取灰度图像 ...

  6. Locust - A modern load testing framework https://locust.io/

    Locust - A modern load testing frameworkhttps://locust.io/

  7. Expression: __acrt_first_block == header

    File: minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp Line: 996 Expression: __acrt_first_block == ...

  8. 为什么要用k8s

    经过几次面试,发现有的公司没有用过k8s,有的公司正在用,但是都问了共同的问题:k8s的好处在哪里.所以总结了一下几点 1.故障迁移:当某一个node节点关机或挂掉后,node节点上的服务会自动转移到 ...

  9. 【leetcode_easy】581. Shortest Unsorted Continuous Subarray

    problem 581. Shortest Unsorted Continuous Subarray 题意:感觉题意理解的不是非常明白. solution1: 使用一个辅助数组,新建一个跟原数组一模一 ...

  10. 502 BAD GATEWAY-k8s的cgroup限制了apache的可用内存

    1.release的组件逻辑图 2.表象:按F12,总是报502 BAD GATEWAY 3.nginx日志 [error] #: * upstream prematurely closed conn ...