HackerRank "Components in a graph"
Regular Union-Find practice one.
#include <cmath>
#include <cstdio>
#include <climits>
#include <vector>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <unordered_set>
#include <unordered_map>
using namespace std; unordered_map<int, int> ps; // for id
unordered_map<int, unsigned> us; // for sets int id(int v)
{
if(!ps.count(v)) return -; while(ps[v] != v) v = ps[v];
return v;
} bool find_(int p0, int p1)
{
return id(p0) == id(p1) && id(p0) != -;
} void union_(int v0, int v1)
{
int p0 = id(v0), p1 = id(v1);
if(p0 != - && p1 != -) // 2 existing sets
{
if(p0 != p1)
{
int sp = min(p0, p1);
int sl = max(p0, p1);
ps[sl] = sp; us[sp] += us[sl];
us.erase(sl);
}
}
else // 1 is wild
{
int pv = p0 != - ? p0 : p1;
int wv = p0 == - ? v0 : v1;
ps[wv] = pv;
us[pv]++;
}
} int main()
{ int n; cin >> n;
while(n--)
{
int a, b; cin >> a >> b;
int s = min(a, b), l = max(a, b);
int is = id(s), il = id(l); if(is == - && il == -)
{
ps[l] = ps[s] = s;
us[s] = ;
}
else
{
if(!find_(s, l))
{
union_(s, l);
}
}
} unsigned minv = INT_MAX, maxv = ;
for(auto &kv: us)
{
maxv = max(maxv, kv.second);
minv = min(minv, kv.second);
}
cout << minv << " " << maxv << endl;
return ;
}
HackerRank "Components in a graph"的更多相关文章
- Sicily connect components in undirected graph
题目介绍: 输入一个简单无向图,求出图中连通块的数目. Input 输入的第一行包含两个整数n和m,n是图的顶点数,m是边数.1<=n<=1000,0<=m<=10000. 以 ...
- sicily 4378 connected components in undirected graph
题意:求图中的连通块数,注意孤立的算自连通! 例如:6个顶点3条路径,其中路径为:1->2 4->5 1->3 那么有(1-2&&1->3) + (4- ...
- [SOJ] connect components in undirected graph
题目描述: 输入一个简单无向图,求出图中连通块的数目 输入: 输入的第一行包含两个整数n和m,n是图的顶点数,m是边数.1<=n<=1000,0<=m<=10000. 以下m行 ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Knowing how all your components work together: distributed tracing with Zipkin
转自: http://aredko.blogspot.com/2014/02/knowing-how-all-your-components-work.html In today's post we ...
- [CodeChef - GERALD07 ] Chef and Graph Queries
Read problems statements in Mandarin Chineseand Russian. Problem Statement Chef has a undirected gra ...
- ZOJ3874 Permutation Graph
Time Limit: 2 Seconds Memory Limit: 65536 KB Edward has a permutation {a1, a2, … an}. He finds ...
- CodeForces - 986C AND Graph
不难想到,x有边连出的一定是 (2^n-1) ^ x 的一个子集,直接连子集复杂度是爆炸的...但是我们可以一个1一个1的消去,最后变成补集的一个子集. 但是必须当且仅当 至少有一个 a 等于 x 的 ...
- Educational Codeforces Round 37 E. Connected Components?(图论)
E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
随机推荐
- dos快速通道
要在文件夹的右键菜单中添加“命令提示符”选项.在注册表HKEY_CLASSES_ROOT\Directory\shell分支下新建一项“CommandPrompt”,修改右侧窗格中的“默认”键值为“命 ...
- Xen虚拟机磁盘镜像模板制作(一)—Windows Server 2008(2012)
这段时间一直在研究如何制作一个适合Xen虚拟化的Windows Server 2008(2012)磁盘镜像,中间虽然遇到了一些阻挠,不过最终还是顺利解决,成功制作出了Xen Windows Serve ...
- NetCDF 格式化数据概述
前言 马上要开始新的项目了.气象局方面要求处理的数据是 NetCDF 格式的,这是一种封装好了的数据结构,需要连接一些库,调用特定的 API 进行操作. 可这方面我知之甚少,遂利用周日上午的时间学习这 ...
- iPhone不为人知的功能常用技巧,看完后才发现很多用iPhone的人实在是愧对乔布斯! - imsoft.cnblogs
很多人花了四五千买部苹果,结果只用到四五百块钱的普通手机功能. iPhone不为人知的功能,常用技巧: 网上搜集整理的iPhone快捷键操作,虽然表面上iPhone按键只有一个HOME键,大部分操作都 ...
- CollectionsAPI
鉴于总用,总结一下: 类别 方法 查找 binarySearch. 返回 enumeration.checkedSortedMap 判断 max.min 移动 reverse(List).revers ...
- ZOJ 1105 FatMouse’s Tour
原题链接 题目大意:FM要去逛街,他可以走任何街道,在任何路口转弯.他走一条陌生街道的速度是20公里每小时,走一条熟悉街道的速度是50公里每小时.输入是街道信息,输出消耗时间. 解法:本质就是浮点运算 ...
- Jquery基础知识;
1.jquery语法 jQuery 语法是通过选取 HTML 元素,并对选取的元素执行某些操作. 基础语法: $(selector).action() 美元符号定义 jQuery 选择符(select ...
- 02java语法基础问题总结
S和t引用的不是同一个对象 不是原始数据类型 结论: 枚举类型是引用类型,枚举不属于原始数据类型.它的每一个具体值都引用一个特定的对象. 2. 以下代码的输出结果是什么? int X=100; int ...
- Apache运行模式
Apache 2.X 支持插入式并行处理模块,称为多路处理模块(MPM: Multi-Processing Modules).在编译apache时必须选择也只能选择一个MPM,对类UNIX系统,有几个 ...
- HDU 5955 Guessing the Dice Roll
HDU 5955 Guessing the Dice Roll 2016 ACM/ICPC 亚洲区沈阳站 题意 有\(N\le 10\)个人,每个猜一个长度为\(L \le 10\)的由\(1-6\) ...