@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. 在 IE 选项那提示 ”某些设置由系统管理员进行管理” 解决

    运行 regedit 打开注册表 修改注册表值HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\CurrentVersion\Interne ...

  2. mysql基础记录

    1. 概念介绍 数据库:专门存储数据,存储数据的仓库,同时提供了对数据的操作方法,增删改查的方法 事务 事务:是作为一个单元的一组有序的数据库操作,如果组当中所有操作都成功,则事务执行成功,如果有一个 ...

  3. CesiumLab V1.4 新功能 BIM数据处理

    我也没想到,BIM数据处理一下拖了这么久才有个交代.我们照例先放图   Revit官方的示例数据   隐藏屋顶+俯视   曾经因为太大而无法导出无法处理的医院模型   室内装修方案模型 最近和很多做b ...

  4. HTML 和 XHTML 区别

    1.初级改善 为页面添加正确的DOCTYPE 很多设计师和开发者都不知道什么是DOCTYPE,DOCTYPE有什么用. DOCTYPE是document type的简写.主要用来说明你用的XHTML或 ...

  5. NFS挂载服务具体的实施方案

    1.服务器磁盘共享实施方案 第一步:安装NFS和rpc. 1. 安装nfs-utils:NFS主程序,rpcbind:PRC主程序 nfs-utils:NFS主程序,包含rpc.nfsd  rpc.m ...

  6. GitHub and Git

    book:https://git-scm.com/book/zh/v2 Git使用简易指南:https://www.bootcss.com/p/git-guide

  7. java根据list中的对象某个属性排序

    1. Collections.sort public class Test { public static void main(String[] args) throws Exception { Ci ...

  8. 【P1203】 【USACO1.1】坏掉的项链Broken Necklace

    P1203 [USACO1.1]坏掉的项链Broken Necklace 题目描述 你有一条由N个红色的,白色的,或蓝色的珠子组成的项链(3<=N<=350),珠子是随意安排的. 这里是 ...

  9. Axure之添加点击页面

    添加悬停字体变色的效果 页面载入时的频道预设(我做错了,英文版本不知道那个是页面载入时的事件) 我的博客不够完善,看不到全部的图片.我后续会修改我的网站的

  10. 模拟7题解 T2visit

    T2 visit [组合数学][中国剩余定理] 一场考试难得见两个数学题 本来想矩阵快速幂,显然空间复杂度不行,主要是没时间,就没打 正解: 首先推波式子 1.$C_{t}^{k}$    在t步中总 ...