当前位置:网站首页>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;
}
边栏推荐
- Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
- 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
- 《代碼整潔之道》讀書筆記
- zabbix5客户端安装和配置
- codeforces每日5题(均1700)-第四天
- Mobile robot path planning: artificial potential field method [easy to understand]
- Yolov3 trains its own data set to generate train txt
- juypter notebook 修改默认打开文件夹以及默认浏览器
- mybatiesHelperPro工具必须的可以生成到对应项目文件夹下
- MySQL
猜你喜欢

Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
![[paper reading] Ca net: leveraging contextual features for lung cancer prediction](/img/ef/bb48ee88d5dc6fe876a498ab53106e.png)
[paper reading] Ca net: leveraging contextual features for lung cancer prediction

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

What is 9D movie like? (+ common sense of dimension space)
![[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)](/img/c1/a00425f2e1824a50644c8fbb15fe38.jpg)
[error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
![[test development] software testing - concept](/img/24/9ee885d46f7200ae7449957ca96b9d.png)
[test development] software testing - concept

性能测试如何创造业务价值

Use cheat engine to modify money, life and stars in Kingdom rush

Processing strategy of message queue message loss and repeated message sending

全志A33使用主线U-Boot
随机推荐
Progress progress bar
[pytorch learning notes] tensor
A4988 drive stepper motor "recommended collection"
Gstore weekly gstore source code analysis (4): black and white list configuration analysis of security mechanism
ORA-01455: converting column overflows integer datatype
PyTorch函数中的__call__和forward函数
PHP asymmetric encryption method private key and public key encryption and decryption method
【pytorch学习笔记】Tensor
[论文阅读] CA-Net: Leveraging Contextual Features for Lung Cancer Prediction
横向越权与纵向越权[通俗易懂]
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
潇洒郎:彻底解决Markdown图片问题——无需上传图片——无需网络——转发给他人图片无缺失
Quanzhi A33 uses mainline u-boot
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
The mybatieshelperpro tool can be generated to the corresponding project folder if necessary
Compile oglpg-9th-edition source code with clion
codeforces每日5题(均1700)-第四天
450-深信服面经1
Excel查找一列中的相同值,删除该行或替换为空值
Notes de lecture sur le code propre