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

    T10

    1N500

    1M500

    1QNM

    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
      这题感觉怎么搞都可以,二分也可以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
    ");
    59         else
    60             printf("%d
    ",ans);
    61     }
    62     return 0;
    63 }
    尽最大的努力,做最好的自己!
  • 相关阅读:
    Nodejs:fs模块 & rimraf模块
    Nodejs:简单的脚手架(一)
    Nodejs:Glob对象
    Nodejs:Path对象
    后台系统组件:一丶bootstrap table
    关于Fekit的安装与测试
    Web存储-Web Storage
    【python】基础入门
    排除正则
    js正则处理千分位
  • 原文地址:https://www.cnblogs.com/TenderRun/p/5326726.html
Copyright © 2020-2023  润新知