题意:

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. 你不知道的Google控制台

    1.页面可编辑 document.body.contentEditable=true 2.console.table() 3.console.dir 4.clear() 清空控制台 5.sources ...

  2. window.print()打印页面指定内容(使用iframe保证原页面不失效)

    使用window.print()时会出现两个问题: (1)直接使用window.print() 打印的是整页内容-->无法实现打印指定区域 (2)打印时替换body中的内容,打印完成后再替换回来 ...

  3. Redis的事务和watch

    redis的事务 严格意义来讲,redis的事务和我们理解的传统数据库(如mysql)的事务是不一样的. redis中的事务定义 Redis中的事务(transaction)是一组命令的集合. 事务同 ...

  4. ConcurrentHashMap 源码分析

    ConcurrentHashMap 源码分析 1. 前言    终于到这个类了,其实在前面很过很多次这个类,因为这个类代码量比较大,并且涉及到并发的问题,还有一点就是这个代码有些真的晦涩,不好懂.前前 ...

  5. mybatis整合oracle 实现一对多查询 备注?

    <resultMap type="com.asiainfo.channel.model.weeklyNewspaper.WorkReportInfo" id="Wo ...

  6. JavaScript 通过队列实现异步流控制

    知乎上面看到一个面试题. 某个应用模块由文本框 input,以及按钮 A,按钮 B 组成.点击按钮 A,会向地址 urlA 发出一个 ajax 请求,并将返回的字符串填充到 input 中(覆盖 in ...

  7. 201621123062《java程序设计》第十周作业总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 思维导图: 2. 书面作业 本次PTA作业题集异常 2.1. 常用异常 结合题集题目7-1回答 2.1.1 自己以前 ...

  8. 需求分析&原型改进

    需求&原型改进 一.给目标用户展现原型,与目标用户进一步沟通理解需求. 1.用户痛点:需要随时随地练习四则运算,并能看到用户的统计数据. 2.用户反馈:较好地解决练习需求,若能加入班级概念则更 ...

  9. 2017-2018-1 1623 bug终结者 冲刺005

    bug终结者 冲刺005 by 20162323 周楠 今日任务:理清游戏运行逻辑,GameView类为游戏核心代码 简要介绍 游戏中整个地图都是由数组组成 1.整个地图为16×16格,主要元素有墙. ...

  10. Java暑假作业

    一.电影观后感 电影<摔跤吧!爸爸>观后感 二.下学期的计划与目标 大一学年总结: 参与了大大小小的学院活动,例如机器人搭建.辩论赛,也参加了学生会的部门,参与了组织活动.通过参与活动获 ...