zoukankan      html  css  js  c++  java
  • 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.
     
    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
     

    题解:

      这是一个连通性的问题。你会发现如果将所有操作逆序来看的话就很容易用并查集来处理了。 首先把所有的山峰都加到图中,然后逆序处理每个操作: 对某次操作,在图中删除该位置的山峰,然后判断两个点是否联通,一旦联通就得到了结果。 这里需要对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
    ",now);
      }
    }
  • 相关阅读:
    WAMP环境下访问PHP提示下载PHP文件
    安装CMS遇到php5.3的问题
    Win2003打不开https的问题
    查看服务器硬件信息
    添加删除程序无法安装IIS 提示没法加载模块
    Spring Autowiring by AutoDetect
    Spring Autowiring by Constructor
    Spring Autowiring by Name
    Spring Autowiring by Type
    Spring Auto-Wiring Beans
  • 原文地址:https://www.cnblogs.com/zxhl/p/5330986.html
Copyright © 2011-2022 走看看