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.
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
5
00100
10000
01001
11101
11000
1 3 2
5
01111
00000
01000
01100
01110
-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的更多相关文章
- Codeforces Beta Round #88  C. Cycle —— DFS(找环)
		题目链接:http://codeforces.com/problemset/problem/117/C C. Cycle time limit per test 2.5 seconds memory ... 
- 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 ... 
- HDU 5215 Cycle(dfs判环)
		题意 题目链接 \(T\)组数据,给出\(n\)个点\(m\)条边的无向图,问是否存在一个奇环/偶环 Sol 奇环比较好判断吧,直接判是否是二分图就行了.. 偶环看起来很显然就是如果dfs到一个和他颜 ... 
- @codeforces - 117C@ Cycle
		目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个竞赛图(有向完全图),请找出里面的某个三元环,或者判断不 ... 
- BZOJ1815 SHOI2006有色图(Polya定理)
		置换数量是阶乘级别的,但容易发现本质不同的点的置换数量仅仅是n的整数拆分个数,OEIS(或者写个dp或者暴力)一下会发现不是很大,当n=53时约在3e5左右. 于是暴力枚举点的置换,并且发现根据点的置 ... 
- 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 ... 
- codeforce Pashmak and Buses(dfs枚举)
		/* 题意:n个同学,k个车, 取旅游d天! 要求所有的学生没有两个或者两个以上的在同一辆车上共同带d天! 输出可行的方案! 对于d行n列的矩阵,第i行第j列表示的是第i天第j个同学所在的车号! 也就 ... 
- codeforce gym/100495/problem/F Snake++——DFS应用
		emmmm.... 在被新生暴打后,我花了很久才补出这道DFS.由于WA1检查了半天,最后竟然是输出少了一个: ,心态小崩. 这里普通的dfs算出的连通区域并不能直接当做最后的答案.所以需要类似模 ... 
- 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 ... 
随机推荐
- PriorityBlockingQueue
			public class PriorityBlockingQueueTest { /** * 有优先级顺序的阻塞队列,底层实现是数组,无边界.默认是11. * 构造方法可以传入一个比较器,不传的话,默 ... 
- 愉快地使用Groovy Shell
			这是一篇有关Groovy Shell的帖子,以及它如何在日常工作中为您提供帮助(只要您是软件开发人员).无论您使用哪种编程语言或技术,都可以从Groovy Shell中受益.唯一真正的要求是您能够编写 ... 
- 天天向上的力量python(举一反三)
			天天向上的力量python实例(举一反三) 实例1: 一年365天,以第1天的能力值为基数,记为1.0,当好好学习时能力值相比前一天提高0.1%,没有学习实能力值相比前一天下降0.1%. 问:每天努力 ... 
- SpringBoot(16)—@ConditionalOnBean与@ConditionalOnClass
			@ConditionalOnBean与@ConditionalOnClass 上一篇讲的@Conditional可以通过条件控制是否注入Bean,这篇讲下有关Bean其它几个常用的注解使用方式 @Co ... 
- Docker实用debug调试技巧锦集
			阅读约 20 分钟 『重用』容器名 但我们在编写/调试Dockerfile的时候我们经常会重复之前的command,比如这种docker run --name jstorm-zookeeper zoo ... 
- PlayJava Day006
			今日所学: /* 2019.08.19开始学习,此为补档. */ 构造方法没有返回值(即return为空). this:实例(对象)的引用. JVM:①static方法区:存静态数据 ②栈区:引用 ... 
- DevExpress的下拉框控件ComboBoxEdit控件的使用
			场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ... 
- JS基础语法---break关键字
			break关键字: 如果在循环中使用,遇到了break,则立刻跳出当前所在的循环 for (var i = 0; i < 10; i++) { while (true ... 
- gzip格式分析与识别
			" 介绍gzip格式,识别gzip压缩的数据流量." 在协议分析过程中,经常会发现gzip压缩的数据,例如在HTTP协议中,在HTTP头中会标示,内容编码为gzip.DEFLATE ... 
- printf打印字节调试
			void print(BYTE *data, INT len) { INT x = 0; INT y = 0; if(data == NULL) { return; } for(x = 0; x &l ... 
