当前位置:网站首页>P1347 排序(topo)
P1347 排序(topo)
2022-06-23 03:45:00 【Harris-H】
P1347 排序(topo)
加深了对topo的理解。
情况1:就是稳定的topo,层数为n的链。
情况2:有环,判环的方法就是存在节点未入队。
情况3:非情况1和2,就是3。
值得注意的是情况1是针对n个节点,情况2是针对当前的前x个节点的判环。
// Problem: P1347 排序
// Contest: Luogu
// URL: https://www.luogu.com.cn/problem/P1347
// Memory Limit: 125 MB
// Time Limit: 1000 ms
// Date: 2022-06-22 10:02:38
// --------by Herio--------
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N=1e3+5,M=2e4+5,inf=0x3f3f3f3f,mod=1e9+7;
const int hashmod[4] = {
402653189,805306457,1610612741,998244353};
#define mst(a,b) memset(a,b,sizeof a)
#define db double
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define x first
#define y second
#define pb emplace_back
#define SZ(a) (int)a.size()
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define per(i,a,b) for(int i=a;i>=b;--i)
#define IOS ios::sync_with_stdio(false),cin.tie(nullptr)
void Print(int *a,int n){
for(int i=1;i<n;i++)
printf("%d ",a[i]);
printf("%d\n",a[n]);
}
template <typename T> //x=max(x,y) x=min(x,y)
void cmx(T &x,T y){
if(x<y) x=y;
}
template <typename T>
void cmn(T &x,T y){
if(x>y) x=y;
}
vector<int>e[N];
int _in[N],in[N];
bitset<N>vis;
int d[26];
int n,m;
void topo(int r){
queue<int>q;
memcpy(in,_in,sizeof in);
int cnt = 0;
vector<int>ans;
mst(d,0);
int _n = 0;
for(int i=0;i<26;i++) if(vis[i]) _n++;
for(int i=0;i<26;i++)
if(vis[i] && !in[i]){
q.push(i);
cnt++;
}
int mx = 0;
while(!q.empty()){
int u = q.front();q.pop();
ans.pb(u);
cmx(mx,d[u]);
for(int v:e[u]){
if(!--in[v]){
q.push(v);
d[v] = d[u] + 1;
cnt++;
}
}
}
if(mx==n-1){
printf("Sorted sequence determined after %d relations: ",r);
for(int x:ans) printf("%c",x+'A');
puts(".");
exit(0);
}
else if(cnt<_n){
printf("Inconsistency found after %d relations.",r);
exit(0);
}
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++){
string s;
cin>>s;
e[s[0]-'A'].pb(s[2]-'A');
vis[s[0]-'A'] = vis[s[2]-'A'] = true;
_in[s[2]-'A']++;
topo(i);
}
puts("Sorted sequence cannot be determined.");
return 0;
}
边栏推荐
猜你喜欢

【owt】owt-client-native-p2p-e2e-test vs2017构建 4 : 第三方库的构建及链接p2pmfc.exe

mysql常用指令

Google Earth Engine(GEE)——长时间序列逐月VCI数据提取分析和面积计算(墨西哥为例)

Code refactoring Guide

Efficient remote office experience | community essay solicitation

Flutter怎么实现不同缩放动画效果

Full analysis of embedded software testing tool tpt18 update

mysql存储引擎之Myisam和Innodb的区别

Differences between MyISAM and InnoDB of MySQL storage engine
![[Zeng shuge's laser slam notes] gmapping filter based slam](/img/93/b940ad95508d1c0d23642022df37f2.png)
[Zeng shuge's laser slam notes] gmapping filter based slam
随机推荐
【LeetCode】179. 最大数
mysql能不能在linux中使用
元素的常用事件
Code refactoring Guide
svg d3.js生成tree树状图
页面导出excel的三种方式
d重载嵌套函数
在 KubeSphere 上部署 Apache Pulsar
直接插入排序
What if the self incrementing IDs of online MySQL are exhausted?
Pytorch---使用Pytorch的预训练模型实现四种天气分类问题
Using jhipster to build microservice architecture
[从零开始学习FPGA编程-40]:进阶篇 - 设计-竞争与风险Risk或冒险
[OWT] OWT client native P2P E2E test vs2017 build 3: no test unit comparison, manually generate vs projects
mysql 数据恢复 (.ibdata1, bin log)
[leetcode] flip linked list II
The first batch of job hunting after 00: don't misread their "different"
2022年的软件开发:首席信息官应该知道的五个现实
Google Earth Engine(GEE)——长时间序列逐月VCI数据提取分析和面积计算(墨西哥为例)
关于sql语句的问题