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 ...
随机推荐
- Hashing filters for very fast massive filtering
If you have a need for thousands of rules, for example if you have a lot of clients or computers, al ...
- TC Hash Filter
Overview The u32 filter allows you to match on any bit field within a packet, so it is in some ways ...
- Canvas 获取颜色值
Canvas 是 HTML5 的画布元素,按照像素绘制图像.有时需要用户点击鼠标的时候获取像素值. 获取画布元素 var canvas = document.getElementById(" ...
- sqlserver mdf向上兼容附加数据库(无法打开数据库 'xxxxx' 版本 611。请将该数据库升级为最新版本。)
最近工作中有一个sqlserver2005版本的mdf文件,还没有log文件,现在需要 附加到sqlserver2012,经过网上一顿搜索,把完整的过程奉上,供大家参考 首先创建数据库 再设置数据库的 ...
- HOG参数简介及Hog特征维数的计算(转)
HOG构造函数 CV_WRAP HOGDescriptor() :winSize(64,128), blockSize(16,16), blockStride(8,8), cellSize( ...
- spring mvc 重定向传参
参考链接如下: http://bbs.csdn.net/topics/391034118?page=1 自己的示例程序: 详细页面提交一个修改动作,修改完成后跳转到检索页面,把检索条件重新赋值给检索页 ...
- JavaScript BOM 遗漏知识再整理;弹窗和记时事件;
1.JavaScript 弹窗 警告框 警告框经常用于确保用户可以得到某些信息. 当警告框出现后,用户需要点击确定按钮才能继续进行操作. window.alert() 方法可以不带上window对象, ...
- Python 判断一个字符串是否在列表中任何一个字符串中出现过
strlist = ['a1', 'a2', 'b1'] if any("a" in s for s in strlist):
- (转) TensorFlow深度学习,一篇文章就够了
TensorFlow深度学习,一篇文章就够了 2016/09/22 · IT技术 · TensorFlow, 深度学习 分享到:6 原文出处: 我爱计算机 (@tobe迪豪 ) 作者: 陈迪 ...
- Linux dbg debugging
http://h41379.www4.hpe.com/doc/84final/4538/4538pro_contents.html https://kgdb.wiki.kernel.org/index ...