题目地址:HDU 1856

水题。因为标号范围太大,而数据数仅仅有10w,所以要先进行离散化。然后就是裸的并查集了。

代码例如以下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <algorithm> using namespace std;
int bin[100001], x[100001], y[100001], z[200001], c[200001], cnt, vis[200000];
int find1(int x)
{
return bin[x]==x?x:bin[x]=find1(bin[x]);
}
void merger(int x, int y)
{
int f1=find1(bin[x]);
int f2=find1(bin[y]);
if(f1!=f2)
bin[f2]=f1;
}
int twofen(int x)
{
int high=cnt-1, low=0, mid;
while(low<=high)
{
mid=low+high>>1;
if(c[mid]==x) return mid;
else if(c[mid]<x) low=mid+1;
else high=mid-1;
}
}
int main()
{
int n, i, max1, a, b, f1, f2;
while(scanf("%d",&n)!=EOF)
{
cnt=1;
for(i=0;i<n;i++)
{
scanf("%d%d",&x[i],&y[i]);
z[2*i]=x[i];
z[2*i+1]=y[i];
}
sort(z,z+2*n);
c[0]=z[0];
for(i=1;i<2*n;i++)
{
if(z[i]!=z[i-1])
c[cnt++]=z[i];
}
for(i=0;i<cnt;i++)
bin[i]=i;
for(i=0;i<n;i++)
{
f1=twofen(x[i]);
f2=twofen(y[i]);
merger(f1,f2);
}
memset(vis,0,sizeof(vis));
max1=-1;
for(i=0;i<cnt;i++)
{
int p=find1(bin[i]);
vis[p]++;
if(max1<vis[p])
max1=vis[p];
}
printf("%d\n",max1);
}
return 0;
}

HDU 1856 More is better(并查集+离散化)的更多相关文章

  1. HDU 1856 More is better (并查集)

    题意: 给你两个数代表这两个人是朋友,朋友的朋友还是朋友~~,问这些人组成的集合里面人最多的是多少... 思路: 属于并查集了,我用的是带路径压缩的,一个集合里面所有元素(除了根节点)的父节点都是根节 ...

  2. HDU 1856 More is better(并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others) ...

  3. HDU HDU1558 Segment set(并查集+判断线段相交)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...

  4. hdu 1257 小希的迷宫 并查集

    小希的迷宫 Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 D ...

  5. hdu 3635 Dragon Balls(并查集应用)

    Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...

  6. <hdu - 1272> 小希的迷宫 并查集问题 (注意特殊情况)

     本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 Problem Description: 上次Gardon的迷宫城堡小希玩了很久(见Probl ...

  7. HDU 4496 D-City(逆向并查集)

    http://acm.hdu.edu.cn/showproblem.php?pid=4496 题意: 给出n个顶点m条边的图,每次选择一条边删去,求每次删边后的连通块个数. 思路: 离线处理删边,从后 ...

  8. HDU 3407.Zjnu Stadium 加权并查集

    Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  9. HDU 1213 - How Many Tables - [并查集模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...

随机推荐

  1. Zeroonepack coming~^.^

    今天抓的四道DP做完了==三道是用背包做的,突然想起来背包知识点总结还没做~反正时间还早..把01背包和完全背包小结了吧~~福利来啦~~噶呜~ 01背包: 基本思路: 01背包问题是最广为人知的动态规 ...

  2. (三)----使用HttpClient发送HTTP请求(分别通过GET和POST方法发送数据)

    文章来源:http://www.cnblogs.com/smyhvae/p/4006009.html 一.GET和POST的对比: 在漫长的时间当中,其他的方法逐渐的退出了历史舞台,最常用的只剩下GE ...

  3. UVA 534 - Frogger(kruskal扩展)

    UVA 534 - Frogger 题目链接 题意:给定一些点.如今要求一条路径从第一个点能跳到第二个点,而且这个路径上的最大距离是最小的 思路:利用kruskal算法,每次加最小权值的边进去,推断一 ...

  4. Eclipse Tips

    一.取消拼写检查 Window -> Preferences -> General -> Editors -> Text Editors -> Spelling -> ...

  5. Android架构设计和软硬整合完整训练

    Android架构设计和软硬整合完整训练 Android架构设计和软硬整合完整训练:HAL&Framework&Native Service&Android Service&a ...

  6. VSTO学习笔记(二)Excel对象模型

    原文:VSTO学习笔记(二)Excel对象模型 上一次主要学习了VSTO的发展历史及其历代版本的新特性,概述了VSTO对开发人员的帮助和效率提升.从这次开始,将从VSTO 4.0开始,逐一探讨VSTO ...

  7. hdu4635(强连通缩点)

    传送门:Strongly connected 题意:求最多可以加多少边,使得最新的图还不是强连通图. 分析:最终添加完边的图,肯定可以分成两个部X和Y,其中只有X到Y的边没有Y到X的边,那么要使得边数 ...

  8. libevent安装总结 - jinfg2008的专栏 - 博客频道 - CSDN.NET

    libevent安装总结 - jinfg2008的专栏 - 博客频道 - CSDN.NET libevent安装总结 分类: linux 系统配置 2013-02-13 22:37 99人阅读 评论( ...

  9. UVA 639 (13.08.25)

     Don't Get Rooked  In chess, the rook is a piece that can move any number of squaresvertically or ho ...

  10. 基于CefGlue的桌面应用开发

    原文地址:http://johnnyfee.github.io/csharp/2013/12/21/cef-glue/ 前言 如果你想使用WEB技术来开发桌面客户端,并且是想使用的语言也是C#时,那请 ...