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"的更多相关文章

  1. Sicily connect components in undirected graph

    题目介绍: 输入一个简单无向图,求出图中连通块的数目. Input 输入的第一行包含两个整数n和m,n是图的顶点数,m是边数.1<=n<=1000,0<=m<=10000. 以 ...

  2. sicily 4378 connected components in undirected graph

    题意:求图中的连通块数,注意孤立的算自连通! 例如:6个顶点3条路径,其中路径为:1->2    4->5  1->3 那么有(1-2&&1->3) + (4- ...

  3. [SOJ] connect components in undirected graph

    题目描述: 输入一个简单无向图,求出图中连通块的数目 输入: 输入的第一行包含两个整数n和m,n是图的顶点数,m是边数.1<=n<=1000,0<=m<=10000. 以下m行 ...

  4. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  5. 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 ...

  6. [CodeChef - GERALD07 ] Chef and Graph Queries

    Read problems statements in Mandarin Chineseand Russian. Problem Statement Chef has a undirected gra ...

  7. ZOJ3874 Permutation Graph

    Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward has a permutation {a1, a2, … an}. He finds ...

  8. CodeForces - 986C AND Graph

    不难想到,x有边连出的一定是 (2^n-1) ^ x 的一个子集,直接连子集复杂度是爆炸的...但是我们可以一个1一个1的消去,最后变成补集的一个子集. 但是必须当且仅当 至少有一个 a 等于 x 的 ...

  9. Educational Codeforces Round 37 E. Connected Components?(图论)

    E. Connected Components? time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

随机推荐

  1. mysql explain用法和结果的含义(转)

    重点是第二种用法,需要深入的了解. 先看一个例子: mysql> explain select * from t_order; +----+-------------+---------+--- ...

  2. Java 最简单的计算器——使用Args参数

    public class Test{ public static void main(String[] args){ if(args.length<3){ System.out.println( ...

  3. Sobel算子 (转)

    幻灯片1 Sobel算子 幻灯片2 一.Sobel边缘检测算子 l 在讨论边缘算子之前,首先给出一些术语的定义: l (1)边缘:灰度或结构等信息的突变处,边缘是一个区域的结束,也是另一个区域的开始, ...

  4. 383. Ransom Note

    
Given
 an 
arbitrary
 ransom
 note
 string 
and 
another 
string 
containing 
letters from
 all 
th ...

  5. POJ 3264 区间最大最小值Sparse_Table算法

    题目链接:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total ...

  6. html部分---样式属性;

    <!--大小--> width:宽度 height:高度 <!--背景与前景--> "background-color:#0F0; 背景颜色 background-i ...

  7. JavaWeb学习记录(二十三)——文件上传与下载

    一.导入jar包

  8. 选数 2002年NOIP全国联赛普及组

    题目描述 Description 已知 n 个整数 x1,x2,-,xn,以及一个整数 k(k<n).从 n 个整数中任选 k 个整数相加,可分别得到一系列的和.例如当 n=4,k=3,4 个整 ...

  9. POJ2125 Destroying The Graph (最小点权覆盖集)(网络流最小割)

                                                          Destroying The Graph Time Limit: 2000MS   Memo ...

  10. thinkphp 验证码的使用

    在thinkphp中使用验证码很容易,只要调用thinkphp现有的方法就可以.当然,php的GD库肯定是要开的(就是在php.ini中要加载gd模块). thinkphp 3.2 --------- ...