• hdu 4612 Warm up 有重边缩点+树的直径


    题目链接

    Warm up

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
    Total Submission(s): 5353    Accepted Submission(s): 1195


    Problem Description
      N planets are connected by M bidirectional channels that allow instant transportation. It's always possible to travel between any two planets through these channels.
      If we can isolate some planets from others by breaking only one channel , the channel is called a bridge of the transportation system.
    People don't like to be isolated. So they ask what's the minimal number of bridges they can have if they decide to build a new channel.
      Note that there could be more than one channel between two planets.
     
    Input
      The input contains multiple cases.
      Each case starts with two positive integers N and M , indicating the number of planets and the number of channels.
      (2<=N<=200000, 1<=M<=1000000)
      Next M lines each contains two positive integers A and B, indicating a channel between planet A and B in the system. Planets are numbered by 1..N.
      A line with two integers '0' terminates the input.
     
    Output
      For each case, output the minimal number of bridges after building a new channel in a line.
     
    Sample Input
    4 4 1 2 1 3 1 4 2 3 0 0
     
    Sample Output
    0
    给一个图, 求增加一条边之后的桥的数量最少是多少。有重边
     
    缩点然后找树的直径, 答案就是缩点之后的边数-直径。
    tarjan的时候注意, vis数组记录访问过的边的编号而不是点。
    找树的直径最好写bfs, dfs据说爆栈
    详细看代码。
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    using namespace std;
    #define mem(a) memset(a, 0, sizeof(a))
    #define mem1(a) memset(a, -1, sizeof(a))
    #define mem2(a) memset(a, 0x3f, sizeof(a))
    #define fi first
    #define se second
    typedef pair<int, int> pll;
    const int inf = 1061109567;
    const int maxn = 2e5+5;
    const int maxe = 1e6+5;
    int head[maxn], head1[maxn], dis[maxn], num, num1, top, cnum, instack[maxn], st[maxn], dfn[maxn], low[maxn], s[maxn];
    int maxx, pos, vis[maxe*2], cnt;
    struct node
    {
        int to, nextt;
    }e[maxe*2], e1[maxe*2];
    void add(int u, int v) {
        e[num].to = v, e[num].nextt = head[u], head[u] = num++;
    }
    void add1(int u, int v) {
        e1[num1].to = v, e1[num1].nextt = head1[u], head1[u] = num1++;
    }
    void init() {
        num = num1 = cnt = cnum = top = 0;
        mem1(head);
        mem(s);
        mem(vis);
        mem1(head1);
        mem(instack);
        mem(st);
        mem(dfn);
        mem(low);
        mem(dis);
    }
    pll edge[maxe];
    void tarjan(int u) {
        instack[u] = 1;
        st[top++] = u;
        dfn[u] = low[u] = ++cnt;
        for(int i = head[u]; ~i; i = e[i].nextt) {
            int v = e[i].to;
            if(vis[i])
                continue;
            vis[i] = vis[i^1] = 1;
            if(!dfn[v]) {
                tarjan(v);
                low[u] = min(low[u], low[v]);
            } else if(instack[v]) {
                low[u] = min(low[u], dfn[v]);
            }
        }
        if(low[u] == dfn[u]) {
            ++cnum;
            int x;
            do {
                x = st[--top];
                instack[x] = 0;
                s[x] = cnum;
            } while(x != u);
        }
    }
    void bfs(int u) {
        queue <int> q;
        q.push(u);
        mem2(dis);
        dis[u] = 0;
        mem(vis);
        vis[u] = 1;
        maxx = 0, pos = u;
        while(!q.empty()) {
            int v = q.front(); q.pop();
            for(int i = head1[v]; ~i; i = e1[i].nextt) {
                int ve = e1[i].to;
                if(vis[ve])
                    continue;
                vis[ve] = 1;
                dis[ve] = dis[v]+1;
                if(dis[ve]>maxx) {
                    maxx = dis[ve];
                    pos = ve;
                }
                q.push(ve);
            }
        }
    }
    int main()
    {
        int n, m, x, y;
        while(cin>>n>>m) {
            if(n+m==0)
                break;
            init();
            for(int i = 0; i<m; i++) {
                scanf("%d%d", &x, &y);
                edge[i].fi = x, edge[i].se = y;
                add(x, y);
                add(y, x);
            }
            tarjan(1);
            int edgenum = 0;
            for(int i = 0; i<m; i++) {
                int x = edge[i].fi, y = edge[i].se;
                if(s[x]!=s[y]) {
                    add1(s[x], s[y]);
                    add1(s[y], s[x]);
                    edgenum++;
                }
            }
            bfs(s[1]);
            bfs(pos);
            int ans = edgenum-maxx;
            printf("%d
    ", ans);
        }
        return 0;
    }
  • 相关阅读:
    HDU 1301 Jungle Roads
    HDU 1671 Phone List
    HDU 1992 Tiling a Grid With Dominoes
    HDU 1251 统计难题
    总结自己的近期表现
    Windows API 函数: SetClassLong
    ModifyStyle
    assert,assert_valid,verify,trace用法
    用VC++绘制位图按钮
    Codeforces 144D. Missile Silos 最短路
  • 原文地址:https://www.cnblogs.com/yohaha/p/5232460.html
Copyright © 2020-2023  润新知