HDU - 4635 Strongly connected(求树的直径)(边双连通分量-有重边)

HDU - 4635 Strongly connected
题目大意:
给你一个无向的连通图,存在重边,然后问你加一条之后,最少的桥的边数。
先求出桥,然后再对重边特判后重新缩点构图,最后获得一棵树,求出直径,把边数减去直径就是答案。

代码

#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
#include   
using namespace std;
#define clr(a,b) memset(a,b,sizeof(a))
#define pb(a) push_back(a)
#define fir first
#define se second
#define LL unsigned long long
typedef pair<int,int> pii;
typedef pairint> pli;
typedef pair pll;
const int maxn = 2e5+5;
const int inf = 0x3f3f3f3f;
LL mod = 1e9+7;

vector<int> g[maxn];
int n,low[maxn],dfn[maxn],dfs_clock,m;
mapint> mp;
set<int> gg[maxn];
pii ss;

void init() {  //初始化
    for(int i = 0;i <= n;i++) {
        g[i].clear();
        gg[i].clear();
    }
    dfs_clock = 0;
    clr(dfn,0);
    clr(low,0);
    mp.clear();
}

int dfs(int u,int fa) {  //dfs求每个点的dfn和low
    low[u] = dfn[u] = ++dfs_clock;
    int child = 0;
    for(int i = 0;i < g[u].size();i++) {
        int v = g[u][i];
        if(!dfn[v]) {
            child++;
            low[v] = dfs(v,u);
            low[u] = min(low[u],low[v]);
            if(low[v] > dfn[u]) {  //判断桥
                int a = v,b = u;
                if(a > b) swap(a,b);
                mp[make_pair(a,b)] += 10;   
            }
        }
        else if(dfn[v] < dfn[u] && v != fa) {
            low[u] = min(low[u],dfn[v]);
        }
    }
    return low[u];
}

void dfs2(int u) {
    dfn[u] = 1;
    low[u] = dfs_clock;
    for(int i = 0;i < g[u].size();i++) {
        int v = g[u][i];
        int a = u,b = v;
        if(a > b) swap(a,b);
        if(mp[make_pair(a,b)] == 11) continue;
        if(!dfn[v]) dfs2(v);
    }
}

void input() {  //输入
    for(int i = 1;i <= m;i++) {
        int u,v;
        scanf("%d%d",&u,&v);
        g[u].pb(v);
        g[v].pb(u);
        int a = u,b = v;
        if(a > b) swap(a,b);
        mp[make_pair(a,b)]++;
    }
}

void bfs(int s) {
    queue q;
    q.push(make_pair(s,0));
    clr(dfn,0);
    ss.se = -1;
    while(!q.empty()) {
        pii tt = q.front();
        if(tt.se > ss.se) ss = tt;
        int u = tt.fir;
        dfn[u] = 1,q.pop();
        for(auto v : gg[u]) {
            if(dfn[v]) continue;
            q.push(make_pair(v,tt.se+1));
        }
    }
}

int main() {
    while(scanf("%d%d",&n,&m) != EOF && (n || m)) {
        init();
        input();
        dfs(1,-1);
        /*for(auto it : mp) {
            cout<
        clr(dfn,0);
        dfs_clock = 0;
        for(int i = 1;i <= n;i++) {
            if(!dfn[i]) {
                dfs_clock++;
                dfs2(i);
            }
        }
        /*for(int i = 1;i <= n;i++) printf("%d ",low[i]);
        cout<
        for(int i = 1;i <= n;i++) {
            for(int v : g[i]) {
                if(low[i] != low[v]) {
                //printf("%d-%d\n",i,v);
                    gg[low[i]].insert(low[v]);
                    gg[low[v]].insert(low[i]);
                }
            }
        }
        for(int i = 1;i <= n;i++) g[i].clear();
        /*for(int i = 1;i <= dfs_clock;i++) {
            cout<
        bfs(1);
        bfs(ss.fir);
        printf("%d\n",dfs_clock-ss.se-1);
    }
}

/*
6 6
1 2 2 3 3 4 4 5 2 6 6 2

7 8
1 2 2 3 3 4 4 5 2 6 6 2 1 3 4 7

5 4
2 1 2 3 2 4 2 5

5 5
2 1 2 3 2 4 2 5 3 4

5 5
1 2 2 3 3 4 3 4 4 5
*/

你可能感兴趣的:(连通图)