码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 1108 - Mining Your Own Business

时间:2015-03-14 15:20:21      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:uva

刘汝佳书上都给出了完整的代码


在这里理一下思路:

由题意知肯定存在一个或者多个双连通分量;

如果某一个双连通分量有割顶,那太平井一定不能打在割顶上。而是选择割顶之外的任意一个点;

如果没有割顶,则要在该双连通分量上打两个井


至于打井方案,见代码

#include <cstdio>  
#include <cstring>  
#include <vector>  
#include <stack>  
#include <map>  
using namespace std;  
  
const int N = 50005;  
  
struct Edge {  
    int u, v;  
    Edge() {}  
    Edge(int u, int v) {  
        this->u = u;  
        this->v = v;  
    }  
};  
  
int pre[N], bccno[N], dfs_clock, bcc_cnt;  
bool iscut[N];  
  
vector<int> g[N], bcc[N];  
stack<Edge> S;  
  
int dfs_bcc(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]) {  
            S.push(e);  
            child++;  
            int lowv = dfs_bcc(v, u);  
            lowu = min(lowu, lowv);  
            if (lowv >= pre[u]) {  
                iscut[u] = true;  
                bcc_cnt++; bcc[bcc_cnt].clear(); //start from 1  
                while(1) {  
                    Edge x = S.top(); S.pop();  
                    if (bccno[x.u] != bcc_cnt) {bcc[bcc_cnt].push_back(x.u); bccno[x.u] = bcc_cnt;}  
                    if (bccno[x.v] != bcc_cnt) {bcc[bcc_cnt].push_back(x.v); bccno[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] = false;  
    return lowu;  
}  
  
int st;  
  
void find_bcc() {  
    memset(pre, 0, sizeof(pre));  
    memset(iscut, 0, sizeof(iscut));  
    memset(bccno, 0, sizeof(bccno));  
    dfs_clock = bcc_cnt = 0;  
    dfs_bcc(0, -1);  
}  
  
int n, m;  
  
typedef long long ll;  
  
void solve() {  
    ll ans1 = 0, ans2 = 1;  
    for (int i = 1; i <= bcc_cnt; i++) {  
        int cut_cnt = 0;  
        for (int j = 0; j < bcc[i].size(); j++)  
            if (iscut[bcc[i][j]]) cut_cnt++;  
        if (cut_cnt == 1) {  
            ans1++;   
            ans2 *= (ll)(bcc[i].size() - cut_cnt);  
        }  
    }  
    if (bcc_cnt == 1) {  
        ans1 = 2;   
        ans2 = (ll)bcc[1].size() * (bcc[1].size() - 1) / 2;  
    }  
    printf(" %lld %lld\n", ans1, ans2);  
}  
  
int main() {  
    int cas = 0;  
    while (~scanf("%d", &m) && m) {  
        int u, v, Max = 0;  
        while (m--) {  
            scanf("%d%d", &u, &v);  
            u--; v--;  
            g[u].push_back(v);  
            g[v].push_back(u);  
            Max = max(Max, u);  
            Max = max(Max, v);  
        }  
        find_bcc();  
        printf("Case %d:", ++cas);  
        solve();  
        for (int i = 0; i <= Max; i++)  
            g[i].clear();  
    }  
    return 0;  
}  



UVA 1108 - Mining Your Own Business

标签:uva

原文地址:http://blog.csdn.net/u013382399/article/details/44259679

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