( ̄▽ ̄)"

//判环:当入度为0的顶点==0时,则有环(inconsistency)
//判序:当入度为0的顶点仅为1时,则能得到有序的拓扑排序,否则无序
//边输入边判断,用continue来做到:得出结果后,对后续的输入不作处理
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std; const int MAXN=30;
int g[MAXN][MAXN];
int degree[MAXN],L[MAXN],n,m;
char ch[5]; int toposort()
{
int tot=0,point,in,flag=1;//flag==1:有序,flag==-1:无序(即不确定)
int t[MAXN]; //由于是边输入边判断,所以用t数列来保存当前(0到i)的顶点入度情况,以此避免修改degree数组
for(int i=1;i<=n;i++)
t[i]=degree[i];
for(int i=1;i<=n;i++)
{
in=0;
for(int j=1;j<=n;j++)
if(!t[j])
{
in++;
point=j;
}
if(in==0) return 0;
if(in>1) flag=-1;
L[tot++]=point; //入度为0的点入队
t[point]=-1; //删点
for(int j=1;j<=n;j++)
if(g[point][j]==1)
t[j]--; //删边
}
return flag;
} void init_input_judge_output()
{
int sign=0;
memset(degree,0,sizeof(degree));
memset(L,0,sizeof(L));
memset(g,0,sizeof(g));
for(int i=1;i<=m;i++)
{
scanf("%s",ch);
if(sign) continue;
int a=ch[0]-'A'+1;
int b=ch[2]-'A'+1;
g[a][b]=1;
degree[b]++;
int judge=toposort();
if(judge==0)
{
printf("Inconsistency found after %d relations.\n",i);
sign=1;
}
if(judge==1)
{
printf("Sorted sequence determined after %d relations: ",i);
for(int j=0;j<n;j++)
printf("%c",L[j]+'A'-1);
printf(".\n");
sign=1;
}
}
if(!sign)
printf("Sorted sequence cannot be determined.\n");
} int main()
{
while(scanf("%d%d",&n,&m)&&n+m)
{
init_input_judge_output();
}
return 0;
}

POJ 1094 Sorting It All Out(经典拓扑+邻接矩阵)的更多相关文章

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

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

  2. nyoj 349&Poj 1094 Sorting It All Out——————【拓扑应用】

    Sorting It All Out 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 An ascending sorted sequence of distinct ...

  3. POJ 1094 Sorting It All Out【拓扑排序】

    题目链接: http://poj.org/problem?id=1094 题意: 给定前n个字母的大小关系,问你是否 根据前xxx个关系得到上升序列 所有关系都无法确定唯一的一个序列 第xxx个关系导 ...

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

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

  5. POJ 1094 Sorting It All Out (拓扑排序,判断序列是否唯一,图是否有环)

    题意:给出n个字符,m对关系,让你输出三种情况:     1.若到第k行时,能判断出唯一的拓扑序列,则输出:         Sorted sequence determined after k re ...

  6. POJ 1094 Sorting It All Out 【拓扑排序】

    <题目链接> 题目大意: 对于N个大写字母,给定它们的一些关系,要求判断出经过多少个关系之后可以确定它们的排序或者排序存在冲突,或者所有的偏序关系用上之后依旧无法确定唯一的排序. 解题分析 ...

  7. POJ - 1094 Sorting It All Out(拓扑排序)

    https://vjudge.net/problem/POJ-1094 题意 对于N个大写字母,给定它们的一些关系,要求判断出经过多少个关系之后可以确定它们的排序或者排序存在冲突,或者所有的偏序关系用 ...

  8. POJ 1094 Sorting It All Out【拓扑排序 / 比较字母大小】

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

  9. 题解报告:poj 1094 Sorting It All Out(拓扑排序)

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

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

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

随机推荐

  1. 怎么删除hao.qquu8.com绑定

    运行 输入 regedit 编辑 - 查找  hao.qquu8.com 然后修改成 你想绑定的 主页 就好

  2. C#动态创建两个按钮,btn2复制btn1的Click事件,匿名委托

    现在有一个按钮btn1,要动态创建出一个btn2,需要btn2点击时调用btn1的点击. 在delphi中这种操作很简单:btn2.onClick:=btn1.onClick,因为onClick就是个 ...

  3. 移动APP脚本录制

    1.安装补丁--LR_03105_patch4----mobile app(http/html) 2.录制软件和移动设备同处同一环境(160wifi连接移动设备),创建wifi热点 3.创建脚本-协议 ...

  4. 关于Python的文件IO

    使用Open函数, 第一个参数为文件名, 例如"C:\abc.txt",这里要注意的是r"C:\abc.txt". 第二个参数为文件的操作方式, 这里着重探讨写 ...

  5. sql 查询表共多少列

    1.oracle: select count(*) from user_tab_cols where table_name='表名';--表名含英文的话应为英文大写字母 2.mysql: select ...

  6. AC日记——【模板】字符串哈希 洛谷 3370

    题目描述 如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字.大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串. 友情提醒:如果真的想好好练习哈希的话,请自觉,否则请右转 ...

  7. The property System

    The property System 和其它编译器厂商一样, Qt 也提供了复杂的属性机制, 但是作为一个编译器无关.平台无关的库,Qt没有那些不被标准编译器支持的特征, 如 BCB的 __prop ...

  8. Base64encode

    一直以来Base64的加密解密都是使用sun.misc包下的BASE64Encoder及BASE64Decoder的sun.misc.BASE64Encoder/BASE64Decoder类.这个类是 ...

  9. java学习记录

    1,接口(不实现任何方法)——>抽象类(实现部分公共方法)——>简单实现类——>具体实现类 2,抽象类不能被直接实例化,只能实现抽象方法,以匿名内部类的方式表现. 3,如果stati ...

  10. kindeditor编辑器上传图片失败 错误 405.0解决办法(亲测)

    HTTP 错误 405.0 - Method Not Allowed(省略)editor/php/upload_json.php?dir=image物理路径 http://www.gdgoga.com ...