Sorting It All Out(拓扑排序)
http://poj.org/problem?id=1094
1.判断所给关系是否为合法的拓扑序列,若存在环,输出
"Inconsistency found after %d relations."但仍继续输入。
2.判断是否可以确定序列,若不可以确定,继续输入,若可以确定输出
"Sorted sequence determined after %d relations: %s."后,继续输入数据。
3.若输入完数据仍不可以确定序列,输出"Sorted sequence cannot be determined.".
#include<stdio.h>
#include<string.h>
const int N=;
int map[N][N],in[N],vis[N];
int n,m,flag,top;
char s[N];
void topo()
{
int cnt,i,j,pos;
top = ;
int d[N];//存每个点的度数
flag = ;
memset(vis,,sizeof(vis));
memcpy(d,in,n*sizeof(int));
for (i = ; i < n; i ++)
{
cnt = ;
for (j = ; j < n; j ++)
{
if (d[j]==)
{
cnt++;
pos = j;
}
}
if (cnt==)//存在环
{
flag = ;
return ;
}
if (cnt > )//不确定序列
{
flag = ;
}
--d[pos];
s[top++] = pos+'A';
for (int k = ; k < n; k ++)
{
if(map[pos][k]==)
--d[k];//更新度数
}
}
s[top++] = '\0'; }
int main()
{
while(~scanf("%d%d%*c",&n,&m))
{
if (n== && m==)
break;
flag = ;
memset(in,,sizeof(in));
memset(map,,sizeof(map));
char a,b,c;
int i;
for (i = ; i <= m; i ++)
{
scanf("%c%c%c%*c",&a,&b,&c);
if (flag)//控制输入
continue;
if (!map[a-'A'][c-'A'])
{
map[a-'A'][c-'A'] = ;
in[c-'A']++;
}
topo();
if (flag==)
{
flag = ;//以便能继续输入下面的点
printf("Inconsistency found after %d relations.\n",i);
}
else if (flag==)
{
printf("Sorted sequence determined after %d relations: %s.\n",i,s);
}
else if (flag==)//无法确定序列,继续输入
{
flag = ;//继续拓扑
}
}
if (flag==)//输入完也无法确定序列
printf("Sorted sequence cannot be determined.\n");
}
return ;
}
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 ...
- [poj1094]Sorting It All Out_拓扑排序
Sorting It All Out poj-1094 题目大意:给出一些字符串之间的大小关系,问能否得到一个唯一的字符串序列,满足权值随下标递增. 注释:最多26个字母,均为大写. 想法:显然,很容 ...
- POJ1094 Sorting It All Out —— 拓扑排序
题目链接:http://poj.org/problem?id=1094 Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Tot ...
- POJ 1094:Sorting It All Out拓扑排序之我在这里挖了一个大大的坑
Sorting It All Out Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 29984 Accepted: 10 ...
- [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 ...
- nyoj349 poj1094 Sorting It All Out(拓扑排序)
nyoj349 http://acm.nyist.net/JudgeOnline/problem.php?pid=349poj1094 http://poj.org/problem?id=10 ...
- 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 ...
- PKU 1094 Sorting It All Out(拓扑排序)
题目大意:就是给定一组字母的大小关系判断他们是否能组成唯一的拓扑序列. 是典型的拓扑排序,但输出格式上确有三种形式: 1.该字母序列有序,并依次输出: 2.判断该序列是否唯一: 3.该序列字母次序之间 ...
随机推荐
- c++将bool变量以文字形式打印
#include <iostream> // std::cout, std::boolalpha, std::noboolalpha int main () { bool b = true ...
- hibernate工作流程、session
hibernate是对jdbc的封装,不建议直接使用jdbc的connection操作数据库,而是通过session操作数据库.session可以理解为操作数据库的对象. session与connec ...
- HDU_1158_Employment Planning_dp
Employment Planning Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- 2 Button
// <summary> /// 设置透明按钮样式 /// </summary> private void SetBtnStyle(Button btn) { btn.Flat ...
- git 的简单使用(4)
多人协作的工作模式通常是这样: 首先,可以试图用git push origin <branch-name>推送自己的修改: 如果推送失败,则因为远程分支比你的本地更新,需要先用git pu ...
- python 未知
import timeimport requestsfrom bs4 import BeautifulSoupimport threading def format_str(s): return s. ...
- windows 实现vue命令行
在代码编辑器里写好文件的位置,以及相关的命令,保存文件类型是.cmd
- Entity Framework Connection String不保留密码的方法
添加Entity Data Model的时候,到最后一步,有两个radio box: 如果选择include sensitive data,虽然很方便,但是在web.config或者app.confi ...
- Solr插件的弊端
在前文<Solr Update插件自定义条件索引>中,我介绍了如何通过插件的模式,自定义Solr的Update过程.但是在大半年的使用过程中,发现这种方式存在如下弊端. 1.环境难以维护. ...
- code vs 2166 Bessie的体重问题
2166 Bessie的体重问题 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description Bessie像她的诸多姊妹一 ...