HDU 1856 More is better(并查集+离散化)
题目地址: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(并查集+离散化)的更多相关文章
- HDU 1856 More is better (并查集)
题意: 给你两个数代表这两个人是朋友,朋友的朋友还是朋友~~,问这些人组成的集合里面人最多的是多少... 思路: 属于并查集了,我用的是带路径压缩的,一个集合里面所有元素(除了根节点)的父节点都是根节 ...
- HDU 1856 More is better(并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=1856 More is better Time Limit: 5000/1000 MS (Java/Others) ...
- HDU HDU1558 Segment set(并查集+判断线段相交)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1558 解题报告:首先如果两条线段有交点的话,这两条线段在一个集合内,如果a跟b在一个集合内,b跟c在一 ...
- hdu 1257 小希的迷宫 并查集
小希的迷宫 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1272 D ...
- hdu 3635 Dragon Balls(并查集应用)
Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...
- <hdu - 1272> 小希的迷宫 并查集问题 (注意特殊情况)
本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 Problem Description: 上次Gardon的迷宫城堡小希玩了很久(见Probl ...
- HDU 4496 D-City(逆向并查集)
http://acm.hdu.edu.cn/showproblem.php?pid=4496 题意: 给出n个顶点m条边的图,每次选择一条边删去,求每次删边后的连通块个数. 思路: 离线处理删边,从后 ...
- HDU 3407.Zjnu Stadium 加权并查集
Zjnu Stadium Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- HDU 1213 - How Many Tables - [并查集模板题]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1213 Today is Ignatius' birthday. He invites a lot of ...
随机推荐
- eclipse如何查看类之间的引用关系
今天遇到这个问题:mark一点点: 在类名上单击右键.选择Reference->Workingspace快捷克债券Ctrl+Shift+G 版权声明:本文博客原创文章,博客,未经同意,不得转载.
- svn自动备份
#!/bin/sh PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH DATE=` ...
- Java Runtime.getRuntime().exec() 执行带空格命令
可执行文件路径如果包含空格,则在java中不能被获取到. 此时Debug一下,会发现 project=null. project.waitFor 的返回值为1.但是去源路径单击bat文件是可以正常运行 ...
- HDU 1394 Minimum Inversion Number (线段树 单点更新 求逆序数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1394 题意:给你一个n个数的序列,当中组成的数仅仅有0-n,我们能够进行这么一种操作:把第一个数移到最 ...
- SpringMVC+Spring3+Hibernate4开发环境的搭建
在项目早期比较简单,大多用JSP .Servlet + JDBC 直接获取,以后使用 Struts1(Struts2)+Spring+Hibernate, 严格格按照分层概念驱动项目开发.利用这段时间 ...
- frontend http 前端名字定义问题
https://www.winfae.com/admin/api/menu haproxy 日志: Jun 24 13:04:49 localhost haproxy[14817]: 115.236. ...
- g++优化选项
g++优化选项 g++优化选项 对于下面的这段代码: 1 2 3 4 5 6 7 8 9 10 11 12 #include <iostream> using namespace std; ...
- Iron Foundry
Iron Foundry Provided by Tier 3 Iron Foundry is a project initiated by the engineers of Tier 3, an e ...
- JavaFX2: 鼠标拖动选择和Ctrl+Shift连续区间选择的ListView
JavaFX2的ListView中的多选没有提供鼠标拖动选择的功能,同时按下Ctrl和Shift后连续的区间选中也不支持,以下代码用于处理这两个问题,细节见代码注释: import com.sun.j ...
- Linux内核源代码解析之TCP面向字节流
本文原创为freas_1990,转载请标明出处:http://blog.csdn.net/freas_1990/article/details/11264237 大家都知道TCP是面向stream,而 ...