@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. jeecms使用小结

    前言: 使用jeecmsV9已经有一段时间,现在PC端的二次开发基本进入尾声,手机端的开发即将开始 ,由于项目时间比较紧,开发时不是每个人都会使用它自带的标签,所以在PC端开发的时候浪费了大量时间,为 ...

  2. Hystrix线程池配置

    Hystrix配置文件配置 断路器: hystrix.command.default.circuitBreaker.requestVolumeThreshold(当在配置时间窗口内达到此数量的失败后, ...

  3. 序列化form表单

    // 序列化JSON字符串 $.fn.serializeObject = function () { let o = {}; let a = this.serializeArray(); $.each ...

  4. leetcode 839 Similar String Groups

    题目 Two strings X and Y are similar if we can swap two letters (in different positions) of X, so that ...

  5. ucore 物理内存探测 lab2 附录A&B

    探测物理内存分布的大小和方法 bootloader 增加的工作 bootasm.S 中对应了 probe_memory 到 finish_probe 的部分. 通过BIOS 中断 获取内存可调用参数为 ...

  6. Leetcode79. Word Search单词搜索

    给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字 ...

  7. pycharm 测试执行成功,但却无法成功生成测试报告(使用HTMLTestRunner)的解决办法

    pycharm 测试执行成功,在对应的测试路径下确未生成测试报告.反复确认代码也是没有问题的,在网上查找了原因:简单的unittest运行是不执行main方法的.是允许方式问题. 于是在mian方法里 ...

  8. CLTPHP5.0发布

    CLTPHP内容管理系统,包含系统设置,权限管理,模型管理,数据库管理,栏目管理,会员管理,网站功能,模版管理,微信管理等相关模块.   CLTPHP内容管理系统=ThinkPHP5+layuiAdm ...

  9. 写论文,没数据?R语言抓取网页大数据

    写论文,没数据?R语言抓取网页大数据 纵观国内外,大数据的市场发展迅猛,政府的扶持也达到了空前的力度,甚至将大数据纳入发展战略.如此形势为社会各界提供了很多机遇和挑战,而我们作为卫生(医学)统计领域的 ...

  10. CSS3渐变效果

    一.线性渐变linear-gradient  1.使用方法: background:-webkit-linear-gradient(red,blue);background:-moz-linear-g ...