D-City

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

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
 
 并查集,题目问每破坏一条边有多上连通块,可以倒推把边连起来,每次查询有多少个集合
 //2016.8.9
#include<iostream>
#include<cstdio>
#include<stack> using namespace std; int fa[], arr[][], cnt; int getf(int i)
{
if(fa[i]==i)return i;
return fa[i] = getf(fa[i]);
} int init(int n)
{
for(int i = ; i < n; i++)
fa[i] = i;
} int Merge(int a, int b)
{
int af = getf(a);
int bf = getf(b);
if(af!=bf){
fa[bf] = af;
cnt--;
}
} int main()
{
int n, m, u, v;
while(cin>>n>>m)
{
init(n);
for(int i = ; i < m; i++)
{
scanf("%d%d", &arr[i][], &arr[i][]);
}
stack<int> s;
cnt = n;
for(int i = m-; i >= ; i--)
{
s.push(cnt);
Merge(arr[i][], arr[i][]);
}
while(!s.empty())
{
int ans = s.top();
cout<<ans<<endl;
s.pop();
}
} return ;
}

HDU4496(并查集)的更多相关文章

  1. hdu4496并查集的删边操作

    题意:       给你一个图,问你删除一些边后还有几个连通快.. 思路:       典型的并查集删边操作,并查集的删边就是先把不删除的边并查集一边(本题没有不删除的边),然后逆序吧所有要删除的边以 ...

  2. HDU4496 D-City【基础并查集】

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

  3. BZOJ 4199: [Noi2015]品酒大会 [后缀数组 带权并查集]

    4199: [Noi2015]品酒大会 UOJ:http://uoj.ac/problem/131 一年一度的“幻影阁夏日品酒大会”隆重开幕了.大会包含品尝和趣味挑战两个环节,分别向优胜者颁发“首席品 ...

  4. 关押罪犯 and 食物链(并查集)

    题目描述 S 城现有两座监狱,一共关押着N 名罪犯,编号分别为1~N.他们之间的关系自然也极不和谐.很多罪犯之间甚至积怨已久,如果客观条件具备则随时可能爆发冲突.我们用"怨气值"( ...

  5. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  6. bzoj1854--并查集

    这题有一种神奇的并查集做法. 将每种属性作为一个点,每种装备作为一条边,则可以得到如下结论: 1.如果一个有n个点的连通块有n-1条边,则我们可以满足这个连通块的n-1个点. 2.如果一个有n个点的连 ...

  7. [bzoj3673][可持久化并查集 by zky] (rope(可持久化数组)+并查集=可持久化并查集)

    Description n个集合 m个操作 操作: 1 a b 合并a,b所在集合 2 k 回到第k次操作之后的状态(查询算作操作) 3 a b 询问a,b是否属于同一集合,是则输出1否则输出0 0& ...

  8. [bzoj3123][sdoi2013森林] (树上主席树+lca+并查集启发式合并+暴力重构森林)

    Description Input 第一行包含一个正整数testcase,表示当前测试数据的测试点编号.保证1≤testcase≤20. 第二行包含三个整数N,M,T,分别表示节点数.初始边数.操作数 ...

  9. 【BZOJ-3673&3674】可持久化并查集 可持久化线段树 + 并查集

    3673: 可持久化并查集 by zky Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 1878  Solved: 846[Submit][Status ...

随机推荐

  1. CodeForces 614B Gena's Code

    #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm& ...

  2. SqlServer tsql语句大全

    下列语句部分是Mssql语句,不可以在access中使用. SQL分类: DDL—数据定义语言(CREATE,ALTER,DROP,DECLARE) DML—数据操纵语言(SELECT,DELETE, ...

  3. 搭建firefly服务端遇到的问题

    1 pylinter pylinter could not automatically determined the path to "lint.py" 这个错误通过安装pylin ...

  4. ARM-LINUX学习笔记-(虚拟机linux串口终端以及USB程序下载,基于TQ2440)

    昨天安装了ssh服务之后今天在windows上用xshell登陆发现登录不上,原因是使用了virtualbox的NAT模式,在NAT模式下,客户机可以很方便地上网,但是想要链接宿主机就需要打开网络地址 ...

  5. onchange、oninput、onpropertyChange事件的异同

    onchange事件适用于input\textarea\select元素上,支持各大浏览器,该事件只有在键盘或者鼠标操作改变对象属性,且失去焦点时触发,脚本触发无效: oninput事件适用于inpu ...

  6. mysql中TPS, QPS 的计算方式

    今天突然有个同事问题一个问题, mysqlTPS和QPS的计算公式是什么? 以前确实也没有关注过这个计算公式,所以查了下学习了下: 下面是参考内容.  在做db基准测试的时候,qps,tps 是衡量数 ...

  7. listen函数里面backlog的意义以及各种情况

    先看了这篇: http://www.cppblog.com/thisisbin/archive/2010/02/07/107444.html 里面说了会维护两个队列,established 和 syn ...

  8. 开启分布式事物DTC

    1.web服务器开启分布式事物配置后,数据库服务器的host文件要设置  “IP  web服务器主机名” 的映射,否则会 出现 “与基础事务管理器的通信失败” #跨网段使用TransactionSco ...

  9. AOP:代理思想 (没有考虑到Spring)

    文件目录: package com.wangcf.entity; public class User { private int id; private String name; private St ...

  10. Heka GeoIpDecoder 配置

    Prepare: 安装geoip-api-c,确保/usr/include/GeoIP.h存在: 源码编译安装Heka (容易出现问题): 下载GeoLiteCity.dat数据库. 配置文件举例: ...