Codeforces Beta Round #88 C. Cycle —— DFS(找环)
题目链接:http://codeforces.com/problemset/problem/117/C
2.5 seconds
256 megabytes
standard input
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.
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).
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.
5
00100
10000
01001
11101
11000
1 3 2
5
01111
00000
01000
01100
01110
-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(找环)的更多相关文章
- 【Codeforces Beta Round #88 C】Cycle
[Link]:http://codeforces.com/problemset/problem/117/C [Description] 问你一张图里面有没有一个三元环,有的话就输出. [Solutio ...
- Codeforces Beta Round #76 (Div. 2 Only)
Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...
- Codeforces Beta Round #63 (Div. 2)
Codeforces Beta Round #63 (Div. 2) http://codeforces.com/contest/69 A #include<bits/stdc++.h> ...
- Codeforces Beta Round #59 (Div. 2)
Codeforces Beta Round #59 (Div. 2) http://codeforces.com/contest/63 A #include<bits/stdc++.h> ...
- Codeforces Beta Round #57 (Div. 2)
Codeforces Beta Round #57 (Div. 2) http://codeforces.com/contest/61 A #include<bits/stdc++.h> ...
- Codeforces Beta Round #55 (Div. 2)
Codeforces Beta Round #55 (Div. 2) http://codeforces.com/contest/59 A #include<bits/stdc++.h> ...
- Codeforces Beta Round #54 (Div. 2)
Codeforces Beta Round #54 (Div. 2) http://codeforces.com/contest/58 A 找子序列 #include<bits/stdc++.h ...
- Codeforces Beta Round #52 (Div. 2)
Codeforces Beta Round #52 (Div. 2) http://codeforces.com/contest/56 A #include<bits/stdc++.h> ...
- Codeforces Beta Round #40 (Div. 2)
Codeforces Beta Round #40 (Div. 2) http://codeforces.com/contest/41 A #include<bits/stdc++.h> ...
随机推荐
- 做IT这几年,我整理了这些干货想要送给你!
没有一条路是容易的,特别是转行计算机这条路. 松哥接触过很多转行做开发的小伙伴,我了解到很多转行人的不容易,记得松哥大二时刚刚决定转行计算机,完全不知道这些东西到底应该怎么学,每天就是抱着书啃,书倒是 ...
- XSY1036 [Apio2012]派遣
题面 Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个 ...
- Extjs grid 单元格事件
celldblclick: function (view, td, cellIndex, record, tr, rowIndex, e, eOpts) { //extjs 4.2下,有时出现,多次不 ...
- PyTorch学习笔记之Tensors 2
Tensors的一些应用 ''' Tensors和numpy中的ndarrays较为相似, 因此Tensor也能够使用GPU来加速运算 ''' # from _future_ import print ...
- Java创建和解析Json数据方法(五)——Google Gson包的使用
(五)Google Gson包的使用 1.简介 Gson包中,使用最多的是Gson类的toJson()和fromJson()方法: ①toJson():将java对象转化为json数据 ...
- Cesium加载三维倾斜摄影数据
具体技术来源自论文 基于Cesium的倾斜摄影三维模型Web加载与应用研究. 技术架构图 应用实例 利用一个实际实例来详细说明如何利用Cesium加载倾斜摄影数据,并进行可视化和交互操作. 首先,利用 ...
- Android开源工具项目集合
最近因为要去外派了,工欲善其事,必先利其器!所以又回顾了一下自己github上所收藏的项目,也算是温故而知新吧. 最流行的Android组件大全 http://www.open-open.com/li ...
- ShadowMap渲染阴影方法及问题 【转】
ShadowMap基于的原理:SM算法是一个2-pass绘制算法,第一pass从光源视点绘制场景,生成SM纹理,第2pass从视点视图按常规方法绘制场景 从光源的位置观察场景,这时候我们看不到的地方就 ...
- java中Volatile修饰符的含义
在java语言中:为了获得最佳速度,同意线程保存共享成员变量的私有拷贝.并且仅仅当线程进入或者离开同步代码块时才与共享成员变量的原始值进行对照. volatilekeyword的作用就是提示vm:对于 ...
- iOS开发UI篇—懒载入
iOS开发UI篇-懒载入 1.懒载入基本 懒载入--也称为延迟载入,即在须要的时候才载入(效率低,占用内存小).所谓懒载入,写的是其get方法. 注意:假设是懒载入的话则一定要注意先推断是否已经有了. ...