【POJ 1094】拓扑排序
题意
给出n,代表有以A开始的n个字母,给出它们的m个小于关系(A<B)。
如果前i个关系可以确定n个字母的一个顺序就输出:
Sorted sequence determined after i relations: 排好的字母.
如果前i个关系开始导致矛盾,就输出:
Inconsistency found after i relations.
m个关系后还不能确定顺序就输出:
Sorted sequence cannot be determined.
代码
/*
g[i][j]为邻接矩阵,e[i]为i的入度
in==1代表有矛盾,d==1代表确定顺序
so存排好的顺序
*/
#include<cstdio>
#include<cstring>
#define N 30
int n, m, g[N][N], e[N], c[N], a, b, in, d, so[N];
char va, vb;
void topo()
{
memcpy(c, e, sizeof e);//复制e到c,因为后面要对c改动,所以不能直接用e
int i, top = -;
for(i = ; i < n; i++)
if(!c[i])
{
c[i] = top;//模拟下标堆栈,把入度为0的点通过下标堆栈到c中
top = i;
}
for(i = ; i < n; i++)
{
if(top == -)//n次循环中,若某次找不到入度为0的点,就会回到-1
{
in = ;//说明存在环
return;
}
int u = top;//以top为起点
top = c[u];
so[i] = u;
for(int v = ; v < n; v++)
if(g[u][v] && (--c[v]) == )//去掉u出发的边后,入度为0的点
{
c[v] = top; //堆栈
top = v;
}
}
d = ;
for(i = ; i < n; i++)//判断是否是确定的顺序
if(!g[so[i - ]][so[i]])d = ;//如果出现相邻两个没有给过偏序关系,那么就是不确定的顺序 }
int main()
{
//freopen("in.txt", "r", stdin);
while(~scanf("%d%d ", &n, &m) && n)
{
memset(g, , sizeof g);
memset(e, , sizeof e);
d = in = ;
for(int i = ; i <= m; i++)
{
scanf("%c<%c ", &va, &vb);
a = va - 'A';
b = vb - 'A';
if(!g[a][b] && !d && !in)
{
g[a][b] = ;
e[b]++;
topo();
if(in)
printf("Inconsistency found after %d relations.\n", i);
else if(d)
{
printf("Sorted sequence determined after %d relations: ", i);
for(int j = ; j < n; j++)printf("%c", so[j] + 'A');
printf(".\n");
}
}
}
if(!d && !in)printf("Sorted sequence cannot be determined.\n");
}
}
这个是我参考lrj的《入门经典》的板子写的,耗时0ms
#include <cstdio>
#include <cstring>
#define N 30
int n, m, g[N][N], c[N], t, ans;
char a, b, topo[N];
int dfs(int u)
{
c[u] = -;
for(int v = ; v < n; v++)if(g[u][v])
{
if(c[v] < )return ;
else if(!c[v] && !dfs(v))return ;
}
c[u] = ;
topo[--t] = u;
return ;
}
int toposort()
{
t = n;
memset(c, , sizeof c);
for(int u = ; u < n; u++)
if(!c[u] && !dfs(u))return ;
for(int i = ; i < n; i++)
if(!g[topo[i - ]][topo[i]])return -;
return ;
}
int main()
{
// freopen("in.txt", "r", stdin);
while(~scanf("%d%d ", &n, &m) && n)
{
ans = -;
memset(g, , sizeof g);
for(int i = ; i <= m; i++)
{
scanf("%c<%c ", &a, &b);
g[a - 'A'][b - 'A'] = ;
if(ans == -)
{
ans = toposort();
if(!ans)
printf("Inconsistency found after %d relations.\n", i);
else if(ans == )
{
printf("Sorted sequence determined after %d relations: ", i);
for(int j = ; j < n; j++)printf("%c", topo[j] + 'A');
printf(".\n");
}
}
}
if(ans == -)printf("Sorted sequence cannot be determined.\n");
}
return ;
}
【POJ 1094】拓扑排序的更多相关文章
- Sorting It All Out POJ - 1094 拓扑排序
题意:给N个字母,和M个偏序关系 求一个可确定的全序,可确定是指没有其他的可能例如A>B D>B 那么有ADB DAB两种,这就是不可确定的其中,M个偏序关系可以看做是一个一个按时间给出的 ...
- nyoj 349 (poj 1094) (拓扑排序)
Sorting It All Out 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 An ascending sorted sequence of distinct ...
- POJ 1094 拓扑排序
Description: 规定对于一个只有大写字母的字符串是有大小顺序的.如ABCD.即A<B.B<C.C<D.那么问题来了.现在第一行给你n, m代表序列里只会出现前n的 ...
- poj 3687(拓扑排序)
http://poj.org/problem?id=3687 题意:有一些球他们都有各自的重量,而且每个球的重量都不相同,现在,要给这些球贴标签.如果这些球没有限定条件说是哪个比哪个轻的话,那么默认的 ...
- POJ 3249 拓扑排序+DP
貌似是道水题.TLE了几次.把所有的输入输出改成scanf 和 printf ,有吧队列改成了数组模拟.然后就AC 了.2333333.... Description: MR.DOG 在找工作的过程中 ...
- poj 3249 拓扑排序 and 动态规划
思路:我们首先来一遍拓扑排序,将点按先后顺序排列于一维数组中,然后扫描一遍数组,将每个点的出边所连接的点进行更新,即可得到最优解. #include<iostream> #include& ...
- poj 2585 拓扑排序
这题主要在于建图.对9个2*2的小块,第i块如果出现了不等于i的数字,那么一定是在i之后被brought的.可以从i到该数字建一条边. 图建好后,进行一次拓扑排序,判段是否存在环.若存在环,那么就是B ...
- Poj(3687),拓扑排序,
题目链接:http://poj.org/problem?id=3687 题意:n个重量为1~n的球,给定一些编号间的重量比较关系,现在给每个球编号,在符合条件的前提下使得编号小的球重量小.(先保证1号 ...
- POJ 1128 拓扑排序 + 深搜
/* (⊙v⊙)嗯 貌似是一个建图 拓扑+深搜的过程.至于为什么要深搜嘛..一个月前敲得题现在全部推了重敲,于是明白了.因为题意要求如果有多个可能的解的话. * 就要输出字典序最小的那个.所以可以对2 ...
- poj 2367 拓扑排序入门
Description The system of Martians' blood relations is confusing enough. Actually, Martians bud when ...
随机推荐
- 2014 UESTC 暑前集训队内赛(2) 部分解题报告
B.Cuckoo for Hashing 模拟题. 代码: #include <iostream> #include <cstdio> #include <cstring ...
- C++ 箭头-> 双冒号:: 点号.操作符区别
点 (.) 如果变量是一个对象或者对象引用,则用它来访问对象成员. 箭头( ->) 如果变量是一个对象指针,则用它来访问对象成员. 双冒号 (::) 如果操作目标是一个具有名空间的标识符,则用它 ...
- http协议详解<一>
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://7826443.blog.51cto.com/7816443/1729227 写在 ...
- 007医疗项目-模块一:用户的查找:3.用户表查询的Action和Service
这里主要写Action和Service. 先写Service层: 架构如下:
- 【转】【C#】【Thread】Interlocked 轻量级锁
为什么说它是轻量级呢?因为它仅对整形数据(即int类型,long也行)进行同步. 具体使用如下表: Interlocked.Increment(ref value) 数值加一(原子性操作) Inter ...
- 【转】【SSE】基于SSE指令集的程序设计简介
基于SSE指令集的程序设计简介 作者:Alex Farber 出处:http://www.codeproject.com/cpp/sseintro.asp SSE技术简介 Intel公司的单指令多数据 ...
- Vim中split的使用方法
Vim中split的使用方法 一.作用 用split可以显示两个不同的文件:或者同时显示一个文件的两个不同地方:又或者并排比较两个文件.这一切都可以通过分割窗口实现.如下图,左边的两个窗口是mytoo ...
- [CareerCup] 1.8 String Rotation 字符串的旋转
1.8 Assume you have a method isSubstring which checks if one word is a substring of another. Given t ...
- 启动图实现:UIScrollView+UIPageControl简单实现
#import "MJViewController.h"#import "RootViewController.h" @interface MJViewCont ...
- Core Data 概述
Core Data是一个模型层的技术.Core Data帮助你建立代表程序状态的模型层.Core Data也是一种持久化技术,它能将模型对象的状态持久化到磁盘,但它最重要的特点是:Core Data不 ...