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

1.判断所给关系是否为合法的拓扑序列,若存在环,输出

"Inconsistency found after %d relations."但仍继续输入。

2.判断是否可以确定序列,若不可以确定,继续输入,若可以确定输出

"Sorted sequence determined after %d relations: %s."后,继续输入数据。

3.若输入完数据仍不可以确定序列,输出"Sorted sequence cannot be determined.".

 #include<stdio.h>
#include<string.h>
const int N=;
int map[N][N],in[N],vis[N];
int n,m,flag,top;
char s[N];
void topo()
{
int cnt,i,j,pos;
top = ;
int d[N];//存每个点的度数
flag = ;
memset(vis,,sizeof(vis));
memcpy(d,in,n*sizeof(int));
for (i = ; i < n; i ++)
{
cnt = ;
for (j = ; j < n; j ++)
{
if (d[j]==)
{
cnt++;
pos = j;
}
}
if (cnt==)//存在环
{
flag = ;
return ;
}
if (cnt > )//不确定序列
{
flag = ;
}
--d[pos];
s[top++] = pos+'A';
for (int k = ; k < n; k ++)
{
if(map[pos][k]==)
--d[k];//更新度数
}
}
s[top++] = '\0'; }
int main()
{
while(~scanf("%d%d%*c",&n,&m))
{
if (n== && m==)
break;
flag = ;
memset(in,,sizeof(in));
memset(map,,sizeof(map));
char a,b,c;
int i;
for (i = ; i <= m; i ++)
{
scanf("%c%c%c%*c",&a,&b,&c);
if (flag)//控制输入
continue;
if (!map[a-'A'][c-'A'])
{
map[a-'A'][c-'A'] = ;
in[c-'A']++;
}
topo();
if (flag==)
{
flag = ;//以便能继续输入下面的点
printf("Inconsistency found after %d relations.\n",i);
}
else if (flag==)
{
printf("Sorted sequence determined after %d relations: %s.\n",i,s);
}
else if (flag==)//无法确定序列,继续输入
{
flag = ;//继续拓扑
}
}
if (flag==)//输入完也无法确定序列
printf("Sorted sequence cannot be determined.\n");
}
return ;
}

Sorting It All Out(拓扑排序)的更多相关文章

  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. [poj1094]Sorting It All Out_拓扑排序

    Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...

  4. POJ1094 Sorting It All Out —— 拓扑排序

    题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Tot ...

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

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

  6. [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 ...

  7. nyoj349 poj1094 Sorting It All Out(拓扑排序)

    nyoj349   http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094   http://poj.org/problem?id=10 ...

  8. 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 ...

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

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

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

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

随机推荐

  1. Twisted web开发教程

    最近在网上看到一篇twisted web开发文章,将它实践了一下,twisted 提供基本的url路由 和 控制器,模板与模型需要外部扩展 1.目录浏览 2.get请求 3.url路由 4.接受带参数 ...

  2. seam remote 返回的map结构

    map结构的数据,js接收到的结构是elements下面的一个 [ {key:***,value:***}, {key:***,value:***} ] 这样子的集合,需要经过下面代码的转换才能重新变 ...

  3. swift 关键字willSet 和 didSet

    // 下面是苹果给出的解释,就是在给属性设置新值的时候,可以在设置前和设置后做一些处理,这两个关键字就好像对该属性变化的监控 If you don’t need to compute the prop ...

  4. 好用的JS数字格式化

    /* *js格式化数字代码 * *value: 要格式化的数字值 *scale: 最多保留几位小数 *zeroed: 是否保留尾0 *percented: 是否转称百分比形式 * */ functio ...

  5. Vivaldi解决flash插件问题

    Vivaldi浏览器 Vivaldi是Opera联合创始人.前CEO谭咏文(Jon von Tetzchner)2015年1月27日发布的一款浏览器,具有个性化的笔记功能,带有迷你屏幕截图的书签,以及 ...

  6. 37.分组聚合操作—其他metric

    课程大纲     要学其他的metric(count,avg,max,min,sum) count:bucket,terms,自动就会有一个doc_count,就相当于是count avg:avg a ...

  7. iOS学习笔记18-CoreData你懂的

    一.CoreData介绍 CoreData是iOS5之后新出来的的一个框架, 是对SQLite进行一层封装升级后的一种数据持久化方式. 它提供了对象<-->关系映射的功能,即能够将OC对象 ...

  8. nyoj 309

    #include<stdio.h> #include<stdlib.h> #define N 1100 int c[N]; int main() { int l,n,i,m,a ...

  9. 09springMVC对ajax的支持

    u  最直接的Ajax处理 u  数据绑定@RequestBody/@ResponseBody u  使用@RequestBody/@ResponseBody来支持Ajax u  使用HttpEnti ...

  10. 08springMVC拦截器

    u  概述 u  拦截器接口 u  拦截器适配器 u  运行流程图 u  拦截器HelloWorld u  常见应用之性能监控 1      概述 1.1    简介     Spring Web M ...