POJ 1094 拓扑排序
Description:
规定对于一个只有大写字母的字符串是有大小顺序的。如ABCD.即A<B。B<C。C<D。那么问题来了。现在第一行给你n, m代表序列里只会出现前n的大写字母、以后的第2到m行给你m组。有三个字符。即“第一个字符” “<”"第二个字符"...请你给出答案。在第几组之后确定了排列顺序或者第几组之后出现矛盾。还是直到输入结束也没有排列顺序也没有出现矛盾。
思路很简单、就是把前n个字符转换成0~n-1的数字。然后,,每次输入都要拓扑一次。如果出现了固定顺序或者矛盾。以后就可以只输有输入操作了。如果直到最后这两种情况都没有出现的话。就是无法确定。
附代码:WA了好久。。仍然有地方不懂。。但是是人家自己敲得啦,,,~~~~(>_<)~~~~
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
int map[27][27];
int degree[27];
int top[27];
int tot;
int n, m;
queue<int>q;
void init()
{
tot = 0;
memset(map, 0, sizeof(map));
memset(degree, 0, sizeof(degree));
memset(top, 0, sizeof(top));
}
int topsort()
{
int l = 0;
int flag = 0;
while(!q.empty())
q.pop();
int dd[30];
memcpy(dd, degree, sizeof(degree));
for (int i=0; i<n; ++i)
{
if (dd[i] == 0)
{
q.push(i);
}
}
while(!q.empty())
{
if (q.size() > 1)
flag = 1;
int k = q.front();
q.pop();
top[l++] = k;
for (int i=0; i<n; ++i)
{
if (map[k][i] == 1)
{
if (--dd[i] == 0)
q.push(i);
}
}
}
if (l != n)
return 2;
else if (flag == 1)
return 1;
// T_T 为什么必须先判断是不是有矛盾。再判断是不是无法确定呢。也就是。有时候同时有两个入度为0的点。并不能说明就是无法判断。
//所以不能在flag == 1处。改为直接return 1.....
// if (flag == 1)
// return 1;
// else if (l != n)
// return 2;
return 0;
}
int main()
{
char a, b, temp;
int i, j;
int aa, bb;
int mao, que;
int now = 0;
while(cin >> n >> m)
{
init();
if (n == 0 && m == 0)
break;
mao = 0;
que = 0;
now = 0;
for (i=0; i<m; ++i)
{
cin >> a >> temp >> b;
now++;
if (!mao && !que)
{
if (map[b-'A'][a-'A'] == 1)
{
mao = 1;
cout << "Inconsistency found after " << now << " relations.\n";
continue;
}
if (map[a-'A'][b-'A'] == 0)
{
map[a-'A'][b-'A'] = 1;
degree[b-'A']++;
}
int res = topsort();
if (res == 0)
{
cout << "Sorted sequence determined after " << now << " relations: ";
for (int j=0; j<n; ++j)
{
cout << char(top[j] + 'A');
}
cout << ".\n";
que = 1;
}
else if (res == 2)
{
cout << "Inconsistency found after " << now << " relations.\n";
mao = 1;
}
}
}
if (!mao && !que)
{
cout << "Sorted sequence cannot be determined.\n";
}
}
return 0;
}
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 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 ...
随机推荐
- 转 C编译: 使用gdb调试
C编译: 使用gdb调试 作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! gdb是the GNU Debugger的简称.它是 ...
- 一个封装较好的删除方法(Delete)
前台的引用 @Html.ActionLink(“删除字样”,“后台的删除方法”,new{绑定id},new{@style="样式"});方法,如何要独立使用的话,一般还要使用到相应 ...
- chubu
python解释型语言,不需要编译成机器认可的二进制码,而是直接从源代码运行程序. python是基于c语言开发的. python很容易嵌入到其他语言. 中文注释,必须在前边加上注释说明 : #_*_ ...
- [转载] 多年积累的 mysql 运维经验
原文: http://mp.weixin.qq.com/s?__biz=MzA3MzYwNjQ3NA==&mid=207132223&idx=1&sn=f5d98146f282 ...
- 02 key concept
本章提要-------------------------------------术语, 选择性与基数, 软解析与硬解析, 绑定变量, 扩展的游标共享SQL语句生命周期, 特别关注解析部分------ ...
- Ubuntu Server14.04 32位安装odoo8.0简单方法
一.wget -O - https://nightly.odoo.com/odoo.key | apt-key add - 二.echo "deb http://nightly.odoo.c ...
- Java中List的使用
package ch8; import java.util.*; /** * Created by Jiqing on 2016/11/27. */ public class ListTest { p ...
- flexbox弹性伸缩布局
<!doctype html><html lang="en"><head> <meta charset="UTF-8" ...
- PO BO VO DTO POJO DAO 概念及其作用
PO BO VO DTO POJO DAO 概念及其作用(附转换图) 博客分类: java javadaovopojobo J2EE开发中大量的专业缩略语很是让人迷惑, 特别是对于刚毕业 ...
- 使用kaptcha生成验证码
原文:http://www.cnblogs.com/xdp-gacl/p/4221848.html kaptcha是一个简单好用的验证码生成工具,通过配置,可以自己定义验证码大小.颜色.显示的字符等等 ...