[description]
:拓撲排序,並且輸出最小字典序的乙個可能順序,
採用最小優先順序佇列可以保證按最小字典序輸出。
[input]:
輸入第一行包含兩個數n, m分別表示有向無環圖的點數和邊數。
接下來m行,每行兩個數ai, bi,表示圖中存在一條ai指向bi的有向邊。
[output]:輸出n個數,每個數用空格隔開,表示該圖的拓撲序。
拓撲排序:1.
統計每個結點的入度,將度為0的結點編號放入
佇列(此題放入優先佇列中)中。
2.進行迴圈:
①取出隊頭結點,視作邊的起點。
②刪除與該點相連的邊,即將這個圖中的該邊另乙個結點(即終點)的入度減一。
③如果減一以後,終點的入度變為了0,那麼將終點的編號入
佇列。④判斷佇列是否為空,若不為空回到①。
優先順序佇列:
stl:priority_queue,greater> q;
#include#include#include#includeusing namespace std;
const int maxn=100005;
int n,m;
int a,b;
struct edge
e[maxn];
int in[maxn];//記錄入度
int head[maxn];
int k;
priority_queue,greater> q;
void add(int u,int v)
void topp()
} while(!q.empty()) }
}int main()
topp();
return 0;
}
佇列 優先順序佇列
優先順序佇列的隊尾是不需要改變的,永遠在低下標處。當佇列增加資料時,隊頭的位置就是資料項的大小減去1.public class priorityq 插入 public void insert long item else quearray j 1 item nitem 刪除 public long ...
優先順序佇列
分為最小優先順序佇列和最大優先順序佇列。優先順序佇列是一種用來維護一組元素構成的集合s的資料結構,這一組元素都有乙個關鍵字key,乙個最大優先順序佇列支援的操作 insert s,x 把x插入到集合s中 maxmum s 返回s中最大元素 extra max s 去掉s中最大關鍵字並返回該最大關鍵子...
優先順序佇列
1 include stdafx.h 2 include3 4 using namespace std 5 6 define max heap len 107 int heap max heap len 8 int heap size 0 the number of elements in heap...