求出 bcc 后再……根据大白书上的思路即可。

  然后我用的是自定义的 stack 类模板:

 #include<cstdio>
#include<cstring>
#include<vector>
//#include<stack>
#include<stdexcept>
#include<algorithm>
using namespace std;
typedef long long LL;
const int N = ; template <typename T, size_t SIZE = N>
class stack {
T *val;
size_t tot;
size_t limit;
public:
stack(size_t limit = SIZE): limit(limit) {
val = (T*)malloc(sizeof(T) * limit);
tot = ;
}
stack(const stack<T> &s2): limit(s2.limit), tot(s2.tot) {
val = (T*)malloc(sizeof(T) * limit);
for(size_t i = ; i < tot; ++i)
val[i] = s2.val[i];
}
stack& operator = (const stack<T> &s2) {
tot = s2.tot;
limit = s2.limit;
T *temp = (T*)malloc(sizeof(T) * s2.limit);
for(size_t i = ; i < s2.tot; ++i)
temp[i] = s2.val[i];
free(val);
this->val = temp;
}
void push(const T &x) {
if(tot == limit) {
stack<T> temp(*this);
free(val);
val = (T*)malloc(sizeof(T) * (limit <<= ));
for(size_t i = ; i < temp.tot; ++i)
val[i] = temp.val[i];
}
val[tot++] = x;
}
void pop() {
if(tot == ) throw out_of_range("Stack less flow at Stack<T>::pop()");
--tot;
}
T top() const {
if(tot == ) throw out_of_range("Stack less flow at Stack<T>::top()");
return val[tot - ];
}
size_t size() const { return tot; }
bool empty() const { return tot == ; }
void clear() { tot = ; }
~stack() { free(val); }
}; struct Edge {
int u,v;
Edge(int u, int v): u(u), v(v) {}
}; vector<int> g[N], bcc[N];
int pre[N], iscut[N], bccno[N], dfs_clock, bcc_cnt;
stack<Edge, N> s; int dfs(int u, int fa) {
int lowu = pre[u] = ++dfs_clock;
int child = ;
for(int i = ; 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(v,u);
lowu = min(lowu, lowv);
if(lowv >= pre[u]) {
iscut[u] = ;
++bcc_cnt;
bcc[bcc_cnt].clear();
while() {
Edge x = s.top(); s.pop();
if(bccno[x.u] != bcc_cnt) {
bccno[x.u] = bcc_cnt;
bcc[bcc_cnt].push_back(x.u);
}
if(bccno[x.v] != bcc_cnt) {
bccno[x.v] = bcc_cnt;
bcc[bcc_cnt].push_back(x.v);
}
if(x.u == u && x.v == v) break;
}
}
}
else if(pre[v] < pre[u] && v != fa) {
lowu = min(lowu, pre[v]);
s.push(e);
}
}
if(fa < && child == ) iscut[u] = ;
return lowu;
} void find_bcc(int n) {
memset(pre, , sizeof pre);
memset(iscut, , sizeof iscut);
memset(bccno, , sizeof bccno);
dfs_clock = bcc_cnt = ;
for(int i = ; i < n; ++i)
if(!pre[i]) dfs(i, -);
} int main() {
int n,x,y,Case = , maxn = ;
while(~scanf("%d",&n),n) {
for(int i = ; i <= maxn; ++i)
g[i].clear();
maxn = ;
for(int i = ; i < n; ++i) {
scanf("%d %d",&x,&y);
maxn = max(maxn, x);
maxn = max(maxn, y);
g[x].push_back(y);
g[y].push_back(x);
}
find_bcc(maxn + );
LL ans1 = , ans2 = ;
for(int i = ; i <= bcc_cnt; ++i) {
int cut = ;
for(int j = ; j < bcc[i].size(); ++j)
if(iscut[bcc[i][j]]) ++cut;
if(cut == ) {
++ans1;
ans2 *= bcc[i].size() - cut;
}
}
if(bcc_cnt == ) {
ans1 = ;
ans2 = (LL)bcc[].size() * (bcc[].size() - ) / ;
}
printf("Case %d: %lld %lld\n",++Case, ans1, ans2);
}
return ;
}

LA 5135 Mining Your Own Business的更多相关文章

  1. UVALive - 5135 - Mining Your Own Business(双连通分量+思维)

    Problem   UVALive - 5135 - Mining Your Own Business Time Limit: 5000 mSec Problem Description John D ...

  2. 【LA】5135 Mining Your Own Business

    [算法]点双连通分量 [题解]详见<算法竞赛入门竞赛入门经典训练指南>P318-319 细节在代码中用important标注. #include<cstdio> #includ ...

  3. UVALive - 5135 Mining Your Own Business

    刘汝佳白书上面的一道题目:题意是给定一个联通分量,求出割顶以及双连通分量的个数,并且要求出安放安全井的种类数,也就是每个双连通分量中结点数(除开 割顶)个数相乘,对于有2个及以上割顶的双连通分量可以不 ...

  4. UVALive 5135 Mining Your Own Business 双连通分量 2011final

    题意:n条隧道由一些点连接而成,其中每条隧道链接两个连接点.任意两个连接点之间最多只有一条隧道.任务就是在这些连接点中,安装尽量少的太平井和逃生装置,使得不管哪个连接点倒塌,工人都能从其他太平井逃脱, ...

  5. UVALive 5135 Mining Your Own Business 双连通分量

    据说这是一道Word Final的题,Orz... 原题链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&a ...

  6. HDU3844 Mining Your Own Business

    HDU3844 Mining Your Own Business 问题描述John Digger是一个大型illudium phosdex矿的所有者.该矿山由一系列隧道组成,这些隧道在各个大型交叉口相 ...

  7. 「题解报告」SP16185 Mining your own business

    题解 SP16185 Mining your own business 原题传送门 题意 给你一个无向图,求至少安装多少个太平井,才能使不管那个点封闭,其他点都可以与有太平井的点联通. 题解 其他题解 ...

  8. Mining Your Own Business UVALive - 5135(点双联通分量)

    these days I‘m tired!,but very happy... #include<cstdio> #include<cstring> #include<s ...

  9. UVA5135 Mining Your Own Business ( 无向图双连通分量)

    题目链接 题意:n条隧道由一些点连接而成,其中每条隧道链接两个连接点.任意两个连接点之间最多只有一条隧道.任务就是在这些连接点中,安装尽量少的太平井和逃生装置,使得不管哪个连接点倒塌,工人都能从其他太 ...

随机推荐

  1. python day6 面向对象

    把一个类变成具体的实例化的过程叫做类 实例化:把一个类变成一个具体对象的过程 __init__  构造函数,在实例化时初始化数据 __init__方法的第一个参数永远是self,表示创建的实例本身,因 ...

  2. php时间函数整理

    PHP中的时间函数有这么些:(1)date用法: date(格式,[时间]);如果没有时间参数,则使用当前时间. 格式是一个字符串,其中以下字符有特殊意义:U 替换成从一个起始时间(好象是1970年1 ...

  3. MFC编程基础

    http://www.cnblogs.com/lzmfywz/archive/2012/03/15/2399403.html 一.MFC类库概述 MFC(Microsoft Foundation cl ...

  4. Spring声明式事务管理与配置详解

    转载:http://www.cnblogs.com/hellojava/archive/2012/11/21/2780694.html 1.Spring声明式事务配置的五种方式 前段时间对Spring ...

  5. lua注释

    1. 单行注释 --  功能等同于C++中的// 2. 多行注释  --[[  注释的内容  ]]   功能等同于C++中的 /**/ 3. 多行注释   --[====[   注释和内容  ]=== ...

  6. 加载页面(Loading)

    /* 文件说明:页面加载时Loading JS 文件描述:解决IE或FF下,初始化加载时,页面布局乱掉的问题,参考:*/var width = $(window).width();var height ...

  7. python学习笔记五 模块上(基础篇)

    模块学习 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其他代码用来调用即可,提供了代码的重用性和代码间的耦合.而对于一个复杂的功能来,可能需要 ...

  8. Win7\xp添加虚拟网Microsoft Loopback Adapter

    安装Microsoft Loopback Adapter 1 依次找到 打开“开始菜单”---“控制面板”---“添加硬件”的菜单选项如下图所示. 步骤阅读 2 如果没有“添加硬件”当然你也可以直接, ...

  9. 【leetcode❤python】Binary Watch

    #-*- coding: UTF-8 -*- from itertools import combinationsclass Solution(object):        hourList=[8, ...

  10. HTML Meta标签中的viewport属性含义及设置

    两篇文章 第1篇文章 第2篇文章 http://blog.hexu.org/archives/1947.shtml 随着高端手机(Andriod,Iphone,Ipod,WinPhone等)的盛行,移 ...