当前位置:网站首页>P1347 sorting (topology + SPFA judgment ring or topology [inner judgment ring])
P1347 sorting (topology + SPFA judgment ring or topology [inner judgment ring])
2022-07-02 13:45:00 【Joanh_ Lan】
Sort
Title Description
An ascending sort sequence of different values refers to a sequence of increasing elements from left to right , for example , An ordered sequence of numbers A , B , C , D A,B,C,D A,B,C,D Express A < B , B < C , C < D A<B,B<C,C<D A<B,B<C,C<D. In this question , We will give you a series of shapes such as A < B A<B A<B The relationship between , And ask you to judge whether you can determine the order of this sequence according to these relationships .
Input format
The first line has two positive integers n , m n,m n,m, n n n Indicates the number of elements to be sorted , 2 ≤ n ≤ 26 2\leq n\leq 26 2≤n≤26, The first 1 1 1 To n n n Elements will be capitalized A , B , C , D … A,B,C,D\dots A,B,C,D… Express . m m m Indicates the shape to be given, such as A < B A<B A<B The number of relationships .
Next there is m m m That's ok , Each row has 3 3 3 Characters , Each is a capital letter , One < Symbol , A capital letter , Represents the relationship between two elements .
Output format
If according to the previous x x x A relationship can determine this n n n The order of the elements yyy..y( Such as ABC), Output
Sorted sequence determined after xxx relations: yyy...y.
If according to the previous x x x A relationship is found to be contradictory ( Such as A < B , B < C , C < A A<B,B<C,C<A A<B,B<C,C<A), Output
Inconsistency found after x relations.
If according to this m m m This relationship cannot be determined n n n The order of the elements , Output
Sorted sequence cannot be determined.
( Tips : determine n n n After the sequence of elements, the program can be ended , There is no need to consider the contradiction after determining the order )
Examples #1
The sample input #1
4 6
A<B
A<C
B<C
C<D
B<D
A<B
Sample output #1
Sorted sequence determined after 4 relations: ABCD.
Examples #2
The sample input #2
3 2
A<B
B<A
Sample output #2
Inconsistency found after 2 relations.
Examples #3
The sample input #3
26 1
A<Z
Sample output #3
Sorted sequence cannot be determined.
Tips
2 ≤ n ≤ 26 , 1 ≤ m ≤ 600 2 \leq n \leq 26,1 \leq m \leq 600 2≤n≤26,1≤m≤600.
Topology + spfa Judgment ring
Ideas :
Judge every time you add edges
1.spfa Determine whether there is a ring
2. Topological sorting to see if there is a solution
Ⅰ. Check the number of elements in the stack before each stack , If more than 1, It shows that the relationship between them is uncertain .
Ⅱ. If the relationship is confirmed , Just record it , Cannot end program , There is also the possibility of contradictions .
Topology [ Inner judgment ring ]
Ideas
1. Judge every time you add edges
2. A topological sort
Ⅰ. Record ** Out of the stack ** Number sz, In a topological sort sz Less than 0, Explain that some points form a ring , Indicates that there is a contradiction , End the program directly after output .
Ⅱ. Check the number of elements in the stack before each stack , If more than 1, It shows that the relationship between them is uncertain .
Ⅲ. If the relationship is confirmed , Just record it , Cannot end program , There is also the possibility of contradictions .
Topology + spfa Judgment ring AC The code is as follows :
#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();
}
边栏推荐
- How to use SAP's metadata framework (MDF) to build custom business rules?
- D如何检查null
- Partner cloud form strong upgrade! Pro version, more extraordinary!
- Achievements in science and Technology (27)
- 机器学习基础(二)——训练集和测试集的划分
- Pattern matching and regular expressions in PostgreSQL - Das
- JS逆向之巨量创意signature签名
- qt中uic的使用
- [youcans' image processing learning course] general contents
- Node.js通过ODBC访问PostgreSQL数据库
猜你喜欢

Bridge of undirected graph

Tupang multi-target tracking! BOT sort: robust correlated multi pedestrian tracking

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

Independent and controllable 3D cloud CAD: crowncad enables innovative design of enterprises

We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse

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!

代码实现MNLM

刚好1000粉丝,记录一下

I did it with two lines of code. As a result, my sister had a more ingenious way

为什么switch 的default后面要跟break?
随机推荐
二、帧模式 MPLS 操作
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!
What are the classifications of SSL certificates? How to choose the appropriate SSL certificate?
The second anniversary of the three winged bird: the wings are getting richer and the take-off is just around the corner
[USACO05JAN]Watchcow S(欧拉回路)
Qt-制作一个简单的计算器-实现四则运算-将结果以对话框的形式弹出来
ArrayList and LinkedList
Integral link, inertia link and proportion link in Simulink
Can automatically update the universal weekly report template, you can use it with your hand!
nohup命令
Node. JS accessing PostgreSQL database through ODBC
SAP MM 因物料有负库存导致MMPV开账期失败问题之对策
Memory management 01 - link script
How to use SAP's metadata framework (MDF) to build custom business rules?
Web基础
Research shows that "congenial" is more likely to become friends
Astro learning notes
Unity skframework framework (XII), score scoring module
每日一题:1175.质数排列
机器学习基础(二)——训练集和测试集的划分