link

题目大意

给出$m$个不等式关系,问可以从第几个开始确定所有之间的大小关系。若无解请输出是无法确定还是与已知矛盾。

试题分析

这题是真的是坑啊,尽然放在$floyd$传到闭包上面,还用二分,是真的强啊。

其实一下子就会知道其实这是一道拓扑排序的题,所以当我们每次输入问一条边时,我们就去拓扑判断是否会形成环,条件是否充足,与顺序,然后就记得分类讨论即可。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
inline int read(){
int f=,ans=;char c=getchar();
while(c<''||c>''){if(c=='-')f=-;c=getchar();}
while(c>=''&&c<=''){ans=ans*+c-'';c=getchar();}
return f*ans;
}
queue<int> que;
int n,m,in[],d[][],find1,k[];
char out[];
char str[];
int topu(){
while(!que.empty()) que.pop();
for(int i=;i<=;i++) k[i]=in[i];
for(int i=;i<=n;i++){if(!in[i]) que.push(i);}
int sx=,num=;
while(!que.empty()){
if(que.size()>) sx=;
int xx=que.front();que.pop();
out[++num]=xx+'A'-;
for(int i=;i<=;i++){
if(d[xx][i]){
k[i]--;
if(k[i]==) que.push(i);
}
}
}
if(num!=n) return ;
if(sx==) return -;
return ;
}
int query,step;
int main(){
// freopen("7.in","r",stdin);
while(){
memset(in,,sizeof(in)),find1=,query=,step=;
memset(d,,sizeof(d));
n=read(),m=read();
if(!n&&!m) return ;
for(int i=;i<=m;i++){
cin>>(str+);
int u=str[]-'A'+,v=str[]-'A'+;
if(str[]=='>') swap(u,v);
if(find1||query) continue;
if(d[v][u]){find1=;printf("Inconsistency found after %d relations.",i);continue;}
if(!d[u][v]){
d[u][v]=;
in[v]++;
}
int pd=topu();
if(pd==){query=;step=i;continue;}
if(pd==){printf("Inconsistency found after %d relations.",i);find1=;continue;}
}
if(query&&find1==){
printf("Sorted sequence determined after %d relations: ",step);
for(int i=;i<=n;i++) cout<<out[i];
printf(".");
}
if(find1==&&query==) printf("Sorted sequence cannot be determined.");
printf("\n");
}
}

[POJ1094] Sorting It All Out的更多相关文章

  1. [poj1094]Sorting It All Out_拓扑排序

    Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...

  2. nyoj349 poj1094 Sorting It All Out(拓扑排序)

    nyoj349   http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094   http://poj.org/problem?id=10 ...

  3. POJ1094 Sorting It All Out(拓扑排序)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30110   Accepted: 10 ...

  4. POJ- 1094 Sorting It All Out---拓扑排序是否唯一的判断

    题目链接: https://vjudge.net/problem/POJ-1094 题目大意: 该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列.是典型的拓扑排序,但输出格式上 ...

  5. POJ1094 Sorting It All Out LUOGU 排序

        Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 40012   Accepted ...

  6. poj1094 Sorting It All Out【floyd】【传递闭包】【拓扑序】

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:39731   Accepted: 139 ...

  7. POJ1094 Sorting It All Out —— 拓扑排序

    题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Tot ...

  8. POJ1094 Sorting It All Out (floyd传递闭包)

    关系具有传递性,可以用floyd解决. 将关系都看做i<j的形式,令d[i][j]=1,如果d[i][j]=d[j][i]=1,说明矛盾:d[i][j]=d[j][i]=0,说明i与j的关系无法 ...

  9. poj1094 拓扑 Sorting It All Out

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29744   Accepted: 10 ...

随机推荐

  1. Java EE JSP内置对象及表达式语言

    一.JSP内置对象 JSP根据Servlet API规范提供了一些内置对象,开发者不用事先声明就可使用标准变量来访问这些对象. JSP提供了9种内置对象: (一).request 简述: JSP编程中 ...

  2. leetcode12_C++整数转罗马数字

    小弟不才,有错误或者更好解,求留言. 罗马数字包含以下七种字符: I, V, X, L,C,D 和 M. 字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, ...

  3. Pearson Distance

    Pearson Distance: where: 1.  is the covariance 2.  is the standard deviation of 3.  is the standard ...

  4. 《英文版c++语言程序设计》

    compatibility [kəm,pætɪ'bɪlɪtɪ] n.兼容 compatible [kəm'pætɪb(ə)l] adj. 兼容的:能共处的:可并立的 interdependent [ɪ ...

  5. Flip the Bits(思维)

    You are given a positive integer n. Your task is to build a number m by flipping the minimum number ...

  6. Scrum立会报告+燃尽图(十月十日总第一次):选题

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2190 Scrum立会master:付佳 一.小组介绍 组长:付佳 组员: ...

  7. 20172330 2017-2018-1 《Java程序设计》第十周学习总结

    20172330 2017-2018-1 <程序设计与数据结构>第十周学习总结 教材学习内容总结 本周的学习内容为集合 集合 对象具有定义良好的接口,从而成为一种实现集合的完善体制. 动态 ...

  8. 《JavaScript》web客户端存储

    Web存储: 兼容IE8在内的所有主流浏览器,不兼容早期浏览器:支持大容量但非无限量 LocalStorage和sessionStorage是window对象的两个属性,这两个属性都代表同一个stor ...

  9. 寒假作业2——Pintia小作业及编程题

    编程题(电梯)       Click to Github       听华一大大说可以用回溯算法,熟练运用搜索引擎的我就百度了一下,琢磨了很多天以为自己会了,真的看到题目还是一脸懵逼(#`-_ゝ-) ...

  10. 0511团队项目2.0--产品product backlog

    介绍Scrum之product backlog Scrum的基本概念其实并不复杂,但是想做好并不容易,大家都知道product backlog的重要性,但是我们如何制定和展现它,如何评定优先级,如何进 ...