当前位置:网站首页>P1347 排序(拓扑 + spfa判断环 or 拓扑[内判断环])
P1347 排序(拓扑 + spfa判断环 or 拓扑[内判断环])
2022-07-02 10:14:00 【Joanh_Lan】
排序
题目描述
一个不同的值的升序排序数列指的是一个从左到右元素依次增大的序列,例如,一个有序的数列 A , B , C , D A,B,C,D A,B,C,D 表示 A < B , B < C , C < D A<B,B<C,C<D A<B,B<C,C<D。在这道题中,我们将给你一系列形如 A < B A<B A<B 的关系,并要求你判断是否能够根据这些关系确定这个数列的顺序。
输入格式
第一行有两个正整数 n , m n,m n,m, n n n 表示需要排序的元素数量, 2 ≤ n ≤ 26 2\leq n\leq 26 2≤n≤26,第 1 1 1 到 n n n 个元素将用大写的 A , B , C , D … A,B,C,D\dots A,B,C,D… 表示。 m m m 表示将给出的形如 A < B A<B A<B 的关系的数量。
接下来有 m m m 行,每行有 3 3 3 个字符,分别为一个大写字母,一个 < 符号,一个大写字母,表示两个元素之间的关系。
输出格式
若根据前 x x x 个关系即可确定这 n n n 个元素的顺序 yyy..y(如 ABC),输出
Sorted sequence determined after xxx relations: yyy...y.
若根据前 x x x 个关系即发现存在矛盾(如 A < B , B < C , C < A A<B,B<C,C<A A<B,B<C,C<A),输出
Inconsistency found after x relations.
若根据这 m m m 个关系无法确定这 n n n 个元素的顺序,输出
Sorted sequence cannot be determined.
(提示:确定 n n n 个元素的顺序后即可结束程序,可以不用考虑确定顺序之后出现矛盾的情况)
样例 #1
样例输入 #1
4 6
A<B
A<C
B<C
C<D
B<D
A<B
样例输出 #1
Sorted sequence determined after 4 relations: ABCD.
样例 #2
样例输入 #2
3 2
A<B
B<A
样例输出 #2
Inconsistency found after 2 relations.
样例 #3
样例输入 #3
26 1
A<Z
样例输出 #3
Sorted sequence cannot be determined.
提示
2 ≤ n ≤ 26 , 1 ≤ m ≤ 600 2 \leq n \leq 26,1 \leq m \leq 600 2≤n≤26,1≤m≤600。
拓扑 + spfa判断环
思路:
每一次加边都判断一下
1.spfa判断是否存在环
2.拓扑排序看是否已经发现有解
Ⅰ.检查每次出栈前栈中元素个数,若大于1,说明它们间关系未定。
Ⅱ.若关系确定,记录一下就好了,不能结束程序,还有出现矛盾的可能。
拓扑[内判断环]
思路
1.每一次加边都判断一下
2.拓扑排序
Ⅰ.记录**出栈**个数sz,一次拓扑排序中sz小于0,说明一些点构成了环,表示存在矛盾,输出后直接结束程序。
Ⅱ.检查每次出栈前栈中元素个数,若大于1,说明它们间关系未定。
Ⅲ.若关系确定,记录一下就好了,不能结束程序,还有出现矛盾的可能。
拓扑 + spfa判断环 AC代码如下:
#include <bits/stdc++.h>
#define buff \ ios::sync_with_stdio(false); \ cin.tie(0);
//#define int long long
using namespace std;
const int N = 1000;
int n, m;
map<char, int> mp;
char s[30];
int cntt;
vector<int> g[30];
bool st[30];
int dist[30], cnt[30];
int d[30];
int dd[30];
int q[30], tt = -1, hh = 0;
bool spfa()
{
memset(dist, 0, sizeof dist);
queue<int> q;
for (int i = 1; i <= cntt; i++)
{
q.push(i);
st[i] = true;
}
while (q.size())
{
int t = q.front();
q.pop();
st[t] = false;
for (auto it : g[t])
{
if (dist[it] < dist[t] + 1)
{
dist[it] = dist[t] + 1;
cnt[it] = cnt[t] + 1;
if (cnt[it] >= cntt)
return true;
if (!st[it])
{
q.push(it);
st[it] = true;
}
}
}
}
return false;
}
bool topusort()
{
int emo = 0;
for (int i = 1; i <= cntt; i++)
dd[i] = d[i];
tt = -1, hh = 0;
for (int i = 1; i <= cntt; i++)
{
if (!dd[i])
q[++tt] = i, emo++;
}
if (emo > 1)
return 0;
while (hh <= tt)
{
int emo = 0;
int t = q[hh++];
for (auto it : g[t])
{
dd[it]--;
if (!dd[it])
q[++tt] = it, emo++;
if (emo > 1)
return 0;
}
}
return tt == n - 1;
}
void solve()
{
cin >> n >> m;
int flag = 0;
int idx;
for (int i = 1; i <= m; i++)
{
string a;
cin >> a;
if (!mp[a[0]])
{
mp[a[0]] = ++cntt;
s[cntt] = a[0];
}
if (!mp[a[2]])
{
mp[a[2]] = ++cntt;
s[cntt] = a[2];
}
g[mp[a[2]]].push_back(mp[a[0]]);
d[mp[a[0]]]++;
if (flag)
{
continue;
}
if (spfa())
{
flag = 1;
idx = i;
continue;
}
if (topusort())
{
idx = i;
flag = 2;
continue;
}
}
if (flag == 1)
{
cout << "Inconsistency found after " << idx << " relations.\n";
return;
}
if (flag == 2)
{
cout << "Sorted sequence determined after " << idx << " relations: ";
for (int i = n - 1; i >= 0; i--)
cout << s[q[i]];
cout << ".\n";
}
if (flag == 0)
{
cout << "Sorted sequence cannot be determined.\n";
}
}
int main()
{
buff;
solve();
}
边栏推荐
- [OpenGL] notes 29. Advanced lighting (specular highlights)
- When tidb meets Flink: tidb efficiently enters the lake "new play" | tilaker team interview
- [技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术
- 【蓝桥杯选拔赛真题43】Scratch航天飞行 少儿编程scratch蓝桥杯选拔赛真题讲解
- 无向图的桥
- Word efficiency guide - word's own template
- 不会看器件手册的工程师不是个好厨子
- P3807 [template] Lucas theorem /lucas theorem
- How to explain binary search to my sister? This is really difficult, fan!
- Performance optimization of memory function
猜你喜欢

EasyDSS点播服务分享时间出错如何修改?

【笔耕不辍勋章活动】生命不止,写作不息

Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm

【云原生数据库】遇到慢SQL该怎么办(上)?

挥发性有机物TVOC、VOC、VOCS气体检测+解决方案

Unity skframework framework (XX), VFX lab special effects library

Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)
![Unity small map production [2]](/img/d6/9d6556d37525b9986b74133f2a7aaa.jpg)
Unity small map production [2]

Find love for speed in F1 delta time Grand Prix

Research shows that "congenial" is more likely to become friends
随机推荐
leetcode621. task scheduler
numpy数组计算
Word efficiency guide - word's own template
Unity skframework framework (XVII), freecameracontroller God view / free view camera control script
Uniapp develops wechat applet Tencent map function and generates sig signature of location cloud
诚邀青年创作者,一起在元宇宙里与投资人、创业者交流人生如何做选择……...
Why can't d link DLL
Quantum three body problem: Landau fall
Unity SKFramework框架(十六)、Package Manager 开发工具包管理器
Numpy array calculation
【OpenGL】笔记二十九、高级光照(镜面高光)
Detailed collection of common MySQL commands
Research shows that "congenial" is more likely to become friends
Download files and preview pictures
研究表明“气味相投”更易成为朋友
Countermeasures for the failure of MMPV billing period caused by negative inventory of materials in SAP mm
Web Foundation
The 29 year old programmer in Shanghai was sentenced to 10 months for "deleting the database and running away" on the day of his resignation!
De4000h storage installation configuration
Don't spend money, spend an hour to build your own blog website