hdu4496 D-City
D-City
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)
Total Submission(s): 0 Accepted Submission(s): 0
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 <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MAXN 10050
int father[MAXN],ans[MAXN];
struct edge{
int s,e;
}l[100005];
int find(int x)
{
if(father[x]!=x)
father[x]=find(father[x]);
return father[x];
}
int main()
{
int n,i,m,all;
while(scanf("%d%d",&n,&m)!=EOF)
{
all=n;
for(i=0;i<=n;i++)
{
father[i]=i;
}
for(i=m-1;i>=0;i--)
{
scanf("%d%d",&l[i].s,&l[i].e);
}
for(i=0;i<m;i++)
{
int a=find(l[i].s);
int b=find(l[i].e);
if(a!=b)
{
father[a]=b;
all--;
}
ans[i]=all;
}
// ans[1]=n;
for(i=m-2;i>=0;i--)
{
printf("%d\n",ans[i]);
}
printf("%d\n",n);
}
return 0;
}
hdu4496 D-City的更多相关文章
- BZOJ 2001: [Hnoi2010]City 城市建设
2001: [Hnoi2010]City 城市建设 Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1132 Solved: 555[Submit][ ...
- History lives on in this distinguished Polish city II 2017/1/5
原文 Some fresh air After your time underground,you can return to ground level or maybe even a little ...
- History lives on in this distinguished Polish city 2017/1/4
原文 History lives on in this distinguished Polish city Though it may be ancient. KraKow, Poland, is a ...
- #1094 : Lost in the City
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 Little Hi gets lost in the city. He does not know where he is ...
- GeoIP Legacy City数据库安装说明
Here is a brief outline of the steps needed to install GeoIP Legacy City on Linux/Unix. The installa ...
- [POJ3277]City Horizon
[POJ3277]City Horizon 试题描述 Farmer John has taken his cows on a trip to the city! As the sun sets, th ...
- 2015年第8本(英文第7本):the city of ember 微光城市
书名:the City of Ember(中文名:微光城市) 作者:Jeanne DuPrau 单词数:6.2万 不重复单词数:未知 首万词不重复单词数:未知 蓝思值:未知 阅读时间:2015年4月2 ...
- 离散化+线段树 POJ 3277 City Horizon
POJ 3277 City Horizon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 18466 Accepted: 507 ...
- HDU 1505 City Game (hdu1506 dp二维加强版)
F - City Game Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submi ...
- URAL 1139 City Blocks(数论)
The blocks in the city of Fishburg are of square form. N avenues running south to north and Mstreets ...
随机推荐
- 2783: [JLOI2012]树( dfs + BST )
直接DFS, 然后用set维护一下就好了.... O(nlogn) ------------------------------------------------------------------ ...
- redisTemplate 操作
redisDao封装类-其他dao集成他 package com.ffcs.wlan.dao.common; import javax.annotation.Resource; import org. ...
- C++ 库研究笔记——通过inline避免hpp 的mutiple definition 错误
C++用了这么多年,这个却第一次知道,以前没用过hpp 这样: // foo.hpp void foo() { /* body */ } // a.cpp #include "foo.hpp ...
- 基于visual Studio2013解决面试题之1405归并排序
题目
- boost 轻量级信号量
#include <boost/thread/condition_variable.hpp> #include <boost/thread/mutex.hpp> #in ...
- MySQL存储引擎:InnoDB和MyISAM的差别/优劣评价/评测/性能测试
InnoDB和MyISAM简介 MyISAM:这个是默认类型,它是基于传统的ISAM类型,ISAM是Indexed Sequential Access Method (有索引的 顺序访问方法) 的缩写 ...
- android5.0(Lollipop) BLE Central牛刀小试
转载请表明作者:http://blog.csdn.net/lansefeiyang08/article/details/46482073 昨天写了android L BLE Peripheral的简单 ...
- asp.net2.0安全性(1)--用户角色篇(代码实现1)--转载来自车老师
创建用户: MembershipCreateStatus mc; Membership.CreateUser(txtUid.Text, txtPwd.Text, txtEmail.Text, txtQ ...
- QtWebkit中如何将网页内容转为图片
原地址:http://www.cnblogs.com/baizx/archive/2010/07/31/1789573.html 如何将webkit中的渲染结果也就是网页画面转换为图片 用抓图软件 ...
- PHP学习之-1.4 计算表达式
计算表达式 不同于HTML和CSS,在PHP中做计算,比如我们写 echo 12*3 计算结果是36.代码如下 <?php echo 12*3;?> 实例 <!DOCTYPE HTM ...