当前位置:网站首页>poj 1094 Sorting It All Out (拓扑排序)
poj 1094 Sorting It All Out (拓扑排序)
2022-07-06 15:10:00 【全栈程序员站长】
大家好,又见面了,我是全栈君。
题意:给定一系列关系(仅仅存在大写字母),推断是否存在矛盾,
或无法确定关系。或能够确定唯一的关系
分析:利用拓扑排序。可是须要边输入关系边排序
矛盾:推断是否存在环
确定关系:能找出唯一的拓扑排序
不能确定关系:不存在环,且全部关系处理后,关系仍无法确定
注:假设出现矛盾,则无需推断接下来的关系了
#include<stdio.h>
#include<string.h>
int n,m,vis[30],rd[30],flag[30],map[30][30],cnt;
char t[30];
int tpsort()
{
int i,j,k,num,mark=1;
cnt=0;
memset(vis,0,sizeof(vis)); //标记是否訪问
for(i=0;i<n;i++)
if(flag[i]){
num=0;
for(j=0;j<n;j++) //计算入度为0的个数
if(flag[j]&&!rd[j]&&!vis[j])
num++;
if(num==0) //若不存在入度为0结点,则存在环
return 0;
if(num>1) //若入度为0的结点大于1。说明无法确立唯一的拓扑关系
mark=0;
for(j=0;j<n;j++){
if(flag[j]&&!rd[j]&&!vis[j]){
t[cnt++]=j+'A';
vis[j]=1;
break;
}
}
for(k=0;k<n;k++)
if(map[j][k])
rd[k]--;
}
if(!mark)
cnt=0;
return 1;
}
int main()
{
int i,j,mark,pos,r[30];
char s[5];
while(scanf("%d%d",&n,&m)!=EOF){
if(m==0&&n==0)
break;
mark=1;
memset(map,0,sizeof(map));
memset(flag,0,sizeof(flag)); //标记该元素是否存在
memset(r,0,sizeof(r));
for(i=1;i<=m;i++){
scanf("%s",s);
if(mark==1){
flag[s[0]-'A']=flag[s[2]-'A']=1;
if(s[1]=='>'){
map[s[2]-'A'][s[0]-'A']=1; //记录边的关系
r[s[0]-'A']++; //记录入度
}
else{
map[s[0]-'A'][s[2]-'A']=1;
r[s[2]-'A']++;
}
for(j=0;j<n;j++)
rd[j]=r[j]; //辅助空间。防止过程中拓扑排序将入度改变了
mark=tpsort();
if(!mark)
pos=i;
if(mark&&cnt==n){ //当没有出现环且关系都确定
mark=2;
pos=i;
t[cnt]=0; //字符数组最后附空字符
}
}
}
if(mark==0)
printf("Inconsistency found after %d relations.\n",pos);
else if(mark==1)
printf("Sorted sequence cannot be determined.\n");
else if(mark==2)
printf("Sorted sequence determined after %d relations: %s.\n",pos,t);
}
return 0;
}版权声明:本文博主原创文章。博客,未经同意不得转载。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/116980.html原文链接:https://javaforall.cn
边栏推荐
- case 关键字后面的值有什么要求吗?
- 剑指offer刷题记录1
- 基於 QEMUv8 搭建 OP-TEE 開發環境
- 3DMAX assign face map
- 十二、启动流程
- Self made j-flash burning tool -- QT calls jlinkarm DLL mode
- [leetcode] 19. Delete the penultimate node of the linked list
- Web APIs DOM time object
- LeetCode 练习——剑指 Offer 26. 树的子结构
- Unity3d minigame unity webgl transform plug-in converts wechat games to use dlopen, you need to use embedded 's problem
猜你喜欢

Hardware development notes (10): basic process of hardware development, making a USB to RS232 module (9): create ch340g/max232 package library sop-16 and associate principle primitive devices

Senior soft test (Information System Project Manager) high frequency test site: project quality management

3DMAX assign face map

重磅新闻 | Softing FG-200获得中国3C防爆认证 为客户现场测试提供安全保障

3DMax指定面贴图

2022-07-04 the high-performance database engine stonedb of MySQL is compiled and run in centos7.9

MySQL数据库基本操作-DML

自定义 swap 函数

云原生技术--- 容器知识点

Build op-tee development environment based on qemuv8
随机推荐
LeetCode刷题(十一)——顺序刷题51至55
枚举与#define 宏的区别
ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv
2022-07-05 stonedb sub query processing parsing time analysis
rust知识思维导图xmind
0 basic learning C language - digital tube
Unity3d minigame-unity-webgl-transform插件转换微信小游戏报错To use dlopen, you need to use Emscripten‘s...问题
uniapp设置背景图效果demo(整理)
重磅新闻 | Softing FG-200获得中国3C防爆认证 为客户现场测试提供安全保障
【雅思口语】安娜口语学习记录part1
Netxpert xg2 helps you solve the problem of "Cabling installation and maintenance"
Dealing with the crash of QT quick project in offscreen mode
CCNA Cisco network EIGRP protocol
Chapter 3: detailed explanation of class loading process (class life cycle)
Export MySQL table data in pure mode
OpenCV VideoCapture. Get() parameter details
基于 QEMUv8 搭建 OP-TEE 开发环境
图像的spatial domain 和 frequency domain 图像压缩
NPDP认证|产品经理如何跨职能/跨团队沟通?
case 关键字后面的值有什么要求吗?