1. 程式人生 > >【lightoj-1026】Critical Links(橋)

【lightoj-1026】Critical Links(橋)

割邊 class 更新 ear \n DC ace n) max

題意:

給出無向圖,求橋的模板題。

#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; }

【lightoj-1026】Critical Links(橋)