当前位置:网站首页>POJ 1094 sorting it all out
POJ 1094 sorting it all out
2022-07-06 22:33:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm the king of the whole stack .
The question : Given a series of relationships ( Only capital letters exist ), Infer whether there is contradiction ,
Or unable to determine the relationship . Or be able to determine the only relationship
analysis : Using topological sorting . But you need to sort while entering the relationship
contradiction : Infer whether there is a ring
Determine the relationship : Can find the unique topological order
Not sure about the relationship : There is no ring , And after all the relationships are processed , The relationship is still uncertain
notes : Suppose there is a contradiction , Then there is no need to infer the next relationship
#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)); // Mark whether to access
for(i=0;i<n;i++)
if(flag[i]){
num=0;
for(j=0;j<n;j++) // The calculated penetration is 0 The number of
if(flag[j]&&!rd[j]&&!vis[j])
num++;
if(num==0) // If there is no penetration, it is 0 node , Then there are rings
return 0;
if(num>1) // If the degree of penetration is 0 Node of is greater than 1. It indicates that a unique topological relationship cannot be established
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)); // Mark whether the element exists
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; // Record the relationship of edges
r[s[0]-'A']++; // Record the degree of penetration
}
else{
map[s[0]-'A'][s[2]-'A']=1;
r[s[2]-'A']++;
}
for(j=0;j<n;j++)
rd[j]=r[j]; // Auxiliary space . Prevent topology sorting from changing the degree of penetration in the process
mark=tpsort();
if(!mark)
pos=i;
if(mark&&cnt==n){ // When there is no ring and the relationship is certain
mark=2;
pos=i;
t[cnt]=0; // Empty characters at the end of the character array
}
}
}
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;
}Copyright notice : This article is the original article of the blogger . Blog , Do not reprint without permission .
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/116980.html Link to the original text :https://javaforall.cn
边栏推荐
- Config:invalid signature solution and troubleshooting details
- GD32F4XX串口接收中断和闲时中断配置
- 自定义 swap 函数
- Installation and use of labelimg
- Should novice programmers memorize code?
- 变量与“零值”的比较
- i. Mx6ull build boa server details and some of the problems encountered
- Puppeteer连接已有Chrome浏览器
- (十八)LCD1602实验
- Windows Auzre 微软的云计算产品的后台操作界面
猜你喜欢

二分图判定

在IPv6中 链路本地地址的优势

软考高级(信息系统项目管理师)高频考点:项目质量管理

LeetCode 练习——剑指 Offer 26. 树的子结构

Leetcode: interview question 17.24 Maximum cumulative sum of submatrix (to be studied)

金融人士必读书籍系列之六:权益投资(基于cfa考试内容大纲和框架)

【编译原理】做了一半的LR(0)分析器
![pytorch_ Yolox pruning [with code]](/img/98/31d6258635ce48ac53819d0ca12d1d.jpg)
pytorch_ Yolox pruning [with code]
![[Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation](/img/2b/15b3d831bba6aa772ad83f3ac91d23.png)
[Digital IC hand tearing code] Verilog burr free clock switching circuit | topic | principle | design | simulation

Aardio - construct a multi button component with customplus library +plus
随机推荐
The nearest common ancestor of binary (search) tree ●●
Aardio - does not declare the method of directly passing float values
The SQL response is slow. What are your troubleshooting ideas?
雅思口语的具体步骤和时间安排是什么样的?
Spatial domain and frequency domain image compression of images
UDP programming
Aardio - Method of batch processing attributes and callback functions when encapsulating Libraries
HDR image reconstruction from a single exposure using deep CNN reading notes
Heavyweight news | softing fg-200 has obtained China 3C explosion-proof certification to provide safety assurance for customers' on-site testing
MySQL教程的天花板,收藏好,慢慢看
Leetcode exercise - Sword finger offer 26 Substructure of tree
手写ABA遇到的坑
0 basic learning C language - digital tube
Export MySQL table data in pure mode
2022-07-04 the high-performance database engine stonedb of MySQL is compiled and run in centos7.9
Installation and use of labelimg
That's why you can't understand recursion
How do I write Flask's excellent debug log message to a file in production?
China 1,4-cyclohexanedimethanol (CHDM) industry research and investment decision-making report (2022 Edition)
如何实现文字动画效果