poj1094-Sorting It All Out-拓扑排序
题意:
1).给你一些大写字母,共n个;大写字母间有m条关系;
2).举例:关系:A<B,意思就是A要排在B的前面(也就是说可能B排在A的前面
3).输出:有三种情况:
1.n个字母在前 i 条关系下可以确定排序关系;
2.n个字母在执行到第 i 条命令时出现错误,即回路:A间接或直接小于B 且 B间接或直接小于A;
3.m条命令不能确定n个字母的排序。
思路:
直观感觉就是拓扑排序,需要注意的是,每读入一条命令就要拓扑排序一次。
如果你对拓扑排序不够了解,请看这篇博客:点击查看
而三种情况判断的顺序更为重要!!
情况1和2可以同时判断,最后判断情况3!!
意思是:
如果执行到第 i 条命令时,可以确定情况1 或2,则直接输出;
如果m条命令执行完了,还不能确定字母顺序,则输出情况3
题目链接:
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<string>
#include<stack>
#include<cmath>
#define test printf("***\n")
#define mm1(a) memset((a),-1,sizeof((a)))
#define mm0(a) memset((a),0,sizeof((a)))
#define mmx(a) memset((a),0x3f,sizeof((a)))
#define ka getchar();getchar()
#define ka1 getchar()
#define iis std::ios::sync_with_stdio(false)
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
const int N = ;
const int M = ;
const int X = ;
const int INF = 1e9;
const double eps = 1e-;
const int mod = 1e9 + ;
//#define DEBUG
int n,m,sum;
int tot,flag;
int in[N],head[N],tin[N];
int ar[N];
struct lp
{
int u,v,nex;
lp(){}
lp(int a,int b,int c):
u(a),v(b),nex(c){}
}cw[N*N];
inline void add(int a,int b){
cw[++tot]=lp(a,b,head[a]);
head[a]=tot;
}
int tuopu(){
queue<int>Q;
while(!Q.empty())Q.pop();
for(int i=;i<=n;++i)tin[i]=in[i];
for(int i=;i<=n;++i){
if(tin[i]==){
Q.push(i);
}
}
int t=;
int ans=;
while(!Q.empty()){
if(Q.size()>)ans=-;
int u=Q.front();Q.pop();
ar[t++]=u;
for(int i=head[u];i!=-;i=cw[i].nex){
int v=cw[i].v;
tin[v]--;
if(tin[v]==){
Q.push(v);
}
}
}
if(t!=n)return ;
return ans;
}
inline void init(){
tot=-;
mm0(in);
mm1(head);
}
int main(int argc, char const *argv[])
{
#ifdef DEBUG
freopen("D:in.in", "r", stdin);
freopen("D:out.out", "w", stdout);
#endif
char a,b;
while(~scanf("%d %d",&n,&m)&&(n+m)){
init();
int ans=;
for(int i=;i<m;++i){
char s[];
scanf("%s",s);
a=s[];b=s[];
if(ans==)continue;
in[b-'A'+]++;
add(a-'A'+,b-'A'+);
int ok=tuopu();
if(ok==){
ans=;
printf("Inconsistency found after %d relations.\n",i+);
}else if(ok==){
ans=;
printf("Sorted sequence determined after %d relations: ",i+);
for(int i=;i<n;++i){
printf("%c", ar[i]+'A'-);
}
printf(".\n");
}
}
if(ans){
printf("Sorted sequence cannot be determined.\n");
}
}
return ;
}
poj1094-Sorting It All Out-拓扑排序的更多相关文章
- [poj1094]Sorting It All Out_拓扑排序
Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...
- nyoj349 poj1094 Sorting It All Out(拓扑排序)
nyoj349 http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094 http://poj.org/problem?id=10 ...
- POJ1094 Sorting It All Out —— 拓扑排序
题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Tot ...
- 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 ...
- POJ- 1094 Sorting It All Out---拓扑排序是否唯一的判断
题目链接: https://vjudge.net/problem/POJ-1094 题目大意: 该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列.是典型的拓扑排序,但输出格式上 ...
- POJ1094 Sorting It All Out LUOGU 排序
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 40012 Accepted ...
- POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29984 Accepted: 10 ...
- [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 ...
随机推荐
- 【数据库】数据库的锁机制,MySQL中的行级锁,表级锁,页级锁
转载:http://www.hollischuang.com/archives/914 数据库的读现象浅析中介绍过,在并发访问情况下,可能会出现脏读.不可重复读和幻读等读现象,为了应对这些问题,主流数 ...
- 数据库 --> MySQL使用
MySQL使用 代码: #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h>#includ ...
- JS图片更换还原操作,通过图片识别标识
//图片更换还原操作,图片识别标识 如图片img.png 可换成 img2.png function img_biaoshi(caozuo,img_id, biaoshi) { var img_src ...
- 宝塔Linux面板命令大全
安装宝塔 Centos安装脚本 yum install -y wget && wget -O install.sh http://download.bt.cn/install/inst ...
- Redis——主从同步原理
刚接触到Redis,首先对Redis有一个初步的了解. 开源,免费,遵守BSD协议,key-value数据库. 可以将内存中的数据保存在磁盘中,重启的时候可以再次加载使用. 多种key-value类型 ...
- C语言函数嵌套调用作业
一.实验作业 1.1 PTA题目:6-4 十进制转换二进制 设计思路 如果n大于1 对n/2继续进行该函数运算 输出n%2的值 代码截图 调试问题 我第一次做的时候判断的边界条件是大于0继续进行运算, ...
- HTTP协议形象展现
关于http协议:我们分成几个模块说: http协议: HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统. HTTP协议的主要特点可概括如下: 1.支持客户 ...
- 201621123068 作业07-Java GUI编程
1. 本周学习总结 1.1 思维导图:Java图形界面总结 2.书面作业 1. GUI中的事件处理 1.1 写出事件处理模型中最重要的几个关键词. 注册.事件.事件源.监听 1.2 任意编写事件处理相 ...
- python 使用Nginx和uWSGI来运行Python应用
参考:http://zmrenwu.com/post/20/ uWSGI是一个Web应用服务器,它具有应用服务器,代理,进程管理及应用监控等功能.它支持WSGI协议,同时它也支持自有的uWSGI协议, ...
- HP DL380服务器RAID信息丢失数据恢复方法和数据恢复过程分享
[数据恢复故障描述] 客户服务器属于HP品牌DL380系列,存储是由6块73GB SAS硬盘组成的RAID5,操作系统是WINDOWS 2003 SERVER,主要作为企业部门内部的文件服务器来 ...