题目地址: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. 检测用户是否具有administrator权限(OpenThreadToken,OpenProcessToken,GetTokenInformation,AllocateAndInitializeSid和EqualSid)

    检测用户是否具有administrator权限const SECURITY_NT_AUTHORITY: TSIDIdentifierAuthority = (Value: (0, 0, 0, 0, 0 ...

  2. php 写session

    function do_login(){ //获取用户名和密码信息,和数据库中比对 echo 111111111; dump($_POST); dump($_SESSION); echo 222222 ...

  3. [C++]new和delete

    Date:2014-1-5 Summary: C++中的动态内存创建与释放(这里就只记录C++中的new和delete了,其他的C风格操作就略过了) 单独记录new和delete的原因是为了学习时候关 ...

  4. tmpfs(/dev/shm)

    tmpfs是一种基于内存的文件系统,它和虚拟磁盘ramdisk比较类似像,但不完全相同,和ramdisk一样,tmpfs可以使用RAM,但它也可以使用swap分区来存储.而且传统的ramdisk是个块 ...

  5. ZOJ3805:Machine

    In a typical assembly line, machines are connected one by one. The first machine's output product wi ...

  6. leetcode day6 -- String to Integer (atoi) &amp;&amp; Best Time to Buy and Sell Stock I II III

    1.  String to Integer (atoi) Implement atoi to convert a string to an integer. Hint: Carefully con ...

  7. 开源工具DbUtils的使用(数据库的增删改查)

    开源工具DbUtils的使用(数据库的增删改查) 一.DbUtils简介: DBUtils是apache下的一个小巧的JDBC轻量级封装的工具包,其最核心的特性是结果集的封装,可以直接将查询出来的结果 ...

  8. .Net——使用.net内置处理程序处理自己定义节点Demo

    在.net中.由于对不同的节点,都相应着类去对它进行处理,.net里面为了方便.已经内置了一些类供我们使用.使我们在读取配置文件时.不必自己去定义类去处理自己定义的自己定义节点. 以下我们写了这样一个 ...

  9. 登录RMAN 报告ORA-12162:TNS:net service name is incorrectly specified错

    登录RMAN 报告ORA-12162:TNS:net service name is incorrectly specified错 [oracle@localhost admin]$ date Tue ...

  10. gulp快速入门

    gulp快速入门 因为之前一直有人给我推荐gulp,说他这里好哪里好的.实际上对我来说够用就行.grunt熟悉以后实际上他的配置也不难,说到效率的话如果真是要完整打包上线也不在乎那么几秒时间,对于项目 ...