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 ...
随机推荐
- Eclipse spket插件 内置js文件
这一篇将怎么在spket内置js文件,而不用用户自己去添加. 1. 在开发的Eclipse的 运行配置将下面几个插件勾选上. 2. 在org.eclipse.ui.startup拓展里执 ...
- Weinre调试移动端页面
Weinre是什么 如果我们做的是Cordova(phonegap)或其他hybird应用,当使用到原生功能时候(类似原生请求数据或页面切换时),没办法在PC chrome浏览器调试页面,一旦页面在手 ...
- 浅析css布局模型1
css是网页的外衣,好不好看全凭css样式,而布局是css中比较重要的部分,下面来分析一下常见的几种布局. 流动模型 流动模型是网页布局的默认模式,也是最常见的布局模式,他有两个特点: 1.块状元素都 ...
- arcmap Command
The information in this document is useful if you are trying to programmatically find a built-in com ...
- Sharepoint学习笔记—习题系列--70-576习题解析 -(Q75-Q77)
Question 75You are designing a feature for a SharePoint 2010 solution that will be activated by defa ...
- Mac本“安全性与隐私”里没有“任何来源”选项
打开"偏号设置"----->"安全性与隐私"----->"通用",里面没有"任何来源",怎么解决? 如果需要 ...
- 操作系统开发系列—10.内核HelloWorld ●
a.我们先来体验一下在Linux下用汇编编程的感觉,见代码 [section .data] ; 数据在此 strHello db "Hello, world!", 0Ah STRL ...
- 修改 Android 5.x 系统默认音量大小
修改系统默认音量需要改两处地方: 1. frameworks\base\media\java\android\media\AudioManager.java /** @hide Default vol ...
- iOS开发之Runtime使用
runtime简介 RunTime简称运行时.OC就是运行时机制,也就是在运行时候的一些机制,其中最主要的是消息机制. 对于C语言,函数的调用在编译的时候会决定调用哪个函数. 对于OC的函数,属于动态 ...
- C# sql语句拼接时 like情况的防sql注入的用法
今天下午同事问我一个比较基础的问题,在拼接sql语句的时候,如果遇到Like的情况该怎么办. 我原来的写法就是简单的拼接字符串,后来同事问我如果遇到sql注入怎么办.我想了下,这确实是个问题. 刚在网 ...