@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. Eclipse配置Maven详细教程

    一.使用eclipse自带的maven插件 首先,现在下载Eclipse Mars之后的版本,基本上都自带了maven插件,无需自己再安装maven. 有几个注意点: 1.默认的本地仓库的目录是在C: ...

  2. Markdown文档使用

    Markdown使用 一.markdown标题:1级-6级 一级 #空格 二级 ##空格 三级 ###空格 ... 六级 ######空格 二.代码块 print("hello world! ...

  3. [jnhs]全套CRC校验 算法

    摘自 https://blog.csdn.net/cp1300/article/details/51443350 uint8_t crc4_itu(uint8_t *data, uint_len le ...

  4. Python3入门机器学习 经典算法与应用

    Python3入门机器学习 整个课程都看完了,这个课程的分享可以往下看,下面有链接,之前做java开发也做了一些年头,也分享下自己看这个视频的感受,单论单个知识点课程本身没问题,大家看的时候可以关注下 ...

  5. docker-4-Dockerfile配置文件详解

    ​ Dockerfile简单一点就是描述你这个镜像安装了哪些软件包,有哪些操作,创建了什么东西.有些人喜欢用 docker commit 命令去打包镜像,这样是不好的,首先commit出来的镜像比你使 ...

  6. 设置Linux系统的空闲等待时间TMOUT的方法和Linux反空闲设置的两种方法

    为了增强linux系统的安全性,我们需要在用户输入空闲一段时间后自动断开,这个操作可以由设置TMOUT值来实现.将以下字段加入到/etc/profile 中即可(对所有用户生效). export TM ...

  7. vue+ElementUI项目中,input只能输入正整数的验证

    代码如下:     <el-input  v-model="famount" placeholder="请输入内容"   @keyup.native=&q ...

  8. 批处理启动应用程序(win)

    @echo off net session >nul 2>&1 " ( echo Oops: This tools must run with administrator ...

  9. poj 2001 Shortest Prefixes(字典树trie 动态分配内存)

    Shortest Prefixes Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15610   Accepted: 673 ...

  10. git解决冲突的最佳方法

    用eclipse egit 去pull 代码出现冲突 点击details  全选复制到记事本上 如上图选择3个冲突中的一个   eclipse快捷键  ctrl + shift+R  张贴   回车 ...