poj 1094 Sorting It All Out (拓扑排序)
http://poj.org/problem?id=1094
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 24505 | Accepted: 8487 |
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
/**
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 (拓扑排序)的更多相关文章
- ACM: poj 1094 Sorting It All Out - 拓扑排序
poj 1094 Sorting It All Out Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%lld & ...
- [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 ...
- 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 ...
- poj 1094 Sorting It All Out_拓扑排序
题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...
- POJ 1094 Sorting It All Out 拓扑排序 难度:0
http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...
- PKU 1094 Sorting It All Out(拓扑排序)
题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. 是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.判断该序列是否唯一: 3.该序列字母次序之间 ...
- POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39602 Accepted: 13 ...
- [ACM] POJ 1094 Sorting It All Out (拓扑排序)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26801 Accepted: 92 ...
- POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29984 Accepted: 10 ...
随机推荐
- Linux下配置文件的位置
系统级的配置存放在 /etc 目录中.用户级的配置存放在用户的主目录 /home/user_login_name. SHELL 默认文件 /etc/bashrc – bash shell 的系统级默认 ...
- 优化Android应用内存的若干方法
原帖地址:http://www.open-open.com/lib/view/open1392013992317.html 在app开发的各个阶段中要考虑RAM的限制问题, 包括在设计阶段(正式开发之 ...
- 老男孩-金角大王-python学习博客地址
http://www.cnblogs.com/alex3714/category/770733.html
- iOS打包及发布
本篇介绍iOS应用的发布流程:由于苹果的发布周期太长, 再介绍一个很好用的测试网站——蒲公英. iOS应用程序的发布和真机调试调试很像,也需要申请各类证书. 1.进入https://developer ...
- OC4_电子词典
// // MyDictionary.h // OC4_电子词典 // // Created by zhangxueming on 15/6/15. // Copyright (c) 2015年 zh ...
- Apache使用mysql认证用户
使用MySQL进行认证 第1步:下载MySQL认证模块,并更名为mod_auth_mysql.so文件,并保存在apache的modules目录下 第2步:apache要加载此功能模块 LoadMod ...
- xml的生成与解析_老师笔记
使用序列化器生成一个xml文件 //1,初始化一个xml文件的序列化器 XmlSerializer serializer = Xml.newSerializer(); //2.初始化序列器参数 Fil ...
- java-多线程-join函数
join()>>不带参数 线程A调用线程B.join,意思就是线程A并入了线程B,当执行完线程B,再去执行线程A后续动作 join(int keepTims)>>带参数,与上面 ...
- DQL_数据查询语言
2014年11月21日 21:43:53 DQL 基础查询-- 注意要点:1.用户友善的标题 ...
- zabbix短信网关调用问题总结
在写调用短信网关的shell脚本的时候,发现了一个百思不得其解的问题,用浏览器访问短信接口地址是可以成功接收到短信的.但在shell 里面调用就报错了!!!在反复测试当中发现,在shell 中对特殊字 ...