poj1094 拓扑 Sorting It All Out
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 29744 | Accepted: 10293 |
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
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int map[100][100];
int m,n;
int tindegree[100],indegree[100];
char str[5];
char s[39];
int toposort(){
bool flag=true;
memset(tindegree,0,sizeof(tindegree));
memset(s,0,sizeof(s));
for(int i=1;i<=n;i++){
tindegree[i]=indegree[i];
}
for(int i=1;i<=n;i++){
int sum=0,k;
for(int j=1;j<=n;j++){
if(!tindegree[j]){
k=j;
sum++;
}
}
if(sum==0){///入度为0,则所剩图为一个环,无法判断了。直接可以返回,
return -1;
}
if(sum>1){///当出现入度为0的点有多个时候,可能判断不了,但是也不能直接返回0,因为带环循环会优先于
///无法判断这种情况,所以需要继续执行该函数,看还有没有死循环为环的情况
flag=false;
}
s[i-1]=k+'A'-1;
tindegree[k]--;
for(int z=1;z<=n;z++){
if(map[k][z]){
tindegree[z]--;
}
} }
s[n]='\0';///s字符串结束标志
if(flag==false)
return 0;
return 1;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
if(n==0&&m==0)
break;
memset(map,0,sizeof(map));
memset(indegree,0,sizeof(indegree));
memset(str,0,sizeof(str));
memset(s,0,sizeof(s));
int ans=2;
int temp;
bool flag=true;
for(int i=1;i<=m;i++){
scanf("%s",str);
if(flag==false)
continue;
int u=str[0]-'A'+1;
int v=str[2]-'A'+1;
if(!map[u][v]){
map[u][v]=1;
indegree[v]++;
}
ans=toposort();
if(ans==-1||ans==1){///此判断语句特别注意,只能记录状态,不能退出,需要继续读边
temp=i;
flag=false;
}
}
if(ans==1)
printf("Sorted sequence determined after %d relations: %s.\n",temp,s);
else if(ans==-1)
printf("Inconsistency found after %d relations.\n",temp);
else if(ans==2)
printf("Sorted sequence cannot be determined.\n"); }
return 0;
}
poj1094 拓扑 Sorting It All Out的更多相关文章
- POJ1094 拓扑排序
问题:POJ1094 本题考查拓扑排序算法 拓扑排序: 1)找到入度为0的点,加入已排序列表末尾: 2)删除该点,更新入度数组. 循环1)2)直到 1. 所有点都被删除,则找到一个拓扑 ...
- POJ1094——拓扑排序和它的唯一性
比较模板的topological-sort题,关键在于每个元素都严格存在唯一的大小关系,而一般的拓扑排序只给出一个可能解,这就需要每趟排序的过程中监视它是不是总坚持一条唯一的路径. 算法导论里面的拓扑 ...
- poj1094拓扑排序
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29539 Accepted: 10 ...
- poj1094 拓扑排序(出度入度简单使用)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 37764 Accepted: 13 ...
- poj1094 拓扑序
题意:现在有多个大写字母(不一定连续),给出字母之间的大小关系,问到第几个关系时就能判断有唯一大小排序或出现矛盾,或是有多个合理排序,若有唯一排序,则输出它. 拓扑序,只不过坑爹的是如果关系处理到一半 ...
- [poj1094]Sorting It All Out_拓扑排序
Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...
- nyoj349 poj1094 Sorting It All Out(拓扑排序)
nyoj349 http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094 http://poj.org/problem?id=10 ...
- POJ1094 Sorting It All Out —— 拓扑排序
题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Tot ...
- POJ1094 Sorting It All Out(拓扑排序)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 30110 Accepted: 10 ...
随机推荐
- [bzoj 1026]windy数(数位DP)
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1026 分析: 简单的数位DP啦 f[i][j]表示数字有i位,最高位的数值为j的windy数总 ...
- IOS 遍历未知对象的属性和方法
/* 注意:要先导入ObjectC运行时头文件,以便调用runtime中的方法*/ #import <objc/runtime.h> @implementation NSObject (P ...
- 2012杀毒软件排行榜TOP10强
2012杀毒软件排行榜TOP10强 1:avast!杀毒软件 来自捷克的avast!,已有数十年的历史,它在国外市场一直处于领先地位.avast!分为家庭版.专业版.家庭网络特别版.和服务 ...
- 关于Jquery 操作Cookie 取值错误
使用JQuery操作cookie时 发生取的值不正确的问题: 结果发现cookie有四个不同的属性: 名称,内容,域,路径 $.cookie('the_cookie'); // 读取 cookie $ ...
- poj3522 kruskal+枚举
题目的意思是求构成生成树的边的最大边和最小边的差最小.枚举即可 #include<stdio.h> #include<string.h> #include<algorit ...
- Java设计模式-建造者模式(Builder)
将一个复杂的构建与其表示相分离,使得同样的构建过程可以创建不同的表示. [构建与表示分离,同构建不同表示] 与抽象工厂的区别:在建造者模式里,有个指导者,由指导者来管理建造者,用户是与指导者联系的,指 ...
- 解决vs2015使用fopen、fprintf等函数报错的问题
出现错误提示: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable ...
- bzoj 3437 斜率优化DP
写题解之前首先要感谢妹子. 比较容易的斜率DP,设sum[i]=Σb[j],sum_[i]=Σb[j]*j,w[i]为第i个建立,前i个的代价. 那么就可以转移了. /**************** ...
- spring 第一篇(1-1):让java开发变得更简单(下)
切面(aspects)应用 DI能够让你的软件组件间保持松耦合,而面向切面编程(AOP)能够让你捕获到在整个应用中可重用的组件功能.在软件系统中,AOP通常被定义为提升关注点分离的一个技术.系统由很多 ...
- 连通性2 无向图的割边 (cut edge)
这是DFS系列的第二篇 割边的概念 In graph theory, a bridge, isthmus, cut-edge, or cut arc is an edge of a graph who ...