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 ...
随机推荐
- python解析smart结构数据
python编程解析如下smart结构数据,得到一行smart信息 run: smartctl -a /dev/sda out: smartctl 6.3 2014-07-26 r3976 [x86_ ...
- Qt之QCheckBox
简述 QCheckBox继承自QAbstractButton,它提供了一个带文本标签的复选框. QCheckBox(复选框)和QRadioButton(单选框)都是选项按钮.这是因为它们都可以在开(选 ...
- SPAdes
用后感: 拼个小基因组还好,对于很大的基因组,文库很多的,还是不要用了.服务器768G内存,都不够用.... 主页: http://bioinf.spbau.ru/spades 说明书: http:/ ...
- xcode中的一些快捷键
隐藏xcode command+h退出xcode command+q关闭窗口 command+w关闭所有窗口 command+option+w关闭当前项目 command+control+w关闭当前文 ...
- centos网页乱码
修改vi /etc/my.cnf [client] (新增)default-character-set=utf8 [mysql] (添加)default-character-set=utf8
- 矩阵基本运算的 Python 实现
from...import与import区别在于import直接导入指定的库,而from....import则是从指定的库中导入指定的模块 import...as则是将import A as B,给予 ...
- poj 3320 技巧/尺取法 map标记
Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...
- php部分---函数、四类常用函数、例子(下拉菜单添加内容);
1.简单函数 四要素:返回类型,函数名,参数列表,函数体 function Show() { echo "hello"; } Show(); 2.有返回值的函数 function ...
- PHP设置图片文件上传大小的具体实现方法
PHP默认的上传限定是最大2M,想上传超过此设定的文件,需要调整PHP.apache等的一些参数 我们简要介绍一下PHP文件上传涉及到的一些参数: •file_uploads :是否允许通过HTTP上 ...
- POJ-2481 Cows (线段树单点更新)
题目大意:给n个区间,对于任意一个区间,求出能完全包含它并且长度比它长的区间的个数. 题目分析:将一个区间视为二位坐标系中的一个点,左端点视作横坐标,右端点视作纵坐标.则题目变成了求每个点的左上方.正 ...