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 ...
随机推荐
- Android 上拉加载更多功能
前几天看了github上面的例子,参照它的实现,自己又稍微改了一点,往项目里面增加了一个上拉加载更多功能.具体的实现如下: 首先要重写ListView: import android.content. ...
- 如何让OpenSSL得到JKS格式的keystore中的public and private key
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- 使用hexdump 查看二进制文件
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html 内部邀请码:C8E245J (不写邀请码,没有现金送) 国 ...
- 【转】Android studio 导入github工程
http://blog.csdn.net/feixiaku/article/details/45155587/ 从github下载两个开源项目: PagerSlidingTabStrip | ...
- 最近看了点C++,分享一下我的进度吧!
#include <iostream> #include <cmath> #include <iomanip> using namespace std; //Stu ...
- Python 对新浪微博的博文元素 (Word, Screen Name)的频率分析
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-7-9 @author: guaguastd @name: we ...
- c# 调用 c++写的DLL
http://www.cnblogs.com/MarsPanda/archive/2012/09/03/2668522.html 解决办法 安装 vcredist 运行库 或者用VC6.0编写DLL ...
- Compiling aSmack
For a recent mobile project we used XMPP. It worked really well and I’m keen to use it again. But, i ...
- 【转】使用Beaglebone Black的I2C (二)——使用C语言和i2c-dev驱动
在本博客的<使用Beaglebone Black的I2C(一)>中,介绍了BBB上无需编程对i2c总线进行读写操作的方法,本文将介绍如何在c语言程序中使用i2c-dev驱动来操作i2c设备 ...
- How to solve GM MDI cannot complete the installation
Dear Joy, I have a problem using GM MDI diagnostic tool. When I installed it on my laptop, the tool ...