点击打开链接

D.D-City

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





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.

反向其实就是一个并查集的合并操作,。听说正向BFS可破,但是没试过

#include<stdio.h>
#include<string.h>
int dele[100010][2];
int father[10010];
int mystack[100010];
int top;
int count;
int getfather(int i)
{
if(father[i] == i)
return i;
else
return father[i] = getfather(father[i]);
}
void merge(int a, int b)
{
a = getfather(a);
b = getfather(b);
if(a == b)
return;
else
{
father[b] = a;
count --;
}
}
int main()
{
int n, m;
while(scanf("%d%d", &n, &m) != EOF)
{
int i;
memset(dele, 0, sizeof(dele));
for(i = 0; i < m; i++)
father[i] = i;
for(i = 0; i < m ; i++)
scanf("%d%d", &dele[i][0], &dele[i][1]);
count = n;
top = 0;
for(i--; i >= 0; i --)
{
mystack[top++] = count;
merge(dele[i][0], dele[i][1]);
}
for(top --; top >= 0; top--)
{
printf("%d\n", mystack[top]);
}
}
return 0;
}

2013 ACM 通化邀请赛D.D-City 并查集的更多相关文章

  1. 2013 吉林通化邀请赛 D-City 离线型的并查集

    题意:给定n个点和m条边,问你拆掉前i条边后,整个图的连同城市的数量. i从1到m. 思路:计算连通的城市,很容易想到并查集,但是题目里是拆边,所以我们可以反向去做. 存下拆边的信息,从后往前建边. ...

  2. 2013 ACM 通化邀请赛 A. Tutor

    A. Tutor Description Lilin was a student of Tonghua Normal University. She is studying at University ...

  3. 2013 吉林通化邀请赛 Play Game 记忆化搜索

    dp[ba][ta][bb][tb]表示a堆牌从下面拿了ba张,从上面拿了ta张.b堆牌从下面拿了bb张,从上面拿了tb张.当前玩家能得到的最大的分数. 扩展方式有4种,ba+1,ta+1,bb+1, ...

  4. 2013 吉林通化邀请赛 Tutor 有点坑的水题

    计算12个数的和的平均数.四舍五入,不能有后导0. 我的做法是,将答案算出后,乘以1000,然后看个位是否大于等于5,判断是否要进位…… #include<iostream> #inclu ...

  5. “玲珑杯”ACM比赛 Round #7 B -- Capture(并查集+优先队列)

    题意:初始时有个首都1,有n个操作 +V表示有一个新的城市连接到了V号城市 -V表示V号城市断开了连接,同时V的子城市也会断开连接 每次输出在每次操作后到首都1距离最远的城市编号,多个距离相同输出编号 ...

  6. HDU ACM 2586 How far away ?LCA-&gt;并查集+Tarjan(离线)算法

    题意:一个村子有n个房子,他们用n-1条路连接起来,每两个房子之间的距离为w.有m次询问,每次询问房子a,b之间的距离是多少. 分析:近期公共祖先问题,建一棵树,求出每一点i到树根的距离d[i],每次 ...

  7. [tsA1491][2013中国国家集训队第二次作业]家族[并查集]

    m方枚举,并查集O(1)维护,傻逼题,,被自己吓死搞成神题了... #include <bits/stdc++.h> using namespace std; struct tri { i ...

  8. 西安邀请赛-D(带权并查集+背包)

    题目链接:https://nanti.jisuanke.com/t/39271 题意:给定n个物品,m组限制,每个物品有个伤害值,现在让两个人取完所有物品,要使得两个人取得物品伤害值之和最接近,输出伤 ...

  9. hduoj 4710 Balls Rearrangement 2013 ACM/ICPC Asia Regional Online —— Warmup

    http://acm.hdu.edu.cn/showproblem.php?pid=4710 Balls Rearrangement Time Limit: 6000/3000 MS (Java/Ot ...

随机推荐

  1. 一个项目中说系统分为表现层、控制层、逻辑层、DAO层和最终数据库五层架构-转

    表现层就是看到的东西,比如你现在看到的当前页面控制层就将你的请求从页面传到后台代码逻辑层就是处理你的请求的代码DAO层就是将数据存到数据库中的代码数据库就是数据库了,存东西用的 ,DAO层就是将访问数 ...

  2. Oracle备份与恢复之exp/imp

    获取帮助 exp help=y/imp help=y 工作方式 交互式 exp 命令行 exp user/pwd@dbname file=/oracle/test.dmp full=y 参数文件方式 ...

  3. 06 Linux下Shell介绍

    一.概述 每个人在成功登陆Linux后,系统会出现不同的提示符号,例如$,~,#等,然后你就可以开始输入需要的命令.若命令正确,系统就会依据命令的要求来执行,直到注销系统为止,在登陆到注销期间,输入的 ...

  4. 解决方案:将已存在的项目 添加到 tfs解决方案中的时候 出现:新项目不能成功加入源码控制

    遇到此问题 可能是因为你的 解决方案文件 没有正确与 tfs服务器绑定导致的 解决方式是: 在打开任意一个源码文件的时候,打开 vs2013的 文件>> Go to File->So ...

  5. Eclipse高效率开发技巧

    工欲善其事,必先利其器.对于程序员来说,Eclipse便是其中的一个"器".本文会从Eclipse快捷键和实用技巧这两个篇章展开介绍.Eclipse快捷键用熟后,不用鼠标,便可进行 ...

  6. 51nod 1392 装盒子

    有n个长方形盒子,第i个长度为Li,宽度为Wi,我们需要把他们套放.注意一个盒子只可以套入长和宽分别不小于它的盒子,并且一个盒子里最多只能直接装入另外一个盒子 (但是可以不断嵌套),例如1 * 1 可 ...

  7. js setTimeout

    setTimeout用法 //每个0.5秒钟改变字体和背景颜色,字体一闪一闪的效果 var flag = 0; function start(){ var text = document.getEle ...

  8. 理解和配置 Linux 下的 OOM Killer

    原文:http://www.vpsee.com/2013/10/how-to-configure-the-linux-oom-killer/ 最近有位 VPS 客户抱怨 MySQL 无缘无故挂掉,还有 ...

  9. IE11登陆交行网银崩溃

    1.打开IE11兼容性视图 2.交通银行就填入95559.com.cn 3.重新打开IE11登录网银

  10. Android中使用am命令实现在命令行启动程序详解

    在Android中,除了从界面上启动程序之外,还可以从命令行启动程序,使用的是命令行工具am. 复制代码代码如下: usage: am [subcommand] [options] start an ...