RQNOJ T480 相连的农场

    【分析】:这道题可以用Tarjan求强连通分量来做。首先建个链表:如果对应输入临接矩阵中为1(即i到j有路相连),那么就将i点到j点这一条路用链表形式记录下来,这样能方便的从某一点一直追溯到最远能到达的点。程序中建链表的方式如下:

void add(int x,int y)
{
      a[++tot].to=y;//以x为出发点,y为到达点的边,在链表中的编号为tot 
      a[tot].next=last[x];//这条边的上一条边(即以x为到达点的边)的编号存在last[x]里 
       last[x]=tot;//记录最新的last[x]值,方便下一次的连边 
}

      边建好后,就进行塔尖算法(Tarjan)

void tarjan(int now)
{
      DFN[now]=LOW[now]=++step;//为节点now设定次序编号和Low初值
      stack[++tot1]=now;//将节点now压入栈中(这里用的是数组模拟栈)
      instack[now]=true;//表示该点存在于栈中 
      for(int i=last[now];i;i=a[i].next)//顺着链表顺序,枚举每一条边
      {
            int now_to=a[i].to;
            if(!DFN[now_to])//如果节点now_to未被访问过
            {
                  tarjan(now_to);//继续向下找
                  LOW[now]=min(LOW[now],LOW[now_to]);
            }
            else if(instack[now_to])//如果节点now_to还在栈内
            {
                  LOW[now]=min(LOW[now],DFN[now_to]);
            }
      }
      if(DFN[now]==LOW[now])//如果节点now是强连通分量的根
      {
            ans++;
            int now_out;
            do
            {
                  now_out=stack[tot1--];//将now_out退栈(now_out为该强连通分量中一个顶点)
                  instack[now_out]=false;
                  belong[now_out]=ans;//记录该点属于哪个强连通分量 
            }while(now!=now_out);
      }
}


      最后按belong数组输出即可。

    【代码】:

  

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<vector>
#include<queue>
using namespace std;
#define MAX 100001
#define MAXN 100001
struct point{int to,next;};
point a[MAX];
int N,last[MAX],tot=0,step=0,belong[MAX],stack[MAX],ans=0,tot1=0;
int DFN[MAX],LOW[MAX];//DFN[u]:节点u搜索的次序编号,LOW[u]:u或u的子树能够追溯到的最早的栈中节点的次序号<DFN>
bool instack[MAX],putout[MAX];
void add(int x,int y)
{
      a[++tot].to=y;
      a[tot].next=last[x];
      last[x]=tot;
}
void tarjan(int now)
{
      DFN[now]=LOW[now]=++step;
      stack[++tot1]=now;
      instack[now]=true;
      for(int i=last[now];i;i=a[i].next)
      {
            int now_to=a[i].to;
            if(!DFN[now_to])
            {
                  tarjan(now_to);
                  LOW[now]=min(LOW[now],LOW[now_to]);
            }
            else if(instack[now_to])
            {
                  LOW[now]=min(LOW[now],DFN[now_to]);
            }
      }
      if(DFN[now]==LOW[now])
      {
            ans++;
            int now_out;
            do
            {
                  now_out=stack[tot1--];
                  instack[now_out]=false;
                  belong[now_out]=ans;
            }while(now!=now_out);
      }
}
int main()
{
	  memset(instack,false,sizeof(instack));
	  memset(putout,false,sizeof(putout));
	  scanf("%d",&N);
	  for(int i=1;i<=N;i++)
	        for(int j=1;j<=N;j++)
	        {
                  int C;
                  scanf("%d",&C);
                  if(C==1)   add(i,j);   
            }
      
      for(int i=1;i<=N;i++)
            if(!DFN[i])
                  tarjan(i);
      
      printf("%d\n",ans);
      for(int i=1;i<=N;i++)
      {
            if(putout[belong[i]])   continue;
            putout[belong[i]]=true;
            for(int j=i;j<=N;j++)
            {
                  if(belong[j]==belong[i])
                        printf("%d ",j);
            }
            printf("\n");
      }
	  //system("pause");
      return 0;
}


 转载注明出处:http://blog.csdn.net/u011400953

 链接:http://blog.csdn.net/u011400953/article/details/9942149    (Tarjan求强连通分量的讲解与实现

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值