题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列。

是典型的拓扑排序,但输出格式上确有三种形式:

1.该字母序列有序,并依次输出;

2.判断该序列是否唯一

3.该序列字母次序之间是否有矛盾,即是否有环存在;

而这三种形式的判断是有顺序的:先判断(3)是否有环,再判断是否有序(1),最后才能判断是否能得出结果(2)。注意:对于(2)必须遍历完整个图,而(1)和(3)一旦得出结果,对后面的输入就不用做处理了。

#include<cstdio>
#include<cstring>
char alp[];
int edge[][],in[];
int TopoSort(int n)
{
int t=,tmp[],flag=;
for(int i=;i<=n;i++)
tmp[i]=in[i];
for(int i=;i<=n;i++){
int in0=,loc;//查找入度为零的顶点个数
for(int j=;j<=n;j++){
if(tmp[j]==){
in0++;
loc=j;
}
}
if(in0==) return ;//有环,那么后面的就不用再判断了
if(in0>) flag=-;//不确定,但不能直接return -1;因为后面的字母中还可能形成环而return 0;
alp[t++]='A'+loc-;//入度为零的字母入队
tmp[loc]=-;
for(int k=;k<=n;k++)
if(edge[loc][k]==)
tmp[k]--;
}
return flag;
}
//flag=1:有序 flag=-1:不确定
int main()
{
int m,n,x,y;
char str[];
while(scanf("%d%d",&n,&m),n|m){
memset(edge,,sizeof(edge));
memset(in,,sizeof(in));
bool sign=;
for(int i=;i<=m;i++){
scanf("%s",str);
if(sign) continue; //一旦得出结果,对后续的输入不做处理,但是不能break,因为还得继续输入
x=str[]-'A'+;
y=str[]-'A'+;
edge[x][y]=;
in[y]++;
int flag=TopoSort(n);
if(flag==){//有环
printf("Inconsistency found after %d relations.\n",i);
sign=;
}
if(flag==){//有序
printf("Sorted sequence determined after %d relations: ",i);
for(int j=;j<n;j++)
printf("%c",alp[j]);
printf(".\n");
sign=;
}//当sign=1时表明已得出结果
}
if(!sign) //不确定
printf("Sorted sequence cannot be determined.\n");
}
return ;
}

PKU 1094 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. [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. POJ 1094 Sorting It All Out 拓扑排序 难度:0

    http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...

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

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

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

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

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

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

随机推荐

  1. 对Python线程池

    本文对Python线程池进行详细说明介绍,IDE选择及编码的解决方案进行了一番详细的描述,实为Python初学者必读的Python学习经验心得. AD: 干货来了,不要等!WOT2015 北京站演讲P ...

  2. Python pycurl使用

    pycurl的学习 (2013-09-26 10:40:31) 转载▼   分类: python pycurl的使用 pycurl是curl的一个python版本. pycurl的使用说明: pycu ...

  3. VC++ GetSafeHwnd用法

    GetSafeHwnd HWND GetSafeHwnd() const; 当我们想得到一个窗口对象(CWnd的派生对象)指针的句柄(HWND)时,最安全的方法是使用GetSafeHwnd()函数. ...

  4. Oracle的优化器的RBO和CBO方式

      1.基于规则的优化方式(Rule-Based Optimization,简称为RBO)       优化器在分析SQL语句时,所遵循的是Oracle内部预定的一些规则,对数据是不敏感的.它只借助少 ...

  5. 转:解决Python2.7的UnicodeEncodeError: ‘ascii’ codec can’t encode异常错误

    操作SQL数据库,Python使用的是版本2.7,但是在运行的时候出现了异常错误UnicodeEncodeError:'ascii' codec can't encode characters in ...

  6. C#中文件和byte[]互换问题

    如何将图片和声音转化成byte[],并通过webservice进行传输?    如何将webservice传输过来的byte[],转化成我们想要的文件?    (一)文件转化为byte[]    方法 ...

  7. 让asp.net程序在修改web.config后不重启

    默认情况下web.config修改后,asp.net程序会被重启.为了防止程序重启,要修改machine.config 文件. machine.config默认在C:\WINDOWS\Microsof ...

  8. (转)Java DecimalFormat 用法(数字格式化)

    我们经常要将数字进行格式化,比如取2位小数,这是最常见的.Java 提供 DecimalFormat 类,帮你用最快的速度将数字格式化为你需要的样子.下面是常用的例子: import java.tex ...

  9. Tortoise SVN 使用

    1.添加文件或文件夹 2.删除文件或文件夹 ①If you want to delete an item from the repository, but keep it locally as an ...

  10. Nginx应用-Location路由反向代理及重写策略 请求转发-URL匹配规则 NGINX Reverse Proxy

    NGINX Docs | NGINX Reverse Proxy https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/ ...