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

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

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. hrbustoj 1104:Leyni, LOLI and Line(解析几何,斜截式的应用)

    Leyni, LOLI and Line Time Limit: 1000 MS    Memory Limit: 65536 K Total Submit: 181(54 users)   Tota ...

  2. 安装wampserver时提示丢失MSVCR110.dll

    安装Wampserver 2后启动的时候提示系统错误:MSVCR110.dll丢失. 在wampserver官网上有例如以下提示: 于是卸载原来的WAMPSERVER 2 ,在http://www.m ...

  3. WebService远程调用(命令行调用)

    远程调用webservice 方法, 通过wsdl命令,生成webservice 对应asmx 文件的对应类(cs文件),然后将此cs文件引用到当前项目中,项目其他地方需要调用webservice方法 ...

  4. IOS实现打电话后回调

    本文转载至 http://blog.csdn.net/cerastes/article/details/38340687   UIWebView *callWebview =[[UIWebView a ...

  5. 【BZOJ1116】[POI2008]CLO 并查集

    [BZOJ1116][POI2008]CLO Description Byteotia城市有n个 towns m条双向roads. 每条 road 连接 两个不同的 towns ,没有重复的road. ...

  6. 【BZOJ1412】[ZJOI2009]狼和羊的故事 最小割

    [BZOJ1412][ZJOI2009]狼和羊的故事 Description “狼爱上羊啊爱的疯狂,谁让他们真爱了一场:狼爱上羊啊并不荒唐,他们说有爱就有方向......” Orez听到这首歌,心想: ...

  7. Javascript-const 常量

    const 常量 常量是块级作用域,很像使用 let语句定义的变量.常量的值不能通过重新赋值来改变,并且不能重新声明. 此声明创建一个常量,其作用域可以是全局或本地声明的块. 与var变量不同,全局常 ...

  8. BSSID,SSID,ESSID区别

    SSID(Service Set Identifier)   SSID,AP唯一的ID码,许多人认为可以将SSID写成ESSID,其实不然,SSID是个笼统的概念,包含了ESSID和BSSID,用来区 ...

  9. python学习笔记(二)— 列表(list)

    列表也叫数组,列表定义,使用[]即可:列表里面可以再套列表,一个里面套一个列表,叫二维数组:一个里面套一个列表,里面的列表再套一个列表,这个叫三维数组,套几层就是几维,定义格式如下: list1 = ...

  10. c++主程这种事情,就是这样,看人先看人品,没人品,他的能力与你何关?

    这就是人品的重要性........ 接手别人的代码,说困难,也困难,说容易也容易 想把别人代码都读通,理顺,在改原代码BUG,在完美的加功能,那项目越大,越难 想把别人代码里面,加点坑,随便找个模块, ...