poj 1094 Sorting It All Out (拓扑排序)
http://poj.org/problem?id=1094
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 24505 | Accepted: 8487 |
Description
Input
Output
Sorted sequence determined after xxx relations: yyy...y. Sorted sequence cannot be determined. Inconsistency found after xxx relations.
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.
Sample Input
4 6
A<B
A<C
B<C
C<D
B<D
A<B
3 2
A<B
B<A
26 1
A<Z
0 0
Sample Output
Sorted sequence determined after 4 relations: ABCD.
Inconsistency found after 2 relations.
Sorted sequence cannot be determined.
Source
/**
Judge Status:Accepted Memory:704K
Time:0MS Language:G++
Code Lenght:2296B Author:cj
*/ #include<iostream>
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm> #define N 30
#define M N*N
using namespace std; int map[N][N],mark[N],in[N],tin[N];
char anstr[N];
int n,flag; void init()
{
memset(map,,sizeof(map));
memset(mark,,sizeof(mark));
memset(in,,sizeof(in));
memset(tin,,sizeof(tin));
} int topCheck(int id) //拓扑排序
{
int i,cnt=,pos=-;
for(i=;i<;i++)
{
if(mark[i]&&!in[i])
{
cnt++;
pos=i;
}
}
if(cnt>) //当有多个入度为0的点
{
flag = ; //标记出现多个入度为0的点的情况
in[pos] = -;
anstr[id] = pos+'A';
for(i=;i<;i++)
{
if(map[pos][i]) in[i]--;
}
return topCheck(id+);
}
if(cnt==)
{
for(i=;i<;i++)
{
if(mark[i]&&in[i]>)
{
return ; //入度都大于0,出现了环
}
}
if(id!=n||flag) return ; //当排序到入度为0时,并且一直没有出现过多个入度为0的情况,则排序可以完成
anstr[id]='\0';
return ;
}
in[pos] = -;
anstr[id] = pos+'A';
for(i=;i<;i++)
{
if(map[pos][i]) in[i]--;
}
return topCheck(id+); //进入下一层拓扑排序
return ;
} int main()
{
int m;
while(~scanf("%d%d",&n,&m))
{
if(!n&&!m) break;
char xx[];
int i;
init();
int temp=,pos=;
for(i=;i<=m;i++)
{
scanf("%s",xx);
int x = xx[]-'A';
int y = xx[]-'A';
mark[x] = mark[y] = ;
tin[y]++;
map[x][y] = ;
memcpy(in,tin,sizeof(int)*);
if(temp!=) continue;
flag = ;
temp = topCheck();
pos = i;
}
if(temp==)
{
anstr[pos+i]='\0';
printf("Sorted sequence determined after %d relations: %s.\n",pos,anstr);
continue;
}
if(temp==)
{
printf("Inconsistency found after %d relations.\n",pos);
continue;
}
puts("Sorted sequence cannot be determined.");
}
return ;
}
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 & ...
- [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 ...
随机推荐
- webView中支持input的file的选择和alert弹出
alert()弹出 input的file现选择(特别说明:不同的android版本弹出的样式不同,选择文件后自动上传) webView.setWebChromeClient(new WebChrome ...
- cogs 餐巾 461(贪心)
/*虽然这暴力剪了又剪 改了又改 还是初始的20分...*/ #include<iostream> #include<cstdio> #include<cstring&g ...
- Linux 命令 - head: 打印文件的开头部分
命令格式 head [OPTION]... [FILE]... 命令参数 -c, --bytes=[-]K 显示每个文件的前 K 字节内容. -n, --lines=[-]K 显示每个文件的前 K 行 ...
- HDOJ2027统计元音
统计元音 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- 有趣的hello word
程序一 #define _________ } #define ________ putchar #define _______ main #define _(a) ________(a); #def ...
- IOS 下拉菜单
由于之前曾经用到过下拉菜单,所以现在花一些时间回过头来细细整理了一下,逐步完善这个下拉菜单,并提供一些比较基本的功能,以便日后如果有需要的话可以进行复用,并提供给需要的人参考.下拉菜单同样分为数据源和 ...
- OC2_使用系统协议
// // Dog.h // OC2_使用系统协议 // // Created by zhangxueming on 15/6/24. // Copyright (c) 2015年 zhangxuem ...
- 关于delete
上面三图在debug下,delete的时候会以fe ee覆盖指针所指向要回收内存前后较大一块区域的值. 下图对于release下delete的时候会先用fe ee覆盖指针所指位置起6*4=24字节, ...
- Linux 内核 链表 的简单模拟(2)
接上一篇Linux 内核 链表 的简单模拟(1) 第五章:Linux内核链表的遍历 /** * list_for_each - iterate over a list * @pos: the & ...
- windows下nginx以服务自启动
1,下载最新版的 Windows Service Wrapper 程序,例如:"winsw-1.9-bin.exe" 也可以修改它的名字,例如:myapp.exe 2, 将重命名后 ...