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. prototype 以及 constructor 属性的理解

    1 为什么 xx.constructor.prototype 可以访问到当前对象的原型. 'str'.constructor.prototype      'str'.constructor 指向当前 ...

  2. jquery调用asp.net 页面后台的实现代码

    先创建一个aspx页面编写一个客户端控件<input type="button" id="AjaxDemo" value="AjaxDemo&q ...

  3. jquery进阶(1)

    今天我们接着来学习jQuery中的内容,包括css的操作.尺寸的操作.文档的操作.动画(有待补充),事件处理操作. 一.CSS 在css中可以设置css的基本属性 - .css("color ...

  4. 父类和子类以及super关键字

    super和this关键字的特点类似:super代表的是父类对象的引用. 当子父类的成员出现同名时,可以通过super来进行区分. 子类的构造方法中,通过super关键字调用父类的构造方法. publ ...

  5. Eucalyptus管理页面密码设置

    桉树环境什么的都已经是配置好了的,但是过了一段时间不用,也不知道密码是什么了,看着下面的页面也不知道如何进去,这里我们通过命令行的方式重置用户名和密码信息. 登陆clc所在机器,输入下命令: euar ...

  6. jquery mmgrid使用

    参考 http://miemiedev.github.io/mmGrid/examples/index.html

  7. ASP.NET的三种开发模式

    前言 ASP.NET 是一个免费的Web开发框架,是由微软在.NET Framework框架中所提供的,或者说ASP.NET是开发Web应用程序的类库,封装在System.Web.dll 文件中.AS ...

  8. C++ vector常用法

    在c++中,vector是一个十分有用的容器,下面对这个容器做一下总结. 1 基本操作 (1)头文件#include<vector>. (2)创建vector对象,vector<in ...

  9. vue+node+mongodb实现的页面

    源代码地址:https://github.com/GainLoss/vue-node-mongodb 目前这个项目实现的是: 1.利用vue-cli实现前台页面的编写 (1)页面的跳转利用的是vue- ...

  10. selenium 使用键盘时 提示java.lang.IllegalArgumentException: Key Down / Up events only make sense for modifier keys.

    输入某个内容后,使用enter键进行确认,最开始使用方式为: driver.findElement(By.xpath("//input[@name='supplier_name'][@id= ...