并查集(逆序处理):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 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.

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

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:

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

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
  这题感觉怎么搞都可以,二分也可以AC,我用的是官方题解提到的并查集。
  由于并查集不便于分开集合,应当考虑逆序处理。
 #include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
const int maxn=;
int fa[maxn];
bool out[maxn];
int X[maxn],Y[maxn];
int map[][];
char s[];
int find(int x){
return fa[x]==x?x:fa[x]=find(fa[x]);
} int main(){
int T,R,C,m;
scanf("%d",&T);
while(T--){
scanf("%d%d",&R,&C);
for(int i=C+;i<=(R+)*C;i++)fa[i]=i;
for(int i=;i<=C;i++)fa[i]=;
for(int i=C*(R+)+;i<=C*(R+);i++)fa[i]=C*(R+)+; for(int i=;i<=R;i++){
scanf("%s",s+);
for(int j=;j<=C;j++)
map[i][j]=s[j]-'';
} scanf("%d",&m);
for(int i=;i<=m;i++){
scanf("%d%d",&X[i],&Y[i]);X[i]++;Y[i]++;
map[X[i]][Y[i]]=;
} for(int i=;i<=R;i++)
for(int j=;j<C;j++){
if(map[i][j])continue;
if(map[i-][j]==)fa[find((i-)*C+j)]=find(i*C+j);
if(map[i+][j]==)fa[find((i+)*C+j)]=find(i*C+j);
if(map[i][j+]==)fa[find(i*C+j+)]=find(i*C+j);
if(map[i][j-]==)fa[find(i*C+j-)]=find(i*C+j);
} int ans=;
for(int i=m;i>=;i--){
if(find()==find(C*(R+)+)){
ans=i+;
break;
}
map[X[i]][Y[i]]=;
if(map[X[i]-][Y[i]]==)fa[find((X[i]-)*C+Y[i])]=find(X[i]*C+Y[i]);
if(map[X[i]+][Y[i]]==)fa[find((X[i]+)*C+Y[i])]=find(X[i]*C+Y[i]);
if(map[X[i]][Y[i]+]==)fa[find(X[i]*C+Y[i]+)]=find(X[i]*C+Y[i]);
if(map[X[i]][Y[i]-]==)fa[find(X[i]*C+Y[i]-)]=find(X[i]*C+Y[i]);
}
if(ans==m+)
printf("-1\n");
else
printf("%d\n",ans);
}
return ;
}
上一篇:hdu 5652 India and China Origins 并查集+逆序


下一篇:HDU 4496 D-City —— (并查集的应用)