当前位置:网站首页>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
边栏推荐
- 数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据
- 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
- ResNet-RS:谷歌领衔调优ResNet,性能全面超越EfficientNet系列 | 2021 arxiv
- NPDP certification | how do product managers communicate across functions / teams?
- 小程序系统更新提示,并强制小程序重启并使用新版本
- Web APIs DOM time object
- 2021 geometry deep learning master Michael Bronstein long article analysis
- [leetcode] 19. Delete the penultimate node of the linked list
- Self made j-flash burning tool -- QT calls jlinkarm DLL mode
- Advantages of link local address in IPv6
猜你喜欢
新手程序员该不该背代码?
重磅新闻 | Softing FG-200获得中国3C防爆认证 为客户现场测试提供安全保障
数据处理技巧(7):MATLAB 读取数字字符串混杂的文本文件txt中的数据
硬件开发笔记(十): 硬件开发基本流程,制作一个USB转RS232的模块(九):创建CH340G/MAX232封装库sop-16并关联原理图元器件
Clip +json parsing converts the sound in the video into text
将MySQL的表数据纯净方式导出
RESNET rs: Google takes the lead in tuning RESNET, and its performance comprehensively surpasses efficientnet series | 2021 arXiv
NPDP认证|产品经理如何跨职能/跨团队沟通?
3DMAX assign face map
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
随机推荐
Build op-tee development environment based on qemuv8
PVL EDI project case
Daily question 1: force deduction: 225: realize stack with queue
MySQL教程的天花板,收藏好,慢慢看
NetXpert XG2帮您解决“布线安装与维护”难题
Assembly and Interface Technology Experiment 6 - ADDA conversion experiment, AD acquisition system in interrupt mode
What are the interface tests? What are the general test points?
PVL EDI 项目案例
sizeof关键字
Attack and defense world miscall
How do I write Flask's excellent debug log message to a file in production?
[leetcode] 19. Delete the penultimate node of the linked list
case 关键字后面的值有什么要求吗?
UDP编程
12、 Start process
做国外LEAD2022年下半年几点建议
(18) LCD1602 experiment
HDR image reconstruction from a single exposure using deep CNNs阅读札记
3DMax指定面贴图
2022-07-04 the high-performance database engine stonedb of MySQL is compiled and run in centos7.9