poj1094
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 29539 | Accepted: 10233 |
Description
Input
Output
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
网上讲解参考代码
/** 这道题WA了好久,其中有几个需要注意的地方
1、当出现正好存在一种情况能够排序完所有节点时,不管以后的边会出现什么情况,都输出
能够排序成功
2、当中间的拓扑排序过程中出现多个几点的入度为0时,只记录当时的状态
(亦即该测试数据要么出现环,要么就是有多组解),不能立即返回,
要继续读边,直到能够排序完成(此时输出有多解的情况)或者出现环。
*/
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
bool G[30][30];
int d[30];
char s[30];
int n;
int toposort()
{
int num,k,i,j,t;
int td[30];
bool flag1=true;
for(i=1;i<=n;++i)
td[i]=d[i];
memset(s,0,sizeof(0));
for(j=0;j<n;++j)
{
num=0;
for(i=1;i<=n;++i)
{
if(td[i]==0)
{
k=i;
++num;
}
}
if(num==0) //有环
return -1;
if(num>1) //有多种情况,还需继续读边判断
{
flag1=false;
}
s[j]='A'+k-1;
td[k]--;
for(t=1;t<=n;++t)
{
if(G[k][t])
--td[t];
}
}
s[n]='\0';
if(flag1==false) //情况不唯一
return 0;
return 1; //全部排好序了返回1.
}
int main()
{
int m,i,ans,k;
bool flag;
char temp[5];
while(scanf("%d%d",&n,&m),m||n)
{
memset(G,false,sizeof(G));
memset(d,0,sizeof(d));
flag=true;
for(i=1;i<=m;++i)
{
scanf("%s",temp);
if(!flag) //已经排好序或者有环
continue;
int u=temp[0]-'A'+1;
int v=temp[2]-'A'+1;
if(!G[u][v])
{
++d[v];
G[u][v]=true;
}
ans=toposort();
if(ans==1||ans==-1)
{
k=i;
flag=false;
}
}
if(ans==1)
{
printf("Sorted sequence determined after %d relations: %s.\n",k,s);
}
else if(ans==-1)
{
printf("Inconsistency found after %d relations.\n", k);
}
else if(flag)
printf("Sorted sequence cannot be determined.\n");
}
return 0;
}
我的代码
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int map[100][100];
int m,n;
int tindegree[100],indegree[100];
char str[5];
char s[39];
int toposort(){
bool flag=true;
memset(tindegree,0,sizeof(tindegree));
memset(s,0,sizeof(s));
for(int i=1;i<=n;i++){
tindegree[i]=indegree[i];
}
for(int i=1;i<=n;i++){
int sum=0,k;
for(int j=1;j<=n;j++){
if(!tindegree[j]){
k=j;
sum++;
}
}
if(sum==0){
return -1;
}
if(sum>1){
flag=false;
}
s[i-1]=k+'A'-1;
tindegree[k]--;
for(int z=1;z<=n;z++){
if(map[k][z]){
tindegree[z]--;
}
}
}
s[n]='\0';
if(flag==false)
return 0;
return 1;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
if(n==0&&m==0)
break;
memset(map,0,sizeof(map));
memset(indegree,0,sizeof(indegree));
memset(str,0,sizeof(str));
memset(s,0,sizeof(s));
int ans=2;
int temp;
bool flag=true;
for(int i=1;i<=m;i++){
scanf("%s",str);
if(flag==false)
continue;
int u=str[0]-'A'+1;
int v=str[2]-'A'+1;
if(!map[u][v]){
map[u][v]=1;
indegree[v]++;
}
ans=toposort();
if(ans==-1||ans==1){
temp=i;
flag=false;
}
}
if(ans==1)
printf("Sorted sequence determined after %d relations: %s.\n",temp,s);//temp不可以改为n
else if(ans==-1)
printf("Inconsistency found after %d relations.\n",temp);
else if(flag)
printf("Sorted sequence cannot be determined.\n");
}
return 0;
}
poj1094的更多相关文章
- POJ1094[有向环 拓扑排序]
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33184 Accepted: 11 ...
- nyoj349 poj1094 Sorting It All Out(拓扑排序)
nyoj349 http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094 http://poj.org/problem?id=10 ...
- POJ1094 拓扑排序
问题:POJ1094 本题考查拓扑排序算法 拓扑排序: 1)找到入度为0的点,加入已排序列表末尾: 2)删除该点,更新入度数组. 循环1)2)直到 1. 所有点都被删除,则找到一个拓扑 ...
- [poj1094]Sorting It All Out_拓扑排序
Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...
- POJ- 1094 Sorting It All Out---拓扑排序是否唯一的判断
题目链接: https://vjudge.net/problem/POJ-1094 题目大意: 该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列.是典型的拓扑排序,但输出格式上 ...
- POJ1094——拓扑排序和它的唯一性
比较模板的topological-sort题,关键在于每个元素都严格存在唯一的大小关系,而一般的拓扑排序只给出一个可能解,这就需要每趟排序的过程中监视它是不是总坚持一条唯一的路径. 算法导论里面的拓扑 ...
- poj1094 拓扑 Sorting It All Out
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29744 Accepted: 10 ...
- poj1094 拓扑序
题意:现在有多个大写字母(不一定连续),给出字母之间的大小关系,问到第几个关系时就能判断有唯一大小排序或出现矛盾,或是有多个合理排序,若有唯一排序,则输出它. 拓扑序,只不过坑爹的是如果关系处理到一半 ...
- poj1094 topsort
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 32275 Accepted: 11 ...
随机推荐
- please tell me the error about java Graphics
this is the code: public class paint extends JFrame{ public paint(){ setLayout(new FlowLayout()); se ...
- 每天一个linux命令(21):tar命令
通过SSH访问服务器,难免会要用到压缩,解压缩,打包,解包等,这时候tar命令就是是必不可少的一个功能强大的工具.linux中最流行的tar是麻雀虽小,五脏俱全,功能强大. tar 命令可以为linu ...
- nginx 配置虚拟主机
文章转载自:http://www.ttlsa.com/html/1571.html 上篇说道我们的nginx是安装在/usr/local/nginx/ cd conf 我们现在把所有的虚拟主机放在一个 ...
- asp.net 回发或回调参数无效的各种情况分析及解决办法
昨天,在实现级联菜单的时候,突然出现一下错误: 回发或回调参数无效.在配置中使用 <pages enableEventValidation="true"/> 或在页面中 ...
- 设置div居中
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- iOS开发编码建议与编程经验
作者:乞力马扎罗的雪(GitHub) 原文 在开发过程中,我们不仅要去看别人的代码,也要让别人看我们的代码.那么,有一个良好的编码习惯将会非常重要.下面将会罗列使用Objective-C来开发iOS的 ...
- 【2011图灵奖得主】我眼中的Judea Pearl
[2011图灵奖得主]我眼中的Judea Pearl 来源: 叶星遥的日志 2011年的图灵奖花落UCLA计算机系的Judea Pearl教授.图灵奖是计算领域的最高奖,由于近年来这个领域的兴盛也算是 ...
- CSS制作图片水平垂直居中
所谓的图片水平垂直居中就是把图片放在一个容器元素中(容器大于图片尺寸或是指定了大小的容器),并且图片位居此容器正中间(中间是指元素容器的正中间),而图片不是以背景图片(background-image ...
- CSS 2D转换 matrix() 详解
2D转换 IE10.Firefox.Opera 支持 transform 属性 Chrome.Safari 需要前缀 -webkit- . IE9 需要前缀 -ms- . translate():接收 ...
- JS 事件练习
QQ拖拽及状态栏选择 HTML <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> &l ...