寒假训练——搜索 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,表示序列长度. 第二 ...
随机推荐
- 解码 id_token
简介 id_token是一个特殊的token,在Microsoft Graph的认证和授权过程中颁发,它包含了已认证用户的一些信息.本文将介绍如何通过实例理解id_token,并且演示了如何解码. 准 ...
- Git如何回滚代码?
摘要: 多年以后,你面对一个需要回滚的Git仓库,准会想起这篇博客. 某一天,用户跟我反馈,他不能分配任务了.我去看了一下Fundebug捕获的报错信息: 可知,出错原因是前端发送的请求参数有问题.这 ...
- angular 去掉url里面的#
1.适合客户端的方法,但是页面不能刷新,一刷新就404 (1)在index.html里添加 <base href="/"> (2)在app.js的config里,注入$ ...
- SAP MM PO 中的Delivery Date并非保存在EKPO表里
采购订单行项目中的deliverydate并非如同其它字段值一样是保存在采购订单行项目表EKPO里的, 而是从EKET表里抓取最早的Delivery Date作为该Item的delivery date ...
- TFS 2017 持续集成速记
VS2017许多激动人 心的功能,升级! TFS2017也升级,不支持SQL2012,升级!不过貌似开发版不能升级,好吧,开发版升2014企业版! 2017据说不支持XAML生成了,但后台菜单中还 ...
- PlugNT CMS v4.6.3 调用文章上一页和下一页及点击数加1
using System; using System.Data; using System.Web; using System.Web.UI; using System.Web.UI.WebContr ...
- (后端)解决code唯一码(java)简便方法
public String next() { long appBootTimes = systemVariableService.getAppBootTimes(); return Long.toSt ...
- (网页)html中页面传递参数不用cookie不用缓存,js方法搞定
function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&] ...
- python LeetCode 两数相除
近一个月一直在写业务,空闲时间刷刷leetcode,刷题过程中遇到了一道比较有意思的题目,和大家分享. 题目描述: 给定两个整数,被除数 dividend 和除数 divisor.将两数相除,要求不使 ...
- coTurn 使用测试方法
做个记录 1.从"../examples/etc/" 目录拷贝turnserver.conf文件到"/usr/local/etc/"目录 2.修改配置文件 主要 ...