题意:

1).给你一些大写字母,共n个;大写字母间有m条关系;

2).举例:关系:A<B,意思就是A要排在B的前面(也就是说可能B排在A的前面

3).输出:有三种情况:

1.n个字母在前 i 条关系下可以确定排序关系;

2.n个字母在执行到第 i 条命令时出现错误,即回路:A间接或直接小于B 且 B间接或直接小于A;

3.m条命令不能确定n个字母的排序。

思路:

直观感觉就是拓扑排序,需要注意的是,每读入一条命令就要拓扑排序一次。

如果你对拓扑排序不够了解,请看这篇博客:点击查看

而三种情况判断的顺序更为重要!!

情况1和2可以同时判断,最后判断情况3!!

意思是:

如果执行到第 i 条命令时,可以确定情况1 或2,则直接输出;

如果m条命令执行完了,还不能确定字母顺序,则输出情况3

题目链接:

  点击做题

AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<string>
#include<stack>
#include<cmath>
#define test printf("***\n")
#define mm1(a) memset((a),-1,sizeof((a)))
#define mm0(a) memset((a),0,sizeof((a)))
#define mmx(a) memset((a),0x3f,sizeof((a)))
#define ka getchar();getchar()
#define ka1 getchar()
#define iis std::ios::sync_with_stdio(false)
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
const int N = ;
const int M = ;
const int X = ;
const int INF = 1e9;
const double eps = 1e-;
const int mod = 1e9 + ;
//#define DEBUG
int n,m,sum;
int tot,flag;
int in[N],head[N],tin[N];
int ar[N];
struct lp
{
int u,v,nex;
lp(){}
lp(int a,int b,int c):
u(a),v(b),nex(c){}
}cw[N*N];
inline void add(int a,int b){
cw[++tot]=lp(a,b,head[a]);
head[a]=tot;
}
int tuopu(){
queue<int>Q;
while(!Q.empty())Q.pop();
for(int i=;i<=n;++i)tin[i]=in[i];
for(int i=;i<=n;++i){
if(tin[i]==){
Q.push(i);
}
}
int t=;
int ans=;
while(!Q.empty()){
if(Q.size()>)ans=-;
int u=Q.front();Q.pop();
ar[t++]=u;
for(int i=head[u];i!=-;i=cw[i].nex){
int v=cw[i].v;
tin[v]--;
if(tin[v]==){
Q.push(v);
}
}
}
if(t!=n)return ;
return ans;
}
inline void init(){
tot=-;
mm0(in);
mm1(head);
}
int main(int argc, char const *argv[])
{
#ifdef DEBUG
freopen("D:in.in", "r", stdin);
freopen("D:out.out", "w", stdout);
#endif
char a,b;
while(~scanf("%d %d",&n,&m)&&(n+m)){
init();
int ans=;
for(int i=;i<m;++i){
char s[];
scanf("%s",s);
a=s[];b=s[];
if(ans==)continue;
in[b-'A'+]++;
add(a-'A'+,b-'A'+);
int ok=tuopu();
if(ok==){
ans=;
printf("Inconsistency found after %d relations.\n",i+);
}else if(ok==){
ans=;
printf("Sorted sequence determined after %d relations: ",i+);
for(int i=;i<n;++i){
printf("%c", ar[i]+'A'-);
}
printf(".\n");
}
}
if(ans){
printf("Sorted sequence cannot be determined.\n");
}
}
return ;
}

poj1094-Sorting It All Out-拓扑排序的更多相关文章

  1. [poj1094]Sorting It All Out_拓扑排序

    Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...

  2. nyoj349 poj1094 Sorting It All Out(拓扑排序)

    nyoj349   http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094   http://poj.org/problem?id=10 ...

  3. POJ1094 Sorting It All Out —— 拓扑排序

    题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Tot ...

  4. ACM: poj 1094 Sorting It All Out - 拓扑排序

    poj 1094 Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & ...

  5. poj 1094 Sorting It All Out (拓扑排序)

    http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  6. POJ- 1094 Sorting It All Out---拓扑排序是否唯一的判断

    题目链接: https://vjudge.net/problem/POJ-1094 题目大意: 该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列.是典型的拓扑排序,但输出格式上 ...

  7. POJ1094 Sorting It All Out LUOGU 排序

        Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 40012   Accepted ...

  8. POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29984   Accepted: 10 ...

  9. [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 ...

  10. 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 ...

随机推荐

  1. 前端的UI设计与交互之反馈示篇

    为了帮助用户了解应用当前要做什么,也给用户的下一步行为做参考,以及了解操作后所产生的结果 ,当用户和系统需要交互时,使用不同的模式来反馈信息或结果.当设计者使用反馈或者自定义一些反馈时,请注意:为用户 ...

  2. PAT-L2-007-gplt真题

    题目分析: 1. 首先,题目说一个家庭有孩子爸爸妈妈等几辈人,可以利用并查集将一个家庭里的所有人变成一个集合: 2. 刚好题目的目的也是这样,输出的是一个家庭人数,人均房产面积,人均房产套数等: 3. ...

  3. ES6中export及export default的区别

    相信很多人都使用过export.export default.import,然而它们到底有什么区别呢? 在JavaScript ES6中,export与export default均可用于导出常量.函 ...

  4. 利用whoosh对mongoDB的中文文档建立全文检索

    1.建立索引 #coding=utf-8 from __future__ import unicode_literals __author__ = 'zh' import sys,os from wh ...

  5. C++迭代器的使用和操作总结

    迭代器是一种检查容器内元素并遍历元素的数据类型.C++更趋向于使用迭代器而不是下标操作,因为标准库为每一种标准容器(如vector)定义了一种迭代器类型,而只用少数容器(如vector)支持下标操作访 ...

  6. java中有关流操作的类和接口

    一.java操作l流有关的类和接口 1.File 文件类 2.RandomAccessFile 随机存储文件类 3.InputStream 字节输入流 4.OutputStream 字节输出流 5.R ...

  7. Spring Boot入门教程2-1、使用Spring Boot+MyBatis访问数据库(CURD)注解版

    一.前言 什么是MyBatis?MyBatis是目前Java平台最为流行的ORM框架https://baike.baidu.com/item/MyBatis/2824918 本篇开发环境1.操作系统: ...

  8. Suricata默认规则集相关

    Suricata规则集 Suricata 基于规则检测和控制数据流量,所有规则的配置文件保存在rules目录内 .这些是已知和确认的活动僵尸网络和其C&C(command and contro ...

  9. 关于VR开发中的穿墙问题随想

    在VR开发中,用户将以第一人称的视角进入虚拟世界,即用户同时身处两个坐标系:1. 现实世界坐标系(如房间的坐标系),用户的身体处于这个坐标系 2. VR世界坐标系,用户的感官处于这个坐标系,即用户觉得 ...

  10. [LTR] 信息检索评价指标(RP/MAP/DCG/NDCG/RR/ERR)

    一.RP R(recall)表示召回率.查全率,指查询返回结果中相关文档占所有相关文档的比例:P(precision)表示准确率.精度,指查询返回结果中相关文档占所有查询结果文档的比例: 则 PR 曲 ...