题目链接:http://codeforces.com/problemset/problem/117/C

C. Cycle
time limit per test

2.5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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

题解:

由于只有三个点,所以在dfs时:对于当前点,判断下一个点是否能到达上一个点。(相当于枚举当前点)。

学习之处:

1.做题技巧:如果题目的限定很小,那么就可以直接枚举,不必要找到通用的方法,找到解决此题的方法即可。

例如:http://blog.csdn.net/dolfamingo/article/details/62887883

此题的限定条件是3条边,那么可以直接枚举第二条边。找到能解决三条边的方法即可,不必寻找能解决n条边的方法。但是题后要思考,寻找通用的方法。

2.有关找环的另一道题:http://blog.csdn.net/dolfamingo/article/details/72566330

3.思考题:找大小为m的环又该怎么办呢? 

此题环的大小为3,所以刚好可以用vis[]来防止重复访问,但是当环为m时,就不能这样了(因为即便某些点被访问过,但是仍能与当前的路径构成m环),这个问题值得思考。

代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const double eps = 1e-6;
const int INF = 2e9;
const LL LNF = 9e18;
const int mod = 1e9+7;
const int maxn = 5e3+10; int n;
int g[maxn][maxn], vis[maxn]; int dfs(int u, int pre)
{
vis[u] = 1;
for(int i = 1; i<=n; i++)
{
if(g[u][i]) //u能到v
{
if(pre!=-1 && g[i][pre]) //不管i有没被访问,只要能构成3环就可以了。
{
printf("%d %d %d", pre, u, i);
return 1;
} if(!vis[i] && dfs(i,u)) //如果i没有被访问,则访问。
return 1;
}
}
return 0;
} int main()
{
scanf("%d",&n);
char s[maxn];
for(int i = 1; i<=n; i++)
{
scanf("%s",s+1);
for(int j = 1; j<=n; j++)
g[i][j] = s[j] - '0';
} for(int i = 1; i<=n; i++)
if(!vis[i] && dfs(i,-1))
return 0; puts("-1");
return 0;
}

Codeforces Beta Round #88 C. Cycle —— DFS(找环)的更多相关文章

  1. 【Codeforces Beta Round #88 C】Cycle

    [Link]:http://codeforces.com/problemset/problem/117/C [Description] 问你一张图里面有没有一个三元环,有的话就输出. [Solutio ...

  2. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

  3. Codeforces Beta Round #63 (Div. 2)

    Codeforces Beta Round #63 (Div. 2) http://codeforces.com/contest/69 A #include<bits/stdc++.h> ...

  4. Codeforces Beta Round #59 (Div. 2)

    Codeforces Beta Round #59 (Div. 2) http://codeforces.com/contest/63 A #include<bits/stdc++.h> ...

  5. Codeforces Beta Round #57 (Div. 2)

    Codeforces Beta Round #57 (Div. 2) http://codeforces.com/contest/61 A #include<bits/stdc++.h> ...

  6. Codeforces Beta Round #55 (Div. 2)

    Codeforces Beta Round #55 (Div. 2) http://codeforces.com/contest/59 A #include<bits/stdc++.h> ...

  7. Codeforces Beta Round #54 (Div. 2)

    Codeforces Beta Round #54 (Div. 2) http://codeforces.com/contest/58 A 找子序列 #include<bits/stdc++.h ...

  8. Codeforces Beta Round #52 (Div. 2)

    Codeforces Beta Round #52 (Div. 2) http://codeforces.com/contest/56 A #include<bits/stdc++.h> ...

  9. Codeforces Beta Round #40 (Div. 2)

    Codeforces Beta Round #40 (Div. 2) http://codeforces.com/contest/41 A #include<bits/stdc++.h> ...

随机推荐

  1. Java学到什么程度可以找到第一份工作

    作者:黄小斜 文章来源:程序员江湖 很多Java初学都关心这么一个问题,Java学到什么程度以后可以找到第一份工作.大家的目标都很明确,也很实在,学习Java无非就是为了找工作,那到底我要学多少Jav ...

  2. BZOJ 1008: [HNOI2008]越狱 组合数学

    原题链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1008 题解: 就很傻逼的组合数学啊... $$ans=M^N-M*(M-1)^{(N-1) ...

  3. Java中的文件上传2(Commons FileUpload:commons-fileupload.jar)

    相比上一篇使用Servlet原始去实现的文件上传(http://www.cnblogs.com/EasonJim/p/6554669.html),使用组件去实现相对来说功能更多,省去了很多需要配置和处 ...

  4. Android动画中Interpolator 详解和演示

    遇到一个项目需求,想让动画变得更活泼一点,于是想到了动画属性中的Interpolator,写了基本例子测试一下Android提供给我们现成的加速器的效果: 效果 代码中方法 xml中属性 越来越快 A ...

  5. EasyHook库系列使用教程之四钩子的启动与停止

    此文的产生花费了大量时间对EasyHook进行深入了解同一时候參考了大量文档 先来简单比較一下EasyHook与Detour钩取后程序流程 Detours:钩取API函数后.产生两个地址,一个地址相应 ...

  6. CMake - boost - 可运行程序 - 静态库

    CMake - boost 最后更新日期:2014-04-25by kagula 阅读前提:<CMake入门(二)>.Linux的基本操作 环境: Windows 8.1 64bit英文版 ...

  7. 基于ACCESS和ASP的SQL多个表查询与计算统计代码(一)

    近期在写几个关于"Project - Subitem - Task"的管理系统,说是系统还是有点夸大了,基本就是一个多表查询调用和insert.update的数据库操作.仅仅是出现 ...

  8. react-redux的connect()方法

    容器组件使用 connect() 方法连接 Redux 我们用 react-redux 提供的 connect() 方法将“笨拙”的 Counter 转化成容器组件.connect() 允许你从 Re ...

  9. C语言-二维背包问题

    二维费用背包问题 问题: 二维费用的背包问题是指:对于每件物品,具有两种不同的费用:选择这件物品必须同时付出这两种代价:对于每种代价都有 一个可付出的最大值(背包容量).问怎样选择物品可以得到最大的价 ...

  10. C语言各种keyword

    1.register 在函数内定义变量时.默认是 auto 类型,变量存储在内存中,当程序用到该变量时,由控制器发出指令将内存中该变量的值送到运算器,计算结束后再从运算器将数据送到内存.假设一个变量用 ...