http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82833#problem/C
UVA链接地址
Root | |
796 - Critical LinksTime limit: 3.000 seconds |
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm> #include <cmath> #include <cstdlib> #include <limits> #include <queue> #include <stack> #include <vector> #include <map> using namespace std; #define N 12005 #define INF 0xfffffff #define PI acos (-1.0) #define EPS 1e-8 struct node { int x, y; bool friend operator < (node a, node b) { if (a.x == a.y) return a.y < b.y; return a.x < b.x; } }bridge[N]; vector <int> G[N]; int n, low[N], dfn[N], f[N], Time, ans; void Init (); void solve (); void tarjan (int u, int fa); int main () { while (~scanf ("%d", &n)) { Init (); for (int i=0; i<n; i++) { int a, b, m; scanf ("%d (%d)", &a, &m); while (m--) { scanf ("%d", &b); G[a].push_back (b); G[b].push_back (a); } } solve (); } return 0; } void Init () { memset (low, 0, sizeof (low)); memset (dfn, 0, sizeof (dfn)); memset (f, 0, sizeof (f)); Time = 0; for (int i=0; i<n; i++) G[i].clear (); } void solve () { ans = 0; for (int i=0; i<n; i++) if (!low[i]) tarjan (i, -1); for (int i=0; i<n; i++) { int v = f[i]; if (v != -1 && dfn[v] < low[i]) { bridge[ans].x = i; bridge[ans].y = v; if (bridge[ans].x > bridge[ans].y) swap (bridge[ans].x, bridge[ans].y); ans++; } } sort (bridge, bridge+ans); printf ("%d critical links\n", ans); for (int i=0; i<ans; i++) printf ("%d - %d\n", bridge[i].x, bridge[i].y); puts (""); } void tarjan (int u, int fa) { low[u] = dfn[u] = ++Time; f[u] = fa; int len = G[u].size (), v; for (int i=0; i<len; i++) { v = G[u][i]; if (!low[v]) { tarjan (v, u); low[u] = min (low[u], low[v]); } else if (fa != v) low[u] = min (low[u], dfn[v]); } }