POJ 1094 Sorting It All Out【拓扑排序】
题目链接:
http://poj.org/problem?id=1094
题意:
给定前n个字母的大小关系,问你是否
- 根据前xxx个关系得到上升序列
- 所有关系都无法确定唯一的一个序列
- 第xxx个关系导致出现环
分析:
此题坑略多。。。。
- m大小没给!!这个很无语啊。。。数组开大点马上AC了。。。
- 无法确定序列必须最后判断。
- 一旦可以判断出上升序列,就不用管后面是否出现闭环了~~
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.
代码:
#include<iostream>
#include<queue>
#include<stack>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxn = 1005;
vector<int>G[maxn];
int in[maxn];
int ef[maxn], et[maxn];
int n, m;
queue<int>s;
int vis[maxn];
int judge(int num)
{
while(!s.empty()) s.pop();
int cnt = 0;
memset(in, 0,sizeof(in));
memset(vis, 0, sizeof(vis));
for(int i = 0; i < maxn; i++)
G[i].clear();
for(int i = 0; i <= num; i++){
G[ef[i]].push_back(et[i]);
in[et[i]]++;
if(!vis[et[i]]) cnt++;
if(!vis[ef[i]]) cnt++;
vis[et[i]] = vis[ef[i]] = 1;
}
queue<int>q;
for(int j =0; j < n; j++){
if(vis[j] && !in[j])
q.push(j);
}
int flg = 0;
while(!q.empty()){
int u = q.front();q.pop();
if(q.size()) flg = 1;
s.push(u);
for(int k = 0; k < G[u].size(); k++){
int v = G[u][k];
in[v]--;
if(!in[v]) q.push(v);
}
}
//cout<<s.size()<<' '<<cnt<<endl;
if(s.size() == n && !flg) return 3;
return s.size() == cnt;
}
int main (void)
{
while(scanf("%d%d",&n, &m) && n+m != 0){
for(int i = 0; i <maxn; i++)
G[i].clear();
getchar();
char a, b;
for(int i = 0; i < m; i++){
scanf("%c%*c%c", &a, &b);
getchar();
ef[i] = a - 'A';
et[i] = b - 'A';
}
int i;
int flag = 0;
for(i = 0; i < m; i++){
int t = judge(i);
if(t == 3){flag = 1;break;}
else if(t == 0){break;}
}
if(flag){
cout<<"Sorted sequence determined after "<<i+1<<" relations: ";
while(!s.empty()){cout<<(char)(s.front()+'A');s.pop();}
cout<<'.'<<endl;
} else if(i == m) cout<<"Sorted sequence cannot be determined."<<endl;
else cout<<"Inconsistency found after "<<i + 1<<" relations."<<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 ...
- [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 ...
随机推荐
- VCS filelist 文件格式
VCS在运行仿真一般都会加仿真参数 –f filelist,filelist 是包含其他的仿真参数和整个工程的文件列表.具体格式如下: //file list format, just for exa ...
- IE8 window.open 不支持此接口 的问题解决
在使用vs2010调试代码时,突然出现 window.open 不支持此接口的提示,开始认为是不是vs的问题,后来上网查询说是系统问题.我不想重装系统,之后发现是IE的问题,使用其他浏览器浏览系统不会 ...
- resharper10 注册方法
注册工具:http://pan.baidu.com/s/1bnFjGfX 注册方法: 1 编辑Products.json文件,留下自己要注册的产品路径即可. 2 运行patch.exe 3 使用Ser ...
- 程序员面试系列之Java单例模式的攻击与防御
我写的程序员面试系列 Java面试系列-webapp文件夹和WebContent文件夹的区别? 程序员面试系列:Spring MVC能响应HTTP请求的原因? Java程序员面试系列-什么是Java ...
- maven打包的含义
我们在用maven构建java项目时,最常用的打包命令有mvn package.mvn install.deploy,这三个命令都可完成打jar包或war(当然也可以是其它形式的包)的功能,但这三个命 ...
- tree iview treeData json数据 添加 selected 数据 要进行vue.set 进行响应式添加
tree iview treeData json数据 添加 selected 数据 要进行vue.set 进行响应式添加
- C-基础:memcpy、memset、memmove、memcmp、memchr
一,原型 void * memcpy ( void * destination, const void * source, size_t num ); 功能:将以source作为起始地址的数据复制nu ...
- 剑指Offer(Python)
014-链表中倒数第k个结点 用快慢指针:p2比p1先走到k:间隔了k-1)步,然后再一起走,当p2为最后一个时,p1就为倒数第k个数 class ListNode: def __init__(sel ...
- NETCORE使用DB First
1)引用 (1)Install-Package Microsoft.EntityFrameworkCore (2)Install-Package Microsoft.EntityFrameworkCo ...
- merge dict key
#!/usr/local/python # -*- coding:utf-8 -*-user_dict = {'python': 23, 'Python': 51, '机器':10, 'PYTHON' ...