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 ...
随机推荐
- 关于SSH框架设计的一些理解
近期在学习企业开发领域非常流行的SSH框架(Struts.Hibernate.Spring).因为之前有做过原生的Servlet+JSP的项目,所以在学习过程中我会跟原生开发模式进行对照,在这里我把自 ...
- 使用aidl绑定远程服务
一.服务端 1.清单文件,因为要远程调用,所以要配个action <service android:name="com.example.alipayservice.AliPayServ ...
- EF 简单的 CRUD、分页 代码笔记
添加: static void Main(string[] args) { CCDBEntities ccdbContext = new CCDBEntities( ...
- 读取中兴3G告警log告警文件到集合
1.文件格式 ALARM_ID=102305_404205 EVENT_TIME=-- :: NOTIFICATION_TYPE= MANAGED_OBJECT_INSTANCE=NodeId=,Bs ...
- C# c++ 传递函数指针
C#和c++之间相互传递函数指针 在C++和C#之中都有很多callback method,可以相互调用吗,怎么传递,是我表弟的问题. 1.定义c++ dll ,导出方法 // sort.cpp : ...
- 【剑指offer】从上向下打印二叉树
转载请注明出处:http://blog.csdn.net/ns_code/article/details/26089165 剑指offer上的第23题,实际上就是考察二叉树的层序遍历,详细思想能够參考 ...
- [置顶] Windows Phone后台音乐详解一
应用于: Windows Phone 8 | Windows PhoneOS 7.1 你可以为winphone编写在后台播放音乐的app.这表示即使当用户点击返回或开始按钮离开你的应用界面时,你的应用 ...
- Android - Animation 贝塞尔曲线之美
概述 贝塞尔曲线于1962,由法国工程师皮埃尔·贝塞尔所广泛发表,他运用贝塞尔曲线来为汽车的主体进行设计.贝塞尔曲线最初由Paul de Casteljau于1959年运用de Casteljau演算 ...
- EDStarRating(IOS星级评定)
链接地址:https://github.com/erndev/EDStarRating EDStarRating
- 重操JS旧业第八弹:面向对象与继承
js里面没有语言语法层面的继承机制,但这并不意味着js就不能实现继承,利用js属性和方法动态性来模拟实现继承,通过总结大概有如下方法实现: 1 原型链继承 我们知道原型在对象中扮演着重要的角色,函数本 ...