Poj/OpenJudge 1094 Sorting It All Out
1.链接地址:
http://poj.org/problem?id=1094
http://bailian.openjudge.cn/practice/1094
2.题目:
Sorting It All Out
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25547 Accepted: 8861 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 0Sample Output
Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.Source
3.思路:
4.代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib> using namespace std; int n,m;
int map[][];
int reg[];
int in[],out[];
char ans[];
int stack[];
void fun()
{
int i,j;
for(i='A',j=;i<='Z';i++,j++)reg[i]=j;
}
void toposort(char *ans)
{
int i,top=,u,s=;
for(i=;i<=n;i++)
if(in[i]==)stack[top++]=i;
while(top!=)
{
u=stack[--top];
ans[s++]=u+;
for(i=;i<=n;i++)
{
if(map[u][i])
{
in[i]--;
if(!in[i])stack[top++]=i;
}
}
}
ans[s]=;
}
int main()
{
int i,j,x,y,k,flag1,flag2,flag;
fun();
char ch[];
while()
{
flag1=flag2=;
memset(map,,sizeof(map));
scanf("%d%d",&n,&m);
if(n==&&m==)break;
for(i=;i<=m;i++)
{
flag=;
scanf("%s",ch);
x=reg[ch[]];
y=reg[ch[]];
map[x][y]=;
if(x==y)flag1=i;
memset(in,,sizeof(in));
memset(out,,sizeof(out));
if(!flag1&&!flag2)
for(j=;j<=n;j++)
for(k=;k<=n;k++)
{
if(j!=x&&k!=y)map[j][k]=map[j][k]||(map[j][x]&&map[y][k]);
if(j==x&&k!=y)map[j][k]=map[j][k]||map[y][k];
if(j!=x&&k==y)map[j][k]=map[j][k]||map[j][x];
if(map[j][k])
{
out[j]++;
in[k]++;
}
}
j=;
if(!flag1)
for(j=;j<=n;j++)
{
if(map[j][j])flag1=i;
if(in[j]+out[j]!=n-)flag=;
}
if(flag&&!flag2&&j>n){flag2=i;toposort(ans);}
}
if(flag2)
{
printf("Sorted sequence determined after %d relations: %s.\n",flag2,ans);
continue;
}
if(flag1)
{
printf("Inconsistency found after %d relations.\n",flag1);
continue;
}
printf("Sorted sequence cannot be determined.\n");
}
return ;
}
Poj/OpenJudge 1094 Sorting It All Out的更多相关文章
- 【POJ】1094 Sorting It All Out(拓扑排序)
http://poj.org/problem?id=1094 原来拓扑序可以这样做,原来一直sb的用白书上说的dfs............ 拓扑序只要每次将入度为0的点加入栈,然后每次拓展维护入度即 ...
- ACM: poj 1094 Sorting It All Out - 拓扑排序
poj 1094 Sorting It All Out Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & ...
- poj 1094 Sorting It All Out (拓扑排序)
http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- poj 2388 insert sorting
/** \brief poj 2388 insert sorting 2015 6 12 * * \param * \param * \return * */ #include <iostrea ...
- POJ 1094 Sorting It All Out 拓扑排序 难度:0
http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...
- poj 1094 Sorting It All Out(图论)
http://poj.org/problem?id=1094 这一题,看了个大牛的解题报告,思路变得非常的清晰: 1,先利用floyd_warshall算法求出图的传递闭包 2,再判断是不是存在唯一的 ...
- poj 1094 Sorting It All Out 解题报告
题目链接:http://poj.org/problem?id=1094 题目意思:给出 n 个待排序的字母 和 m 种关系,问需要读到第 几 行可以确定这些字母的排列顺序或者有矛盾的地方,又或者虽然具 ...
- POJ 1094 Sorting It All Out【拓扑排序】
题目链接: http://poj.org/problem?id=1094 题意: 给定前n个字母的大小关系,问你是否 根据前xxx个关系得到上升序列 所有关系都无法确定唯一的一个序列 第xxx个关系导 ...
- poj.1094.Sorting It All Out(topo)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28762 Accepted: 99 ...
随机推荐
- The Sorrows of Young Werther
The Sorrows of Young Werther J.W. von Goethe Thomas Carlyle and R.D. Boylan Edited by Nathen Haskell ...
- 软件测试之WEB测试经典总结
在Web工程过程中,基于Web系统的测试.确认和验收是一项重要而富有挑战性的工作.基于Web的系统测试与传统的软件测试不同,它不但需要检查和验证是否按照设计的要求运行,而且还要测试系统在不同用户的浏览 ...
- 用RelativeLayout布局可以在imageview中写上文字
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- CAS Tomcat实现单点登录
转贴: http://www.cnblogs.com/ja-net/archive/2012/07/25/2608536.html 最近这两天在搞单点登录,第一次使用老出状况.以下是配置过程: 1.安 ...
- 【48】认识template元编程
1.TMP(template metaprogramming),模版元编程有两个效力:第一,它让某些事情更容易:第二,可将工作从运行期转移到编译期.
- Aizu 2302 On or Off dfs/贪心
On or Off Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.act ...
- 【Android开发】完美解决Android完全退出程序
背景:假说有两个Activity, Activity1和Activity2, 1跳转到2,如果要在2退出程序,一般网上比较常见的说法是用 System.exit(0) 或是 android.os.Pr ...
- Xcode中的Info.plist字段列表详解
Info.plist用于向iOS提供关于app,bundle或者framework的一些重要信息.它指定了比如一个应用应该怎样启动,它如何被本地化,应用的名称,要显示的图标,还有更多.Info.pli ...
- C 双向链表
单链表的结点都只有一个指向下一个结点的指针 单链表的数据元素无法直接访问其前驱元素 逆序访问单链表中的元素是极其耗时的操作! len = LinkList_Length(list); for (i=l ...
- IIS 之 添加MIME扩展类型及常用的MIME类型列表
经常用IIS作为下载服务器的时候有时传上去的文件比如 example.mp4 文件名上传后,但是用http打开的时候确显示为 404 文件不存在.其实是IIS对文件的一种保护,不在IIS指定的MIME ...