http://poj.org/problem?id=1094

Sorting It All Out
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 24505   Accepted: 8487

Description

An ascending sorted sequence of distinct values is one in which some form of a less-than operator is used to order the elements from smallest to largest. For example, the sorted sequence A, B, C, D implies that A < B, B < C and C < D. in this problem, we will give you a set of relations of the form A < B and ask you to determine whether a sorted order has been specified or not.

Input

Input consists of multiple problem instances. Each instance starts with a line containing two positive integers n and m. the first value indicated the number of objects to sort, where 2 <= n <= 26. The objects to be sorted will be the first n characters of the uppercase alphabet. The second value m indicates the number of relations of the form A < B which will be given in this problem instance. Next will be m lines, each containing one such relation consisting of three characters: an uppercase letter, the character "<" and a second uppercase letter. No letter will be outside the range of the first n letters of the alphabet. Values of n = m = 0 indicate end of input.

Output

For each problem instance, output consists of one line. This line should be one of the following three: 
Sorted sequence determined after xxx relations: yyy...y.  Sorted sequence cannot be determined.  Inconsistency found after xxx relations. 
where xxx is the number of relations processed at the time either a sorted sequence is determined or an inconsistency is found, whichever comes first, and yyy...y is the sorted, ascending sequence. 

Sample Input

4 6
A<B
A<C
B<C
C<D
B<D
A<B
3 2
A<B
B<A
26 1
A<Z
0 0

Sample Output

Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.

Source

 
【题解】:
  拓扑排序到底就行,注意:
    当遇到两个入度为0的情况,需要判断是否出现环
  唉,不说了,代码写得真挫,乱七八糟,不过能AC
   
【code】:
 /**
Judge Status:Accepted Memory:704K
Time:0MS Language:G++
Code Lenght:2296B Author:cj
*/ #include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm> #define N 30
#define M N*N
using namespace std; int map[N][N],mark[N],in[N],tin[N];
char anstr[N];
int n,flag; void init()
{
memset(map,,sizeof(map));
memset(mark,,sizeof(mark));
memset(in,,sizeof(in));
memset(tin,,sizeof(tin));
} int topCheck(int id) //拓扑排序
{
int i,cnt=,pos=-;
for(i=;i<;i++)
{
if(mark[i]&&!in[i])
{
cnt++;
pos=i;
}
}
if(cnt>) //当有多个入度为0的点
{
flag = ; //标记出现多个入度为0的点的情况
in[pos] = -;
anstr[id] = pos+'A';
for(i=;i<;i++)
{
if(map[pos][i]) in[i]--;
}
return topCheck(id+);
}
if(cnt==)
{
for(i=;i<;i++)
{
if(mark[i]&&in[i]>)
{
return ; //入度都大于0,出现了环
}
}
if(id!=n||flag) return ; //当排序到入度为0时,并且一直没有出现过多个入度为0的情况,则排序可以完成
anstr[id]='\0';
return ;
}
in[pos] = -;
anstr[id] = pos+'A';
for(i=;i<;i++)
{
if(map[pos][i]) in[i]--;
}
return topCheck(id+); //进入下一层拓扑排序
return ;
} int main()
{
int m;
while(~scanf("%d%d",&n,&m))
{
if(!n&&!m) break;
char xx[];
int i;
init();
int temp=,pos=;
for(i=;i<=m;i++)
{
scanf("%s",xx);
int x = xx[]-'A';
int y = xx[]-'A';
mark[x] = mark[y] = ;
tin[y]++;
map[x][y] = ;
memcpy(in,tin,sizeof(int)*);
if(temp!=) continue;
flag = ;
temp = topCheck();
pos = i;
}
if(temp==)
{
anstr[pos+i]='\0';
printf("Sorted sequence determined after %d relations: %s.\n",pos,anstr);
continue;
}
if(temp==)
{
printf("Inconsistency found after %d relations.\n",pos);
continue;
}
puts("Sorted sequence cannot be determined.");
}
return ;
}

poj 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. [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 ...

  3. 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 ...

  4. poj 1094 Sorting It All Out_拓扑排序

    题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...

  5. POJ 1094 Sorting It All Out 拓扑排序 难度:0

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

  6. PKU 1094 Sorting It All Out(拓扑排序)

    题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. 是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.判断该序列是否唯一: 3.该序列字母次序之间 ...

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

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

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

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

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

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

随机推荐

  1. # li鼠标移入移出,点击,变背景色,变checkbox选中状态

    移入移出背景色改变和点击背景色改变,两者是否相互覆盖? 若移出背景色恢复,影响点击事件的背景色改变,会产生效果为: 点击时,背景色改变,并且checkbox选中 鼠标移开后,checkbox仍选中,但 ...

  2. CSS3—六边形

    整理了2种方法,看完肯定觉得超简单~ 一.旋转型 话不多说先看下需要的样式: 1.transform:rotate(angle) 2.overflow 3.visibility 效果:演示效果,run ...

  3. Contoso 大学 - 9 - 实现仓储和工作单元模式

    原文 Contoso 大学 - 9 - 实现仓储和工作单元模式 By Tom Dykstra, Tom Dykstra is a Senior Programming Writer on Micros ...

  4. ios 微信细节

    1.登录后,下次登录保存其用户名. * 官方的登录实现          * 1.把用户名和密码放在沙盒 NSString *user = self.userField.text;    NSStri ...

  5. 在ASP.NET中实现压缩多个文件为.zip文件,实现批量下载功能 (转载并优化处理篇)

    转自:http://blog.csdn.net/yanlele424/article/details/6895986 这段时间一直在做一个网站,其中遇到了一个问题,就是在服务器端压缩多个服务器端的文件 ...

  6. Hibernate 拥有 Mybits 的SQL/HQL特性 (注解、XML两不误)

        第一次写博客.文章有点渣,喜欢就看看,不喜欢路过点个赞.     效果:直接一条语句多种用法     FROM User A    WHERE    1=1    <#if id??&g ...

  7. c#基础学习汇总----------base和this,new和virtual

    base和this是c#中的两访问关键字,目的是用于实现继承机制的访问操作,来满足对对象成员的访问,从而为多态机制提供更加灵活的处理方式. 在看<你必须知道的.Net>一书中有一个例子很好 ...

  8. Excel 数据分析技巧

    分享一个小技巧,Excel中,统计数据后,根据数据点之间的趋势,描绘出大致的曲线图,并且得到对于的公式. 1. 给出示例数据 2. 插入->散点图,右键点,选择添加趋势线,可以根据点数的走向,来 ...

  9. Qt绘制异形窗体

    异形窗体即不规则窗体,一般采用png图片,一般绘制异形窗体分两步: 1.设置遮罩区 2.绘制图片   使用png图片的透明部分作为遮罩区,然后绘制图片,这样我们就看到一个只绘制了非透明部分的图形,废话 ...

  10. asp.net 中使用less

    首先 ,需要知道 whats the less; 实际上less 只是针对css比较难于维护和抽象这种现象,而创造的一个工具. 然后,在抛开语言环境的情况下(例如.net 是vs环境,java是ecl ...