日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

luogu1347 排序

發布時間:2023/12/20 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 luogu1347 排序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

題目大意

?  一個不同的值的升序排序數列指的是一個從左到右元素依次增大的序列。給你一系列形如A<B的關系,并要求你判斷是否能夠根據這些關系確定這個數列的順序(能,矛盾,不確定)。確定n個元素的順序后即可結束程序,可以不用考慮確定順序之后出現矛盾的情況。

?題解

  如果A<B,則在圖中A結點向B結點連一條有向邊,這樣,如果出現了矛盾情況,則有向圖中出現了環。如果確定了數列的順序,則圖中存在一條鏈把1~n所有結點都串起來了。換句話說,把這個有向圖的邊權都設為1,則該有向圖中的最長路徑為n時,能夠確定序列順序。那么這道題就是拓撲排序的模板題了。

#include <cstdio> #include <cstdarg> #include <iostream> #include <cstring> #include <algorithm> #include <vector> #include <stack> #include <string> #include <iostream> using namespace std;#define NotVis 0 #define Finished 1 #define InStack -1const int MAX_NODE = 50;struct TopSort {int N;bool HaveCircle;int MaxDist;struct Node{int Color;//0:NotVis 1:Finished -1:InStackint Dist;vector<Node*> Next;}_nodes[MAX_NODE];stack<Node*> St;void Dfs(Node *cur){if (cur->Color == InStack){HaveCircle = true;return;}if (cur->Color == Finished)return;cur->Color = InStack;for (int i = 0; i < cur->Next.size(); i++){if (HaveCircle)return;Dfs(cur->Next[i]);}cur->Color = Finished;St.push(cur);}TopSort(int n):N(n){}void Build(int from, int to){_nodes[from].Next.push_back(_nodes + to);}void Init(){MaxDist = -1;while (!St.empty())St.pop();HaveCircle = false;for (int i = 1; i <= N; i++)_nodes[i].Color = _nodes[i].Dist = 0;}void GetMaxDist(){if (HaveCircle){MaxDist = -1;return;}stack<Node*> tempSt = St;while (!tempSt.empty()){Node *cur = tempSt.top();tempSt.pop();MaxDist = max(MaxDist, cur->Dist);for (int i = 0; i < cur->Next.size(); i++)cur->Next[i]->Dist = max(cur->Next[i]->Dist, cur->Dist + 1);}}void Proceed(){Init();for (int i = 1; i <= N; i++)Dfs(_nodes + i);GetMaxDist();} };int main() {int totNode, opCnt;string s;cin >> totNode >> opCnt;static TopSort g(totNode);for (int i = 1; i <= opCnt; i++){cin >> s;int a = s[0] - 'A' + 1, b = s[2] - 'A' + 1;if (s[1] == '>')swap(a, b);g.Build(a, b);g.Proceed();if (g.HaveCircle){printf("Inconsistency found after %d relations.\n", i);return 0;}else if (g.MaxDist == totNode - 1){printf("Sorted sequence determined after %d relations: ", i);stack<TopSort::Node*> temp = g.St;while (!temp.empty()){printf("%c", (int)(temp.top() - g._nodes) - 1 + 'A');temp.pop();}printf(".\n");return 0;}}printf("Sorted sequence cannot be determined.\n");return 0; }

  

轉載于:https://www.cnblogs.com/headboy2002/p/9180447.html

總結

以上是生活随笔為你收集整理的luogu1347 排序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。