HDU 5652 India and China Origins(并查集)

India and China Origins

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

Total Submission(s): 441    Accepted Submission(s): 133

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  which represents the number of test cases. For each test case, the first line contains two space seperated integers . next  lines consists of strings composed of  characters.  denoting that there's already a mountain at that place,  denoting the plateau. on  line there will be an integer  denoting the number of mountains that rised up in the order of times. Next  lines contain  space seperated integers  denoting that at ith year a mountain rised up at location .

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

BestCoder Round #77 (div.2)

用二分加验证可以过,用并查集也可以过。

这个是并查集,

</pre><p style="height:auto; margin:0px; padding:0px 20px; font-size:14px; font-family:'Times New Roman'"><pre name="code" class="html">#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>

using namespace std;
#define MAX 250000
int father[MAX+5];
int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
int find(int x)
{
	if(father[x]!=x)
		father[x]=find(father[x]);
	return father[x];
}
int b[MAX][2];
char a[505][505];
int c[505][505];
int q;
int n,m;

int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		for(int i=1;i<=n;i++)
			scanf("%s",a[i]+1);
		scanf("%d",&q);
		for(int i=1;i<=q;i++)
		{
			scanf("%d%d",&b[i][0],&b[i][1]);
			b[i][0]++;b[i][1]++;
			a[b[i][0]][b[i][1]]='1';
		}
		for(int i=0;i<=n*m+1;i++)
			father[i]=i;
		for(int i=1;i<=m;i++)
		{
			if(a[1][i]=='0') father[i]=0;
			if(a[n][i]=='0') father[(n-1)*m+i]=n*m+1;
		}
		for(int i=2;i<=n;i++)
		{
			for(int j=1;j<=m;j++)
			{
				if(a[i][j]=='1')
					continue;
				if(a[i][j-1]=='0'&&j!=1)
				{
					int fx=find((i-1)*m+j);
					int fy=find((i-1)*m+j-1);
					if(fx!=fy)
						father[fx]=fy;
				}
				if(a[i-1][j]=='0')
				{
					int fx=find((i-1)*m+j);
					int fy=find((i-2)*m+j);
					if(fx!=fy)
						father[fx]=fy;
				}
			}
		}
		
		if(find(0)==find(n*m-1))
		{
			printf("-1\n");
			continue;
		}
		int i;
		for( i=q;i>=1;i--)
		{
			for(int j=0;j<4;j++)
			{
				int x=b[i][0],y=b[i][1];
				if(x==1)
					father[(x-1)*m+y]=0;
				if(x==n)
					father[(x-1)*m+y]=n*m+1;
				int xx=x+dir[j][0];int yy=y+dir[j][1];
				if(xx<1||xx>n||yy<1||yy>m||a[xx][yy]=='1')
					continue;
				int fx=find((x-1)*m+y);
				int fy=find((xx-1)*m+yy);
				if(fx!=fy)
					father[fx]=fy;
			}
			a[b[i][0]][b[i][1]]='0';
			if(find(0)==find(n*m+1))
				break;
		}
		printf("%d\n",i);
	}
	return 0;
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏算法修养

HDU 5157 Harry and magic string(回文树)

Harry and magic string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: ...

37580
来自专栏算法修养

HDU 5877 2016大连网络赛 Weak Pair(树状数组,线段树,动态开点,启发式合并,可持久化线段树)

Weak Pair Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144...

413100
来自专栏小樱的经验随笔

HDU 1002 A + B Problem II(高精度加法(C++/Java))

A + B Problem II Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/3...

37870
来自专栏ml

HDUOJ----(1030)Delta-wave

Delta-wave Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K...

35670
来自专栏小樱的经验随笔

POJ 1012 Joseph

Joseph Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 53862 ...

33460
来自专栏ml

HDU----(3294)Girls' research(manacher)

Girls' research Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/3...

29050
来自专栏算法修养

HDU 5618 Jam's problem again(三维偏序,CDQ分治,树状数组,线段树)

Jam's problem again Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 655...

535160
来自专栏ml

poj-------Common Subsequence(poj 1458)

Common Subsequence Time Limit: 1000MS Memory Limit: 10000K Total Submiss...

30750
来自专栏算法修养

HDU 2476 String painter(区间DP)

String painter Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 32768/327...

35980
来自专栏ml

HDUOJ----剪花布条

剪花布条 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java...

367120

扫码关注云+社区

领取腾讯云代金券