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

链接 :


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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。