当前位置:网站首页>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;
}
边栏推荐
- Gstore weekly gstore source code analysis (4): black and white list configuration analysis of security mechanism
- Page title component
- Machine learning notes - time series prediction research: monthly sales of French champagne
- Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
- 潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失
- 第七章-类基础
- Date tool class (updated from time to time)
- GMapping代码解析[通俗易懂]
- 450-深信服面经1
- End to end object detection with transformers (Detr) paper reading and understanding
猜你喜欢

Data dimensionality reduction factor analysis

High frequency interview questions

Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径

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

思维意识转变是施工企业数字化转型成败的关键

搭建主从模式集群redis

Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5

潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失

新手必看,点击两个按钮切换至不同的内容

仿京东放大镜效果(pink老师版)
随机推荐
开发固定资产管理系统,开发固定资产管理系统用什么语音
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
【测试开发】软件测试—概念篇
二进制操作
[0701] [论文阅读] Alleviating Data Imbalance Issue with Perturbed Input During Inference
High frequency interview questions
451-memcpy、memmove、memset的实现
Advanced performance test series "24. Execute SQL script through JDBC"
数据降维——主成分分析
IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
《重构:改善既有代码的设计》读书笔记(下)
Imitation Jingdong magnifying glass effect (pink teacher version)
Quanzhi A33 uses mainline u-boot
C文件输入操作
Hospital online inquiry source code hospital video inquiry source code hospital applet source code
2022.7.1-----leetcode.241
mybatiesHelperPro工具必须的可以生成到对应项目文件夹下
论文导读 | 机器学习在数据库基数估计中的应用
Typescript 之 快速入门
Markdown基础语法