首页 > 代码库 > XidianOJ 1072 National Disaster

XidianOJ 1072 National Disaster

技术分享

--正文

求无向图桥模板题

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
#define SIZE 10005
int dfn[SIZE],low[SIZE];
vector<int> link[SIZE];
int n,m;
int res = 0,times = 0;
void dfs(int u,int pre){
    times ++;
    dfn[u] = times; low[u] = times;
    int i;
    for (i=0;i<link[u].size();i++){
        int v = link[u][i];
        if (v == pre) continue;
        if (dfn[v] == 0){
            dfs(v,u);
            low[u] = min(low[u],low[v]);
            if (dfn[u] < low[v]) res ++;
        }
        else low[u] = min(low[u],dfn[v]);
    }
}

int main()
{
    int time,T;
    scanf("%d",&T);
    for (time=1;time<=T;time++){
        scanf("%d %d",&n,&m);
        res = 0; times = 0;
        memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low));
        int i;
        for (i=0;i<n;i++){
            link[i].clear();
        }
        for (i=1;i<=m;i++){
            int a,b;
            scanf("%d %d",&a,&b);
            link[a].push_back(b); link[b].push_back(a);
        }
        dfs(0,-1);
        printf("%d\n",res);
    }
    return 0;
}

 

XidianOJ 1072 National Disaster