@description@

给定一个竞赛图(有向完全图),请找出里面的某个三元环,或者判断不存在这样的环。

Input

第一行包含一个整数 n (1 ≤ n ≤ 5000)。

接下来 n 行包含这个图的邻接矩阵,其中 A[i, j] = 1 表示存在一条 i 到 j 的边。

保证 A[i, i] = 0, A[i, j] ≠ A[j, i] (1 ≤ i, j ≤ n, i ≠ j)。

Output

如果无解,输出 -1;否则输出三个不同的顶点 a1, a2, a3 使得 A[a1, a2] = 1, A[a2, a3] = 1, A[a3, a1] = 1。

任意解即可。

Examples

Input

5

00100

10000

01001

11101

11000

Output

1 3 2

Input

5

01111

00000

01000

01100

01110

Output

-1

@solution@

根据竞赛图的性质,如果有环,则一定存在三元环。证明可以通过一个 n 元环一步步缩成一个三元环。

那么假如前 i 个点没有三元环,则一定构成 DAG,考虑维护前 i 个点的拓扑序 p[1...i]。

考虑加入第 i + 1 个点,如果依然无法构成环,则存在一个 j 使得 p[1...j] 连向 i + 1 且 i + 1 连向 p[j+1...i]。将 i 塞到 j 和 j+1 之间即可。

否则,可以反证证明出一定存在一个 j 使得 i 连向 p[j] 且 p[j+1] 连向 i。而这就是我们要找的三元环。

@accepted code@

#include<cstdio>
const int MAXN = 5000;
int A[MAXN + 5][MAXN + 5], n;
char str[MAXN + 5];
int nxt[MAXN + 5];
int main() {
scanf("%d", &n);
for(int i=1;i<=n;i++) {
scanf("%s", str + 1);
for(int j=1;j<=n;j++)
A[i][j] = str[j] - '0';
}
int hd = 1, tl = 1; nxt[1] = -1;
for(int i=2;i<=n;i++) {
nxt[i] = -1;
if( A[i][hd] ) {
int p = nxt[hd];
while( p != -1 ) {
if( A[p][i] ) {
printf("%d %d %d\n", p, i, hd);
return 0;
}
p = nxt[p];
}
nxt[i] = hd, hd = i;
}
else if( A[tl][i] ) {
int p = hd;
while( p != tl ) {
if( A[i][p] ) {
printf("%d %d %d\n", i, p, tl);
return 0;
}
p = nxt[p];
}
nxt[tl] = i, tl = i;
}
else {
int p = hd;
while( true ) {
if( A[p][i] && A[i][nxt[p]] )
break;
p = nxt[p];
}
int q = hd;
while( q != p ) {
if( A[i][q] ) {
printf("%d %d %d\n", i, q, p);
return 0;
}
q = nxt[q];
}
q = nxt[nxt[p]];
while( q != -1 ) {
if( A[q][i] ) {
printf("%d %d %d\n", q, i, nxt[p]);
return 0;
}
q = nxt[q];
}
nxt[i] = nxt[p], nxt[p] = i;
}
}
puts("-1");
}

@details@

tips:代码写得其实和 solution 描述的有一点点不一样,具体表现是我代码中是不管三七二十一先插入过后才判断是否有这样一个三元环。。。

@codeforces - 117C@ Cycle的更多相关文章

  1. CodeForce 117C Cycle DFS

    A tournament is a directed graph without self-loops in which every pair of vertexes is connected by ...

  2. Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 图论

    D. Vitaly and Cycle Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/557/p ...

  3. Codeforces Beta Round #88 C. Cycle —— DFS(找环)

    题目链接:http://codeforces.com/problemset/problem/117/C C. Cycle time limit per test 2.5 seconds memory ...

  4. 【34.57%】【codeforces 557D】Vitaly and Cycle

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  5. [Codeforces 1205B]Shortest Cycle(最小环)

    [Codeforces 1205B]Shortest Cycle(最小环) 题面 给出n个正整数\(a_i\),若\(a_i \& a_j \neq 0\),则连边\((i,j)\)(注意i- ...

  6. codeforces 962F.simple cycle(tarjan/点双连通分量)

    题目连接:http://codeforces.com/contest/962/problem/F 题目大意是定义一个simple cycle为从一个节点开始绕环走一遍能经过simple cycle内任 ...

  7. Codeforces Round #161 (Div. 2) D. Cycle in Graph(无向图中找指定长度的简单环)

    题目链接:http://codeforces.com/problemset/problem/263/D 思路:一遍dfs即可,dp[u]表示当前遍历到节点u的长度,对于节点u的邻接点v,如果v没有被访 ...

  8. codeforces 557 D. Vitaly and Cycle 组合数学 + 判断二分图

    D. Vitaly and Cycle       time limit per test 1 second memory limit per test 256 megabytes input sta ...

  9. Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 奇环

    题目链接: 点这里 题目 D. Vitaly and Cycle time limit per test1 second memory limit per test256 megabytes inpu ...

随机推荐

  1. DjangoORM查询、分页、ckeditor

    查询数据 Django的批量查询(查询所有,或者条件查询)返回的是queryset对象. Queryset对象是一个惰性对象,在不执行 1.排序 2.循环 3.截取 操作的情况下,不会遍历序列的内容. ...

  2. Codeforces 608E. Marbles

    E. Marbles time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  3. 用 Python 写一个 NoSQL 数据库Python

    NoSQL 这个词在近些年正变得随处可见. 但是到底 “NoSQL” 指的是什么? 它是如何并且为什么这么有用? 在本文, 我们将会通过纯 Python (我比较喜欢叫它, “轻结构化的伪代码”) 写 ...

  4. JavaScript远程调用

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. laravel-admin

    laravel-admin 文档地址: http://laravel-admin.org/docs/#/zh/

  6. Laravel5.5/6 报错call to undefined function openssl cipher iv length()

    在安装laravel5.5后, 访问显示报错. call to undefined function openssl cipher iv length() 经查为php7.1的OpenSSL扩展加载失 ...

  7. MyEclipse编写ExtJS卡死问题解决方法

    MyEclipse 8.6  在 jsp 中编写 ExtJS时,会出现卡死现象,让人甚是头疼.网上找了很多方法,折腾半天,还是不管用. 什么MyEclipse 优化,Validation 取消,MyE ...

  8. 如何高效地在github上找开源项目学习?

    1.高级条件组合(精确搜索) in:readme 微服务 stars:>1000 in:readme spring security stars:>3000 in:name python  ...

  9. springboot-mybatis双数据源配置

    yml文件 spring: datasource: test1: driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://localhost: ...

  10. Python接口自动化(二)接口开发

    django 配置开发环境 相关命令 python manage.py runserver 127.0.0.1:8000在指定的地址和端口启动服务 python manage.py startapp ...