POJ 1094 Sorting It All Out
题意:给出m对关于n个字母的小于关系,输出通过这些关系能得到的结论,如果可以排序就输出至少知道第几个关系时就可以知道顺序,从小到大输出顺序;如果产生歧义就输出在第几个关系时出现歧义,如果不能得出准确的大小关系就输出无法排序。
解法:拓扑排序。拓扑排序的大致流程就是先找入度为0的点,然后删去跟这个点相邻的边,再继续找入度为0的点,如果能一直找下去直到删掉所有边,则说明拓扑有序,否则拓扑无序。对于这道题,1.如果每次找到的入度为0的点有且只有一个且拓扑有序,则说明已有确定的顺序;2.如果找不到入度为0的点且还没删掉所有边则说明有环,即产生歧义;3.如果出现某一次找到的入度为0的点超过1个且拓扑有序,则说明还没有确定的顺序。
综合以上几点和网上的一些题解,有如下结论:
1.在拓扑排序的时候必须遍历整个图才可以得出结论,不可以发现入度为0的点超过1时就认为无法排序,还可能是产生歧义,此处注意上述第三条加粗部分。
2.题目中貌似(可能是我没看见)没说如果歧义出现在可以排序之后算歧义还是可以排序,我现在可以确定是算可以排序。
就这些了吧……貌似
代码:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<limits.h>
#include<time.h>
#include<stdlib.h>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#define LL long long
using namespace std;
int n, m;
string sorted;
int indegree[30];
vector <int> edge[30];
int topoSort()
{
int degree[30];
memcpy(degree, indegree, sizeof degree);
stack <int> s;
for(int i = 0; i < n; i++)
if(degree[i] == 0) s.push(i);
int res = 1;
int cnt = 0;
sorted.clear();
while(!s.empty())
{
cnt++;
if(s.size() > 1) res = 0;
int tmp = s.top();
sorted += ('A' + tmp);
s.pop();
for(int i = 0; i < edge[tmp].size(); i++)
{
if(--degree[edge[tmp][i]] == 0) s.push(edge[tmp][i]);
}
}
if(cnt == n)
return res;
else return -1;
}
int main()
{
while(~scanf("%d%d", &n, &m) && !(n == 0 && m == 0))
{
memset(indegree, 0, sizeof indegree);
for(int i = 0; i < 26; i++) edge[i].clear();
int ans = 0, pos = m;
for(int i = 0; i < m; i++)
{
char input[5];
scanf("%s", input);
if(!ans)
{
edge[input[0] - 'A'].push_back(input[2] - 'A');
indegree[input[2] - 'A']++;
ans = topoSort();
}
else pos = min(i, pos);
}
if(ans == 1)
cout << "Sorted sequence determined after " << pos << " relations: " << sorted << "." << endl;
else if(ans == -1)
cout << "Inconsistency found after " << pos << " relations." << endl;
else
cout << "Sorted sequence cannot be determined." << endl;
}
return 0;
}
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 ...
- POJ 1094 Sorting It All Out 拓扑排序 难度:0
http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...
- poj 1094 Sorting It All Out(图论)
http://poj.org/problem?id=1094 这一题,看了个大牛的解题报告,思路变得非常的清晰: 1,先利用floyd_warshall算法求出图的传递闭包 2,再判断是不是存在唯一的 ...
- poj 1094 Sorting It All Out 解题报告
题目链接:http://poj.org/problem?id=1094 题目意思:给出 n 个待排序的字母 和 m 种关系,问需要读到第 几 行可以确定这些字母的排列顺序或者有矛盾的地方,又或者虽然具 ...
- POJ 1094 Sorting It All Out【拓扑排序】
题目链接: http://poj.org/problem?id=1094 题意: 给定前n个字母的大小关系,问你是否 根据前xxx个关系得到上升序列 所有关系都无法确定唯一的一个序列 第xxx个关系导 ...
- poj.1094.Sorting It All Out(topo)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28762 Accepted: 99 ...
- poj 1094 Sorting It All Out(nyoj 349)
点击打开链接 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24544 Accep ...
- POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 39602 Accepted: 13 ...
- nyoj 349&Poj 1094 Sorting It All Out——————【拓扑应用】
Sorting It All Out 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 An ascending sorted sequence of distinct ...
随机推荐
- POJ 3111
K Best Time Limit: 8000MS Memory Limit: 65536K Total Submissions: 5177 Accepted: 1411 Case Time ...
- Oracle的学习二:表管理(数据类型、创建/修改表、添加/修改/删除数据、数据查询)
1.Oracle表的管理 表名和列名的命名规则: 必须以字母开头: 长度不能超过30个字符: 不能使用oracle的保留字: 只能使用如下字符:A-Z, a-z, 0-9, $, # 等. Oracl ...
- 如何在linux系统下对文件夹名有空格的文件夹进行操作
http://www.2cto.com/os/201409/335119.html 在Windows操作系统中可以轻易地创建\移动\删除文件夹名带有空格的文件夹, 而在linux则需要进行一些特殊的处 ...
- 李洪强iOS开发之 - 实现九宫格并使用SDWebImage下载图片
李洪强iOS开发之 - 实现九宫格并使用SDWebImage下载图片 源码: // // ViewController.m // 08-九宫格扩展 // // Created by 李洪强 ...
- Unix编程之size_t、ssize_t
http://blog.csdn.net/lalor/article/details/7426184 首先,我非常肯定以 及确定的告诉你ssize_t是有符号整型,在32位机器上等同与int,在64位 ...
- Python中的SET集合操作
python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和 ...
- java:接口实例
接口:打印机接口 interface Printer { public void read(); } 函数一:佳能打印机 class CanPrinter implements Printer { p ...
- sudo和su
su命令就是切换用户的工具 sudo 授权许可使用的su,也是受限制的su 1. sudo 的适用条件 由于su 对切换到超级权限用户root后,权限的无限制性,所以su并不能担任多个管理员所管理的系 ...
- [iOS]解决模拟器无法输入中文问题
第一步:设置schem 菜单项 -> Product-> Scheme -> Edit Scheme -> 然后在弹出的界面里 选择OPtion 项, 设置 Applicat ...
- c语言中static的用法
1.static定义变量: 1).局部: a.静态局部变量在函数内部定义,生存期为整个源代码,但作用域与自动变量相同,只能在定义的函数里面使用.退出该函数后,虽然此变量还存在内存中,但不能使用. b. ...