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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【POJ - 2553】The Bottom of a Graph(tarjan强连通分量缩点,模板题)

發布時間:2023/12/10 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【POJ - 2553】The Bottom of a Graph(tarjan强连通分量缩点,模板题) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

We will use the following (standard) definitions from graph theory. Let?V?be a nonempty and finite set, its elements being called vertices (or nodes). Let?E?be a subset of the Cartesian product?V×V, its elements being called edges. Then?G=(V,E)is called a directed graph.?
Let?n?be a positive integer, and let?p=(e1,...,en)?be a sequence of length?n?of edges?ei∈E?such that?ei=(vi,vi+1)?for a sequence of vertices?(v1,...,vn+1). Then?p?is called a path from vertex?v1?to vertex?vn+1?in?G?and we say that?vn+1?is reachable from?v1, writing?(v1→vn+1).?
Here are some new definitions. A node?v?in a graph?G=(V,E)?is called a sink, if for every node?w?in?G?that is reachable from?v,?v?is also reachable from?w. The bottom of a graph is the subset of all nodes that are sinks, i.e.,?bottom(G)={v∈V|?w∈V:(v→w)?(w→v)}. You have to calculate the bottom of certain graphs.

Input

The input contains several test cases, each of which corresponds to a directed graphG. Each test case starts with an integer number?v, denoting the number of vertices of?G=(V,E), where the vertices will be identified by the integer numbers in the setV={1,...,v}. You may assume that?1<=v<=5000. That is followed by a non-negative integer?e?and, thereafter,?e?pairs of vertex identifiers?v1,w1,...,ve,we?with the meaning that?(vi,wi)∈E. There are no edges other than specified by these pairs. The last test case is followed by a zero.

Output

For each test case output the bottom of the specified graph on a single line. To this end, print the numbers of all nodes that are sinks in sorted order separated by a single space character. If the bottom is empty, print an empty line.

Sample Input

3 3 1 3 2 3 3 1 2 1 1 2 0

Sample Output

1 3 2

題目大意:

定義點v是匯點須滿足 --- 對圖中任意點u,若v可以到達u則必有u到v的路徑;若v不可以到達u,則u到v的路徑可有可無。問在n個點m條邊的有向圖里面,問有多少個點是匯點。

解題報告:

縮點后看出度為0的點的個數就行了。

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair using namespace std; typedef pair<int,int> PII; const int MAX = 2e5 + 5; vector<int> vv[MAX],ans; int n,m; int dfn[MAX],low[MAX],vis[MAX],out[MAX],cnt[MAX],col[MAX]; int stk[MAX],index,clk,scc; void init() {for(int i = 1; i<=n; i++) {vv[i].clear();dfn[i]=low[i]=vis[i]=out[i]=cnt[i]=0;}clk = index = scc = 0;ans.clear(); } void tarjan(int x) {stk[++index] = x;vis[x] = 1;dfn[x] = low[x] = ++clk;int up = vv[x].size();for(int i = 0; i<up; i++) {int v = vv[x][i];if(dfn[v] == 0) {tarjan(v);low[x] = min(low[x],low[v]);}else if(vis[v] == 1) low[x] = min(low[x],dfn[v]);}if(dfn[x] == low[x]) {scc++;while(1) {int tmp = stk[index];index--;col[tmp] = scc;cnt[scc]++;vis[tmp]=0;if(tmp == x) break;}} } int main() {while(~scanf("%d",&n)) {if(n == 0) break;scanf("%d",&m);init();for(int u,v,i = 1; i<=m; i++) {scanf("%d%d",&u,&v);vv[u].pb(v);}for(int i = 1; i<=n; i++) {if(dfn[i] == 0) tarjan(i);}for(int up,u = 1; u<=n; u++) {up = vv[u].size();for(int v,j = 0; j<up; j++) {v = vv[u][j];if(col[u] == col[v]) continue;out[col[u]]++;}}for(int i = 1; i<=n; i++) {if(out[col[i]] == 0) ans.pb(i);}sort(ans.begin(),ans.end());int up = ans.size();for(int i = 0; i<up; i++) printf("%d%c",ans[i],i == up-1 ? '\n' : ' ');}return 0 ; }

?

總結

以上是生活随笔為你收集整理的【POJ - 2553】The Bottom of a Graph(tarjan强连通分量缩点,模板题)的全部內容,希望文章能夠幫你解決所遇到的問題。

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