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. ansible命令参数介绍

    -m:要执行的模块,默认为command -a:模块的参数 -u:ssh连接的用户名,默认用root,ansible.cfg中可以配置 -k:提示输入ssh登录密码.当使用密码验证的时候用 -s:su ...

  2. copy-and-swap idiom

    This answer is from https://stackoverflow.com/a/3279550/10133369 Overview Why do we need the copy-an ...

  3. PHP判断设备访问来源

    /** * 判断用户请求设备是否是移动设备 * @return bool */ function isMobile() { //如果有HTTP_X_WAP_PROFILE则一定是移动设备 if (is ...

  4. UserControl关闭

    直接 Application.Current.Shutdown();关闭程序.

  5. LinkedHashMap,源码解读就是这么简单

    概述 LinkedHashMap是HashMap的子类,它的大部分实现与HashMap相同,两者最大的区别在于,HashMap的对哈希表进行迭代时是无序的,而LinkedHashMap对哈希表迭代是有 ...

  6. Web基础--JavaScript入门

    一.JavaScript 1.什么是JavaScript(JS) (1)JavaScript是嵌入HTML中的代码,在浏览器中执行的脚本语言,具有与Java和C语言类似的语法.(2)一种网页编程技术, ...

  7. Android框架Volley使用:Get请求实现

    首先我们在项目中导入这个框架: implementation 'com.mcxiaoke.volley:library:1.0.19' 在AndroidManifest文件当中添加网络权限: < ...

  8. 计算机基础 python安装时的常见致命错误 pycharm 思维导图

    计算机基础 1.组成 人 功能 主板:骨架 设备扩展 cpu:大脑 计算 逻辑处理 硬盘: 永久储存 电源:心脏 内存: 临时储存,断电无 操作系统(windonws mac linux): 软件,应 ...

  9. SRDC - ORA-22924 or ORA-1555 on LOB data: Checklist of Evidence to Supply (Doc ID 1682707.1)

    SRDC - ORA-22924 or ORA-1555 on LOB data: Checklist of Evidence to Supply (Doc ID 1682707.1) Action ...

  10. 关于如何清除某个特定网站的缓存---基于Chrome浏览器

    1.清除浏览器缓存 直接在浏览器设置里面清除浏览器的缓存会清除所有网站的缓存信息,这在某些时候是非常不方便的,毕竟不只有测试网站,还会有一些我们不想清除的信息也会被清除掉: 2.通过F12功能去清除浏 ...