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. Vue provide/inject 部分源码分析 实现响应式数据更新

    provide/inject 数据响应式更新的坑及源码解析 下面是我自己曾经遇到 一个问题,直接以自己QA的形式来写吧 自问自答了,需要的同学也可以直接访问segmentfault地址 官网给出实例, ...

  2. ORA-29875 出现问题解决办法

    出现问题:Underlying DBMS error [ORA-29875: 无法执行 ODCIINDEXINSERT 例行程序ORA-20085: Insert Spatial Reference ...

  3. 让zepto支持requirejs的方法

    window.Zepto = Zepto '$' in window || (window.$ = Zepto) if ( typeof define === "function" ...

  4. Ashx增删改查_动软

    1.首先展示列表 ashx 讲究的是个替换 这些就是属于ashx麻烦的地方 public void ProcessRequest(HttpContext context) { context.Resp ...

  5. hibernate课程 初探单表映射3-4 组件属性

    本节内容: 1 简介组件属性 2 demo 1 简介组件属性: <component name = "address" class = "Address" ...

  6. windows下 php集成环境如何通过cmd执行命令

    ---恢复内容开始--- php学习过程中 Windows环境下的php集成程序很多 这样方便了学习 但是在熟悉命令使用方面可以说是十分不便 本文将从两个方便 向大家介绍如何快速通过cmd命令实现命令 ...

  7. 云为 | 提供海外 IT 人才派遣、猎头、人力资源外包服务

    云为是大连信为软件开发有限公司为人力资源外包服务创建的品牌,是中国专业的人力资源外包领域的服务商,在信息技术行业为海外企业雇主招聘合格.专业且技能熟练的精英人士.我们的客户涵盖了日本上市公司和​​株式 ...

  8. selenium模糊匹配控件

    起因:在查找一些控件时,可能控件的一些属性是变化的,那在匹配时需要进行模糊匹配,模糊匹配,使用xpath 定位方式有种: contains(属性名,字符串):使用文本匹配,功能很强大 starts-w ...

  9. UVA 1213 - Sum of Different Primes(递推)

    类似一个背包问题的计数问题.(虽然我也不记得这叫什么背包了 一开始我想的状态定义是:f[n = 和为n][k 个素数]. 递推式呼之欲出: f[n][k] = sigma f[n-pi][k-1]. ...

  10. hdu-1532 Drainage Ditches---最大流模板题

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1532 题目大意: 给出有向图以及边的最大容量,求从1到n的最大流 思路: 传送门:最大流的增广路算法 ...