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 ...
随机推荐
- POJ1664(简单动态规划)
#include<iostream> #include<string> #include<cstring> using namespace std; ][]; vo ...
- Android实现Live Photos 加源代码
在Android手机上实现类似于iphone中的LivePhoto的功能 源代码分享 github:https://github.com/amazingyyc/DeepRed 代码说明: 1.改变视频 ...
- UNIX时间戳与日期的相互转换
mysql中UNIX时间戳与日期的相互转换 UNIX时间戳转换为日期用函数:FROM_UNIXTIME() select FROM_UNIXTIME(1410318106); 日期转换为UNIX时间戳 ...
- hdu 5335 Walk Out(bfs+寻找路径)
Problem Description In an n∗m maze, the right-bottom corner or a written on it. An explorer gets los ...
- 【软件技巧】Sublime Text为不同语法定义不同高亮
Sublime Text默认的语法高亮已经非常美丽了,可是对于个别语言还是有些不爽. 默认高亮规则叫Monokai,能够从Preferences->Settings - Default中看到: ...
- samba服务器概述
一.samba服务器概述 Samba是一个能让Linux系统应用Microsoft网络通信协议的软件.而SMB是Server Message Block的缩写,即为服务器消息块.SMB主要作为Micr ...
- 在Java中编写带占位符的SQL语句
C#中SQL中带占位符的语句 假设有一张学员信息表Student,通过表中的ID来找学员,查询的SQL语句如下 string sql = string.Format("select * fr ...
- C# 实现预览dwg文件完整源代码(无需autocad环境)
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using Sys ...
- Android 使用开源xUtils来实现多线程下载(非原创)
1.程序员自己也是可以实现多线程下载的,只是代码量比较大,而且,其中有许多细节需要考虑到,在GitHub上有人写好的代码,我们可以拿过来使用下,节省了我们开发程序的时间 2.导包:xUtils-2.6 ...
- 空合并操作符??(C#)
??二元操作符在对first??second求值时,大致会经历以下步骤: 1)对first进行求值: 2)如果结果非空,则该结果就是整个表达式的结果: 3)否则求second的值,其结果作为整个表达式 ...