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).
OutputPrint 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.
Examples5
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] = 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
- DW Replace Open Sans:将WordPress 后台中的open-sans字体加载源从Google Fonts换为360 CDN
- Angularjs基础(三)
- 低多边形(Low-Poly)简介及相关素材分享下载
- 开发者的福音:GenerateWP 自动生成WordPress 开发相关代码
- 自定义WordPress 密码文章提示文字
- 解决WordPress文章密码保护在首页(摘要)不起作用
- WordPress 3.9+的 TinyMCE 4 编辑器增强开发
- Angularjs基础(二)
- Angularjs基础(一)
- iOS 7 Web App的初级优化之道
- Less 常用基础知识
- WooCommerce 结算页面自定义(删除/添加)表单元素
- Waves:类Material Design 的圆形波浪(涟漪)点击特效插件
- 让WooCommerce 中文网关支持PayPal 并自动按汇率进行转换
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- Python 批量合并 Excel
- 用 Python 了解一下最炫国漫《雾山五行》
- Python 基础(六):列表与元组
- Word 批量转 PDF
- Python 数据分析(一):NumPy 基础知识
- 每日一题 | 环形排列问题
- 每日一题 | QQ群撩妹问题
- 每日一题 | 老板出的下棋问题
- Qt音视频开发12-mpv解码播放
- 算法专题 | 10行代码实现的最短路算法——Bellman-ford与SPFA
- 每日一题 | 土豪割草问题
- 高阶面试:伯努利过程
- Python 分析电影《南方车站的聚会》
- Python 基础(十八):命名空间 & 作用域
- Python爬虫(三):BeautifulSoup库