poj 2553 The Bottom of a Graph(强连通分量+缩点)
2013-08-10 18:00
447 查看
题目地址:http://poj.org/problem?id=2553
The Bottom of a Graph
Description
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 graph G. 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 set V={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
Sample Output
Source
Ulm Local 2003
【题解】:
找到入度为0的所有强连通分量,将其中的点排序后输出
这题跟poj 2186 很类似
【code】:
The Bottom of a Graph
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 7881 | Accepted: 3263 |
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 graph G. 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 set V={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
Source
Ulm Local 2003
【题解】:
找到入度为0的所有强连通分量,将其中的点排序后输出
这题跟poj 2186 很类似
【code】:
/** Judge Status:Accepted Memory:1488K Time:125MS Language:G++ Code Length:2443B Author:cj */ #include<iostream> #include<stdio.h> #include<string.h> #include<stack> #include<vector> #include<algorithm> #define N 10010 using namespace std; vector<int> G ; stack<int> stk; int pre ,lowlink ,sccno ,scc_cnt,dfn_clock,out ,counter ; void DFN(int u) //tarjan算法 { lowlink[u] = pre[u] = ++dfn_clock; stk.push(u); int i; for(i=0;i<G[u].size();i++) { int v = G[u][i]; if(!pre[v]) { DFN(v); lowlink[u] = min(lowlink[u],lowlink[v]); } else if(!sccno[v]) { lowlink[u] = min(lowlink[u],pre[v]); } } if(lowlink[u]==pre[u]) { scc_cnt++; //强连通图的个数标记 while(1) { int x = stk.top(); stk.pop(); sccno[x] = scc_cnt; if(x==u) break; } } } void findscc(int n) { int i; scc_cnt = dfn_clock = 0; memset(pre,0,sizeof(pre)); memset(lowlink,0,sizeof(lowlink)); memset(sccno,0,sizeof(sccno)); for(i=1;i<=n;i++) if(!pre[i]) DFN(i); } int main() { int n,m; while(~scanf("%d",&n)&&n) { scanf("%d",&m); int i; for(i=1;i<=n;i++) G[i].clear(); for(i=0;i<m;i++) { int a,b; scanf("%d%d",&a,&b); G[a].push_back(b); // 得到图 } findscc(n); //查找强连通图 int j; memset(out,0,sizeof(out)); memset(counter,0,sizeof(counter)); for(i=1;i<=n;i++) //遍历一边图,查找统计个点缩点后的出度 { for(j=0;j<G[i].size();j++) { int v = G[i][j]; if(sccno[i]!=sccno[v]) { out[sccno[i]]++; //出度 } } } for(i=1;i<=scc_cnt;i++) { if(!out[i]) //出度为0的强连通分量 { counter[i] = 1; //标记 } } int pl = 0; for(i=1;i<=n;i++) if(counter[sccno[i]]) //是否被标记,从下到大 { if(pl) printf(" %d",i); else printf("%d",i); pl = 1; } putchar(10); } return 0; }
相关文章推荐
- POJ2553 The Bottom of a Graph(强连通分量+缩点)
- POJ 2553 The Bottom of a Graph (强连通分量+缩点)
- POJ 2553 The Bottom of a Graph 强连通分量+缩点 tarjan or kosaraju
- Poj 2553 The Bottom of a Graph【强连通Kosaraju+缩点染色】
- poj2553 The Bottom of a Graph
- The Bottom of a Graph poj 2553 缩点+Tarjan
- POJ2553 The Bottom of a Graph 强连通 tarjan
- POJ 2553 The Bottom of a Graph
- POJ 2553 The Bottom of a Graph(Tarjan)
- poj 2553 The Bottom of a Graph
- poj 2553 The Bottom of a Graph 【有向图tarjan 求SCC 处理出度为0的SCC】
- POJ2553 The Bottom of a Graph 强连通 tarjan
- POJ 2553 The Bottom of a Graph 强连通分量
- POJ 2553 The Bottom of a Graph 【scc tarjan】
- 强连通分量 ( Tarjan,邻接链表 )——The Bottom of a Graph ( POJ 2553 )
- POJ 2553 - The Bottom of a Graph(Tarjan + 缩点 + 计算出度)
- POJ 2553 —— The Bottom of a Graph
- POJ 2553 The Bottom of a Graph
- poj 2553 The Bottom of a Graph(强连通分量)
- [poj][2553][The Bottom of a Graph]