hdu 5652 India and China Origins 并查集+二分
India and China Origins
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Let's assume from my crude drawing that the only way to reaching from India to China or viceversa is through that grid, blue portion is the ocean and people haven't yet invented the ship. and the yellow portion is desert and has ghosts roaming around so people can't travel that way. and the black portions are the location which have mountains and white portions are plateau which are suitable for travelling. moutains are very big to get to the top, height of these mountains is infinite. So if there is mountain between two white portions you can't travel by climbing the mountain.
And at each step people can go to 4 adjacent positions.
Our archeologists have taken sample of each mountain and estimated at which point they rise up at that place. So given the times at which each mountains rised up you have to tell at which time the communication between India and China got completely cut off.
For each test case, the first line contains two space seperated integers N,M. next N lines consists of strings composed of 0,1 characters. 1 denoting that there's already a mountain at that place, 0 denoting the plateau. on N+2 line there will be an integer Q denoting the number of mountains that rised up in the order of times. Next Q lines contain 2 space seperated integers X,Y denoting that at ith year a mountain rised up at location X,Y.
T≤10
1≤N≤500
1≤M≤500
1≤Q≤N∗M
0≤X<N
0≤Y<M
print -1 if these two countries still connected in the end.
Hint:
From the picture above, we can see that China and India have no communication since 4th year.
#include<iostream>
#include<cstdio>
#include<cmath>
#include<string>
#include<queue>
#include<algorithm>
#include<stack>
#include<cstring>
#include<vector>
#include<list>
#include<set>
#include<map>
using namespace std;
#define ll __int64
#define mod 1000000007
int scan()
{
int res = , ch ;
while( !( ( ch = getchar() ) >= '' && ch <= '' ) )
{
if( ch == EOF ) return << ;
}
res = ch - '' ;
while( ( ch = getchar() ) >= '' && ch <= '' )
res = res * + ( ch - '' ) ;
return res ;
}
int l,k;
char a[][];
struct is
{
int l,r;
}ch[];
int father[];
int vis[][];
int xz[]={,,-,};
int yz[]={,,,-};
int find_father(int x)
{
return x==father[x]? x:father[x]=find_father(father[x]);
}
void hebing(int x,int y)
{
int xx=find_father(x);
int yy=find_father(y);
if(xx!=yy)
father[xx]=yy;
}
int number(int x,int y)
{
return (x-)*k+y;
}
int check(int ff)
{
memset(vis,,sizeof(vis));
for(int i=;i<=l*k;i++)
father[i]=i;
father[]=;
father[]=;
for(int i=;i<=l;i++)
for(int t=;t<=k;t++)
if(a[i][t]=='')
vis[i][t]=;
else
vis[i][t]=;
for(int i=;i<=ff;i++)
vis[ch[i].l+][ch[i].r+]=;
for(int i=;i<=l;i++)
for(int t=;t<=k;t++)
{
if(vis[i][t])continue;
for(int j=;j<;j++)
{
int xx=i+xz[j];
int yy=t+yz[j];
if(yy<=||yy>k||vis[xx][yy])continue;
if(xx==)hebing(number(i,t),);
else if(xx==l+)hebing(number(i,t),);
else hebing(number(i,t),number(xx,yy));
}
}
int ans1=find_father();
int ans2=find_father();
if(ans1==ans2)
return ;
return ;
}
int main()
{
int x,y,i,t;
scanf("%d",&x);
while(x--)
{
scanf("%d%d",&l,&k);
for(i=;i<=l;i++)
scanf("%s",a[i]+);
scanf("%d",&y);
for(t=;t<=y;t++)
scanf("%d%d",&ch[t].l,&ch[t].r);
if(!check())
{
printf("-1\n");
continue;
}
int st=,en=y,mid;
while(st<en)
{
mid=(st+en)>>;
if(check(mid))
st=mid+;
else
en=mid;
}
printf("%d\n",st);
}
return ;
}
代码
hdu 5652 India and China Origins 并查集+二分的更多相关文章
- hdu 5652 India and China Origins 并查集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...
- hdu 5652 India and China Origins 并查集+逆序
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题意:一张n*m个格子的点,0表示可走,1表示堵塞.每个节点都是四方向走.开始输入初始状态方格, ...
- hdu5652 India and China Origins(并查集)
India and China Origins Accepts: 49 Submissions: 426 Time Limit: 2000/2000 MS (Java/Others) Memo ...
- HDU 5652 India and China Origins 二分+并查集
India and China Origins 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5652 Description A long time ...
- 并查集(逆序处理):HDU 5652 India and China Origins
India and China Origins Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- HDU 5652 India and China Origins(并查集)
India and China Origins Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/ ...
- HDU 5652 India and China Origins(经典并查集)
特别经典的一个题,还有一种方法就是二分+bfs 题意:空间内n*m个点,每个点是0或者1,0代表此点可以走,1代表不能走.接着经过q年,每年一个坐标表示此点不能走.问哪年开始图上不能出现最上边不能到达 ...
- hdu 5652 India and China Origins(二分+bfs || 并查集)BestCoder Round #77 (div.2)
题意: 给一个n*m的矩阵作为地图,0为通路,1为阻碍.只能向上下左右四个方向走.每一年会在一个通路上长出一个阻碍,求第几年最上面一行与最下面一行会被隔开. 输入: 首行一个整数t,表示共有t组数据. ...
- (hdu)5652 India and China Origins 二分+dfs
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5652 Problem Description A long time ago there ...
随机推荐
- Sequence(priority_queue)
这题很智慧. VJ上4000多ms #include<cstdio> #include<algorithm> #include<queue> #include &l ...
- HTTP请求返回状态码详解
当用户试图通过 HTTP 访问一台正在运行 Internet 信息服务 (IIS) 的服务器上的内容时,IIS 返回一个表示该请求的状态的数字代码.状态代码可以指明具体请求是否已成功,还可以揭示请求失 ...
- malloc 实现原理
1. Reference: 如何实现一个malloc http://blog.codinglabs.org/articles/a-malloc-tutorial.html 2.
- 【upstream】Nginx配置upstream实现负载均衡
如果Nginx没有仅仅只能代理一台服务器的话,那它也不可能像今天这么火,Nginx可以配置代理多台服务器,当一台服务器宕机之后,仍能保持系统可用.具体配置过程如下: 1. 在http节点下,添加ups ...
- 基于Axis1.4的webservice接口开发(环境搭建)
基于Axis1.4的webservice接口开发(环境搭建) 一.环境搭建: 1.搜索关键字“Axis1.4”下载Axis1.4相关的jar包. 下载地址:http://download.csdn.n ...
- Linux基础命令---lsattr
lsattr 显示指定文件或者目录的属性. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法 lsattr [选项 ...
- php array_multisort对数据库结果多个字段进行排序
php array_multisort对数据库结果多个字段进行排序$data 数组中的每个单元表示一个表中的一行.这是典型的数据库记录的数据集合. 例子中的数据如下:volume | edition ...
- DBus学习网站
http://blog.csdn.net/thonrbirdxb/article/details/11482007 DBus的基本资料可以参考 DBus学习笔记(博客园) http://dotnet. ...
- ACM题目————困难的串
题目描述 如果一个字符串包含两个相邻的重复子串,则称他是“容易的串”,其他串称为"困难的串".例如,BB,ABCDACABCAB,ABCDABCD都是容易的串,而D,DC,ABDA ...
- Django 将数据库查出的 QuerySet 对象转换为 json 字符串
通过Django查询出MySQL数据库的数据,并将查询出的QuerySet 对象转化成 json 字符串. 写这个例子的作用主要是用来为手机端提供接口用,记录一下,以后 说不准 肯定能用到! ---- ...