标签:
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <cstdlib> #include <algorithm> #include <vector> usingnamespace std; #define INF 0x7ffffff #define maxn 5005 typedef longlong LL; #define Min(a,b) (a<b?a:b) #define MOD 1000000007 int m, n, Time, top, ans; int Stack[maxn], dfn[maxn], low[maxn], blocks[maxn], Out[maxn]; bool InStack[maxn]; vector<vector<int> > G; void init() { memset(Out, 0, sizeof(Out)); memset(dfn, 0, sizeof(dfn)); memset(low, 0, sizeof(low)); ans = Time = top = 0; G.clear(); G.resize(n+2); } void Tarjan(int u) { dfn[u] = low[u] = ++Time; Stack[top++] = u; InStack[u] = true; int len = G[u].size(), v; for(int i=0; i<len; i++) { v = G[u][i]; if( !low[v] ) { Tarjan(v); low[u] = min(low[u], low[v]); } elseif( InStack[v] ) low[u] = min(low[u], dfn[v]); } if(low[u] == dfn[u]) { do { v = Stack[--top]; InStack[v] = false; blocks[v] = ans; } while(u != v); ans ++; } } void solve() { for(int i=1; i<=n; i++) { if(!low[i]) Tarjan(i); } for(int i=1; i<=n; i++) { int len = G[i].size(), v; for(int j=0; j<len; j++) { v = G[i][j]; if(blocks[i] != blocks[v]) Out[blocks[i]] ++; } } int flag = 0; for(int i=1; i<=n; i++) { if(Out[blocks[i]] == 0 && !flag) { flag ++; printf("%d", i); } elseif(Out[blocks[i]] == 0) printf(" %d", i); } printf("\n"); } int main() { while(scanf("%d",&n), n) { scanf("%d",&m); init(); while(m --) { int a, b; scanf("%d %d",&a, &b); G[a].push_back(b); } solve(); } return0; }
POJ 2553 The Bottom of a Graph
标签:
原文地址:http://www.cnblogs.com/chenchengxun/p/4718761.html