POJ1094 Sorting It All Out(拓扑排序)
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 30110 | Accepted: 10411 |
Description
Input
Output
Sorted sequence determined after xxx relations: yyy...y.
Sorted sequence cannot be determined.
Inconsistency found after xxx relations.
where xxx is the number of relations processed at the time either a
sorted sequence is determined or an inconsistency is found, whichever
comes first, and yyy...y is the sorted, ascending sequence.
Sample Input
4 6
A<B
A<C
B<C
C<D
B<D
A<B
3 2
A<B
B<A
26 1
A<Z
0 0
Sample Output
Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined. 题意:这题题意好蛋疼啊!!!一个一个给边,一个一个的判断,先判断是否有环。然后再判断是否确定序列。最后前两者都不满足,则此序列不确定。
收获:1.知道只有有环时才k<n。2.入度为0的点不唯一,则此序列不确定。3.入度很重要。
下面是能AC代码,但是 0 0会有问题,不知道为什么?还请各位指教。
#include <cstdio>
#include <iostream>
//#include <cstdlib>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <string>
#include <cstring>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <set>
using namespace std; const int INF=0x3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0); const int maxn=;
struct Edge
{
int u, v, next;
};
Edge edge[maxn];
int head[maxn];
int num, n,m;
void init_edge()
{
num = ;
memset(head, -, sizeof(head));
}
void addedge(int u, int v)
{
edge[num].u = u;
edge[num].v = v;
edge[num].next = head[u];
head[u] = num++;
}
int ans;
int topo[maxn];
int in[maxn];
int topsort()
{
queue<int> q;
int indeg[];
for(int i = ; i < +n; i++)
{
indeg[i] = in[i];
if(indeg[i] == ) q.push(i);
}
int k = ;
int flag=;
while(!q.empty())
{
if(q.size()>)
flag = ;
int u = q.front();
q.pop();
topo[k++] = u;
for(int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].v;
indeg[v]--;
if(indeg[v]==)
q.push(v);
}
}
if(k < n)
return ;
if(flag)
return -;
else
return ;
}
char s[];
int main()
{
while(~scanf("%d%d", &n, &m))
{
if(n== && m==)
break;
init_edge();
memset(in, , sizeof(in));
int flag2 = ;
int flag3 = ;
for(int j = ; j <= m; j++)
{
scanf("%s",s);
if(!flag2&&!flag3)
{
in[s[]]++;
addedge(s[],s[]);
int res = topsort();
if(res == )
{
printf("Inconsistency found after %d relations.\n", j);
flag2 = ;
}
if(res == )
{
printf("Sorted sequence determined after %d relations: ", j);
for(int i=; i < n; i++)
printf("%c", topo[i]);
printf(".\n");
flag3 = ;
}
}
}
if(!flag2&&!flag3)
puts("Sorted sequence cannot be determined."); //puts("QAQ");
}
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 ...
随机推荐
- Makefile中使用foreach生成一类规则
CSDN上,有朋友发帖问了这样一个问题(我按自己的理解翻译一下): 当前目录下有四个静态库文件: liba.a libb.a libc.a libd.a.现在想将它们做成一个动态库libp.so. ...
- Eclipse 里的 Classpath Variables M2_REPO 无法修改(maven)
解决方法: 在C:\Documents and Settings\Administrator\.m2中放入setting.xml,并修改本地仓库为 <localRepository>D ...
- Oracle 10g轻量级客户端安装[转]
http://www.oracle.com/technetwork/cn/topics/winsoft-095945-zhs.html oracle技术官方网 http://www.oracle.co ...
- [Regex Expression] Tagline --- {0, } {1,10}
Using a character set repeated 1 or more times, make a pattern to search for strings that do not con ...
- [spring入门学习笔记][spring的IoC原理]
什么叫IoC 控制反转(Inversion of Control,缩写为IoC),是面向对象编程中的一种设计原则,可以用来减低计算机代码之间的耦合度.其中最常见的方式叫做依赖注入(Dependency ...
- 虚拟化之docker
1,什么是docker dokcer是应用容器,它是基于系统的虚拟化,他不是基于硬件的虚拟化(KVM),可能这样说有点抽象,我在知乎看到过一个比喻, docker思想是来源于集装箱的,在一艘大船上,可 ...
- EffectiveC#5--始终提供ToString()
1.System.Object版的ToString()方法只返回类型的名字 2.知道要重写它,返回更有意义的信息,最好是提供几个重载版本. 3.当你设计更多的复杂的类型时(格式化文本)应该实现应变能力 ...
- node 安装express
环境:win7 64位旗舰版 1 安装nodejs 2 安装npm 3 安装express 重点介绍安装express,前提是你已经安装nodejs和npm了. 1 安装express npm ins ...
- WeiFenLuo.winFormsUI.Docking.dll的使用(停靠效果)
1. 重置工具箱: 新建一个WinForm程序,项目名称为TestDockPanelControl.选中Form1窗体后选择工具箱--->>新建个添加选项卡命名为WeiFenLuo--- ...
- 自定义带弹性效果的pageControl
分三部分实现,在drawrect方法里画出灰色背景,根据pageCount创建对应个数的dotView放置在对应位置,并隐藏,创建一个CAShapeView类型的layer,根据scrollView的 ...