RQNOJ T480 相连的农场

<span style="font-size:14px;">#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;
}


—————————————————————————————————

本文原创自Sliencecsdn技术博客。

本博客所有原创文章请以链接形式注明出处。

欢迎关注本技术博客,本博客的文章会不定期更新。


大多数人想要改造这个世界,但却罕有人想改造自己。

世上没有绝望的处境,只有对处境绝望的人。

                                              ————By slience

—————————————————————————————————


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值