当前位置:网站首页>AcWing 343. 排序 题解(floyd性质实现传递闭包)
AcWing 343. 排序 题解(floyd性质实现传递闭包)
2022-07-02 18:27:00 【乔大先生】
AcWing 343. 排序
利用floyd三重循环实现闭包传递,学到了,利用最短路性质实现别的功能
#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; //矛盾
}
//关系不能确定
for(int i = 0; i < n; i ++ ){
for(int j = 0; j < i; j ++ ){
if(!d[j][i] && !d[i][j]) return 0; //在前面的字母不一定小,所以正反都要判断
}
}
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;
}
}
//要遍历完所有字母之后进行输出
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'; //字符转化成数字
if(!type){
g[a][b] = 1;
floyd(); //添加新关系
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;
}
边栏推荐
- 2022.7.1-----leetcode. two hundred and forty-one
- Excel finds the same value in a column, deletes the row or replaces it with a blank value
- [论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction
- Emmet基础语法
- LeetCode 0871.最低加油次数 - 类似于POJ2431丛林探险
- 聊聊电商系统中红包活动设计
- MySQL表历史数据清理总结
- A4988驱动步进电机「建议收藏」
- 教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
- Reduce -- traverse element calculation. The specific calculation formula needs to be passed in and combined with BigDecimal
猜你喜欢
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
Markdown basic grammar
Chic Lang: completely solve the problem of markdown pictures - no need to upload pictures - no need to network - there is no lack of pictures forwarded to others
【测试开发】软件测试—概念篇
Processing strategy of message queue message loss and repeated message sending
守望先锋世界观架构 ——(一款好的游戏是怎么来的)
安装单机redis详细教程
codeforces每日5题(均1700)-第四天
Introduction to the paper | application of machine learning in database cardinality estimation
Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
随机推荐
Memory management of C
数字滚动带动画
Page title component
[pytorch learning notes] tensor
LeetCode 0871.最低加油次数 - 类似于POJ2431丛林探险
Which video recording software is better for the computer
Talk about the design of red envelope activities in e-commerce system
MySQL
Usage of ieda refactor
潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失
Refactoring: improving the design of existing code (Part 1)
codeforces每日5题(均1700)-第四天
Emmet基础语法
Novice must see, click two buttons to switch to different content
2022.7.1-----leetcode. two hundred and forty-one
A4988 drive stepper motor "recommended collection"
Qpropertyanimation use and toast case list in QT
450-深信服面经1
预处理和预处理宏
451-memcpy、memmove、memset的实现