A tournament is a directed graph without self-loops in which every pair of vertexes is connected by exactly one directed edge. That is, for any two vertexes u and v (u ≠ v) exists either an edge going from u to v, or an edge from v to u.

You are given a tournament consisting of n vertexes. Your task is to find there a cycle of length three.

Input

The first line contains an integer n (1 ≤ n ≤ 5000). Next n lines contain the adjacency matrix A of the graph (without spaces). Ai, j = 1 if the graph has an edge going from vertex i to vertex j, otherwise Ai, j = 0. Ai, j stands for the j-th character in the i-th line.

It is guaranteed that the given graph is a tournament, that is, Ai, i = 0, Ai, j ≠ Aj, i (1 ≤ i, j ≤ n, i ≠ j).

Output

Print three distinct vertexes of the graph a1, a2, a3 (1 ≤ ai ≤ n), such that Aa1, a2 = Aa2, a3 = Aa3, a1 = 1, or "-1", if a cycle whose length equals three does not exist.

If there are several solutions, print any of them.

Examples

Input
5
00100
10000
01001
11101
11000
Output
1 3 2 
Input
5
01111
00000
01000
01100
01110
Output
-1

cycle
思路:
就是用搜索,以一排展开,dfs含有两个参数,这两个参数代表这个位置为1,所有,再去搜索,按照没有搜索的一排展开,
找到这个值。
AC之后,之前这个题目思路有一些混乱,再整理一下
在一个dfs要考虑到三个数,dfs里面的两个参数就是其中两个数,然后找到这两个参数中任意一个对应得第三个数,
再判断第三个数和另一个参数有没有相互对应,有就返回了,没有继续判断这一行有没有被标记,标记了就不用,没有标记就
继续进行搜索。值得确定得是,第一确实每一个都搜到了,没有遗漏,第二,就是搜索得递归,可以再仔细想想

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
const int maxn=5050;
char ana[maxn][maxn];
bool bna[maxn][maxn],vis[maxn];
int n,a,b,c; int dfs(int x,int y)
{
vis[x]=1;
for(int i=1;i<=n;i++)
{
if(bna[x][i])
{
if(y&&bna[i][y])
{
a=y;
b=x;
c=i;
return 1;
}
if(!vis[i]) if(dfs(i,x)) return 1;
}
}
return 0;
} int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%s",ana[i]+1);
for(int j=1;j<=n;j++)
{
bna[i][j]=ana[i][j]-'0';
}
}
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
if(dfs(i,0))
{
printf("%d %d %d\n",a,b,c);
return 0;
}
}
}
printf("-1\n");
return 0;
}

  

寒假训练——搜索 K - Cycle的更多相关文章

  1. 寒假训练——搜索 G - Xor-Paths

    There is a rectangular grid of size n×mn×m . Each cell has a number written on it; the number on the ...

  2. 寒假训练——搜索 E - Bloxorz I

    Little Tom loves playing games. One day he downloads a little computer game called 'Bloxorz' which m ...

  3. 寒假训练——搜索——C - Robot

    The Robot Moving Institute is using a robot in their local store to transport different items. Of co ...

  4. 蓝桥杯 算法训练 区间k大数查询(水题)

    算法训练 区间k大数查询 时间限制:1.0s   内存限制:256.0MB 问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个数n,表示序列长度. ...

  5. 算法训练 区间k大数查询

    http://lx.lanqiao.org/problem.page?gpid=T11 算法训练 区间k大数查询   时间限制:1.0s   内存限制:256.0MB        问题描述 给定一个 ...

  6. 蓝桥杯--算法训练 区间k大数查询

                                                                                 算法训练 区间k大数查询   时间限制:1.0 ...

  7. 算法训练 区间K大数

    算法训练 区间k大数查询 时间限制:1.0s   内存限制:256.0MB 问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个数n,表示序列长度. ...

  8. 蓝桥杯算法训练 区间k大数查询

    算法训练 区间k大数查询   问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个数n,表示序列长度. 第二行包含n个正整数,表示给定的序列. 第三个 ...

  9. Java实现 蓝桥杯 算法训练 区间k大数

    算法训练 区间k大数查询 时间限制:1.0s 内存限制:256.0MB 问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个数n,表示序列长度. 第二 ...

随机推荐

  1. [linux] tcpdump抓包案例

    1.常见参数 tcpdump -i eth0 -nn -s0 -v port 80 -i 选择监控的网卡 -nn 不解析主机名和端口号,捕获大量数据,名称解析会降低解析速度 -s0 捕获长度无限制 - ...

  2. Hibernate入门(八)级联保存或更新(含问题在末尾,求大佬指点..)

    级联保存或更新CASCADE 级联保存或更新: 作用就是:保存一方的数据的时候,会把关联的对象也同时保存. 级联保存或更新的配置: 属性名:cascade 属性值: 1.none:所有情况下均不进行关 ...

  3. JavaScript中的三种弹窗

    ---恢复内容开始--- 1.警告窗口(alert) <script type="text/javascript"> alert("我是警告弹框!" ...

  4. (一):C++分布式实时应用框架----整体介绍

    C++分布式实时应用框架 (Cpp Distributed Real-time Application Framework) 版权声明:本文版权及所用技术归属smartguys团队所有,对于抄袭,非经 ...

  5. hihoCoder编程练习赛52

    题目1 : 字符串排序 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 一般我们在对字符串排序时,都会按照字典序排序.当字符串只包含小写字母时,相当于按字母表" ...

  6. Dev、GridControl的模糊查询

    /// <summary> /// 设置girid为每一列都模糊搜索 /// </summary> /// <param name="gdv"> ...

  7. @RequestMapping 详解

    RequestMapping是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应请求的方法都是以该地址作为父路径.RequestMapping注解有六个属性,下面我们把她分 ...

  8. file上传图片,base64转换、压缩图片、预览图片、将图片旋转到正确的角度

    /** * 将base64转换为文件对象 * (即用文件上传输入框上传文件得到的对象) * @param {String} base64 base64字符串 */ function convertBa ...

  9. Ehcache配置详解及CacheManager使用

    <?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://w ...

  10. 【代码笔记】Web-JavaScript-JavaScript调试

    一,效果图. 二,代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...