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 ...
随机推荐
- 【题解】【直方图】【Leetcode】Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- threeSum_0
//找出数组中三个数相加为0,返回存在的组数 //输入指正*A,长度为size,返回*B和长度num int threeSum_0(int *A,int size,int *B,int &nu ...
- GFF format
后记: ************************************************************************ 在使用cufflinks和cuffmerge中 ...
- Vimium 快捷键记录
, <c-e> : Scroll down k, <c-y> : Scroll up h : Scroll left l : Scroll right gg : Scroll ...
- html部分---a标签的用法、放置图片与表格;
a标签的用法: 1.加链接 herf <a href="http://www.baidu.com">百度一下</a> 2.加载本地文件 <a hre ...
- linux笔记_文件搜索命令
一.locate命令 locate命令属于mlocate包,如果执行locate filename提示命令未找到执行安装mlocate包 # yum -y install mlocate 安装后执行l ...
- UVa 272 Tex Quotes --- 水题
题目大意:在TeX中,左引号是 ``,右引号是 ''.输入一篇包含双引号的文章,你的任务是把他转成TeX的格式 解题思路:水题,定义一个变量标记是左引号还是右引号即可 /* UVa 272 Tex Q ...
- codevs3872 邮递员送信(SPFA)
邮递员送信 时间限制: 1 Sec 内存限制: 64 MB提交: 10 解决: 5[提交][状态][讨论版] 题目描述 有一个邮递员要送东西,邮局在节点1.他总共要送N-1样东西,其目的地分别是2 ...
- Python 判断一个字符串是否在列表中任何一个字符串中出现过
strlist = ['a1', 'a2', 'b1'] if any("a" in s for s in strlist):
- 转贴:sudo apt-get install 可以安装的一些软件
Ubuntu 下的一些软件安装sudo apt-get install virtualbox#华主席推荐 2007年年度最佳软件,最佳编辑选择奖得主.....sudo apt-get install ...