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. 软件测试_测试工具_APP测试工具_对比

    以下是我自己整理的APP测试工具对比,各个工具相关并不全面.尤其关于收费一项,我只是针对自己公司的实际情况进行对比的,每个工具还有其他收费套餐可以选择,详情可进入相关官网进行查看 以下是部分官网链接: ...

  2. 外网主机访问虚拟机下的web服务器(NAT端口转发)-----端口映射

    主机:系统win7,ip地址172.18.186.210 虚拟机:VMware Workstation 7,虚拟机下安装了Centos操作系统,ip地址是192.168.202.128,部署了LAMP ...

  3. 使用开源项目免费申请 JetBrains 全家桶 IDEA 开源许可证

    JetBrains 公司旗下的 IDEA 功能都十分强大,深受各种编程语言相关的程序员的喜爱.我个人而言,经常使用 WebStorm,也使用过 PyCharm. 正常情况下 JetBrains 公司的 ...

  4. 详细聊聊k8s deployment的滚动更新(二)

    一.知识准备 ● 本文详细探索deployment在滚动更新时候的行为 ● 相关的参数介绍:   livenessProbe:存活性探测.判断pod是否已经停止   readinessProbe:就绪 ...

  5. PowerDesigner数据库设计实用技巧

    欢迎大家补充,谢谢! 1. 原始单据与实体之间的关系 可以是一对一.一对多.多对多的关系.在一般情况下,它们是一对一的关系:即一张原始单据对应且只对应一个实体.在特殊情况下,它们可能是一对多或多对一的 ...

  6. Android 公共库的建立方法

    本文主要介绍在android工程中如何将共用代码建成公共包方便其他工程引用.引用后的工程结构分析.library引入方式的优缺点. 自己也写了一些android公共的库,有兴趣的可以参考 Trinea ...

  7. C++ new和delete 堆和栈

    一.new和delete基本用法 程序开发中内存的动态分配与管理永远是一个让C++开发者头痛的问题,在C中,一般是通过malloc和free来进行内存分配和回收的,在C++中,new和delete已经 ...

  8. mysql数据出现Unknown column 'user_uid' in 'field list' sql错误

    来源:https://blog.csdn.net/gnail_oug/article/details/53606608 在操作mysql数据库时提示com.mysql.jdbc.exceptions. ...

  9. ELK 性能(3) — 在 Docker 上运行高性能容错的 Elasticsearch 集群

    ELK 性能(3) - 在 Docker 上运行高性能容错的 Elasticsearch 集群 介绍 在 Docker 上运行高性能容错的 Elasticsearch 集群 内容 通常熟悉的开发流程是 ...

  10. JS animate动画

    <!DOCTYPE html><html lang="zh-cn"><head> <meta charset="utf-8&qu ...