POJ1094 Sorting It All Out LUOGU 排序
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 40012 | Accepted: 14072 |
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.
P1347 排序
题目描述
一个不同的值的升序排序数列指的是一个从左到右元素依次增大的序列,例如,一个有序的数列A,B,C,D 表示A<B,B<C,C<D。在这道题中,我们将给你一系列形如A<B的关系,并要求你判断是否能够根据这些关系确定这个数列的顺序。
输入输出格式
输入格式:
第一行有两个整数n,m,n表示需要排序的元素数量,2<=n<=26,第1到n个元素将用大写的A,B,C,D....表示。m表示将给出的形如A<B的关系的数量。
接下来有m行,每行有3个字符,分别为一个大写字母,一个<符号,一个大写字母,表示两个元素之间的关系。
输出格式:
若根据前x个关系即可确定这n个元素的顺序yyy..y(如ABC),输出
Sorted sequence determined after xxx relations: yyy...y.
若根据前x个关系即发现存在矛盾(如A<B,B<C,C<A),输出
Inconsistency found after 2 relations.
若根据这m个关系无法确定这n个元素的顺序,输出
Sorted sequence cannot be determined.
(提示:确定n个元素的顺序后即可结束程序,可以不用考虑确定顺序之后出现矛盾的情况)
输入输出样例
1:
4 6
A<B
A<C
B<C
C<D
B<D
A<B 2:
3 2
A<B
B<A 3:
26 1
A<Z
1:
Sorted sequence determined after 4 relations: ABCD.
2:
Inconsistency found after 2 relations.
3:
Sorted sequence cannot be determined.

由题目描述可知:
该题 典型的拓扑排序。
1、在 拓扑排序时,当出现同时可访问两个节点或多个节点时,则信息不完全。
2、当出现有环时,则有冲突,判断是否有环可以在拓扑排序完时判断是否有点未访问。因为若无环,每个点都可以访问。
不多说了,附上我的AC代码:
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
const int MAXN=;
int n,m,cas,id; struct edge
{
int v,nx;
}set[MAXN];
int head[],d[],ok[],write[];
queue<int> Q; void Addedge(int u,int v)
{
id++;set[id].v=v;set[id].nx=head[u];
head[u]=id;
} int bfs()
{
cas=;
while(!Q.empty())Q.pop();
int out=,t,u;t=;
for(int i=;i<=n;i++)
if(!ok[i])
{
t++;Q.push(i);
}
if(t>)out=;
while(!Q.empty())
{
u=Q.front();Q.pop();t=;write[++cas]=u;
for(int k=head[u];k>;k=set[k].nx)
{
ok[set[k].v]--;
if(!ok[set[k].v])
{
t++;Q.push(set[k].v);
}
}
if(t>)out=;
}
for(int i=;i<=n;i++)if(ok[i]>){out=-;break;}
return out;
} char print()
{
for(int i=;i<=cas;i++)printf("%c",(char)(write[i]+));
return '.';
} int main()
{
while(~scanf("%d%d",&n,&m))
{
memset(write,,sizeof(write));
memset(d,,sizeof(d));
memset(head,-,sizeof(head));
id=;
int now=,loca;
char a,b,c;
if(n== && m==)break;
for(int i=;i<=m;i++)
{
cin>>a>>b>>c;
if(b=='<')
{
Addedge(a-,c-);d[c-]++;
}
else
{
Addedge(c-,a-);d[a-]++;
}
if(now==)
{
for(int j=;j<=n;j++)ok[j]=d[j];
now=bfs();loca=i;
}
}
if(now==)puts("Sorted sequence cannot be determined.");
else if(now==)printf("Sorted sequence determined after %d relations: ",loca),printf("%c\n",print());
else printf("Inconsistency found after %d relations.\n",loca);
}
return ;
}
记得点赞欧。
P1347 排序
POJ1094 Sorting It All Out LUOGU 排序的更多相关文章
- [poj1094]Sorting It All Out_拓扑排序
Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...
- nyoj349 poj1094 Sorting It All Out(拓扑排序)
nyoj349 http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094 http://poj.org/problem?id=10 ...
- POJ- 1094 Sorting It All Out---拓扑排序是否唯一的判断
题目链接: https://vjudge.net/problem/POJ-1094 题目大意: 该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列.是典型的拓扑排序,但输出格式上 ...
- POJ1094 Sorting It All Out —— 拓扑排序
题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Tot ...
- POJ1094 Sorting It All Out(拓扑排序)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 30110 Accepted: 10 ...
- 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 ...
- PAT A1028 List Sorting (25 分)——排序,字符串输出用printf
Excel can sort records according to any column. Now you are supposed to imitate this function. Input ...
- poj1094 Sorting It All Out【floyd】【传递闭包】【拓扑序】
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions:39731 Accepted: 139 ...
随机推荐
- 对HTML5的初步认识(一)
一.概述 1.HTML5是什么? HTML5是最新一代的HTML标准,它不仅拥有HTML中所有的特性,而且增加了许多实用的特性,如视频.音频.画布(canvas)等. 2012年12月17日,万维网联 ...
- Java学习笔记之——线程的生命周期、线程同步
一. 线程的生命周期 新建(new Thrad):创建线程后,可以设置各个属性值,即启动前 设置 就绪(Runnable):已经启动,等待CPU调动 运行(Running):正在被CPU调度 阻塞(B ...
- Maven项目POM文件错误,提示“Plugin execution not covered by lifecycle configuration”的解决方案
一. 问题 Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-depend ...
- 关于mybatis条件查询 报错:元素内容必须由格式正确的字符数据或标记组成
原查询 select sum(case when age<=16 then 1 else 0 end ) age1, sum(case when age>16 and age<=25 ...
- react 函数子组件(Function ad Child Component)
今天学习了react中的函数子组件的概念,然后在工作中得到了实际应用,很开心,那么好记性不如烂笔头,开始喽~ 函数子组件(FaCC )与高阶组件做的事情很相似, 都是对原来的组件进行了加强,类似装饰者 ...
- 使用ArcGIS Earth矢量化高精度的数据(kml转图层转shp/要素类)
大家好,这次来分享干货.做地理分析的同学,或者需要使用地图却不知道哪里有精度较高矢量数据(如校园图)的时候,怎么办呢? 我们知道ArcGIS提供了精度较高的全球影像图,基于此,可以自己进行矢量化,然后 ...
- 滑动和animate以及如何停止动画
又是一天过去了,今天复习了slideDown.slideUp.slideToggle以及animate和stop的用法. <!DOCTYPE html> <html> < ...
- Mac 系统占用100g的解决办法
Mac 关于本机-磁盘管理,如果发现系统占用超过80g以上的小伙伴们可以做以下操作只需要以下4个步骤,轻松降到30g以内!!!!!!!(仅适用于安装了Xcode的小伙伴) 打开Finder,comma ...
- 浅谈Semaphore类
Semaphore类有两个重要方法 1.semaphore.acquire(); 请求一个信号量,这时候信号量个数-1,当减少到0的时候,下一次acquire不会再执行,只有当执行一个release( ...
- Android 图片Bitmap,drawable,res资源图片之间转换
一.知识介绍 ①res资源图片是放在项目res文件下的资源图片 ②BitMap位图,一般文件后缀为BMP,需要编码器编码,如RGB565,RGB8888等.一种逐像素的显示对象,其执行效率高,但缺点也 ...