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 ...
随机推荐
- android开发工具eclipse的安装与配置
l开发主要应用Eclipse 3.7版本. l辅助工具为jdk.Androidsdk Android环境搭建 –1.1.JDK安装 –1.2.Eclipse安装 –1.3.Android SDK安 ...
- Linux安装技巧--安装Uuntu与windows8/10共存
1.准备安装双系统所需工具. 系统: Linux有众多的衍生版本,选择一个自己喜欢的版本下载,建议新手上ubuntu吧,中文教程较多,出了问题容易解决,等到熟悉了再用其他的也行,新手的话ubuntu也 ...
- VC++绘制金刚石(MFC)
void CTxx1View::OnDraw(CDC* pDC){ CTxx1Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); // TODO: add d ...
- 中间件及tomcat的内存溢出调优
主要是这三个选项的调整需要根据主机的内存配置 以及业务量的使用情况调节 -Xmx4g -Xms4g -Xmn2g xmx 与xms一般设置为一样 xmn大致设置为xmx xms的三分之一 可以使用 ...
- 数据库sql语句limit区别
注意:并非所有的数据库系统都支持 SELECT TOP 语句. MySQL 支持 LIMIT 语句来选取指定的条数数据, Oracle 可以使用 ROWNUM 来选取. SQL Server / MS ...
- CAD交互绘制圆(网页版)
CAD绘制图像的过程中,画圆的情况是非常常见的,用户可以在控件视区点取任意一点做为圆心,再动态点取半径绘制圆. 主要用到函数说明: _DMxDrawX::DrawCircle 绘制一个圆.详细说明如下 ...
- 在CNN网络中roi从原图映射到feature map中的计算方法
在使用fast rcnn以及faster rcnn做检测任务的时候,涉及到从图像的roi区域到feature map中roi的映射,然后再进行roi_pooling之类的操作.比如图像的大小是(600 ...
- 关于MessageBox返回值
风格设置MB_OK. 此时无论点击确定还是点击X,都返回IDOK.风格设置MB_OKCANCEL,点击确认返回IDOK,点击取消和X都返回IDCANCEL.风格设置MB_YESNO,点击是返回IDYE ...
- 如何在windows 2008 IIS7 上实现AD域的访问控制
1.服务器加入域 2.创建点站 3.对站站进行设置 3.1设置网站的连接模式 选中站点,在控制台右侧 选择 基本设置 => 选择 应用程序用户 3.2 开启访问模式 选择站点 => 身份验 ...
- C++位域和内存对齐问题
1. 位域: 1. 在C中,位域可以写成这样(注:位域的数据类型一律用无符号的,纪律性). struct bitmap { unsigned a : ; unsigned b : ; unsigned ...