借鉴自:http://www.cnblogs.com/shuguangzw/p/5324207.html
India and China Origins
Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1128 Accepted Submission(s): 362
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
题意:
中国和印度直接隔着许多山,问能不能找到一条没有山阻挡的路联通彼此。
每过一年就会多一座山阻挡,题目问哪一年彻底封死路,其实就是倒推回来问哪一年路联通。
分析:
先把每年产生的山都放进地图中,从最后一年开始往前逐年把山去除,然后判断是否联通。
判断主要靠并查集,做题的时候想的太多,用了十分愚蠢的并查集配dfs,耿直的TLE了。
借鉴了大神代码,感觉很有道理,用一个r数组判断了并查集使用时合并的方向,就可以很便利的解决问题。
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
#define INF 0x3f3f3f3f
const int N=505;
const int mod=1e9+7;
struct A{
int x,y;
}q[N*N];
int t,n,m;
int dx[4]={0,0,-1,1},dy[4]={-1,1,0,0};
int s[N][N],x,y,ques,f[N*N],r[N*N];
int fa(int u){
return u==f[u]? u:f[u]=fa(f[u]);
}
void fun(int x,int y){
x=fa(x),y=fa(y);
if (x!=y) {
if (r[x]>r[y]) {
f[y]=x;
} else if (r[x]<r[y]){
f[x]=y;
} else {
f[y]=x;
r[x]++;
}
}
}
int main() {
cin>>t;
string temp;
while (t--) {
cin>>n>>m;
memset(s, 0, sizeof(s));
memset(r, 0, sizeof(r));
for (int i=1; i<=n; i++) {
cin>>temp;
for (int j=1; j<=m; j++) {
if (temp[j-1]=='1') {
s[i][j]=1;
}
}
}
cin>>ques;
for (int i=1; i<=ques; i++) {
scanf("%d%d",&q[i].x,&q[i].y);
q[i].x++;
q[i].y++;
s[q[i].x][q[i].y]=1;
}
for (int i=1; i<=n*m+2; i++) {
f[i]=i;
}
int k1=n*m+1,k2=n*m+2;
for (int i=1; i<=m; i++) {
if (s[1][i]==0) fun(k1, i);
if (s[n][i]==0) fun(k2, (n-1)*m+i);
}
for (int i=1; i<=n; i++) {
for (int j=1; j<=m; j++) {
if (s[i][j]==1) continue;
if (i<n&&s[i+1][j]==0) fun((i-1)*m+j, i*m+j);
if (j<m&&s[i][j+1]==0) fun((i-1)*m+j, (i-1)*m+j+1);
}
}
if (fa(k1)==fa(k2)) {
cout<<"-1\n";
continue;
}
int ans=0;
for (int i=ques; i>0; i--) {
int x=q[i].x,y=q[i].y;
s[x][y]=0;
if (x==1) {
fun(k1, y);
}
if (x==n) {
fun(k2, (n-1)*m+y);
}
for (int j=0; j<4; j++) {
int xx=x+dx[j];
int yy=y+dy[j];
if (xx<1||xx>n||yy<1||yy>m) {
continue;
}
if (s[xx][yy]==0) {
fun((x-1)*m+y, (xx-1)*m+yy);
}
}
if (fa(k1)==fa(k2)) {
ans=i;
break;
}
}
cout<<ans<<endl;
}
return 0;
}