题意:
给出无向图,求桥的模板题。
#include <bits/stdc++.h> using namespace std; const int N = 10004; int dfn[N], low[N];//时间戳;low[i]以i为根子树的最小祖先的时间戳 bool vis[N]; vector<int>V[N]; int n, num, c; pair<int, int>P[N]; void dfs(int s, int f) { low[s] = dfn[s] = ++num; for(unsigned int i = 0; i < V[s].size(); i++) { int v = V[s][i]; if(!dfn[v])//未访问过(s-v为树边) { dfs(v, s);//dfs完更新出low[v] low[s] = min(low[s], low[v]); if(low[v] > dfn[s])//割边不加= P[++c].first = min(v, s), P[c].second = max(v, s); } else if(v != f) low[s] = min(low[s], dfn[v]);//回边且不是父子 } } int main() { int t, n, cas = 0, m, a, b; cin>>t; while(t--) { memset(dfn, 0, sizeof dfn); memset(low, 0, sizeof low); memset(vis, 0, sizeof vis); scanf("%d", &n); for(int i = 0 ; i < n; i++) V[i].clear(); num = 0, c = 0; for(int i = 1; i <= n; i++) { scanf("%d (%d)", &a, &m); while(m--) { scanf("%d", &b); V[a].push_back(b); V[b].push_back(a); } } for(int i = 0; i < n; i++) { if(!vis[i]) dfs(i, i); } sort(P+1, P+1+c); printf("Case %d:\n%d critical links\n", ++cas, c); for(int i = 1; i <= c; i++) printf("%d - %d\n", P[i].first, P[i].second); } return 0; }
原文地址:https://www.cnblogs.com/lesroad/p/8747237.html
时间: 2024-10-10 20:29:58