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

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

  这题感觉怎么搞都可以,二分也可以AC,我用的是官方题解提到的并查集。

  由于并查集不便于分开集合,应当考虑逆序处理。

 1 #include <iostream>
 2 #include <cstring>
 3 #include <cstdio>
 4 using namespace std;
 5 const int maxn=250000;
 6 int fa[maxn];
 7 bool out[maxn];
 8 int X[maxn],Y[maxn];
 9 int map[510][510];
10 char s[510];
11 int find(int x){
12     return fa[x]==x?x:fa[x]=find(fa[x]);
13 }
14
15 int main(){
16     int T,R,C,m;
17     scanf("%d",&T);
18     while(T--){
19         scanf("%d%d",&R,&C);
20         for(int i=C+1;i<=(R+1)*C;i++)fa[i]=i;
21         for(int i=1;i<=C;i++)fa[i]=1;
22         for(int i=C*(R+1)+1;i<=C*(R+2);i++)fa[i]=C*(R+1)+1;
23
24         for(int i=1;i<=R;i++){
25             scanf("%s",s+1);
26             for(int j=1;j<=C;j++)
27                 map[i][j]=s[j]-‘0‘;
28         }
29
30         scanf("%d",&m);
31         for(int i=1;i<=m;i++){
32             scanf("%d%d",&X[i],&Y[i]);X[i]++;Y[i]++;
33             map[X[i]][Y[i]]=1;
34         }
35
36         for(int i=1;i<=R;i++)
37             for(int j=2;j<C;j++){
38                 if(map[i][j])continue;
39                 if(map[i-1][j]==0)fa[find((i-1)*C+j)]=find(i*C+j);
40                 if(map[i+1][j]==0)fa[find((i+1)*C+j)]=find(i*C+j);
41                 if(map[i][j+1]==0)fa[find(i*C+j+1)]=find(i*C+j);
42                 if(map[i][j-1]==0)fa[find(i*C+j-1)]=find(i*C+j);
43             }
44
45         int ans=0;
46         for(int i=m;i>=1;i--){
47             if(find(1)==find(C*(R+1)+1)){
48                 ans=i+1;
49                 break;
50             }
51             map[X[i]][Y[i]]=0;
52             if(map[X[i]-1][Y[i]]==0)fa[find((X[i]-1)*C+Y[i])]=find(X[i]*C+Y[i]);
53             if(map[X[i]+1][Y[i]]==0)fa[find((X[i]+1)*C+Y[i])]=find(X[i]*C+Y[i]);
54             if(map[X[i]][Y[i]+1]==0)fa[find(X[i]*C+Y[i]+1)]=find(X[i]*C+Y[i]);
55             if(map[X[i]][Y[i]-1]==0)fa[find(X[i]*C+Y[i]-1)]=find(X[i]*C+Y[i]);
56         }
57         if(ans==m+1)
58             printf("-1\n");
59         else
60             printf("%d\n",ans);
61     }
62     return 0;
63 }
时间: 2024-12-22 18:01:54

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

特别经典的一个题,还有一种方法就是二分+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

【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

(并查集)POJ 1308 &amp; HDU 1325

一开始以为两道题是一样的,POJ的过了直接用相同代码把HDU的交了,结果就悲剧了.最后发现HDU的没有考虑入度不能大于一. 题意:用树的定义来 判断吧,无环,n个结点最多有n-1条边,不然就会有环.只有一个入度为0的结点,不存在入度大于1的结点. 思路:并查集. AC代码: #include<stdio.h> #include<string.h> #define N 100005 int in[N],pre[N],a,b,c[N]; void init()//初始化 { for(i

hdu1811 并查集+拓扑序

题意:现在有一个排名系统,有一系列信息,分别是 > < = 的比较,而如果最终相等,就会将这些相等的按照序号从小到大排,问给出的信息是否可以确定完整的排序. 由于如果很多点相等,他们肯定能够确定名次,那么我们只要让他们共同拥有与其他点的大小关系就行了.所以就用到了并查集,将相等的点加入并查集,再对并查集排拓扑序,如果能够排出唯一的拓扑序,那么在每个并查集内部也能够有唯一的顺序. 1 #include<stdio.h> 2 #include<string.h> 3 #in