https://vjudge.net/problem/POJ-1094

题意

对于N个大写字母,给定它们的一些关系,要求判断出经过多少个关系之后可以确定它们的排序或者排序存在冲突,或者所有的偏序关系用上之后依旧无法确定唯一的排序。

分析

拓扑排序模板题。唯一麻烦点的是判断在第几个式子就可以确定关系。由于在题目中,每个元素的关系都是严格定义的,那么当队列中存在不止一个入度为0的顶点时,可以认为未能成功排序,即还需要更多条件。当拓扑排序算法进行完后,拓扑序列的个数不够,则认为排序出现了矛盾。

#include<iostream>
#include<cmath>
#include<cstring>
#include<queue>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<map>
#include<set>
#define rep(i,e) for(int i=0;i<(e);i++)
#define rep1(i,e) for(int i=1;i<=(e);i++)
#define repx(i,x,e) for(int i=(x);i<=(e);i++)
#define X first
#define Y second
#define PB push_back
#define MP make_pair
#define mset(var,val) memset(var,val,sizeof(var))
#define scd(a) scanf("%d",&a)
#define scdd(a,b) scanf("%d%d",&a,&b)
#define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define pd(a) printf("%d\n",a)
#define scl(a) scanf("%lld",&a)
#define scll(a,b) scanf("%lld%lld",&a,&b)
#define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define IOS ios::sync_with_stdio(false);cin.tie(0) using namespace std;
typedef long long ll;
template <class T>
void test(T a){cout<<a<<endl;}
template <class T,class T2>
void test(T a,T2 b){cout<<a<<" "<<b<<endl;}
template <class T,class T2,class T3>
void test(T a,T2 b,T3 c){cout<<a<<" "<<b<<" "<<c<<endl;}
template <class T>
inline bool scan_d(T &ret){
char c;int sgn;
if(c=getchar(),c==EOF) return ;
while(c!='-'&&(c<''||c>'')) c=getchar();
sgn=(c=='-')?-:;
ret=(c=='-')?:(c-'');
while(c=getchar(),c>=''&&c<='') ret = ret*+(c-'');
ret*=sgn;
return ;
}
//const int N = 1e6+10;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const ll mod = ;
int T;
void testcase(){
printf("Case %d:",++T);
}
const int MAXN = ;
const int MAXM = ;
const double eps = 1e-;
const double PI = acos(-1.0); bool g[][];
int topu[];
int indeg[];
int n,m;
int topuSort(){
queue<int>q;
int cnt=;
int in[];
memcpy(in,indeg,sizeof(indeg));
for(int i=;i<n;i++){
if(in[i]==){
q.push(i);
}
}
int u;
bool zeros = false;
while(!q.empty()){
if(q.size()>) zeros=true;
u=q.front();
q.pop();
topu[cnt++]=u;
for(int i=;i<n;i++){
if(g[u][i]){
in[i]--;
if(in[i]==) q.push(i);
}
}
}
if(cnt!=n) return -;
if(zeros) return ;
return ;
}
int main() {
#ifdef LOCAL
freopen("data.in","r",stdin);
#endif // LOCAL
while(~scanf("%d%d ",&n,&m)&&n){
char temp[];
mset(g,false);
mset(indeg,);
int pos;
bool f1 = false;
bool f2 = false;
for(int i=;i<m;i++){
scanf("%s",temp);
if(f1||f2) continue;
int u = temp[]-'A';
int v = temp[]-'A';
if(!g[u][v]){
g[u][v]=true;
indeg[v]++;
}
int flag = topuSort();
if(flag==-){
f1=true;
pos=i+;
}else if(flag==){
pos=i+;
f2=true;
}
}
if(f1){
printf("Inconsistency found after %d relations.\n",pos);
}else if(f2){
printf("Sorted sequence determined after %d relations: ",pos);
for(int i=;i<n;i++) printf("%c",'A'+topu[i]);
puts(".");
}else{
printf("Sorted sequence cannot be determined.\n");
}
}
return ;
}

POJ - 1094 Sorting It All Out(拓扑排序)的更多相关文章

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

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

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

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

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

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

  5. poj 1094 Sorting It All Out_拓扑排序

    题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...

  6. POJ 1094 Sorting It All Out 拓扑排序 难度:0

    http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...

  7. PKU 1094 Sorting It All Out(拓扑排序)

    题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. 是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.判断该序列是否唯一: 3.该序列字母次序之间 ...

  8. POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)

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

  9. [ACM] POJ 1094 Sorting It All Out (拓扑排序)

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

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

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

随机推荐

  1. 【转载】SSD断电保护原理

    异常掉电的隐患 若没有合理的掉电保护机制,而异常掉电的发生又不可避免,当发生异常掉电,会引发很多问题. (1)丢盘 异常掉电,会使得映射表来不及保存,丢失逻辑地址到物理地址的映射,待重新上电后,SSD ...

  2. Azure SQL Database Active Geo-Replication 简介

    对于数据库的维护来说,备份工作可谓是重中之重.MS Azure 当然也提供了很完善的数据库备份功能.但是在动手创建备份计划前请思考一下备份工作的真实目的.当然首先要保证数据的安全,一般来说定时创建数据 ...

  3. 记录:tf.saved_model 模块的简单使用(TensorFlow 模型存储与恢复)

    虽然说 TensorFlow 2.0 即将问世,但是有一些模块的内容却是不大变化的.其中就有 tf.saved_model 模块,主要用于模型的存储和恢复.为了防止学习记录文件丢失或者蠢笨的脑子直接遗 ...

  4. 1092. To Buy or Not to Buy (20)-map

    给出两个字符串,判断第二个字符串中的字符是否都出现在第一个中. 是,则输出Yes,以及多余的字符的个数. 否,则输出No,以及缺失的个数. #include <iostream> #inc ...

  5. 1.AKATSUKI

    ## 1.AKATSUKI - “晓”,日本漫画<火影忍者>及其衍生作品中的一个秘密组织. - 成立之初是为了给自己的国家带来和平. ## 2.团队成员 - 邱东宝 - 211606325 ...

  6. 20170831 php

    今天开始学习php 发现这个网站教程感觉入门很轻松 http://www.php.cn/code/25.html 配置环境遇到了端口占用的问题 解决方案: http://www.weekdian.co ...

  7. WAMP的一些配置修改

    一.修改php运行的目录,即www目录 1. 在工具栏里点击 Apache->httpd.conf 2. 找到 DocumentRoot "G:/PHP/wamp/www/" ...

  8. Maven 学习笔记——将普通的Java项目转换成Maven项目(3)

    将一个普通的java项目转换成Maven项目并不是一个很大的任务,仅仅只需要下面的几步就能将转换成功.下面我是用一个简单的Selenium测试小demon作为例子来说的. 移调项目中所有关联的Libr ...

  9. 解决局域网IP冲突

    进入cmd ipconfig -all 查看现有IP,发现IP不是192.168.1.*的形式,而是192.168.0.*等异常 ipconfig -release  释放现有IP ipconfig ...

  10. Docker(二十三)-Docker使用pipework配置本地网络

    需求 在使用Docker的过程中,有时候我们会有将Docker容器配置到和主机同一网段的需求.要实现这个需求,我们只要将Docker容器和主机的网卡桥接起来,再给Docker容器配上IP就可以了. 下 ...