( ̄▽ ̄)"

//判环:当入度为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. shell-正则表达式

    证则表达式:在计算机科学中,是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串.在很多文本编辑器或其他工具里,正则表达式通常被用来检索和/或替换那些符合某个模式的文本内容.许多程序设计 ...

  2. java基础:int和integer区别

    int是基础数据类型: integer是包装类,里面包含一些基础的方法,最常见的就是数据转换: 比如int转String: int a=0: String b=Integer.toString(a):

  3. 关于_cmd

    实际上是该方法名(@selector的名称). 比如: - (void)someCategoryMethod { NSString *string = objc_getAssociatedObject ...

  4. 梅特卡夫法则(Metcalfe's law)

    如果一个网络中有n个人,那么网络对于每个人的价值与网络中其他人的数量成正比,于是网络对于所有人的总价值与n*(n-1)成正比.

  5. [转] 你真的会写单例模式吗——Java实现

    你真的会写单例模式吗——Java实现 原文:http://www.tuicool.com/articles/MBrUfy6 单例模式可能是代码最少的模式了,但是少不一定意味着简单,想要用好.用对单例模 ...

  6. action解耦方式

    ServletAction方式,必须要有Servlet容器作支持 package com.hanqi.action; import javax.servlet.ServletContext; impo ...

  7. html绑定

    目的 html绑定可以绑定DOM元素内的HTML内容. 示例: <div data-bind="html: details"></div> <scri ...

  8. dpkg -P <pkg>

    http://www.linuxquestions.org/questions/debian-26/how-do-i-get-rid-of-those-rc-packages-as-seen-in-d ...

  9. textview设置不同字体大小

    <style name="style0"> <item name="android:textSize">19dip</item&g ...

  10. HDU 5130 Signal Interference(计算几何 + 模板)

    HDU 5130 Signal Interference(计算几何 + 模板) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=5130 Descripti ...