http://poj.org/problem?id=1094

#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
int in[27],out[27];
char index[27];
bool vis[27];
int mem[27][27];
int n,m;
int tlen;
bool floyd(){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
for(int k=0;k<n;k++){
if(mem[i][k]&&mem[k][j])mem[i][j]=1;
}
}
}
for(int i=1;i<n;i++){
if(mem[i][i])return false;
}
return true;
}
bool calc(){
memset(in,0,sizeof(in));
memset(out,0,sizeof(out));
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(mem[i][j]==1){
out[i]++;
in[j]++;
}}
}
for(int i=0;i<n;i++){
if(in[i]+out[i]!=n-1)return false;
}
return true;
}
bool topologicalsort(){
memset(vis,0,sizeof(vis));
int len=0;
bool fl=false;
while(len<n){
fl=false;
for(int i=0;i<n;i++){
if(in[i]==0&&!vis[i]){
fl=true;
vis[i]=true;
index[len]=i+'A';
len++;
for(int j=0;j<n;j++){
if(mem[i][j])in[j]--;
}
break;
}
}
if(!fl)return false;
}
return true;
}
int main(){
char ch,ch2;
while (scanf("%d %d",&n,&m)==2&&n){
bool fl=false;
getchar();
memset(mem,0,sizeof(mem));
memset(index,0,sizeof(index));
for(int i=1;i<=m;i++){
scanf(" %c< %c",&ch,&ch2);
in[ch2-'A']++;
out[ch-'A']++;
mem[ch-'A'][ch2-'A']=1;
getchar();
if(fl)continue;
if(floyd()){
if(calc()){
if(topologicalsort()){
fl=true;
index[n]=0;
printf("Sorted sequence determined after %d relations: %s.\n",i,index);
continue;
}
}
}
else {
printf("Inconsistency found after %d relations.\n",i);
fl=true;
continue;
}
}
if(!fl)printf("Sorted sequence cannot be determined.\n");
}
return 0;
}

  

POJ 1094 Sorting It All Out 拓扑排序 难度:0的更多相关文章

  1. ACM: poj 1094 Sorting It All Out - 拓扑排序

    poj 1094 Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & ...

  2. poj 1094 Sorting It All Out (拓扑排序)

    http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  3. [ACM_模拟] POJ 1094 Sorting It All Out (拓扑排序+Floyd算法 判断关系是否矛盾或统一)

    Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...

  4. POJ 1094 Sorting It All Out (拓扑排序) - from lanshui_Yang

    Description An ascending sorted sequence of distinct values is one in which some form of a less-than ...

  5. poj 1094 Sorting It All Out_拓扑排序

    题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...

  6. PKU 1094 Sorting It All Out(拓扑排序)

    题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. 是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.判断该序列是否唯一: 3.该序列字母次序之间 ...

  7. POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39602   Accepted: 13 ...

  8. [ACM] POJ 1094 Sorting It All Out (拓扑排序)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26801   Accepted: 92 ...

  9. POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29984   Accepted: 10 ...

随机推荐

  1. 十七、Java基础---------集合框架之Map

    前两篇文章中介绍了Collection框架,今天来介绍一下Map集合,并用综合事例来演示. Map<K,V> Map<K,V>:Map存储的是键值对形式的元素,它的每一个元素, ...

  2. Eclipse默认标签TODO,XXX,FIXME和自定义标签[转]

    http://www.blogjava.net/Guides/archive/2011/11/14/363686.html   Eclipse中的一些特殊的注释技术包括:    1.    // TO ...

  3. Bellman-Ford

    看来一千个acmer有一千个迪杰斯特拉,Bellman-Ford也是一样. 看了刘汝佳的bellman-ford,简直和spfa一模一样啊!!! 松弛n -1 次还是可以松弛,说明有负环; 刘汝佳写得 ...

  4. Java开发中经典的小实例-( 鸡蛋0.1元一个,鸭蛋3元一个,鹅蛋6元一个。求一百元买一百个蛋。)

    public class Test24 {    public static void main(String[] args) {        // 鸡蛋0.1元一个,鸭蛋3元一个,鹅蛋6元一个.求 ...

  5. JSP 基础语法

    1.JSP简介 含义:是运行在服务器端的java页面,是动态网页技术,开发时采用html嵌套java代码的方式实现的 JSP的执行流程是什么? 翻译阶段:web服务器收到jsp请求时,把jsp文件翻译 ...

  6. Key Figure、Exception Aggreagion、Non-Cumulative KeyFigure

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  7. 16s及宏基因组测序公司资源--20161104

    锐羿基因: http://www.realbio.cn/index.php?c=list&cs=keyandongtai& 科研动态更新及时 青岛过程能源所: http://www.c ...

  8. [bzoj2124]等差子序列(hash+树状数组)

    我又来更博啦     2124: 等差子序列 Time Limit: 3 Sec  Memory Limit: 259 MBSubmit: 941  Solved: 348[Submit][Statu ...

  9. Claims Identity

    using System;using System.Collections.Generic;using System.Linq;using System.Security.Claims;using S ...

  10. iOS - Mac Apache WebServer 服务器配置

    前言 Apache 是目前使用最广的 Web 服务器,可以支持各种脚本的执行. Mac 系统自带,无需单独安装,只需要修改几个配置就可以,简单,快捷. 有些特殊的服务器功能,Apache 都能很好的支 ...