拓扑排序 +Floyd(poj 1094)
题意:字母表前n个字母,有m组他们中的大小关系,判断n个字母是否构成唯一序列:
1.Sorted sequence determined after xxx relations: yyy...y.
2.Sorted sequence cannot be determined.
3.Inconsistency found after xxx relations.
思路:判断环好判断,但是判断是否唯一就搞不懂了,后来看了下别人的,
用的是Floyd传递闭包:如果能构成唯一序列,则定点1~n,必定分布着0~n-1的入度,
才能保证0个入度的定点排在第一位,1个入度的地点排在第2位,以此类推。
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <set> #define c_false ios_base::sync_with_stdio(false); cin.tie(0)
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define zero_(x,y) memset(x , y , sizeof(x))
#define zero(x) memset(x , 0 , sizeof(x))
#define MAX(x) memset(x , 0x3f ,sizeof(x))
#define swa(x,y) {LL s;s=x;x=y;y=s;}
using namespace std ;
#define N 100 const double PI = acos(-1.0);
typedef long long LL ; int topo[N],t;
int G[N][N];
int n,m,flag,pos,i,num;
char s[N];
char output[N]; int Floyd(){
int i, j, k;
for(i = ; i<n ;i++)
for(k = ; k<n; k++)
for(j = ; j<n; j++)
if(G[k][i] && G[i][j])
G[k][j] = ;
for(i = ; i< n; i++){
if(G[i][i] == )
return ;
}
return ;
} int Topsort(){
int cou, ind, k ,i;
int degree[N],used[N];
num = ;
zero(used);
zero(degree);
for(i = ; i< n ;i++)
for(k = ; k< n ;k++)
if(G[i][k])
degree[k]++;
for(i = ; i< n; i++){
cou = ;
for(k = ; k< n ; k++)
if(degree[k] == && !used[k]){
output[num++] = k + 'A';
used[k] = ;
cou++;
ind = k;
}
if(cou > ) return ;
if(cou == ) return ;
for(k = ; k < n;k++)
if(G[ind][k]) degree[k]--;
}
output[num] = '\0';
return ;
}
int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
while(cin>>n>>m){
if(n == && m == ) break;
flag = ;
zero(G);
for(i = ;i < m; i++){
scanf("%s",s);
G[s[]-'A'][s[]-'A'] = ;
if(flag != ) continue;
flag = Floyd();
if(flag == )
pos = i;
if(flag == )
flag = Topsort();
else continue;
if(flag == )
pos = i;
}
pos++;
if(flag == ){
printf("Sorted sequence determined after %d relations: %s.\n",pos,output);
continue;
}
if(flag == ){
printf("Inconsistency found after %d relations.\n",pos);
continue;
}
printf("Sorted sequence cannot be determined.\n");
}
return ;
}
拓扑排序 +Floyd(poj 1094)的更多相关文章
- 【改了一天的拓扑排序】POJ 1094——Sorting It All Out
来源:点击打开链接 不知道怎么回事,wa了整整一天..在绝望的时候AC了. 重点是分步处理和三种情况的判断. 1.判断是否成环,成环了直接输出错误信息. 2.然后一条边一条边的加入,进行拓扑排序,如果 ...
- 拓扑排序(poj 1094)
前置知识:拓扑排序 详细注释都在代码里 //该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. //是典型的拓扑排序,但输出格式上确有三种形式: // 1.该字母序列有序,并依 ...
- [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 ...
- Sorting It All Out (拓扑排序+floyd)
An ascending sorted sequence of distinct values is one in which some form of a less-than operator is ...
- 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 (传递闭包 + 拓扑排序)
题目链接: POJ 1094 题目大意:有 1 ~ N 个大写字母,且从 A 开始依次 N 个.再给你 M 个小于的关系,比如 A < B ,让你判断三种可能: 1.在第 i 个关系罗列之后,是 ...
- POJ 1094 Sorting It All Out (拓扑排序,判断序列是否唯一,图是否有环)
题意:给出n个字符,m对关系,让你输出三种情况: 1.若到第k行时,能判断出唯一的拓扑序列,则输出: Sorted sequence determined after k re ...
- 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 ...
随机推荐
- 38. Same Tree && Symmetric Tree
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- C++设计模式-Builder建造者模式
作用:将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. Builder模式和AbstractFactory模式在功能上很相似,因为都是用来创建大的复杂的对象,它们的区别是:B ...
- 剑指Offer:面试题28——字符串的排列(java实现)(待序)
问题描述: 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 结果请按字母 ...
- Response.Redirect引起的“无法在发送HTTP标头之后进行重定向”
博客后台切换至i.cnblogs.com之后,在日志中发现大量的“无法在发送HTTP标头之后进行重定向”(Cannot redirect after HTTP headers have been se ...
- CSS3属性transition
CSS3 Transitions 指定过渡 语法: transition: property duration timing-function delay; 参数一: transition-p ...
- Linux下编译带x264的ffmpeg的配置方法,包含SDL2
一.环境准备 ffmpeg下载:http://www.ffmpeg.org/download.html x264下载:http://download.videolan.org/x264/snapsho ...
- javascript 自调用函数 闭包
<script type="text/javascript"> var car = {name:"lhs",model:500}; window.o ...
- 【JavaScript】前端插件
树形结构: http://www.jeasyui.com/documentation/index.php 网上有对这个插件的说明,总的来说这个插件将selected和checked作为两种状态: 1. ...
- 计算hashCode的常见方法
把某个非零常数值,比如说17,保存在一个叫result的int类型的变量中. 2.对于对象中每一个关键域f(值equals方法中考虑的每一个域),完成以下步骤: a.为该域计算int类型的散列吗c: ...
- HIS与CIS的区别与联系
医院的医疗信息系统总体可以分为两类:一类是关于医院管理运作的,另一类是关于临床医疗护理的,即面向医院管理和病人管理,前者被称为HIS(医院信息系统),后者被称为CIS(临床信息系统).HIS是以经济核 ...