PKU 1094 Sorting It All Out(拓扑排序)
题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列。
是典型的拓扑排序,但输出格式上确有三种形式:
1.该字母序列有序,并依次输出;
2.判断该序列是否唯一;
3.该序列字母次序之间是否有矛盾,即是否有环存在;
而这三种形式的判断是有顺序的:先判断(3)是否有环,再判断是否有序(1),最后才能判断是否能得出结果(2)。注意:对于(2)必须遍历完整个图,而(1)和(3)一旦得出结果,对后面的输入就不用做处理了。
#include<cstdio>
#include<cstring>
char alp[];
int edge[][],in[];
int TopoSort(int n)
{
int t=,tmp[],flag=;
for(int i=;i<=n;i++)
tmp[i]=in[i];
for(int i=;i<=n;i++){
int in0=,loc;//查找入度为零的顶点个数
for(int j=;j<=n;j++){
if(tmp[j]==){
in0++;
loc=j;
}
}
if(in0==) return ;//有环,那么后面的就不用再判断了
if(in0>) flag=-;//不确定,但不能直接return -1;因为后面的字母中还可能形成环而return 0;
alp[t++]='A'+loc-;//入度为零的字母入队
tmp[loc]=-;
for(int k=;k<=n;k++)
if(edge[loc][k]==)
tmp[k]--;
}
return flag;
}
//flag=1:有序 flag=-1:不确定
int main()
{
int m,n,x,y;
char str[];
while(scanf("%d%d",&n,&m),n|m){
memset(edge,,sizeof(edge));
memset(in,,sizeof(in));
bool sign=;
for(int i=;i<=m;i++){
scanf("%s",str);
if(sign) continue; //一旦得出结果,对后续的输入不做处理,但是不能break,因为还得继续输入
x=str[]-'A'+;
y=str[]-'A'+;
edge[x][y]=;
in[y]++;
int flag=TopoSort(n);
if(flag==){//有环
printf("Inconsistency found after %d relations.\n",i);
sign=;
}
if(flag==){//有序
printf("Sorted sequence determined after %d relations: ",i);
for(int j=;j<n;j++)
printf("%c",alp[j]);
printf(".\n");
sign=;
}//当sign=1时表明已得出结果
}
if(!sign) //不确定
printf("Sorted sequence cannot be determined.\n");
}
return ;
}
PKU 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& ...
- POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29984 Accepted: 10 ...
- POJ1094 Sorting It All Out —— 拓扑排序
题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Tot ...
- nyoj349 poj1094 Sorting It All Out(拓扑排序)
nyoj349 http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094 http://poj.org/problem?id=10 ...
随机推荐
- bootstrap基础学习八篇
bootstrap辅助类 a.对于文本颜色 以下不同的类展示了不同的文本颜色.如果文本是个链接鼠标移动到文本上会变暗: 类 描述 .text-muted "text-muted" ...
- gomobile build
You need to set the NDK path in gomobile init using the -ndk flag - if you follow these instructions ...
- Python爬虫(七)
源码: import requests import re from my_mysql import MysqlConnect # 获取详情页链接和电影名称 def get_urls(page): u ...
- Android屏幕和尺寸
DisplayMetrics dm=new DisplayMetrics(); //获取的像素高度不包含虚拟键所占空间 ((WindowManager)context.getSystemService ...
- FluentNhibernate 不支持存储过程
一直以为没有使用FN进行存储过程的操作,这次因为后台首页想统计下数据,就利用了存储过程,但在使用中却发现FN目前还不支持存储过程(点击查看官方),没有办法,只能利用Fluent Configurati ...
- 【BZOJ4517】[Sdoi2016]排列计数 组合数+错排
[BZOJ4517][Sdoi2016]排列计数 Description 求有多少种长度为 n 的序列 A,满足以下条件: 1 ~ n 这 n 个数在序列中各出现了一次 若第 i 个数 A[i] 的值 ...
- Maven开发系统
Maven的优点: 自动从互联网中获取jar包,并实现了一步构建. pom.xml的配置 依赖管理(导入对应的jar包) 通过坐标(定位到仓库中的包的位置,并将jar包导入到项目中,如果版本升级,只需 ...
- Django -- some config
1.主项目下的url配置:urls.py文件 from django.contrib import adminfrom django.urls import path, includefrom dja ...
- CH5105 Cookies【贪心】【线性dp】
5105 Cookies 0x50「动态规划」例题 描述 圣诞老人共有M个饼干,准备全部分给N个孩子.每个孩子有一个贪婪度,第 i 个孩子的贪婪度为 g[i].如果有 a[i] 个孩子拿到的饼干数比第 ...
- WEB状态码
这些状态代码表示临时的响应.客户端在收到常规响应之前,应准备接收一个或多个 1xx 响应. 100 - 继续. 101 - 切换协议. 2xx - 成功 这类状态代码表明服务器成功地接受了客户端请求. ...