缩点找环
#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;
}