强连通分量 POJ 2375 Cow Ski Area

Description

Farmer John's cousin, Farmer Ron, who lives in the mountains of Colorado, has recently taught his cows to ski. Unfortunately, his cows are somewhat timid and are afraid to ski among crowds of people at the local resorts, so FR has decided to construct his own private ski area behind his farm. 

FR's ski area is a rectangle of width W and length L of 'land squares' (1 <= W <= 500; 1 <= L <= 500). Each land square is an integral height H above sea level (0 <= H <= 9,999). Cows can ski horizontally and vertically between any two adjacent land squares, but never diagonally. Cows can ski from a higher square to a lower square but not the other way and they can ski either direction between two adjacent squares of the same height. 

FR wants to build his ski area so that his cows can travel between any two squares by a combination of skiing (as described above) and ski lifts. A ski lift can be built between any two squares of the ski area, regardless of height. Ski lifts are bidirectional. Ski lifts can cross over each other since they can be built at varying heights above the ground, and multiple ski lifts can begin or end at the same square. Since ski lifts are expensive to build, FR wants to minimize the number of ski lifts he has to build to allow his cows to travel between all squares of his ski area. 

Find the minimum number of ski lifts required to ensure the cows can travel from any square to any other square via a combination of skiing and lifts.

Input

* Line 1: Two space-separated integers: W and L 

* Lines 2..L+1: L lines, each with W space-separated integers corresponding to the height of each square of land.

Output

* Line 1: A single integer equal to the minimal number of ski lifts FR needs to build to ensure that his cows can travel from any square to any other square via a combination of skiing and ski lifts

Sample Input

9 3

1 1 1 2 2 2 1 1 1

1 2 1 2 3 2 1 2 1

1 1 1 2 2 2 1 1 1

Sample Output

3

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed. 

OUTPUT DETAILS: 

FR builds the three lifts. Using (1, 1) as the lower-left corner, 
the lifts are (3, 1) <-> (8, 2), (7, 3) <-> (5, 2), and (1, 3) <-> 
(2, 2). All locations are now connected. For example, a cow wishing 
to travel from (9, 1) to (2, 2) would ski (9, 1) -> (8, 1) -> (7, 
1) -> (7, 2) -> (7, 3), take the lift from (7, 3) -> (5, 2), ski 
(5, 2) -> (4, 2) -> (3, 2) -> (3, 3) -> (2, 3) -> (1, 3), and then 
take the lift from (1, 3) - > (2, 2). There is no solution using 
fewer than three lifts.
 
分析: 首先建图,就是一个点如果可以访问周围点(高度不小与周围的点),就连边。

建图后求强连通分量:

如果都在同一分量中,答案是0,而不是1,这个要注意。

如果不在同一分量中,答案是出度是0的个数和入度是0的个数最大的。

 

AC代码:

View Code
  1 #include<stdio.h>

  2 #include<string.h>

  3 typedef struct

  4 {

  5     int v,next;

  6 }Node;

  7 Node e[250010*8];

  8 int move[4][2]={{-1,0},{0,-1},{1,0},{0,1}};

  9 int first[250010],map[501][501],belong[250010],in[250010],out[250010];

 10 int low[250010],dfn[250010],stack[250010],instack[250010];

 11 int l,w,index,g,n,top,count,sum1,sum2,sum;

 12 void add(int u,int v)

 13 {

 14     e[g].v=v;

 15     e[g].next=first[u];

 16     first[u]=g++;

 17 }

 18 void Tarjan(int i)

 19 {

 20     int j,k;

 21     dfn[i]=low[i]=index++;

 22     stack[++top]=i;

 23     instack[i]=1;

 24     for(j=first[i];j!=-1;j=e[j].next)

 25     {

 26         k=e[j].v;

 27         if(!dfn[k])

 28         {

 29             Tarjan(k);

 30             if(low[k]<low[i])

 31                 low[i]=low[k];

 32         }

 33         else if(instack[k]&&dfn[k]<low[i])

 34             low[i]=dfn[k];

 35     }

 36     if(low[i]==dfn[i])

 37     {

 38         count++;

 39         do{

 40             k=stack[top--];

 41             instack[k]=0;

 42             belong[k]=count;

 43         }while(k!=i);

 44     }

 45 }

 46 int main()

 47 {

 48     int i,j,k,x,y,u,v;

 49     scanf("%d%d",&w,&l);

 50     for(i=1;i<=l;i++)

 51         for(j=1;j<=w;j++)

 52             scanf("%d",&map[i][j]);

 53     memset(first,-1,sizeof(first));

 54     memset(dfn,0,sizeof(dfn));

 55     memset(instack,0,sizeof(instack));

 56     memset(low,0,sizeof(low));

 57     memset(in,0,sizeof(in));

 58     memset(out,0,sizeof(out));

 59     g=0; index=1; top=-1; count=sum1=sum2=0;

 60     for(i=1;i<=l;i++)

 61         for(j=1;j<=w;j++)

 62         {

 63             u=(i-1)*w+j;

 64             for(k=0;k<4;k++)

 65             {

 66                 x=i+move[k][0];

 67                 y=j+move[k][1];

 68                 v=(x-1)*w+y;

 69                 if(x<1||x>l||y<1||y>w)

 70                     continue;

 71                 if(map[i][j]>=map[x][y])

 72                     add(u,v);

 73                 if(map[i][j]<=map[x][y])

 74                     add(v,u);

 75             }

 76         }

 77     n=l*w;

 78     for(i=1;i<=n;i++)

 79         if(!dfn[i])

 80             Tarjan(i);

 81     for(i=1;i<=n;i++)

 82         for(j=first[i];j!=-1;j=e[j].next)

 83         {

 84             k=e[j].v;

 85             if(belong[i]!=belong[k])

 86             {

 87                 out[belong[i]]++;

 88                 in[belong[k]]++;

 89             }

 90         }

 91     for(i=1;i<=count;i++)

 92     {

 93         if(!in[i])

 94             sum1++;

 95         if(!out[i])

 96             sum2++;

 97     }

 98     sum=sum1>sum2?sum1:sum2;

 99     if(count==1)

100         sum=0;

101     printf("%d\n",sum);

102     return 0;

103 }

 

你可能感兴趣的:(poj)