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 ...
随机推荐
- winform批量更新数据_长时间的执行会导致界面卡死
前言:使用winform触发一个事件后执行的代码,如果耗时非常长,则会导致窗口界面假死! 本人最近通过winform窗体执行一项:需要批量更新一批数据库的数据的操作的任务时,由于数据量达到百万级别, ...
- .NET MVC5简介(五)管道处理模型IHttpModule
https://www.cnblogs.com/JimmyZhang/archive/2007/09/04/880967.html IHttpModule HTTPRuntime(运行时).在一个控制 ...
- python连接sqlserver工具类
上代码: # -*- coding:utf-8 -*- import pymssql import pandas as pd class MSSQL(object): def __init__(sel ...
- ASP.NET MVC IOC 之 Autofac(一)
新建一个MVC项目,如 AutoFacTest,引用autofac,如下图: 接下来就是开始进行编程了 首先,新建一个类库,名为 AutoFacTest.Service,该类库编写服务层代码,我们的接 ...
- Python 内置函数补充匿名函数
Python3 匿名函数 定义一个函数与变量的定义非常相似,对于有名函数,必须通过变量名访问 def func(x,y,z=1): return x+y+z print(func(1,2,3)) 匿名 ...
- jquery中的ajax请求到php(学生笔记)
首先ajax的基本语法基础.(必须得引入一个jquery文件,下面的例子展示用了网上的jquery文件,要联网.) 2.请求成功(复制代码运行观察效果) <!DOCTYPE html> & ...
- DevExpress的分页Tab控件XtraTabControl控件的使用
场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...
- FCC---Change Animation Timing with Keywords--两个小球从A都B,相同循环时间 duration, 不同的速度 speed
In CSS animations, the animation-timing-function property controls how quickly an animated element c ...
- js中的作用域
作用域: 域:空间.范围.区域…… 作用:读.写 浏览器:“JS解析器” 1)“找一些东西” :var function 参数 a = ...未定义所有的变量,在正式运行代码之前,都提前 ...
- 详解TCP与UDP
一.TCP的特点 面向连接的.可靠的.基于字节流的传输层通信协议. 将应用层的分割成报文段,并发送发给目标节点的TCP层. 数据包都有序号,对方收到则发送ACK确认,未收到则重传. 使用效验和来效验数 ...