寒假训练——搜索 K - Cycle
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
- 5
00100
10000
01001
11101
11000
- 1 3 2
- 5
01111
00000
01000
01100
01110
- -1
思路:
就是用搜索,以一排展开,dfs含有两个参数,这两个参数代表这个位置为1,所有,再去搜索,按照没有搜索的一排展开,
找到这个值。
在一个dfs要考虑到三个数,dfs里面的两个参数就是其中两个数,然后找到这两个参数中任意一个对应得第三个数,
再判断第三个数和另一个参数有没有相互对应,有就返回了,没有继续判断这一行有没有被标记,标记了就不用,没有标记就
继续进行搜索。值得确定得是,第一确实每一个都搜到了,没有遗漏,第二,就是搜索得递归,可以再仔细想想
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- using namespace std;
- const int maxn=5050;
- char ana[maxn][maxn];
- bool bna[maxn][maxn],vis[maxn];
- int n,a,b,c;
- int dfs(int x,int y)
- {
- vis[x]=1;
- for(int i=1;i<=n;i++)
- {
- if(bna[x][i])
- {
- if(y&&bna[i][y])
- {
- a=y;
- b=x;
- c=i;
- return 1;
- }
- if(!vis[i]) if(dfs(i,x)) return 1;
- }
- }
- return 0;
- }
- int main()
- {
- scanf("%d",&n);
- for(int i=1;i<=n;i++)
- {
- scanf("%s",ana[i]+1);
- for(int j=1;j<=n;j++)
- {
- bna[i][j]=ana[i][j]-'0';
- }
- }
- for(int i=1;i<=n;i++)
- {
- if(!vis[i])
- {
- if(dfs(i,0))
- {
- printf("%d %d %d\n",a,b,c);
- return 0;
- }
- }
- }
- printf("-1\n");
- return 0;
- }
寒假训练——搜索 K - Cycle的更多相关文章
- 寒假训练——搜索 G - Xor-Paths
There is a rectangular grid of size n×mn×m . Each cell has a number written on it; the number on the ...
- 寒假训练——搜索 E - Bloxorz I
Little Tom loves playing games. One day he downloads a little computer game called 'Bloxorz' which m ...
- 寒假训练——搜索——C - Robot
The Robot Moving Institute is using a robot in their local store to transport different items. Of co ...
- 蓝桥杯 算法训练 区间k大数查询(水题)
算法训练 区间k大数查询 时间限制:1.0s 内存限制:256.0MB 问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个数n,表示序列长度. ...
- 算法训练 区间k大数查询
http://lx.lanqiao.org/problem.page?gpid=T11 算法训练 区间k大数查询 时间限制:1.0s 内存限制:256.0MB 问题描述 给定一个 ...
- 蓝桥杯--算法训练 区间k大数查询
算法训练 区间k大数查询 时间限制:1.0 ...
- 算法训练 区间K大数
算法训练 区间k大数查询 时间限制:1.0s 内存限制:256.0MB 问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个数n,表示序列长度. ...
- 蓝桥杯算法训练 区间k大数查询
算法训练 区间k大数查询 问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个数n,表示序列长度. 第二行包含n个正整数,表示给定的序列. 第三个 ...
- Java实现 蓝桥杯 算法训练 区间k大数
算法训练 区间k大数查询 时间限制:1.0s 内存限制:256.0MB 问题描述 给定一个序列,每次询问序列中第l个数到第r个数中第K大的数是哪个. 输入格式 第一行包含一个数n,表示序列长度. 第二 ...
随机推荐
- C#与C++数据类型比较及结构体转换[整理]
//c++:HANDLE(void *) ---- c#:System.IntPtr//c++:Byte(unsigned char) ...
- 【Java每日一题】20170302
20170301问题解析请点击今日问题下方的“[Java每日一题]20170302”查看(问题解析在公众号首发,公众号ID:weknow619) package Mar2017; public cla ...
- SSM-Netty实现软硬件通信,真实项目案例
今天分享的是Myself自己工作项目中的一个模块实例实现的思路还有流程,在这过程中也是遇到了很多问题,能过顺利解决也是团队沟通的结果. 项目模拟背景:假设我们有一个软件平台,我们的线下产品是一些探测器 ...
- SpringBoot中异步请求和异步调用(看这一篇就够了)
原创不易,如需转载,请注明出处https://www.cnblogs.com/baixianlong/p/10661591.html,否则将追究法律责任!!! 一.SpringBoot中异步请求的使用 ...
- python网络聊天器多线程版
在之前的一篇文章(python网络编程-udp)中实现了一个简单的udp聊天器,只能在单线程下进行收发数据,在学习完多线程之后,实现一个能同时收发数据的udp聊天器. 说明: 编写一个有2个线程的程序 ...
- bash array
bash 仅支持一维数组. 而且数组下标是从0开始的为数组赋值:array=(1 4 7 2 5 8) #以空格为分割符,()为数组str="this is test string& ...
- markdown 语法指南
说明:左边是markdown的语法 右边是预览.(我这里用了黑色的背景,一般白色较多) 1. 标题 2.列表 3.引用 (1)一层引用 (2)多层引用 4.图片(如果是本地:按照语法写图片路径:如果是 ...
- BZOJ 1022: [SHOI2008]小约翰的游戏John (Anti-nim)
Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 3134 Solved: 2003[Submit][Status][Discuss] Descripti ...
- 不能用notepad++编辑器编写python
不能用notepad++编辑器编写python,因为notepad对空格支持不是很良好,会出现莫名其妙的错误!建议用vim或emacs. 有人这样解释:不要混合使用制表符和空格来缩进,因为这在跨越不同 ...
- Python3 jupyter notebook 服务器搭建
1. jupyter notebook 安装 创建 jupyter 目录 mkdir jupyter cd jupyter/ 创建独立的 Python3 运行环境,并激活进入该环境 virtualen ...