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 ...
随机推荐
- Java中的Calendar日历用法详解
第一部分 Calendar介绍 public abstract class Calendar implements Serializable, Cloneable, Comparable<Cal ...
- 调试OV2735总结
调试找到的问题: 1:开始调试的时候因为之前的工程师原理图和BOM出错,导致本来是2.8V电压的焊接的是1.8V的LDO所以这个是第一个问题 2:因为FAE反应说sensor没有反应I2C没有通信,所 ...
- 了不起的Node.js--之五 TCP连接
TCP连接 传输控制协议(TCP)是一个面向连接的协议,它保证了两台计算机之间数据传输的可靠性和顺序. TCP是一种传输层协议,它可以让你将数据从一台计算机完整有序地传输到另一台计算机. Node.j ...
- centos 升级python2.6 到python3.3(实测可行)
http://blog.csdn.net/harith/article/details/17538233
- 第一Sprint阶段对各组提出的意见
组号 组名 意见 1 理财猫 1.界面深色的部分,字体应调成亮色,可以适当美化字体. 2.希望能够自动记录花销,而不是只能手动“记一笔”. 3.功能略少,比如可以添加“收入详情”.“支出详情”. 2 ...
- UIO,大页内存,CPU亲和性,NUMA机制等
Linux环境下的UIO(Userspace I/O) UIO 用户空间下驱动程序的支持机制.DPDK使用UIO机制使网卡驱动程序运行在用户态,并采用轮询和零拷贝方式从网卡收取报文,提高收发报文的性能 ...
- 第二个spring冲刺第7天
今天因为停电,所以没什么进展,延迟一天工作,今天当作休息
- Window环境下RabbitMQ 添加用户、设置角色和权限
基本上新增用户.角色和权限的方法都一样,大概如下: REM 添加一个帐号 密码 rabbitmqctl.bat add_user zhangfujun zhangfujun123 REM 添加角色 r ...
- 『编程题全队』Alpha 阶段冲刺博客Day7
1.每日站立式会议 1.会议照片 2.昨天已完成的工作统计 孙志威: 1.添加了网络通信管理类 2.稍微修改了燃尽图模块ChartWidget 3.在主窗口中添加了用户信息框 4.重构了项目中的文件结 ...
- final评论2
1-约跑APP 小组准备的非常的充分,还带了摄像头,整个发布过程清晰很多.可能是由于上次发布时没有完全展现出自己组的作品,所以这次发布做了充分的准备,本组重点放在了修改其他组发现的bug,团队的约跑项 ...