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. Caffe FCN:可视化featureMaps和Weights(C++)、获取FCN结果

    为何不使用C++版本FCN获取最后的分割掩模,何必要使用python呢!因此需要获取网络最后层的featureMaps,featureMaps的结果直接对应了segmentation的最终结果,可以直 ...

  2. Matrix computations in C

    meschach配置使用 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !im ...

  3. Linux 之CentOS7使用firewalld打开关闭防火墙与端口

    一.firewalld的基本使用 启动: systemctl start firewalld 关闭: systemctl stop firewalld 查看状态: systemctl status f ...

  4. strcmp 与 _tcscmp

    strcmp 用来比较ANSI字符串,而_tcscmp用来比较UNICODE(宽字符)的字符串.ANSI字符串中,1个英文字母为1个字节,1个中文字符为2个字节,遇到0字符表示字符串结束.而在UNIC ...

  5. groupbox

    使用groupbox将radiobox 放入其中可以使组框中只选中一个

  6. Sprinboot优雅配置监听,并记录所有启动事件

    在阅读Springboot启动源码的时候,发现Springboot自动启动listeners是通过uopeizhi文件配置的,本文就是采用Springboot方式自动装入listeners. 项目依赖 ...

  7. C# 泛基

    1 你有时候希望在父类规定一些行为,让子类无法修改,但是这些实现是依赖一个子类才能获取的值,你又不可能知道所有的子类 ,没办法替它在父类里面初始化,这时候就需要在父类里面定义一个每个子类一个的,但又是 ...

  8. iptables简单了解

    简介 Iptables是unix/linux自带的一款优秀且开源的基于包过滤的防火墙工具. 怎么用 可以用来做主机防火墙. 可以做局域网共享上网. 可以做ip及端口映射. Iptables工作流程 i ...

  9. Python os.listdir() 方法

    概述 os.listdir() 方法用于返回指定的文件夹包含的文件或文件夹的名字的列表.这个列表以字母顺序. 它不包括 '.' 和'..' 即使它在文件夹中. 只支持在 Unix, Windows 下 ...

  10. Problem 56

    Problem 56 https://projecteuler.net/problem=56 Powerful digit sum A googol (10100) is a massive numb ...