D-City

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)

Total Submission(s): 2448 Accepted Submission(s): 862

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.


解题心得:

-题意就是给你一个图,m个边,从第一个边开始每次取消一个,问你每次取消之后有多少个单独的集合

-这个题看起来挺唬人的,其实就是将给出的点拿来从从后面开始合并就可以了。只要是不同源的相合并,那么就必然少一个单独的集合,思维不要僵化,换个方向想就可以了。


#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
const int maxn2 = 1e4+10;
int n,m,father[maxn2];
stack <int> st;//因为是从后面开始合并,所以用栈来存放
struct node
{
int a,b;
}edge[maxn]; int find(int a)
{
if(father[a] == a)
return a;
else
return father[a] = find(father[a]);
} int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=0;i<=n;i++)
father[i] = i;
for(int i=0;i<m;i++)
scanf("%d%d",&edge[i].a,&edge[i].b); int ans = n;
st.push(n);
for(int i=m-1;i>0;i--)//从后往前开始合并,注意第一条边并没有实际存在,因为在一开始就已经被破坏了
{
int fa = find(edge[i].a);
int fb = find(edge[i].b);
if(fa != fb)//不同源,集合必然少一个
{
father[fa] = fb;
ans--;
}
st.push(ans);
} while(!st.empty())
{
printf("%d\n",st.top());
st.pop();
}
}
return 0;
}

并查集:HDU4496-D-City(倒用并查集)的更多相关文章

  1. 并查集 - UVALive 6889 City Park

    City Park Problem's Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=129725 Mean: 在 ...

  2. Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)

    D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: =  的情况我们用并查集把他们扔到一个集合,然后根据 > ...

  3. Solr集群的搭建以及使用(内涵zookeeper集群的搭建指南)

    1   什么是SolrCloud SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud.当一个系统的索引数据量少的时候 ...

  4. Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列之集群部署环境规划(一)

    0.前言 整体架构目录:ASP.NET Core分布式项目实战-目录 k8s架构目录:Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录 一.环境规划 软件 版本 ...

  5. 通过jedis连接redis单机成功,使用redis客户端可以连接集群,但使用JedisCluster连接redis集群一直报Could not get a resource from the pool

    一,问题描述: (如题目)通过jedis连接redis单机成功,使用JedisCluster连接redis集群一直报Could not get a resource from the pool 但是使 ...

  6. 【Oracle 集群】Oracle 11G RAC教程之集群安装(七)

    Oracle 11G RAC集群安装(七) 概述:写下本文档的初衷和动力,来源于上篇的<oracle基本操作手册>.oracle基本操作手册是作者研一假期对oracle基础知识学习的汇总. ...

  7. Mongodb 笔记06 副本集的组成、从应用程序连接副本集、管理

    副本集的组成 1. 同步:MongoDB的复制功能是使用操作日志oplog实现的,操作日志包含了主节点的每一次写操作.oplog是主节点的local数据库中的一个固定集合.备份节点通过查询整个集合就可 ...

  8. hadoop集群环境搭建之安装配置hadoop集群

    在安装hadoop集群之前,需要先进行zookeeper的安装,请参照hadoop集群环境搭建之zookeeper集群的安装部署 1 将hadoop安装包解压到 /itcast/  (如果没有这个目录 ...

  9. Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录

    0.目录 整体架构目录:ASP.NET Core分布式项目实战-目录 k8s架构目录:Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录 一.感谢 在此感谢.net ...

随机推荐

  1. AD Framework 发布(一)

      1.     EF Code First 发布时,需要配置数据库账号,账号需要存在服务器角色中具备 diskadmin,public,sysadmin 权限. 2.    数据库不存在时,会通过数 ...

  2. 内容显示分页数字分页 aspx

    此处是aspx里面分页显示,数据层和业务层是由动软生成 当然,我们也可以可以利用listView实现分页ListView(高效分页) public partial class NewList : Sy ...

  3. Cocos2d-x v3.1 安装图文教程(二)

       Cocos2d-x v3.1 安装图文教程(二) 如果我们需要在Android平台上运行就必须安装android的SDK,如果我们只想在window上运行就只需要安装Cocos2d-x就行了.当 ...

  4. CentOS服务器初始化设置

    CentOS服务器初始化设置 以阿里云服务器为例 一.挂载硬盘 1.磁盘分区 fdisk -l #查看设备,一般可以看到设备名为/dev/xvdb,或者为/dev/vdb(阿里云io优化型) fdis ...

  5. WebUploader实现采集图片的功能

    项目最开始用百度团队的文件上传组件做了个物料照片采集的功能,后来做员工头像采集时竟然不知道怎么使用了. 参照官方Demo: http://fex.baidu.com/webuploader/getti ...

  6. JavaWeb中五种转发方式(转)

    今天本来是想找一下在jsp中实现转发的方式的,无意中看到了一篇文章,然后稍微综合了把服务器端的转发也包括在内.   1. RequestDispatcher.forward() 是在服务器端起作用,当 ...

  7. Html+css实现带图标的控件

    </pre><pre name="code" class="html"><!DOCTYPE html> <html l ...

  8. 兼容ie8的圆形进度条

    主要是利用html5中的svg 画出圆形进度条 并且兼容ie8 https://github.com/GainLoss/Circular-progress-bar

  9. HDU4417 线段树 + 离线处理

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 , 线段树(或树状数组) + 离线处理 最近看了几道线段树的题都是需要离线处理数据的,正好这块比 ...

  10. 【洛谷1580】yyy loves Easter_Egg I(字符串处理题)

    点此看题面 大致题意: 略.(一道模拟题,自己去看题面吧) 几个字符数组函数 纯粹是一道字符串处理题,就当是学了一下各种与字符数组相关的函数吧! \(gets()\):这个是比较常用的函数,就是读入一 ...