POJ - 1094 Sorting It All Out(拓扑排序)
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(拓扑排序)的更多相关文章
- 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 ...
- [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 ...
- poj 1094 Sorting It All Out_拓扑排序
题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...
- POJ 1094 Sorting It All Out 拓扑排序 难度:0
http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...
- PKU 1094 Sorting It All Out(拓扑排序)
题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. 是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.判断该序列是否唯一: 3.该序列字母次序之间 ...
- POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39602 Accepted: 13 ...
- [ACM] POJ 1094 Sorting It All Out (拓扑排序)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26801 Accepted: 92 ...
- POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29984 Accepted: 10 ...
随机推荐
- Bitmap 位图 Java实现
一.结构思想 以 bit 作为存储单位进行布尔值存取的数据结构. 表现为:给定第i位,该bit为1则表示true,为0则表示false. 二.使用场景及优点 适用于对布尔或0.1值进行(大量)存取的场 ...
- Python机器学习/LinearRegression(线性回归模型)(附源码)
LinearRegression(线性回归) 2019-02-20 20:25:47 1.线性回归简介 线性回归定义: 百科中解释 我个人的理解就是:线性回归算法就是一个使用线性函数作为模型框架($ ...
- win2003无线网卡驱动无法安装解决方法
Windows 2003 Server对无线网卡的pci资源分配出了问题,而笔记本bios中屏蔽了pci配置项,无法修改. 打开资源管理器菜单,工具-文件夹选项-显示,去掉“隐藏受保护的操作系统文件” ...
- 在CentOS上搭建PHP服务器环境(可用)
原文:https://www.cnblogs.com/zy2009/p/7047828.html 1,先安装apache: yum install httpd 配置ServerName vi /etc ...
- linux内核分析第四周学习笔记
linux内核分析第四周学习笔记 标签(空格分隔): 20135328陈都 陈都 原创作品转载请注明出处 <Linux内核分析>MOOC课程http://mooc.study.163.co ...
- C语言版本:单链表的实现(优化版本)
未优化版本:http://www.cnblogs.com/duwenxing/p/7569376.html slist.h #ifndef __SLIST_H__ #define __SLIST_H_ ...
- 冲刺Two之站立会议6
今天继续了昨天的工作,对视频进行优化.因为昨天的工作没有达到预期的效果,所以又继续对音质和画面质量做了相应的优化.还对相应的聊天室界面进行了优化.
- Linux命令(二十) 显示系统内存状态 free
一.命令简介 free 命令会显示内存的使用情况,包括实体内存,虚拟的交换文件内存.共享内存区段,以及系统核心使用的缓冲区等. 二.参数说明 -b 以Byte为单位显示内存使用情况 -K 以KB为单位 ...
- Windows下多线程编程(二)
线程的分类 1. 有消息循环线程 MFC中有用户界面线程,从CWinThread派生出一个新的类作为UI线程类CUIThread,然后调用AfxBeginthread(RUNTIME_CLAS ...
- 贝云cms内容管理系统(thinkphp5.0开源cms管理系统)
byCms包含文章,图片,下载,视频模型,基于thinkphp5.0.9,可无缝升级至thinkphp.1.0,是一套简单,易用的内容管理系统,旨在帮助开发者节约web应用后台开发时间和精力,以最快的 ...