【HDOJ 5652】 India and China Origins(并查集)

【HDOJ 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): 805    Accepted Submission(s): 272

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

BestCoder Round #77 (div.2)

Recommend

wange2014   |   We have carefully selected several similar problems for you:  5650 5649 5648 5646 5645

题目大意:许多多年前,中国和印第安是相连接的……恕我地理不好……。。

之间有一些山 其余为平原

地图以1 0表示

1为山脉 0为平原

中国在最上方(横坐标为0

中国在最下方(横坐标为n-1

每个点可以向上下左右四个方向走

有q年地壳运动,每年会增加一座山(以坐标表示<x,y>)

问中国与印第安最早断开连接的年代

思路是离线处理。

存下每年地壳活动的坐标。

从第一年到最后一年把所有出现的山脉在地图上标记出。然后把能相到达的平原合并

然后从最后一年到第一年开始"拆山" 然后找到第一次两国可以相同的时间

这个时间+1就是第一次断开的年代

那么最后一个问题就是求两国此时此刻是否连通。

我的做法是枚举行为n-1(印第安)的所有平原 跑并查集看是否缩到某个行为0(中国)的点上

只要在缩点的时候缩到下标小的点上就行。

注意进行压缩的时候每次压缩玩要重新Find.否则原本的根一定是新根 会出错(在这里卡了老久……

代码如下:

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout)

using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 1e9+7;
const double eps = 1e-8;

Pr pr[266666];
int pre[266666];
int dirx[] = { 0, 0,-1, 1};
int diry[] = { 1,-1, 0, 0};
char mp[555][555];
int tp;

void init(int n)
{
	for(int i = 0; i <= n; ++i) pre[i] = i;
}

int Find(int x)
{
	return pre[x] == x? pre[x]: (pre[x] = Find(pre[x]));
}

int n,m;
bool cal()
{
	for(int i = 0; i < m; ++i)
	{
		//printf("%d:%d %d:%d\n",i,Find(i),j,Find((n-1)*m+j));
		if(Find((n-1)*m+i) < m) return 1;
	}
	return 0;
}

int main()
{
	//fread();
	//fwrite();

	int t;
	int q,k,r;

	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		for(int i = 0; i < n; ++i)
			scanf("%s",mp[i]);

		init((n-1)*m+m-1);
		scanf("%d",&q);
		for(int i = 0; i < q; ++i)
		{
			scanf("%d%d",&pr[i].first,&pr[i].second);
			mp[pr[i].first][pr[i].second]++;
		}
	//	for(int i = 0; i < n; ++i)
	//		for(int j = 0; j < m; ++j)
	//		{
	//			printf("pre[%d](%d,%d):%d\n",i*m+j,i,j,pre[i*m+j]);
	//		}

		for(int i = 0; i < n; ++i)
			for(int j = 0; j < m; ++j)
			{
				if(mp[i][j] != '0') continue;
				k = Find(i*m+j);
				if(j+1 < m && mp[i][j+1] == '0')
				{
					r = Find(i*m+j+1);
					if(k < r) pre[r] = k;
					else pre[k] = r;
				}

				k = Find(i*m+j);
				if(i+1 < n && mp[i+1][j] == '0')
				{
					r = Find((i+1)*m+j);
					if(k < r) pre[r] = k;
					else pre[k] = r;
				}
			}

		int id = -2;
	//	for(int i = 0; i < n; ++i)
	//		puts(mp[i]);
	//	for(int i = 0; i < n; ++i)
	//		for(int j = 0; j < m; ++j)
	//		{
	//			printf("pre[%d](%d,%d):%d\n",i*m+j,i,j,pre[i*m+j]);
	//		}
		if(cal()) id = -1;
		int x,y,xx,yy;
		for(int i = q-1; i >= 0 && id == -2; --i)
		{
			x = pr[i].first;
			y = pr[i].second;
			mp[x][y]--;
			if(mp[x][y] == '0')
			{
				k = Find(x*m+y);

				for(int j = 0; j < 4; ++j)
				{
					xx = x+dirx[j];
					yy = y+diry[j];
					if(0 <= xx && xx < n && 0 <= yy && yy < m && mp[xx][yy] == '0')
					{
						r = Find(xx*m+yy);
						if(k < r) pre[r] = k;
						else pre[k] = r;
						k = Find(x*m+y);
					}
				}

				if(cal()) id = i+1;
			}
		}
		printf("%d\n",id == -2? 0: id);
	}

	return 0;
}

时间: 2024-12-29 05:27:39

【HDOJ 5652】 India and China Origins(并查集)的相关文章

HDU 5652 India and China Origins 并查集

India and China Origins 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 communa

hdu 5652 India and China Origins 并查集+二分

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

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题意:一张n*m个格子的点,0表示可走,1表示堵塞.每个节点都是四方向走.开始输入初始状态方格,之后输入Q个操作,表示第(x,y)个格子由0变为1:问你在第几次时不能由最下的一行到最上面的一行.中国在最上面一行的上面,印度在最下面一行的下面:如果最终还是连通的,输出-1: 思路:直接离线逆序处理,用并查集维护: #include<bits/stdc++.h> using namespace

并查集(逆序处理):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): 676    Accepted Submission(s): 227 Problem Description A long time ago there are no himalayas between India and China, the

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

特别经典的一个题,还有一种方法就是二分+bfs 题意:空间内n*m个点,每个点是0或者1,0代表此点可以走,1代表不能走.接着经过q年,每年一个坐标表示此点不能走.问哪年开始图上不能出现最上边不能到达最下边的情况了 图上连通性可以使用并查集判断,但是并查集不善于删边,却善于添边.所以我们倒着来想就是离线倒序添边(横向并查,再纵向并查),当某次判断时图已经连通,就结束. 我使用二维并查集,其实就是使用结构体代替一维数组.接着就是每次一定要从x轴小的点到达x轴大的点,最后注意添边时,我们需要此点向四

HDU 5652 India and China Origins 二分优化+BFS剪枝

题目大意:给你一个地图0代表可以通过1代表不可以通过.只要能从第一行走到最后一行,那么中国与印度是可以联通的.现在给你q个点,每年风沙会按顺序侵蚀这个点,使改点不可通过.问几年后中国与印度不连通.若一直联通输出-1. 题目思路:看数据这道题就是卡时间的,我们的基本思路是每当风沙侵蚀一个点,我们就进行一次广搜,看看图上下是否联通,我们应尽可能的去优化这个过程. 所以我们可以在遍历年的时候用二分查找: 若当年图可以上下联通,则继续向右查找 若当年图上下无法联通,则向左查找 剪枝: 为了省时间我们应该

HDU 5652 India and China Origins(二分 + DFS)

题意: 中国和印度之间有一片地方,把这片地方抽象化,于是就可以看成一个N * M矩阵,其中黑色的代表高山不能走过去,白色的代表平原,可以通行,人每次可以选择往上下左右四个方向移动,但是随着时间的变化某些白色的平原会变成黑色的高山,从而变为不可通行,题目中给出一个代表地势的图,然后有 Q 次操作,第 i 次操作 代表在第 i 年 (x, y)处的平原变成了高山,即白色变为了黑色.问中国印度最早彻底断绝的时间,如果在 Q 年后还没有断绝就输出 -1: 思路: 对于0 - Q年份进行二分,然后利用DF

hdu5652:India and China Origins(并查集)

倒序操作用并查集判断是否连通,新技能get√(其实以前就会了 这题细节很多...搞得整个程序都是调试输出,几度看不下去想要重写 并查集到现在大概掌握了两个基本用途:判断是否连通 / 路径压缩(上一篇blog) #include<iostream> #include<cstdlib> #include<cstring> #include<cstdio> #define ll long long using namespace std; const int dx

hdoj 1116 Play on Words 【并查集】+【欧拉路】

Play on Words Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5622    Accepted Submission(s): 1850 Problem Description Some of the secret doors contain a very interesting word puzzle. The team