poj1094-Sorting It All Out-拓扑排序
题意:
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-拓扑排序的更多相关文章
- [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 ...
- POJ1094 Sorting It All Out —— 拓扑排序
题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Tot ...
- 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 ...
- POJ- 1094 Sorting It All Out---拓扑排序是否唯一的判断
题目链接: https://vjudge.net/problem/POJ-1094 题目大意: 该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列.是典型的拓扑排序,但输出格式上 ...
- POJ1094 Sorting It All Out LUOGU 排序
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 40012 Accepted ...
- POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29984 Accepted: 10 ...
- [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 ...
随机推荐
- 1. 初识 Lucene
在学习Lucene之前呢,我们当然首先要了解下什么是Lucene. 0x01 什么是Lucene ? Lucene是一套用于全文检索和搜索的开放源代码程序库,由Apache软件基金会支持和提供. Lu ...
- poj2991 Crane(线段树+集合)白书例题
题目大意:起重机有n节,题目给出要调节的k节,每节调节成x度,求最后底部的起重机的坐标(最顶上的起点为(0,0)). 分析:一开始我看白书,看不懂他那个向量旋转的坐标是怎么来的,翻了很多博客,才发现, ...
- 使用.NET开发AutoCAD——设计师不做画图匠(一)
(一)前言--如何避免加班那些事 我是谁?我是一名工程设计师,有点"不务正业",在工作之余长期从事软件开发工作,开发了公路铁路行业广泛应用的设计软件.说正题之前,聊聊加班那些事.话 ...
- 听翁恺老师mooc笔记(12)--结构中的结构
结构数组: 和C语言中的int,double一样,一旦我们做出一个结构类型,就可以定义这个结构类型的变量,也可以定义这个结构类型的数组.比如下面这个例子: struct date dates[100] ...
- 20162308 实验一《Java开发环境的熟悉》实验报告
a 20162308 实验一<Java开发环境的熟悉>实验报告 实验内容 使用JDK编译.运行简单的Java程序. 使用IDEA 编辑.编译.运行.调试Java程序. 实验要求 没有Lin ...
- 20155215 第二周测试1 与 myod
课堂测试 第一题 每个.c一个文件,每个 .h一个文件,文件名中最好有自己的学号 用Vi输入图中代码,并用gcc编译通过 在Vi中使用K查找printf的帮助文档 提交vi编辑过程截图,要全屏,包含自 ...
- Alpha冲刺博客合集
Alpha冲刺序列: Alpha冲刺Day1:Alpha No.1 Alpha冲刺Day2:Alpha No.2 Alpha冲刺Day3:Alpha No.3 Alpha冲刺Day4:Alpha No ...
- 201621123062《java程序设计》第七周作业总结
1. 本周学习总结 1.1 思维导图:Java图形界面总结 1.2 可选:使用常规方法总结其他上课内容. 1.布局管理器的具体使用方法 2.事件处理模型及其代码的编写 3.Swing中的常用组件 4. ...
- 201621123062《java程序设计》第五周作业总结
1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 关键词:interface.Comparable.comparator 1.2 尝试使用思维导图将这些关键词组织起来.注:思维导 ...
- 转 Eclipse快捷键调试大全
(1)Ctrl+M --切换窗口的大小(2)Ctrl+Q --跳到最后一次的编辑处(3)F2 ---重命名类名 工程名 --当鼠标放在一个标记处出现Tooltip时候按F2则把鼠标移开时To ...