game

缩点找环 

#include <iostream>
#include <cstdio>
using namespace std;
int read()
{
	int x=0,f=1;
	char c=getchar();
	while(c<'0'||c>'9')
	{
		if(c=='-')  f=-1;
		c=getchar();
	}
	while(c>='0'&&c<='9')
	{
		x=x*10+c-'0';
		c=getchar();
	}
	return f*x;
}
int readm()
{
	int x;
	char c=getchar();
	while(c!='0'&&c!='1') c=getchar();
	if(c=='0'||c=='1')  x=c-'0';
	return x;
}
const int N=5005;
int n,had[N],to[N*N],nxt[N*N],p,edge[N][N],a,b,c;
int low[N],dfn[N],num,st[N],top,co[N],sum[N],col;
bool f;
void add(int x,int y)
{
	nxt[++p]=had[x];
	to[p]=y;
	had[x]=p;
}
void tarjan(int u)
{
	dfn[u]=low[u]=++num;
	st[++top]=u;
	for(int i=had[u];i;i=nxt[i])
	{
		int v=to[i];
		if(!dfn[v])
		{
			tarjan(v);
			low[u]=min(low[u],low[v]);
		}
		else  if(!co[v])  low[u]=min(low[u],low[v]);
	}
	if(low[u]==dfn[u])
	{
		co[u]=++col;
		sum[col]=1;
		while(st[top]!=u)
		{
			co[st[top]]=col;
			sum[col]++;
			top--;
		}
		top--;
	}
}
int main()
{
	freopen("game.in","r",stdin);
	freopen("game.out","w",stdout);
	n=read();
	for(int i=1;i<=n;i++)
	  for(int j=1;j<=n;j++)
	  {
		edge[i][j]=readm();
		if(edge[i][j])  add(i,j);
	  }
	for(int i=1;i<=n;i++)
	  if(!co[i])  tarjan(i);
	for(int i=1;i<=n;i++)
	  if(sum[co[i]]>=3)
	  {
	  	a=i;
	  	break;
	  }
	for(int i=1;i<=n;i++)
	  if(edge[a][i])
	  {
	  	for(int j=1;j<=n;j++)
	  	  if(edge[i][j]&&edge[j][a])
	  	  {
	  	    f=1;
	  	    b=i;  c=j;
	  	    break;
		  }
		if(f)   break;
	  }
	if(a)  cout<<a<<" "<<b<<" "<<c;
	else  cout<<-1;
	fclose(stdin);  fclose(stdout);
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值