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 ...
随机推荐
- 很全的atom问题解决方案
atom插件 http://blog.csdn.net/qq_30100043/article/details/53558381 atom社区 https://atom-china.org/
- java.lnag.Throwable详细解读
public class Throwable extends Object implemnts Serializable Throwable类是所有错误或异常的超类.只有当对象是此类(或其中之 ...
- linq分组求和_实体类和datatable
1.数据分组求合,分别用的实体类以及datatable来分组求合,还有分组求和之后的如何取值 //实体类版本 List<ProgramTimeModel> TotalAllList = G ...
- NEO从入门到开窗(4) - NEO CLI
一.唠叨两句 首先,我们都知道区块链是去中心化的,其中节点都是对等节点,每个节点都几乎有完整的区块链特性,CLI就是NEO的一个命令行对等节点,当然也有GUI这个项目,图形化的NEO节点.节点之间需要 ...
- Alpha第二天
Alpha第二天 听说 031502543 周龙荣(队长) 031502615 李家鹏 031502632 伍晨薇 031502637 张柽 031502639 郑秦 1.前言 任务分配是VV.ZQ. ...
- 项目Beta冲刺第二天
1.昨天的困难,今天解决的进度,以及明天要做的事情 昨天的困难:昨天主要是在确认需求方面花了一些时间,后来终于确认了企业自查风险模块的需求问题 今天解决的进度:根据昨天确认下来的需求,我们基本上完成了 ...
- networkx 学习
import networkx as nx import pylab import numpy as np #自定义网络 row=np.array([,,,,,,]) col=np.array([,, ...
- JAVA中GridBagLayout布局管理器应用详解
很多情况下,我们已经不需要通过编写代码来实现一个应用程序的图形界面,而是通过强大的IDE工具通过拖拽辅以简单的事件处理代码即可很轻松的完成.但是我们不得不面对这样操作存在的一些问题,有时候我们希望能够 ...
- selenium 爬虫
from selenium import webdriver import time driver = webdriver.PhantomJS(executable_path="D:/pha ...
- js定时刷新页面.
//页面定时刷新.2017.09.27 $(document).ready(function () { self.setInterval(function () { var d = new Date( ...