题目链接:

http://poj.org/problem?id=1094

题意:

给定前n个字母的大小关系,问你是否

  1. 根据前xxx个关系得到上升序列
  2. 所有关系都无法确定唯一的一个序列
  3. 第xxx个关系导致出现环

分析:

此题坑略多。。。。

  1. m大小没给!!这个很无语啊。。。数组开大点马上AC了。。。
  2. 无法确定序列必须最后判断。
  3. 一旦可以判断出上升序列,就不用管后面是否出现闭环了~~

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【拓扑排序】的更多相关文章

  1. ACM: poj 1094 Sorting It All Out - 拓扑排序

    poj 1094 Sorting It All Out Time Limit:1000MS     Memory Limit:10000KB     64bit IO Format:%lld & ...

  2. poj 1094 Sorting It All Out (拓扑排序)

    http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Su ...

  3. [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 ...

  4. 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 ...

  5. poj 1094 Sorting It All Out_拓扑排序

    题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...

  6. POJ 1094 Sorting It All Out 拓扑排序 难度:0

    http://poj.org/problem?id=1094 #include <cstdio> #include <cstring> #include <vector& ...

  7. PKU 1094 Sorting It All Out(拓扑排序)

    题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. 是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.判断该序列是否唯一: 3.该序列字母次序之间 ...

  8. POJ 1094 Sorting It All Out(拓扑排序+判环+拓扑路径唯一性确定)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39602   Accepted: 13 ...

  9. [ACM] POJ 1094 Sorting It All Out (拓扑排序)

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 26801   Accepted: 92 ...

  10. POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑

    Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29984   Accepted: 10 ...

随机推荐

  1. IOS应用

    下面是这个类的一些功能: 1.设置icon上的数字图标 //设置主界面icon上的数字图标,在2.0中引进, 缺省为0 [UIApplicationsharedApplication].applica ...

  2. Java集合类工具CollectionUtils的操作方法

    集合判断: 例1: 判断集合是否为空:CollectionUtils.isEmpty(null): trueCollectionUtils.isEmpty(new ArrayList()): true ...

  3. qt qtableview 样式设置

    转载请注明出处:http://www.cnblogs.com/dachen408/p/7531159.html 1.设置tableview的列宽时,必须先setModel再setColumnWidge ...

  4. js 根据指定个数切割数组

    Part.1 问题 写项目时,遇到需要前端做 假分页 的问题:后端会将数据全部返回,前端自己做分页 Part.2 思路 拿到后端全部返回的数据后,按照 产品需求  进行分页,如每页显示 10 条数据为 ...

  5. 使用Spring AOP切面解决数据库读写分离

    http://blog.jobbole.com/103496/ 为了减轻数据库的压力,一般会使用数据库主从(master/slave)的方式,但是这种方式会给应用程序带来一定的麻烦,比如说,应用程序如 ...

  6. (1) openssl基础概念

    1.1 背景知识 对称加密     :加密解密使用同一密钥,加解密速度快.随着人数增多,密钥数量急增n(n-1)/2. 非对称加密 :使用公私钥配对加解密,速度慢.公钥是从私钥中提取出来的,一般拿对方 ...

  7. MyBatis 3 分页

    利用MyBatis 3进行分页,选定的数据库表c_province,有3列,id列,provinceid列,province列,用Oracle数据库.首先建立一个对应的实体类,Province有3个属 ...

  8. 儿子写日记 : " 夜深了,妈妈在打麻将,爸爸在上网……"

    儿子写日记 : " 夜深了,妈妈在打麻将,爸爸在上网……"              爸爸检查时,很不满意地说 : " 日记源于生活,但要高于生活 !"    ...

  9. LaTeX模板

    学校linux机子根本跑不动libreoffice,wps没有公式,只好上LaTeX了. 先 beamer: 需要安装firasans和firamono字体,思源黑体SC \documentclass ...

  10. Tomcat 7.0.3x 启动慢并且遇到StackOverflowError的异常的解决办法

    http://qiuboboy.iteye.com/blog/1853216 使用tomcat 7.0.3x版本的同学可以发现tomcat启动慢了不少,而且还可能遇到如下启动时异常: Unable t ...