码迷,mamicode.com
首页 > 移动开发 > 详细

UVA - 10765 Doves and bombs (双联通分量)

时间:2015-05-25 01:00:20      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:

链接 :


http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34798

给N个点的无向图并且联通,问删除每次一个点之后还剩多少联通分量。

找割顶 如果删除的是割顶 联通分量就会增加,否则还是1(因为原图是联通图),删除割顶之后 联通块的数目 就要看该割顶在几个双联通分量里出现过。


#pragma comment(linker, "/STACK:10240000,10240000")
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define mod 4294967296
#define MAX 0x3f3f3f3f
#define lson o<<1, l, m
#define rson o<<1|1, m+1, r
#define SZ(x) ((int)ans.size())
#define MAKE make_pair
#define INFL 0x3f3f3f3f3f3f3f3fLL
#define mem(a) memset(a, 0, sizeof(a))
const double pi = acos(-1.0);
const double eps = 1e-9;
const int N = 10005;
const int M = 20005;
typedef long long ll;
using namespace std;

int n, m;
int pre[N], iscut[N], bcc[N], dfs_clock, bcc_cnt;
vector <int> G[N], BCC[N];
struct Edge {
    int u, v;
};
stack <Edge> S;

struct C {
    int id, num;
} out[N];

int dfs(int u, int fa) {
    int lowu = pre[u] = ++dfs_clock;
    int child = 0;
    for(int i = 0; i < G[u].size(); i++) {
        int v = G[u][i];
        Edge e = (Edge) {u, v};
        if(pre[v] == 0) {
            S.push(e);
            child++;
            int lowv = dfs(v, u);
            lowu = min(lowu, lowv);
            if(lowv >= pre[u]) {
                iscut[u] = 1;
                bcc_cnt++;
                BCC[bcc_cnt].clear();
                for(;;) {
                    Edge x = S.top(); S.pop();
                    if(bcc[x.u] != bcc_cnt) {
                        BCC[bcc_cnt].push_back(x.u);
                        bcc[x.u] = bcc_cnt;
                    }
                    if(bcc[x.v] != bcc_cnt) {
                        BCC[bcc_cnt].push_back(x.v);
                        bcc[x.v] = bcc_cnt;
                    }
                    if(x.u == u && x.v == v) break;
                }
            }
        } else if(pre[v] < pre[u] && v != fa) {
            S.push(e);
            lowu = min(lowu, pre[v]);
        }
    }

    if(fa < 0 && child == 1) {
        iscut[u] = 0;
    }
    return lowu;
}

void find_bcc() {
    mem(pre);
    mem(iscut);
    mem(bcc);
    //mem(ans);
    dfs_clock = bcc_cnt = 0;
    for(int i = 0; i < n; i++) {
        if(pre[i] == 0) dfs(i, -1);
    }
}

bool cmp(C x, C y) {
    if(x.num != y.num)
    return x.num > y.num;
    return x.id < y.id;
}
int ans[N];

int main()  {

    //freopen("in.txt","r",stdin);

    while(cin >> n >> m && n + m) {
        int x, y;
        mem(ans);
        for(int i = 0; i < n; i++) {
            G[i].clear();
        }
        while(scanf("%d%d", &x, &y) && x != -1) {
            G[x].push_back(y);
            G[y].push_back(x);
        }

        find_bcc();

        for(int i = 1; i <= bcc_cnt; i++) {
            for(int j = 0; j < BCC[i].size(); j++) {
                ans[ BCC[i][j] ] ++;
            }
        }
        int k = 0;
        for(int i = 0; i < n; i++) {
            out[i].id = i;
            if(iscut[i]) {
                out[i].num = ans[i];
            } else {
                out[i].num = 1;
            }
        }
        sort(out, out + n, cmp);
        for(int i = 0; i < m; i++) {
            printf("%d %d\n", out[i].id, out[i].num);
        }
        puts("");
    }

    return 0;
}

UVA - 10765 Doves and bombs (双联通分量)

标签:

原文地址:http://blog.csdn.net/u013923947/article/details/45958205

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!