当前位置:网站首页>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;
}
边栏推荐
- How the innovative use of adobe international certification 𞓜 3D changes the entire industry
- 怎么使用Shell脚本实现监测文件变化
- Twitter cooperates with Shopify to introduce merchant products into twitter shopping
- redisTemplate和cacheManager操作redis有什么不同
- 【LeetCode】179. 最大数
- [binary tree] 993 Cousins in Binary Tree
- Insert sort directly
- 粒子动画背景登录页面particles.js
- Source code encryption of data encryption technology
- 【LeetCode】翻转链表II
猜你喜欢

【曾书格激光SLAM笔记】Gmapping基于滤波器的SLAM

浅析2022年物联网现状

Talk about memory model and memory order

node+express如何操作cookie

QMainWindow

移动端城市列表排序js插件vercitylist.js

The new version of Kali switches the highest account

MySQL data recovery (.Ibdata1, bin log)

仿360桌面悬浮球插件

What if the self incrementing IDs of online MySQL are exhausted?
随机推荐
Form development mode
pyspark,有偿询问数据清洗和上传到数据库的问题
粒子动画背景登录页面particles.js
Banknext microservice: a case study
MySQL common instructions
第一批00后下场求职:不要误读他们的“不一样”
The power of code refactoring: how to measure the success of refactoring
How the innovative use of adobe international certification 𞓜 3D changes the entire industry
Questions about SQL statements
Navar's Treasure Book: the principle of getting rich without luck
【LeetCode】179. Maximum number
Similar to RZ / SZ, trzsz supporting TMUX has released a new version
What is the difference between ArrayList and array?
What is the difference between redistemplate and CacheManager operation redis
虫子 STM32 高级定时器 (哈哈我说实话硬件定时器不能体现实力,实际上想把内核定时器发上来的,一想算了,慢慢来吧)
城链科技董事长肖金伟:践行数据经济系国家战略,引领数字时代新消费发展!
会话和守护进程
APM 工具 SkyWalking 是什么
redis 精讲系列介绍八 - 淘汰策略
AI video cloud vs narrowband HD, who is the favorite in the video Era