HDU 5652 India and China Origins 并查集
Posted Meek
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 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.
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
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.
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
题解:
这是一个连通性的问题。你会发现如果将所有操作逆序来看的话就很容易用并查集来处理了。 首先把所有的山峰都加到图中,然后逆序处理每个操作: 对某次操作,在图中删除该位置的山峰,然后判断两个点是否联通,一旦联通就得到了结果。 这里需要对China和India分别新建一个对应的节点。
#pragma comment(linker, "/STACK:10240000,10240000") #include<stdio.h> #define N 505 #define M 250005 using namespace std; int f[M],x[M],y[M];char a[N][N]; int m,n,Q,st,ed,i,j,T,now; int get(int u){return f[u]==u?u:f[u]=get(f[u]);} void read(int &x){ char ch=getchar();x=0; for (;ch<‘0‘;ch=getchar()); for (;ch>=‘0‘;ch=getchar()) x=x*10+ch-‘0‘; } inline int ID(int x,int y){return (x-1)*m+y;} inline void Union(int x,int y){f[get(x)]=get(y);} inline void work(int i,int j){ if (i==1) Union(ID(i,j),st);else if (a[i-1][j]==‘0‘) Union(ID(i-1,j),ID(i,j)); if (j>1&&a[i][j-1]==‘0‘) Union(ID(i,j),ID(i,j-1)); if (j<m&&a[i][j+1]==‘0‘) Union(ID(i,j),ID(i,j+1)); if (i==n) Union(ID(i,j),ed);else if (a[i+1][j]==‘0‘) Union(ID(i+1,j),ID(i,j)); } int main(){ read(T); while (T--){ read(n);read(m); for (i=1;i<=n;i++) scanf("%s",a[i]+1); read(Q); for (i=1;i<=Q;i++) read(x[i]),read(y[i]),a[++x[i]][++y[i]]=‘1‘; for (i=1;i<=n*m+2;i++) f[i]=i; st=n*m+1;ed=st+1; for (i=1;i<=n;i++) for (j=1;j<=m;j++) if (a[i][j]==‘0‘) work(i,j); if (get(st)==get(ed)) {puts("-1");continue;} for (now=Q;now;){ a[x[now]][y[now]]=‘0‘; work(x[now],y[now]); if (get(st)==get(ed)) break; now--; }printf("%d\n",now); } }
以上是关于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 并查集+逆序