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

OJ-ID: 
CodeForce 117C author:
Caution_X date of submission:
20190930 tags:
DFS description modelling:
给定一个有向图,边权都为1,问能否找到权值和为3的环,找到则输出对应的点标号,否则输出-1 major steps to solve it:
1.vis[]表示该点是否访问过
2.从一个未被访问过的点开始DFS,找到与该点相连且未被访问过的点继续DFS
3.如果形成了环,结束DFS,否则继续2操作 AC CODE:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <map>
#include <set>
#include <iomanip> using namespace std;
//#pragma comment(linker, "/STACK:102400000,102400000")
#define maxn 5005
#define MOD 1000000007
#define mem(a , b) memset(a , b , sizeof(a))
#define LL long long
#define ULL unsigned long long
#define FOR(i , n) for(int i = 1 ; i<= n ; i ++)
typedef pair<int , int> pii;
const long long INF= 0x3fffffff;
int n , flag;
int a , b , c;
char arr[maxn][maxn];
int vis[maxn]; void dfs(int u , int v)
{
if(a && b && c) return;
vis[u] = ;
for(int i = ; i < n &&(!a ||!b || !c); i ++)
{
if(arr[u][i] == '' )
{
if(v != - && arr[i][v] == '')
{
a = v + , b = u + , c = i + ;
return ;
}
if(!vis[i]) dfs(i , u);
}
} } int main()
{
while(scanf("%d" , &n) != EOF)
{
mem(vis , );
for(int i = ; i < n; i ++)
{
scanf("%s" , arr[i]);
}
flag = ;
a = b = c = ;
for(int i = ; i < n ; i ++)
{
if(!vis[i])
{
dfs(i , -);
}
}
if(!a) printf("-1\n");
else printf("%d %d %d\n" , a , b , c);
}
return ;
}

CodeForce 117C Cycle DFS的更多相关文章

  1. Codeforces Beta Round #88 C. Cycle —— DFS(找环)

    题目链接:http://codeforces.com/problemset/problem/117/C C. Cycle time limit per test 2.5 seconds memory ...

  2. Codeforce 263D Cycle in Graph 搜索 图论 哈密尔顿环

    You've got a undirected graph G, consisting of n nodes. We will consider the nodes of the graph inde ...

  3. HDU 5215 Cycle(dfs判环)

    题意 题目链接 \(T\)组数据,给出\(n\)个点\(m\)条边的无向图,问是否存在一个奇环/偶环 Sol 奇环比较好判断吧,直接判是否是二分图就行了.. 偶环看起来很显然就是如果dfs到一个和他颜 ...

  4. @codeforces - 117C@ Cycle

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个竞赛图(有向完全图),请找出里面的某个三元环,或者判断不 ...

  5. BZOJ1815 SHOI2006有色图(Polya定理)

    置换数量是阶乘级别的,但容易发现本质不同的点的置换数量仅仅是n的整数拆分个数,OEIS(或者写个dp或者暴力)一下会发现不是很大,当n=53时约在3e5左右. 于是暴力枚举点的置换,并且发现根据点的置 ...

  6. Codeforces Round #580 (Div. 2)-D. Shortest Cycle(思维建图+dfs找最小环)

    You are given nn integer numbers a1,a2,…,ana1,a2,…,an. Consider graph on nn nodes, in which nodes ii ...

  7. codeforce Pashmak and Buses(dfs枚举)

    /* 题意:n个同学,k个车, 取旅游d天! 要求所有的学生没有两个或者两个以上的在同一辆车上共同带d天! 输出可行的方案! 对于d行n列的矩阵,第i行第j列表示的是第i天第j个同学所在的车号! 也就 ...

  8. codeforce gym/100495/problem/F Snake++——DFS应用

    emmmm.... 在被新生暴打后,我花了很久才补出这道DFS.由于WA1检查了半天,最后竟然是输出少了一个:   ,心态小崩. 这里普通的dfs算出的连通区域并不能直接当做最后的答案.所以需要类似模 ...

  9. codeforce -39E-What Has Dirichlet Got to Do with That?(博弈+dfs)

    You all know the Dirichlet principle, the point of which is that if n boxes have no less than n + 1  ...

随机推荐

  1. 学习UML类图

    在类图中一共包含以下几种模型元素,分别是:类(class).接口(interface)以及类之间的关系. 1.类(class) 在面向对象编程中,类是对现象世界中一组具有相同特征的物体的抽象. 2.接 ...

  2. [IDA]系统注释给改掉

    IDA系统自动注释的,如果按 ';',则不会修改成功. 应该按 Shift + ;  这样才可以修改.

  3. Spring Boot 2.X整合Spring-cache,让你的网站速度飞起来

    计算机领域有人说过一句名言:“计算机科学领域的任何问题都可以通过增加一个中间层来解决”,今天我们就用Spring-cache给网站添加一层缓存,让你的网站速度飞起来. 本文目录 一.Spring Ca ...

  4. sql server pivot

    SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[YearSalary]( [year] [int] NULL, ...

  5. ArchLinux 2019.11.01安装流程--安装基本系统

    安装前的一些话 本文是参考官方文档ArchLinux的Installation guide(简体中文)加实际操作编写的. 有啥都好说,转载时请注明作者,这是基本素质,也是法律要求 安装是在虚拟机上进行 ...

  6. C#中对文件进行选择对话框打开和保存对话框进行复制

    场景 通过文件选择对话框选择文件 复制文件到指定路径 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众号霸道的程序猿获取编程相关电子书.教 ...

  7. Tomcat安装、使用(Windows)

    一.下载.安装 1.下载 进官网下载 : https://tomcat.apache.org/ 选择自己适合的版本.在这里演示的是下载 Tomcat 7(解压安装版). 2.解压.启动tomcat 解 ...

  8. Python工具库分享

    漏洞及渗透练习平台: WebGoat漏洞练习平台: https://github.com/WebGoat/WebGoat webgoat-legacy漏洞练习平台: https://github.co ...

  9. Linux下用户管理:删除用户

    基本语法: userdel 用户名 但是我们在删除用户的时候很显然需要利用root用户权限来进行删除才是可以的.但是利用这种方法进行删除的话是会保留家目录的,意思是该用户所对应的家目录不会被删除. 不 ...

  10. 从server.xml看Tomcat容器的层次结构

    先来看一下server.xml的结构: <Server> ​ <Listener/> ​ <GlobalNamingResources> ​ <Resourc ...