求出 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. Android 5.1 - 状态栏充电标志问题

    Android 5.1 Ubuntu14.04  SourceInsigh 电量已满,插着USB头,观察Settings - Battery,电量为100%,状态为full,但仍有充电图标rust 之 ...

  2. 转载-python学习笔记之文件I/O

    Python 文件I/O 本章只讲述所有基本的的I/O函数,更多函数请参考Python标准文档. 打印到屏幕 最简单的输出方法是用print语句,你可以给它传递零个或多个用逗号隔开的表达式.此函数把你 ...

  3. php cli模式学习(PHP命令行模式)

    http://www.jb51.net/article/37796.htm php_cli模式简介  php-cli是php Command Line Interface的简称,如同它名字的意思,就是 ...

  4. nyoj CO-PRIME 莫比乌斯反演

    CO-PRIME 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 This problem is so easy! Can you solve it? You are ...

  5. promise理解

    每个操作都返回一样的promise对象,保证链式操作 每个链式都通过then方法 每个操作内部允许犯错,出了错误,统一由catch error处理 操作内部,也可以是一个操作链,通过reject或re ...

  6. cetos6.5安装Tomcat

    一. 下载Tomcat 官网下载Tomcat  tar.gz文件 二. 解压tar.gz文件 tar -zxvf tomcat.tar.gz 三. 在catalina.sh最上面添加一下内容 expo ...

  7. java.util.zip对zip文件解压

    //通过构造方法,来创建一个新的ZIP输入流 ZipInputStream in = new ZipInputStream(new FileInputStream("G:/jquery.ca ...

  8. syslog及syslog-ng详解 日志服务器

    服务器的日志对系统工程师来说是至关重要的,一旦服务器出现故障或被入侵,我们需要查看日志来定位问题的关键所在,所以说对于线上跑的服务器而言日志应该合理的处理及管理.下面来   服务器的日志对系统工程师来 ...

  9. MVC系列之二 Model层细解

    一.简介 在上一篇将MVC的时候,有很有朋友对简单三层的概念不是很熟悉,因此,今天进行简单三层的一个简单介绍,同时为理解MVC中的Model做知识累计. 传统的三层主要指的是UI层,BLL层,DAL层 ...

  10. 1-NPM

    什么是NPM NPM是随同NodeJS一起安装的包管理工具,能解决NodeJS代码部署上的很多问题. 常见的使用场景有以下几种: 允许用户从NPM服务器下载别人编写的第三方包到本地使用. 允许用户从N ...