并查集:HDU4496-D-City(倒用并查集)
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(倒用并查集)的更多相关文章
- 并查集 - UVALive 6889 City Park
City Park Problem's Link: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=129725 Mean: 在 ...
- Codeforces Round #541 (Div. 2) D(并查集+拓扑排序) F (并查集)
D. Gourmet choice 链接:http://codeforces.com/contest/1131/problem/D 思路: = 的情况我们用并查集把他们扔到一个集合,然后根据 > ...
- Solr集群的搭建以及使用(内涵zookeeper集群的搭建指南)
1 什么是SolrCloud SolrCloud(solr 云)是Solr提供的分布式搜索方案,当你需要大规模,容错,分布式索引和检索能力时使用 SolrCloud.当一个系统的索引数据量少的时候 ...
- Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列之集群部署环境规划(一)
0.前言 整体架构目录:ASP.NET Core分布式项目实战-目录 k8s架构目录:Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录 一.环境规划 软件 版本 ...
- 通过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 但是使 ...
- 【Oracle 集群】Oracle 11G RAC教程之集群安装(七)
Oracle 11G RAC集群安装(七) 概述:写下本文档的初衷和动力,来源于上篇的<oracle基本操作手册>.oracle基本操作手册是作者研一假期对oracle基础知识学习的汇总. ...
- Mongodb 笔记06 副本集的组成、从应用程序连接副本集、管理
副本集的组成 1. 同步:MongoDB的复制功能是使用操作日志oplog实现的,操作日志包含了主节点的每一次写操作.oplog是主节点的local数据库中的一个固定集合.备份节点通过查询整个集合就可 ...
- hadoop集群环境搭建之安装配置hadoop集群
在安装hadoop集群之前,需要先进行zookeeper的安装,请参照hadoop集群环境搭建之zookeeper集群的安装部署 1 将hadoop安装包解压到 /itcast/ (如果没有这个目录 ...
- Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录
0.目录 整体架构目录:ASP.NET Core分布式项目实战-目录 k8s架构目录:Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录 一.感谢 在此感谢.net ...
随机推荐
- window.open 打开Excel或者Word 无权限问题
场景:后端C# ashx 前端:js js在对ashx返回结果进行window.open(url) url为后端保存excel的一个地址 提示:无操作权限 url:为后端处理后,服务器上一个完整的路 ...
- PIO导出
1..HSSFWorkbook 声明一个工作簿,创建一个excel文件 //创建HSSFWork对象(excel的文档对象) HSSFWorkbook wb=new HSSFWorkbook(); / ...
- 给浏览器绑定鼠标滚动事件(兼容FireFox)
var bs = new Browser(); if(bs.userBrowser() == 'firefox'){ document.body.addEventListener("DOMM ...
- 妙用Object
妙用Object 当你在写C#程序时,经常会用到“ToString()”这个方法,而且如果你细心你点就会发现所有的引用类型都含有“ToString()”这个方法,那么你知道为什么会这样吗?很简单,因为 ...
- 导入maven多模块项目 出现的问题
近日导入maven多模块项目 出现的问题以及解决过程2017年12月04日 20:43:04 守望dfdfdf 阅读数:815 标签: jdkmavenmaven pom.xml 更多个人分类: 工作 ...
- OpenStack Ocata Telemetry 警告服务部署
下列操作在控制节点上进行: 1 准备条件 在配置OpenStack Telemetry服务之前,你必须创建数据库.服务凭证和API端点. 1.1 数据库 以root用户连接数据库服务器,创建glanc ...
- 学习日记---java
1.构造函数 构造函数:首字母大写:对象创建时,就会调用与之对应的构造函数,对对象进行初始化. 只调用一次. 一般函数:对象创建后,需要函数功能时才调用.可以多次调用.首字母小写. 构造函数--重载: ...
- (转载)C#线程优先级详解
计算机中经常会有多个任务同时运行,其中总有一些看起来更紧急,更需要优先完成.比如我们现在有两个任务,一个任务是下载一部电影,另一个任务是检测用户的输入.显然及时响应用户操作应具有更高的优先级,因为我们 ...
- 【Android开发笔记】返回上层Activity的正确打开方式
技术支持 http://stackoverflow.com/questions/12276027/how-can-i-return-to-a-parent-activity-correctly 首先, ...
- hihoCoder hiho一下 第二周 #1014 : Trie树(Trie树基本应用)
思路: 完全看题目中的介绍就行了.还有里面的input写道:不保证是英文单词,也有可能是火星文单词哦.比赛结束后的提交是不用考虑26个字母之外的,都会AC,如果考虑128种可能的话,爆了内存.步骤就是 ...