D-City

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

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

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.

 
Source
 
Recommend
liuyiding
 
 /* ***********************************************
Author :kuangbin
Created Time :2013/8/24 12:20:44
File Name :F:\2013ACM练习\比赛练习\2013通化邀请赛\1004.cpp
************************************************ */ #include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
const int MAXN = ;
int F[MAXN];
int find(int x)
{
if(F[x] == -)return x;
return F[x] = find(F[x]);
}
pair<int,int>p[];
int ans[];
int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout); int n,m;
while(scanf("%d%d",&n,&m) == )
{
memset(F,-,sizeof(F));
for(int i = ;i <= m;i++)
{
scanf("%d%d",&p[i].first,&p[i].second);
}
ans[m] = n;
for(int i = m;i > ;i --)
{
int t1 = find(p[i].first);
int t2 = find(p[i].second);
if(t1 != t2)
{
ans[i-] = ans[i] - ;
F[t1] = t2;
}
else ans[i-] = ans[i];
}
for(int i = ;i <= m;i++)
printf("%d\n",ans[i]);
}
return ;
}

HDU 4496 D-City (并查集,水题)的更多相关文章

  1. Brain Network (easy)(并查集水题)

    G - Brain Network (easy) Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & ...

  2. HDU 4496 D-City (并查集)

    题意:有n个城市,m条路,首先m条路都连上,接着输出m行,第i行代表删除前i行的得到的连通块个数 题解:正难则反,我们反向考虑使用并查集添边.首先每个点都没有相连,接着倒着来边添加边计算,当两个点父节 ...

  3. HDU 4496 D-City —— (并查集的应用)

    给出n个点和m条边,一条一条地删除边,问每次删除以后有多少个联通块. 分析:其实就是并查集的应用,只是前一阵子一直做图论思路一直囿于tarjan了..方法就是,记录每一条边,然后从最后一条边开始不断的 ...

  4. poj2524(并查集水题)

    题目链接:http://poj.org/problem?id=2524 题目大意:学校共有n个同学,告诉你m对同学信仰同一宗教,问这个学校学生信仰宗教的数目最多为多少. 例: Sample Input ...

  5. 【PAT-并查集-水题】L2-007-家庭房产

    L2-007. 家庭房产 给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产套数. 输入格式: 输入第一行给出一个正整数N(<=1000),随后N行,每行按下 ...

  6. hdu 4496 D-City(并查集)

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

  7. hdu 4496 其实还是并查集

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

  8. POJ2524并查集水题

    Description There are so many different religions in the world today that it is difficult to keep tr ...

  9. HDU1863(Kruskal+并查集水题)

    https://cn.vjudge.net/problem/HDU-1863 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可). ...

随机推荐

  1. openjudge-NOI 2.6-2728 摘花生

    题目链接:http://noi.openjudge.cn/ch0206/2728/ 题解: 某一个点只能从其左边或者上边走过来 f[i][j]存储(i,j)这个点上的结果,即f[i][j]=max(f ...

  2. centos上Jenkins搭建

    Jenkins可以提供持续集成服务,它的运行环境(runtime)需要Tomcat和JDK 要把Jenkins让Tomcat启动服务,而Tomcat需要JDK的环境 详情配置参见: http://ww ...

  3. 在Linux上安装pycharm

    1.首先在官网下载pycharm并进行提取,将提取的文件夹放在/usr下面(或者任意位置) 2.然后vi /etc/hosts 编辑 将0.0.0.0 account.jetbrains.com添加到 ...

  4. C++——map注意事项

    1. C++标准模块库STL中提供了两种基本的关联容器:set和map.其内部实现是都是采用红黑树,但不同的是,set中结点存储的是键值,map中结点存储的是<key,value>的键值对 ...

  5. C/C++——C语言库函数大全

    本文转载自:https://blog.csdn.net/yanfan0916/article/details/6450442###; 1. 分类函数: ctype.h  int isalpha(int ...

  6. javascript sleep方法

    function sleep(numberMillis) {     var now = new Date();     var exitTime = now.getTime() + numberMi ...

  7. Linux下快速查找文件

    1 locate 查找内容.查找数据库,updatedb命令更新数据库 2 which 命令 3 find 路径 -name 查找内容.find命令会磁盘查找,比较耗时. 4 grep 查找内容一般为 ...

  8. Mysql中的Btree与Hash索引

    B-Tree 索引特征 B-Tree索引可以被用在像=,>,>=,<,<=和BETWEEN这些比较操作符上.而且还可以用于LIKE操作符,只要它的查询条件是一个不以通配符开头的 ...

  9. 应用nslookup命令查看A记录、MX记录、CNAME记录和NS记录

    https://blog.csdn.net/qq_38058202/article/details/80468688

  10. 小甲鱼Python笔记(下)

    二十八 二十九  文件 打开文件 open(文件名[,模式][,缓冲]) 注意open是个函数不是方法 模式: 缓冲: 大于1的数字代表缓冲区的大小(单位是字节),-1(或者是任何负数)代表使用默认缓 ...