当前位置:网站首页>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;
}
边栏推荐
- 安装单机redis详细教程
- GMapping代码解析[通俗易懂]
- ICDE 2023|TKDE Poster Session(CFP)
- metric_logger小解
- 以太网PHY层芯片LAN8720A简介
- 453-atoi函数的实现
- 2022 software engineering final exam recall Edition
- Qpropertyanimation use and toast case list in QT
- xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
- When converting from list to map, if a certain attribute may cause key duplication and exceptions, you can set the way to deal with this duplication
猜你喜欢

《重构:改善既有代码的设计》读书笔记(下)

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

Data dimensionality reduction principal component analysis

High frequency interview questions

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

消息队列消息丢失和消息重复发送的处理策略

Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出

教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5

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

仿京东放大镜效果(pink老师版)
随机推荐
Excel finds the same value in a column, deletes the row or replaces it with a blank value
《代码整洁之道》读书笔记
论文导读 | 关于将预训练语言模型作为知识库的分析与批评
【pytorch学习笔记】Tensor
ORA-01455: converting column overflows integer datatype
冒泡排序数组
Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
Novice must see, click two buttons to switch to different content
mysql备份后缀是什么_mysql备份还原
Binary operation
When converting from list to map, if a certain attribute may cause key duplication and exceptions, you can set the way to deal with this duplication
C file input operation
450-深信服面经1
使用xml文件打印mybaties-log插件的方式
Compile oglpg-9th-edition source code with clion
《重构:改善既有代码的设计》读书笔记(上)
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
消息队列消息丢失和消息重复发送的处理策略
451-memcpy、memmove、memset的实现
思考变量引起的巨大变化