【POJ】1094 Sorting It All Out(拓扑排序)
http://poj.org/problem?id=1094
原来拓扑序可以这样做,原来一直sb的用白书上说的dfs。。。。。。。。。。。。
拓扑序只要每次将入度为0的点加入栈,然后每次拓展维护入度即可。。
我是个大sb,这种水题调了一早上。。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << (#x) << " = " << (x) << endl
#define printarr2(a, b, c) for1(_, 1, b) { for1(__, 1, c) cout << a[_][__]; cout << endl; }
#define printarr1(a, b) for1(_, 1, b) cout << a[_] << '\t'; cout << endl
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; } const int N=27, oo=~0u>>1;
int n, m, e[N][N], in[N], s[N], vis[N], top, tp[N], ans[N], tot;
int toposort() {
top=tot=0; int num=0, ret=0;
for1(i, 1, n) if(vis[i] && in[i]==0) s[++top]=i;
for1(i, 1, n) if(vis[i]) ++num;
for1(i, 1, n) tp[i]=in[i];
while(top) {
if(top>1) ret=-1;
int u=s[top--]; ans[tot++]=u;
for1(i, 1, n) if(e[u][i] && --tp[i]==0) s[++top]=i;
}
if(tot!=num) return ret=1;
return ret;
}
int main() {
while(~scanf("%d%d", &n, &m) && !(n==0 && m==0)) {
CC(e, 0); CC(in, 0); CC(vis, 0);
int u, v; char s[10];
int flag=0;
for1(i, 1, m) {
scanf("%s", s);
if(flag==1) continue;
u=s[0]-'A'+1; v=s[2]-'A'+1;
e[u][v]=vis[u]=vis[v]=1; ++in[v];
flag=toposort();
if(flag==0) {
if(tot!=n) continue;
printf("Sorted sequence determined after %d relations: ", i);
rep(j, tot) printf("%c", 'A'+ans[j]-1);
puts(".");
flag=1;
}
else if(flag==1) printf("Inconsistency found after %d relations.\n", i);
}
if(flag==-1) puts("Sorted sequence cannot be determined.");
}
return 0;
}
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.
Source
【POJ】1094 Sorting It All Out(拓扑排序)的更多相关文章
- 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 ...
- [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 ...
- poj 1094 Sorting It All Out_拓扑排序
题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...
- POJ 1094 Sorting It All Out 拓扑排序 难度:0
http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...
- PKU 1094 Sorting It All Out(拓扑排序)
题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. 是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.判断该序列是否唯一: 3.该序列字母次序之间 ...
- POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39602 Accepted: 13 ...
- [ACM] POJ 1094 Sorting It All Out (拓扑排序)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 26801 Accepted: 92 ...
- POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29984 Accepted: 10 ...
随机推荐
- 一致Hash算法
一致性哈希算法是分布式系统中经常使用的算法.比方,一个分布式的存储系统,要将数据存储到详细的节点上.假设採用普通的hash方法.将数据映射到详细的节点上,如key%N.key是数据的key.N是机器节 ...
- hdu 4494 Teamwork (可行流的最小流)
去年通话邀请赛的B题,当时居然过的那么少...明明是一道非常裸的可行流最小流麽..仅仅要对每种人分别求一下可行最小流加起来就能够了.建图是对每一个点拆点,容量上下届都设为v[i],然后每一个点间能连边 ...
- iDempiere VS World
怀揣着为中小企业量身定做一整套开源软件解决方案的梦想开始了一个网站的搭建.http://osssme.org/ 第四篇 iDempiere VS World 接下来就接着第一篇,再说一说iDempei ...
- EL和自定义函数库
问题:在数据显示的时候经常需要调用一些方法对需要显示的数据进行基本的处理,如: 数据过滤.求子串等操作.那么就需要使用EL表达式进行快速的函数调用. 引入HTMLFilter.java类 描述为函数 ...
- jquery tmpl 详解(转)
动态请求数据来更新页面是现在非常常用的方法,比如博客评论的分页动态加载,微博的滚动加载和定时请求加载等. 这些情况下,动态请求返回的数据一般不是已拼好的 HTML 就是 JSON 或 XML,总之不在 ...
- python --curl重定向到文件范例
import sys import os import subprocess import time start = time.time() old=sys.stdout f=open('test ...
- django 常用命令 详解
check compilemessages createcachetable dbshell diffsettings dumpdata flush ...
- [svc]expect的爱恨情仇
背景 openvpn生成证书想把它做成一键化,这样添加新用户时候就方便 遇到的问题 我的代码 gg_vpn_keys.exp #!/usr/bin/expect set user [lindex $a ...
- uint64, sizet_t, ssizet_t
uint64 在32位平台 typedef unsigned long long int uint64_t;在64位平台 typedef unsigned long int uint64_t;不同的t ...
- linux中使用lftp上传下载文件
lftp是linux中一款ftp服务器相比windows中的ftp显得要复杂不少了,下面我来总结一下lftp文件上传,文件下载,及文件查找等等相关命令吧. lftp连接的几种方法,最常用的是lftp ...