CodeForce 117C Cycle DFS

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

50010010000010011110111000

Output

1 3 2 

Input

50111100000010000110001110

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,找到与该点相连且未被访问过的点继续DFS3.如果形成了环,结束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] = 1;
    for(int i = 0 ; i < n &&(!a ||!b || !c); i ++)
    {
        if(arr[u][i] == ‘1‘ )
        {
            if(v != -1 && arr[i][v] == ‘1‘)
            {
                a = v + 1 , b = u + 1 , c = i + 1;
                return ;
            }
            if(!vis[i]) dfs(i , u);
        }
    }

}

int main()
{
    while(scanf("%d" , &n) != EOF)
    {
        mem(vis , 0);
        for(int i = 0 ; i < n; i ++)
        {
            scanf("%s" , arr[i]);
        }
        flag = 0;
        a = b = c  = 0;
        for(int i = 0 ; i < n ; i ++)
        {
            if(!vis[i])
            {
                dfs(i , -1);
            }
        }
        if(!a) printf("-1\n");
        else printf("%d %d %d\n" , a , b , c);
    }
    return 0;
}



原文地址:https://www.cnblogs.com/cautx/p/11612451.html

时间: 2024-10-10 18:01:01

CodeForce 117C Cycle DFS的相关文章

codeforce 124B——全排列dfs——Permutations

You are given nk-digit integers. You have to rearrange the digits in the integers so that the difference between the largest and the smallest number was minimum. Digits should be rearranged by the same rule in all integers. Input The first line conta

codeforce Pashmak and Buses(dfs枚举)

1 /* 2 题意:n个同学,k个车, 取旅游d天! 3 要求所有的学生没有两个或者两个以上的在同一辆车上共同带d天! 输出可行的方案! 4 5 对于d行n列的矩阵,第i行第j列表示的是第i天第j个同学所在的车号! 6 也就是保证所有行不全相同,即每一列都是不相同的! 7 如果每一列都不相同就是表示第j个同学(第j列)在这d天中不会和其他同学(列)在这d天中 都在同一辆车中! 8 9 思路:对于每一列我们枚举d天该学生所在的车号!它的下一列只保证有一个元素和它不同就行了!依次下去! 10 11

codeforce 14D 无向图 求树的长度 bfs是一种方法 先用dfs做

枚举每一条边  将树分为两部分 分别dfs求出树的直径 从一点开始最长路加次长路为树的直径 #include<iostream> #include<vector>#include<algorithm>using namespace std;vector<int> G[100000];int n,s;inline int max(int a,int b)//注意是inline 不然超时{       return a>b?a:b;}int dfs(int

CodeForce 540C:(DFS)

终点必须是X时才能完成,如果是“."则意味着终点需要走两次 用mat[i][j]表示该点还能经过的次数 #include"cstdio" #include"cstring" #include"queue" #include"iostream" #define MAXN 505 using namespace std; int dx[4]={-1,1,0,0}; int dy[4]={0,0,-1,1}; int ma

CodeForce 377 A mazes(dfs+连通性问题)

Pavel 喜欢网格迷宫.一个网格迷宫是一个 n × m 的长方形迷宫,其中每个单元格要么是空白的,要么是墙体.您可以从一个单元格走到另一个单元格,只要两个单元格均是空白的,且拥有一条公共的边. Pavel 绘制了一个网格迷宫,包含的全部空白单元格形成了一个连通区域.换言之,您可以从任何一个空白的单元格,走到其它任意的空白单元格.Pavel 的迷宫如果墙体太少,他就不喜欢这个迷宫.他希望将 k 个空白的单元格转换为墙体,使得剩余的全部单元格仍然能够形成一个连通区域.请帮助他实现这个任务. 输入

CodeForces 510 B. Fox And Two Dots(DFS 啊)

题目链接:http://codeforces.com/problemset/problem/510/B Fox Ciel is playing a mobile puzzle game called "Two Dots". The basic levels are played on a board of size n?×?m cells, like this: Each cell contains a dot that has some color. We will use diff

HDU-2821-Pusher(DFS)

Problem Description PusherBoy is an online game http://www.hacker.org/push . There is an R * C grid, and there are piles of blocks on some positions. The goal is to clear the blocks by pushing into them. You should choose an empty area as the initial

CodeForce 448C 木片填涂问题

题目大意:有多片木片需要填涂,可以每次横着涂一行,也可以一次涂一列,当然你涂一行时遇到中间长度不够高的木片,填涂到此中断 这题目运用dfs能更容易的解出,虽然还是十分不容易理解 1 #include <iostream> 2 3 using namespace std; 4 5 #define N 5010 6 int a[N],n; 7 8 int Min(int c,int d) 9 { 10 return c<d?c:d; 11 } 12 int dfs(int c,int d,i

HDU 1524 - A Chess Game(sg函数 + dfs)

A Chess Game Problem Description Let's design a new chess game. There are N positions to hold M chesses in this game. Multiple chesses can be located in the same position. The positions are constituted as a topological graph, i.e. there are directed