拓扑排序(poj 1094)
前置知识:拓扑排序
详细注释都在代码里
//该题题意明确,就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列。
//是典型的拓扑排序,但输出格式上确有三种形式: // 1.该字母序列有序,并依次输出; // 2.该序列不能判断是否有序; // 3.该序列字母次序之间有矛盾,即有环存在。 // 而这三种形式的判断是有顺序的:先判断是否有环(3),
//再判断是否有序(1),最后才能判断是否能得出结果(2)。
//注意:对于(2)必须遍历完整个图,而(1)和(3)一旦得出结果,
//对后面的输入就不用做处理了。 //有一个问题,我不太清楚,就是在确定有序之后,就直接输出答案,
//对右面的输入也不再处理,但是后面的输入可以让其成为环的话又就会
//让其成为环,所以我想这道题就是在确定有序之后,后面的数据,就都不要了。
//应该是这意思把
#include<cstdio>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn=1e2+;
int G[][];
int in[];
int q[];
void init()
{
memset(G,,sizeof(G)); //图
memset(in,,sizeof(in)); //入度
}
int Toposort(int n)
{
int aim;int cot=;
int tmpin[];
int flag=; //1的时候表示有序
for(int i=;i<=n;i++) tmpin[i]=in[i]; //将入度复制到tmpin里面来
for(int i=;i<=n;i++){
int num=;
for(int j=;j<=n;j++)
if(!tmpin[j]) num++,aim=j; //这里来判断入度数量,aim是入度为0的那个点
//这里如果出现多个入度,就会选择最后面那个先进行操作;
if(!num) return ; //有环;
if(num>) flag=-; //无序
q[cot++]=aim;
tmpin[aim]=-;
for(int j=;j<=n;j++)
if(G[aim][j]) tmpin[j]--;
}
return flag; }
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
if(!n&&!m) break;
init();
char str[];
int flag=;
for(int i=;i<=m;i++){
scanf("%s",str);
if(flag) continue;
int l=str[]-'A'+;
int r=str[]-'A'+;
G[l][r]=;
in[r]++;
int judge=Toposort(n);
//有环
if(!judge) printf("Inconsistency found after %d relations.\n",i),flag=;
if(judge==){ //有序
printf("Sorted sequence determined after %d relations: ",i);
for(int j=;j<n;j++)
printf("%c",q[j]+'A'-);
printf(".\n");
flag=;
}
}
//无序
if(!flag) printf("Sorted sequence cannot be determined.\n");
}
return ;
}
拓扑排序(poj 1094)的更多相关文章
- 拓扑排序 POJ 1094 Sorting It All Out
题意:给定N个字和M行他们之间的关系,要求输出他们的拓扑排序.此题采用边输入边检测的方式,如果发现环,就结束并输出当前行号:如果读取到当前行时,可以确定拓扑序列就输出,不管后面的输入(可能包含环路): ...
- 拓扑排序 POJ 2367
今天网易的笔试,妹的,算法题没能A掉,虽然按照思路写了出来,但是尼玛好歹给个测试用例的格式呀,吐槽一下网易的笔试出的太烂了. 就一道算法题,比较石子重量,个人以为解法应该是拓扑排序. 就去POJ找了道 ...
- 拓扑排序 POJ 1049 Sorting It All Out
题目传送门 /* 拓扑排序裸题:有三种情况: 1. 输入时发现与之前的矛盾,Inconsistency 2. 拓扑排序后,没有n个点(先判断cnt,即使一些点没有边连通,也应该是n,此时错误是有环): ...
- 图论之拓扑排序 poj 2367 Genealogical tree
题目链接 http://poj.org/problem?id=2367 题意就是给定一系列关系,按这些关系拓扑排序. #include<cstdio> #include<cstrin ...
- 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 (拓扑排序,判断序列是否唯一,图是否有环)
题意:给出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 ...
- poj 1094 Sorting It All Out_拓扑排序
题意:是否唯一确定顺序,根据情况输出 #include <iostream> #include<cstdio> #include<cstring> #include ...
随机推荐
- burpsuite各个板块儿详细讲解
burpsuite实战指南,想要的都在这里:https://t0data.gitbooks.io/burpsuite/content/
- 计算器程序编写_python
一.计算一串字符串的最终值,相当于eval函数功能: #!/usr/bin/env python # _*_ coding:utf-8 _*_ #Author:chenxz import re def ...
- Linux运维---16. Kolla部署OpenStack外接rabbit集群
# 前提时rabbit集群已经搭建完成 vim /etc/kolla/globals.yml enable_rabbitmq: "no" rpc_transport_url: &q ...
- BK: Data mining, Chapter 2 - getting to know your data
Why: real-world data are typically noisy, enormous in volume, and may originate from a hodgepodge of ...
- phpstorm同步服务器文件
配置服务器 1.连接配置 打开菜单栏 Tools -> Deployment -> Configuration 点击 + 选择 SFTP,并填写相关服务器信息: Type:连接类型,这里选 ...
- linux - mysql:安装mysql
安装环境 系统是 centos6.5 1.下载 下载地址:http://dev.mysql.com/downloads/mysql/5.6.html#downloads 下载版本:我这里选择的5.6. ...
- 2018-2019 ACM-ICPC, Asia East Continent Final L Eventual … Journey
#include<iostream> using namespace std; ; int cnt[MAX]; int ans[MAX]; int a[MAX]; int main() { ...
- 插件与App的跳转,及路由的关系
在SDK中 无法直接跳App 的界面,这个时候需要使用 路由,或者通过 NSClassFromString 的 presentViewController 来跳转. 直接贴代码: UIViewCont ...
- FPGA设计的注意事项
设计文档 一个完整的软件是由程序. 数据和文档三部分组成的. 在FPGA电路设计中, 撰写完善的设计文档是非常重要的. 对于一个比较复杂的设计来说, 各个子单元的功能各不相同, 实现的方法也不一样,各 ...
- RESTful 【个人理解总结】
RESTful 个人理解总结 一.什么是 RESTful 面向资源 简单的说:RESTful是一种架构的规范与约束.原则,符合这种规范的架构就是RESTful架构. 先看REST是什么意思,英文Re ...