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. [py][mx]django课程页显示city和机构封面图

    city和课程机构信息展示到前台去 organization/views.py from django.views.generic.base import View from organization ...

  2. Python随机数生成方法

    假设你对在Python生成随机数与random模块中最经常使用的几个函数的关系与不懂之处.以下的文章就是对Python生成随机数与random模块中最经常使用的几个函数的关系,希望你会有所收获,以下就 ...

  3. R中双表操作学习[转载]

    转自:https://www.jianshu.com/p/a7af4f6e50c3 1.原始数据 以上是原有的一个,再生成一个新的: > gene_exp_tidy2 <- data.fr ...

  4. C语言常用函数大全

    一.数学函数 调用数学函数时,要求在源文件中包下以下命令行: #include <math.h> 函数原型说明 功能 返回值 说明 int abs( int x) 求整数x的绝对值 计算结 ...

  5. #C语言初学记录(位运算)

    位运算 Problem Description7-1 数组元素循环右移问题 一个数组A中存有N(>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(≥0)个位置,即将A中的数据由 ...

  6. vue性能优化2--引入cdn

    当我们加载页面时,需要将我们所需要的一些依赖加载到当前会话中然后再开始执行,如果我们首屏,模块比较多是,需要等待的时间会比较长,而且.浏览器内存最多执行四十个进程,需要等到加载完前面的才能执行后面的代 ...

  7. discuz财付通也阵亡了

    今日做交易部分,然后焦头烂额. 首先这积分,威望,金钱,什么鬼,乱七八糟的...... 然后这支付宝,啊,,,,,竟然停止个人接口了,不得已要使用财付通. %&……*&……&不 ...

  8. redis和memcached相关

    应该选择哪一种缓存机制 redis相较于memcached更加年轻,功能更加强大. 对小型静态数据进行缓存处理,最具代表性的例子就是HTML代码片段.使用memcached所消耗内存更少. 其他情况下 ...

  9. Codeforces 1144G Two Merged Sequences

    题意: 将一个序列分成两个序列,两个序列中元素的相对顺序保持和原序列不变,使得分出的两个序列一个严格上升,一个严格下降. 思路: 我们考虑每个元素都要进入其中一个序列. 那么我们维护一个上升序列和一个 ...

  10. jQuery 是javascript的一个库(常用插件、处理器)

    jQuery校验官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery就是javascript的一个库,把我 ...