当前位置:网站首页>AcWing 343. Sorting problem solution (Floyd property realizes transitive closure)
AcWing 343. Sorting problem solution (Floyd property realizes transitive closure)
2022-07-02 19:32:00 【Mr. Qiao Da】
AcWing 343. Sort
utilize floyd The triple loop implements closure passing , Learned , Use the shortest path property to realize other functions
#include<bits/stdc++.h>
using namespace std;
const int N = 26;
bool d[N][N], g[N][N], st[N];
int n, m;
void floyd(){
memcpy(d, g, sizeof g);
for(int k = 0; k < n; k ++ ){
for(int i = 0; i < n; i ++ ){
for(int j = 0; j < n; j ++ ){
d[i][j] |= d[i][k] && d[k][j];
}
}
}
}
int check(){
for(int i = 0; i < n; i ++ ){
if(d[i][i]) return 2; // contradiction
}
// The relationship is uncertain
for(int i = 0; i < n; i ++ ){
for(int j = 0; j < i; j ++ ){
if(!d[j][i] && !d[i][j]) return 0; // The letters in front are not necessarily small , So both positive and negative should be judged
}
}
return 1;
}
char get_min(){
for(int i = 0; i < n; i ++ ){
if(!st[i]){
bool flag = true;
for(int j = 0; j < n; j ++ ){
if(!st[j] && d[j][i]){
flag = false;
break;
}
}
// Output after traversing all the letters
if(flag){
st[i] = true;
return i + 'A';
}
}
}
}
int main()
{
while(cin>>n>>m, m || n){
memset(g, 0, sizeof g);
int type = 0, t;
for(int i = 1; i <= m; i ++ ){
char str[5];
cin>>str;
int a = str[0] - 'A', b = str[2] - 'A'; // Convert characters to numbers
if(!type){
g[a][b] = 1;
floyd(); // Add new relationship
type = check();
if(type) t = i;
}
}
if(!type) cout<<"Sorted sequence cannot be determined."<<endl;
else if(type == 2) cout<<"Inconsistency found after "<<t<<" relations."<<endl;
else{
cout<<"Sorted sequence determined after "<<t<<" relations: ";
memset(st, 0, sizeof st);
for(int i = 0; i < n; i ++ ){
cout<<get_min();
}
cout<<"."<<endl;
}
}
return 0;
}
边栏推荐
- Golang:[]byte to string
- Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
- 二进制操作
- 【pytorch学习笔记】Tensor
- LeetCode 0871.最低加油次数 - 类似于POJ2431丛林探险
- In pytorch function__ call__ And forward functions
- Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
- Reduce -- traverse element calculation. The specific calculation formula needs to be passed in and combined with BigDecimal
- Emmet basic syntax
- 《重构:改善既有代码的设计》读书笔记(下)
猜你喜欢

Watchful pioneer world outlook Architecture - (how does a good game come from)

Usage of ieda refactor

Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3

高级性能测试系列《24. 通过jdbc执行sql脚本》

Registration opportunity of autowiredannotationbeanpostprocessor in XML development mode

Thread application instance

数据降维——主成分分析

Educational Codeforces Round 129 (Rated for Div. 2) 补题题解

High frequency interview questions

According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
随机推荐
以太网PHY层芯片LAN8720A简介
Refactoring: improving the design of existing code (Part 1)
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
Memory management of C
线程应用实例
Data dimensionality reduction principal component analysis
C文件输入操作
Bubble sort array
Reduce -- traverse element calculation. The specific calculation formula needs to be passed in and combined with BigDecimal
MySQL
MySQL表历史数据清理总结
开发固定资产管理系统,开发固定资产管理系统用什么语音
AcWing 1125. 牛的旅行 题解(最短路、直径)
[pytorch learning notes] tensor
Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
Machine learning notes - time series prediction research: monthly sales of French champagne
Processing strategy of message queue message loss and repeated message sending
2022.7.1-----leetcode. two hundred and forty-one
移动机器人路径规划:人工势场法[通俗易懂]
C的内存管理