@codeforces - 117C@ Cycle
@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的更多相关文章
- CodeForce 117C Cycle DFS
A tournament is a directed graph without self-loops in which every pair of vertexes is connected by ...
- 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 ...
- Codeforces Beta Round #88 C. Cycle —— DFS(找环)
题目链接:http://codeforces.com/problemset/problem/117/C C. Cycle time limit per test 2.5 seconds memory ...
- 【34.57%】【codeforces 557D】Vitaly and Cycle
time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...
- [Codeforces 1205B]Shortest Cycle(最小环)
[Codeforces 1205B]Shortest Cycle(最小环) 题面 给出n个正整数\(a_i\),若\(a_i \& a_j \neq 0\),则连边\((i,j)\)(注意i- ...
- codeforces 962F.simple cycle(tarjan/点双连通分量)
题目连接:http://codeforces.com/contest/962/problem/F 题目大意是定义一个simple cycle为从一个节点开始绕环走一遍能经过simple cycle内任 ...
- Codeforces Round #161 (Div. 2) D. Cycle in Graph(无向图中找指定长度的简单环)
题目链接:http://codeforces.com/problemset/problem/263/D 思路:一遍dfs即可,dp[u]表示当前遍历到节点u的长度,对于节点u的邻接点v,如果v没有被访 ...
- codeforces 557 D. Vitaly and Cycle 组合数学 + 判断二分图
D. Vitaly and Cycle time limit per test 1 second memory limit per test 256 megabytes input sta ...
- Codeforces Round #311 (Div. 2) D. Vitaly and Cycle 奇环
题目链接: 点这里 题目 D. Vitaly and Cycle time limit per test1 second memory limit per test256 megabytes inpu ...
随机推荐
- 安装最新版本 nginx
有时用操作系统默认安装 yum install apt install , 版本都会低,当然存在的bug 也会有.装最新版本 nginx 需要用他自己的源: sudo add-apt-reposito ...
- Spring松耦合示例(转)& IOC
Spring松耦合示例 轻松学习Spring<一> IoC容器和Dependency Injection模式 最近公司需要,项目中要用到Spring和Ibatis.趁着过年好好学习学习.I ...
- WPF:数据绑定--PropertyChangeNotification属性更改通知
PropertyChangeNotification属性更改通知 实现效果:1.拍卖金额自动随属性值变化而通知界面绑定的值变化. 关键词 : INotifyPropertyChanged Obse ...
- CesiumLab V1.2 新功能 倾斜数据处理
一转眼又是一周的时间,我们的实验室功能又强大了. 照旧我们先放毒,放图,图,太晚了,字都敲不到一起了 lod以及包围盒 大雁塔实例,按楼层单体化 倾斜数据处理参数设置 简单介绍一下 Ces ...
- python基础--类的基础使用
什么是面向对象: 面向对象是一种编程思想,其中的核心是对象,程序是一系列对象的集合,程序员负责调度控制这些对象来交互着完成某些任务. 在面向对象中程序员的角度发生改变,从具体的操作者变成了指挥者 面向 ...
- union /union all/ intersect / minus
- SEO优化步骤
SEO技术并不是简单的几个建议,而是一项需要足够耐心和细致的脑力劳动.大体上,SEO优化主要分为8小步: 1.关键词分析(也叫关键词定位) 这是进行SEO优化最重要的一环,关键词分析包括:关键词关注量 ...
- SQL优化系列(三)- 用最少的索引获得最大的性能提升
从全局出发优化索引 对于高负载的数据库,如何创建最少的索引,让数据库的整体性能提高呢?例如,对于100 条SQL语句,如何创建最佳的5条索引? SQL自动优化工具SQL Tuning Expert P ...
- yum源配置及详解
红帽系列中,进行软件安装可以有三种方法,编译安装,rpm包安装,和yum源安装.其中yum方法安装最简单,因为它可以自动解决软件包之间的依赖关系... 一.常用yum源 yum源可以来源于多种文件 ...
- Python接口自动化(一)接口基础
HTTP接口熟悉 常见接口介绍 接口工具的使用 fiddler如何mock数据 常见接口基础面试 如何理解接口?前后端解耦,前端和后端数据对接桥梁 接口测试和功能测试区别在哪?接口测试是功能测试的一种 ...