当前位置:网站首页>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();
}
边栏推荐
- 2022零代码/低代码开发白皮书【伙伴云出品】附下载
- Japan bet on national luck: Web3.0, anyway, is not the first time to fail!
- Verification failed, please check your call back website. You can follow the instructions
- Find love for speed in F1 delta time Grand Prix
- 693. 行程排序(map + 拓扑)
- Qt-制作一个简单的计算器-实现四则运算-将结果以对话框的形式弹出来
- De4000h storage installation configuration
- 代码实现MNLM
- How to explain binary search to my sister? This is really difficult, fan!
- 石子合并板子【区间DP】(普通石子合并 & 环形石子合并)
猜你喜欢
错误:EACCES:权限被拒绝,访问“/usr/lib/node_modules”
Node.js通过ODBC访问PostgreSQL数据库
三翼鸟两周年:羽翼渐丰,腾飞指日可待
OpenApi-Generator:简化RESTful API开发流程
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!
Unity small map production [2]
Independent and controllable 3D cloud CAD: crowncad enables innovative design of enterprises
Find love for speed in F1 delta time Grand Prix
【蓝桥杯选拔赛真题43】Scratch航天飞行 少儿编程scratch蓝桥杯选拔赛真题讲解
Unity skframework framework (XII), score scoring module
随机推荐
口袋奇兵点评
Partner cloud form strong upgrade! Pro version, more extraordinary!
最近公共祖先LCA的三种求法
Redis database persistence
Sum of the first n terms of Fibonacci (fast power of matrix)
Daily question: 1175 Prime permutation
Runhe hi3516 development board openharmony small system and standard system burning
Verification failed, please check your call back website. You can follow the instructions
De4000h storage installation configuration
[true topic of the Blue Bridge Cup trials 43] scratch space flight children's programming explanation of the true topic of the Blue Bridge Cup trials
【笔耕不辍勋章活动】生命不止,写作不息
[USACO05JAN]Watchcow S(欧拉回路)
nohup命令
[技术发展-22]:网络与通信技术的应用与发展快速概览-2- 通信技术
mysql ---- Oracle中的rownum转换成MySQL
Student course selection information management system based on ssm+jsp framework [source code + database]
P3807 [template] Lucas theorem /lucas theorem
Bridge of undirected graph
【文档树、设置】字体变小
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!