POJ1094[有向环 拓扑排序]
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 33184 | Accepted: 11545 |
Description
Input
Output
Sorted sequence determined after xxx relations: yyy...y.
Sorted sequence cannot be determined.
Inconsistency found after xxx relations.
where xxx is the number of relations processed at the time either a sorted sequence is determined or an inconsistency is found, whichever comes first, and yyy...y is the sorted, ascending sequence.
Sample Input
4 6
A<B
A<C
B<C
C<D
B<D
A<B
3 2
A<B
B<A
26 1
A<Z
0 0
Sample Output
Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.
Source
因为要求几个relation,所以没加一个进行一次topoSort
//
// main.cpp
// poj1094
//
// Created by Candy on 9/11/16.
// Copyright © 2016 Candy. All rights reserved.
// #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=;
int n,m,ind[N],ch[N][N];
char tmp[],topo[N];
int q[N];
int topoSort(){
int flag=;//only sorted
int indt[N];
for(int i=;i<=n;i++) indt[i]=ind[i];
for(int i=;i<=n;i++){//printf("%d %d %d \n",i,ind[i],ch[i][0]);
int zero=,u=;
for(int j=;j<=n;j++) if(indt[j]==) zero++,u=j;
if(zero==) return ;//circle
if(zero>) flag=;//no sorted
indt[u]--;
topo[i-]=(char)u+'A'-;
q[i-]=u;
for(int j=;j<=ch[u][];j++)
indt[ch[u][j]]--;
}
return flag;
}
int main(int argc, const char * argv[]) {
while(cin>>n>>m){
if(n==&&m==) break;
memset(ch,,sizeof(ch));
memset(ind,,sizeof(ind));
memset(topo,,sizeof(topo));
int sign=; for(int i=;i<=m;i++){
scanf("%s",tmp);
if(sign) continue;
int x=tmp[]-'A'+,y=tmp[]-'A'+;
ind[y]++;
ch[x][++ch[x][]]=y;
int flag=topoSort();
if(flag==){sign=;printf("Inconsistency found after %d relations.\n",i);}
if(flag==){
sign=;printf("Sorted sequence determined after %d relations: %s.\n",i,topo);
// printf("Sorted sequence determined after %d relations: ",i);
// for(int j=0;j<n;j++)
// printf("%c",q[j]+'A'-1);
// printf(".\n");
// sign=1;
}
}
if(!sign) printf("Sorted sequence cannot be determined.\n");
}
return ;
}
POJ1094[有向环 拓扑排序]的更多相关文章
- HDU 3342 Legal or Not(有向图判环 拓扑排序)
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- [LOJ2114][HNOI2015]-菜肴制作-拓扑排序+贪心
<题面> 一个蒟蒻的痛苦一天 在今天的节目集训中,麦蒙将带领大家学习9种错误的解题策略 $15\%$算法(看两个就往下走吧) 1> puts("Impossible!&qu ...
- [LeetCode] 207. 课程表(拓扑排序,BFS)
题目 现在你总共有 n 门课需要选,记为 0 到 n-1. 在选修某些课程之前需要一些先修课程. 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1] 给定课程总量 ...
- nyoj349 poj1094 Sorting It All Out(拓扑排序)
nyoj349 http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094 http://poj.org/problem?id=10 ...
- POJ1094 拓扑排序
问题:POJ1094 本题考查拓扑排序算法 拓扑排序: 1)找到入度为0的点,加入已排序列表末尾: 2)删除该点,更新入度数组. 循环1)2)直到 1. 所有点都被删除,则找到一个拓扑 ...
- [poj1094]Sorting It All Out_拓扑排序
Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...
- 有向无环图的应用—AOV网 和 拓扑排序
有向无环图:无环的有向图,简称 DAG (Directed Acycline Graph) 图. 一个有向图的生成树是一个有向树,一个非连通有向图的若干强连通分量生成若干有向树,这些有向数形成生成森林 ...
- POJ 1094 Sorting It All Out (拓扑排序,判断序列是否唯一,图是否有环)
题意:给出n个字符,m对关系,让你输出三种情况: 1.若到第k行时,能判断出唯一的拓扑序列,则输出: Sorted sequence determined after k re ...
- hdoj 4324 Triangle LOVE【拓扑排序判断是否存在环】
Triangle LOVE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
随机推荐
- 通过gulp为requireJs引入的模块添加版本号
由于项目用到requireJs,并且通过gulp来对项目进行统一的管理,为了防止浏览器对文件进行缓存,所以通过gulp为项目中的文件添加版本号. 1.分别安装gulp-rev.gulp-rev-col ...
- SAP中查询用户操作日志的事务码
事务码:STAD 注意:查询的时间跨度范围不要太大,否则会很慢! 事务码:ST03N 工作负载和性能统计
- SharePoint2013的头像显示和读取
前言 有个时候SP的二次开发,需要用代码获取头像显示,如SP基于AD验证,AD有头像属性,做为头像数据来源(因为Exchange和lync的头像也来自AD),说道这里大家都知道有2种办法,一种从A ...
- myeclipse 2015 CI 16发布【附下载】
2015升级版再次来袭! 更新日志: Slack Integration 新版本集成了Slack,你只需要注册一个Slack帐号然后就可以发送和接收代码片段.你甚至不需要登录Slack就可以直接在Ec ...
- Websphere 系列的https证书的配置说明
术语解释 v IHS IBM HTTP Server v WP Websphere portal v WAS Websphere Application Server 系统安装 WAS6.1 安装了例 ...
- iOS 三方库fmdb 的使用
使用fmdb 做本地数据的存储非常方便, 1. github 上搜索fmdb 下载压缩包 导入到工程中 (如果你的mac 有cocoapod 也可以直接通过cocoapod 添加) 2. 以下代码是通 ...
- 小谈KVC中KeyPath的集合运算符
由于知识点比较简单,这里不再陈述一大堆的原理,直入主题. KVC中的集合运算符有以下三类: 1.简单集合运算符:@avg.@sum.@max.@min.@count (只能用在集合对象中,对象属性必须 ...
- iOS9 HTTP 通信报错解决方案
UIWebView *myview = [[UIWebView alloc] initWithFrame:CGRectMake(, , [UIScreen mainScreen].bounds.siz ...
- Android 系统API实现数据库的增删改查和SQLite3工具的使用
在<Android SQL语句实现数据库的增删改查>中介绍了使用sql语句来实现数据库的增删改查操作,本文介绍Android 系统API实现数据库的增删改查和SQLite3工具的使用. 系 ...
- iOS开发之AFN的基本使用
本篇将从四个方面对iOS开发中经常使用到的AFNetworking框架进行讲解: 一.什么是 AFN 二.为什么要使用 AFN 三.AFN 怎么用 三.AFN和ASI的区别 一.什么是 AFN AFN ...