India and China Origins

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Problem Description
A long time ago there are no himalayas between India and China, the both cultures are frequently exchanged and are kept in sync at that time, but eventually himalayas rise up. With that at first the communation started to reduce and eventually died.

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.

 
Input
There are multi test cases. the first line is a sinle integer T which represents the number of test cases.

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

 
Output
Single line at which year the communication got cut off.

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.

 
Sample Input
1
4 6
011010
000010
100001
001000
7
0 3
1 5
1 3
0 0
1 2
2 4
2 1
 
Sample Output
4
 
Source
思路:这是一个连通性的问题。你会发现如果将所有操作逆序来看的话就很容易用并查集来处理了。 首先把所有的山峰都加到图中,然后逆序处理每个操作: 对某次操作,在图中删除该位置的山峰,然后判断两个点是否联通,一旦联通就得到了结果。 这里需要对China和India分别新建一个对应的节点。(然而数据好像没有-1的情况~~~)
#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 并查集+二分的更多相关文章

  1. hdu 5652 India and China Origins 并查集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题目大意:n*m的矩阵上,0为平原,1为山.q个询问,第i个询问给定坐标xi,yi,表示i年后这 ...

  2. hdu 5652 India and China Origins 并查集+逆序

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题意:一张n*m个格子的点,0表示可走,1表示堵塞.每个节点都是四方向走.开始输入初始状态方格, ...

  3. hdu5652 India and China Origins(并查集)

    India and China Origins  Accepts: 49  Submissions: 426  Time Limit: 2000/2000 MS (Java/Others)  Memo ...

  4. HDU 5652 India and China Origins 二分+并查集

    India and China Origins 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5652 Description A long time ...

  5. 并查集(逆序处理):HDU 5652 India and China Origins

    India and China Origins Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  6. HDU 5652 India and China Origins(并查集)

    India and China Origins Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/ ...

  7. HDU 5652 India and China Origins(经典并查集)

    特别经典的一个题,还有一种方法就是二分+bfs 题意:空间内n*m个点,每个点是0或者1,0代表此点可以走,1代表不能走.接着经过q年,每年一个坐标表示此点不能走.问哪年开始图上不能出现最上边不能到达 ...

  8. hdu 5652 India and China Origins(二分+bfs || 并查集)BestCoder Round #77 (div.2)

    题意: 给一个n*m的矩阵作为地图,0为通路,1为阻碍.只能向上下左右四个方向走.每一年会在一个通路上长出一个阻碍,求第几年最上面一行与最下面一行会被隔开. 输入: 首行一个整数t,表示共有t组数据. ...

  9. (hdu)5652 India and China Origins 二分+dfs

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5652 Problem Description A long time ago there ...

随机推荐

  1. 图结构练习——判断给定图是否存在合法拓扑序列(sdutoj)

    #include<stdio.h>#include<string.h>int d[15],map[15][15],vis[15];int main(){    int i,j, ...

  2. C#实现无标题栏窗体点击任务栏图标正常最小化或还原的解决方法

    对于无标题栏窗体,也就是FormBorderStyle等于System.Windows.Forms.FormBorderStyle.None的窗体,点击任务栏图标的时候,是不能象标准窗体那样最小化或还 ...

  3. 1.keras实现-->使用预训练的卷积神经网络(VGG16)

    VGG16内置于Keras,可以通过keras.applications模块中导入. --------------------------------------------------------将 ...

  4. Summary: Binary Search

    Iterative ways: int binarySearch (int[] a, int x) { int low = 0; int high = a.length - 1; int mid; w ...

  5. canvas实现验证码功能

    我们在做一些后台系统登录功能的时候,一般都会用到验证码,最多的就是后台生成的验证码图片返回给前端的.也可以不调用后端接口,前端使用canvas直接生成验证码. 由于功能过于简单,不需要多少代码和文字说 ...

  6. Java应用开发的一条重要经验:先建立基础设施

    一旦为应用建立良好的基础设施, 后续的开发就会变得容易而快速.这些基础设施包括: 1.    线程池的建立与配置: 在 JDK 并发库的基础上建立适合于应用的多任务接口和框架: 2.   外部系统服务 ...

  7. linux下删除大量文件提示参数过长解决办法

    linux下删除大量文件提示参数过长解决办法:在当前目录下rm -rf * 在linux中删除大量文件时,直接用rm会出现:-bash: /bin/rm: 参数列表过长的错误. 这时可以用find命令 ...

  8. Linux基础命令---ziinfo

    zipinfo 在不解压的情况下,获取zip压缩文件的的详细信息.zipinfo列出了ZIP档案中有关文件的技术信息,最常见的是在MS-DOS系统上.这些信息包括文件访问权限.加密状态.压缩类型.版本 ...

  9. java多线程----拒绝策略

    本章介绍线程池的拒绝策略.内容包括:拒绝策略介绍拒绝策略对比和示例 转载请注明出处:http://www.cnblogs.com/skywang12345/p/3512947.html 拒绝策略介绍 ...

  10. JavaScript的 基本数据类型---对象

    第一:Javascript对象是 第二:Javascript中 第三:Javascript的对象是数据: 第四:JavaScript 中的对象可以简单理解成"名称:值"对(name ...