HDOJ 5652 India and China Origins(并查集)

Posted 小胡子Haso

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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.

T10

1N500

1M500

1QN?M

0X<N

0Y<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
 

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;
}



























































以上是关于HDOJ 5652 India and China Origins(并查集)的主要内容,如果未能解决你的问题,请参考以下文章

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

HDU 5652 India and China Origins 并查集

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

hdu 5652 India and China Origins 并查集

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

hdu5652:India and China Origins(并查集)