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 ...
随机推荐
- P65
#include<stdio.h> #define N 6 main() { char c[N]; int i=0; for(;i<N;c[i]=getchar(),i++); pr ...
- 【linux】linux启动流程
欢迎转载,转载时请保留作者信息,谢谢. 邮箱:tangzhongp@163.com 博客园地址:http://www.cnblogs.com/embedded-tzp Csdn博客地址:http:// ...
- iOS- 如何集成支付宝
链接地址:http://www.cnblogs.com/qingche/p/3556365.html 现在不少app内都集成了支付宝功能 使用支付宝进行一个完整的支付功能,大致有以下步骤: 1&g ...
- call、apply以及bind
call与apply都可以改变js的this指向,两者最主要的区别就是使用时传参的不同,apply的参数可以以数组的形式传进来,但是call方法的参数必须要一个一个的传进来,就像这样. func.ca ...
- 2数组的slice和splice方法
var colors=["blue","red","black","yellow","gray",& ...
- MFC获取rgb图像数据后动态显示及保存图片的方法
该情况可用于视频通信中获取的位图数据回放显示或显示摄像头捕获的本地图像 第一种方法 #include<vfw.h> 加载 vfw32.lib 链接库 //---------------- ...
- Boost::Thread 多线程的基础知识
Boost.Thread可以使用多线程执行可移植C++代码中的共享数据.它提供了一些类和函数来管理线程本身,还有其它一些为了实现在线程之间同步数据或者提供针对特定单个线程的数据拷贝.头文件:#incl ...
- VS2008通过 map 和 cod 文件定位崩溃代码行
VS 2005/2008使用map文件查找程序崩溃原因 一般程序崩溃可以通过debug,找到程序在那一行代码崩溃了,最近编一个多线程的程序,都不知道在那发生错误,多线程并发,又不好单行调试,终于找到一 ...
- 王立平--PopupWindow
MainActivity.java <span style="font-size:14px;">package com.main; import android.app ...
- JAVA中字符串比較equals()和equalsIgnoreCase()的差别
.使用equals( )方法比較两个字符串是否相等.它具有例如以下的一般形式: boolean equals(Object str) 这里str是一个用来与调用字符串(String)对象做比較的字符串 ...