LA 5135 Mining Your Own Business
求出 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的更多相关文章
- UVALive - 5135 - Mining Your Own Business(双连通分量+思维)
Problem UVALive - 5135 - Mining Your Own Business Time Limit: 5000 mSec Problem Description John D ...
- 【LA】5135 Mining Your Own Business
[算法]点双连通分量 [题解]详见<算法竞赛入门竞赛入门经典训练指南>P318-319 细节在代码中用important标注. #include<cstdio> #includ ...
- UVALive - 5135 Mining Your Own Business
刘汝佳白书上面的一道题目:题意是给定一个联通分量,求出割顶以及双连通分量的个数,并且要求出安放安全井的种类数,也就是每个双连通分量中结点数(除开 割顶)个数相乘,对于有2个及以上割顶的双连通分量可以不 ...
- UVALive 5135 Mining Your Own Business 双连通分量 2011final
题意:n条隧道由一些点连接而成,其中每条隧道链接两个连接点.任意两个连接点之间最多只有一条隧道.任务就是在这些连接点中,安装尽量少的太平井和逃生装置,使得不管哪个连接点倒塌,工人都能从其他太平井逃脱, ...
- UVALive 5135 Mining Your Own Business 双连通分量
据说这是一道Word Final的题,Orz... 原题链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&a ...
- HDU3844 Mining Your Own Business
HDU3844 Mining Your Own Business 问题描述John Digger是一个大型illudium phosdex矿的所有者.该矿山由一系列隧道组成,这些隧道在各个大型交叉口相 ...
- 「题解报告」SP16185 Mining your own business
题解 SP16185 Mining your own business 原题传送门 题意 给你一个无向图,求至少安装多少个太平井,才能使不管那个点封闭,其他点都可以与有太平井的点联通. 题解 其他题解 ...
- Mining Your Own Business UVALive - 5135(点双联通分量)
these days I‘m tired!,but very happy... #include<cstdio> #include<cstring> #include<s ...
- UVA5135 Mining Your Own Business ( 无向图双连通分量)
题目链接 题意:n条隧道由一些点连接而成,其中每条隧道链接两个连接点.任意两个连接点之间最多只有一条隧道.任务就是在这些连接点中,安装尽量少的太平井和逃生装置,使得不管哪个连接点倒塌,工人都能从其他太 ...
随机推荐
- quick lua 3.3常用方法和学习技巧之functions.lua目录
1.functions.lua (framework->functions.lua) 提供一组常用函数,以及对 Lua 标准库的扩展 1.printf 2.checknumber checkin ...
- SlickGrid example 8:折线图
根据数据生成折线图,使用相当简单,也很容易. 主要方法: 数据: var vals = [12,32,5,67,5,43,76,32,5]; 生成折线图: $("testid&quo ...
- c#之双色球
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- IoC容器概述
IoC(Inverse of Control: 控制反转)是spring容器的内核, 字面意思是: 控制反转, 包含两个内容:(1) 控制, (2) 反转.那到底是什么东西的控制被反转了呢? 对于软件 ...
- ubuntu下安装花生壳
下载地址:http://hsk.oray.com/download/#type=linux 官方的文档: 花生壳(公网版) for linux的安装以及使用 Linux花生壳(公网版)将大大简化大家的 ...
- 在Visual Studio中使用MonoTouch开发iOS应用程序
前段时间在工作机上装了Mac OS X,这主要是因为我最近需要开发iPhone应用程序.虽然Xcode,Objective C一定是开发iOS应用程序的主流,但是经过一番考虑,我还是决定尝试一下使用M ...
- MSComm函数说明(来自网络)
CommPort 设置并返回端口号 void CMSComm::SetCommPort(short nNewValue) short CMSComm::GetCommPort() RThreshold ...
- Shell 脚本面试问题大全
我们为你的面试准备选择了 70 个你可能遇到的 shell 脚本面试问题及解答.了解脚本或至少知道基础知识对系统管理员来说至关重要,它也有助于你在工作环境中自动完成很多任务.在过去的几年里,我们注意到 ...
- Mybatis+SpringMVC+Spring整合
1,先添加spring支持: applicationContext.xml 配在WEBINF下,四个命名空间:aop,context,tx,p 配Listener:ContextLoaderList ...
- [SAP ABAP开发技术总结]OLE
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...