【算法】点双连通分量

【题解】详见《算法竞赛入门竞赛入门经典训练指南》P318-319

细节在代码中用important标注。

#include<cstdio>
#include<algorithm>
#include<vector>
#include<stack>
#include<cstring>
using namespace std;
const int maxm=;
struct edge{int u,v,from;}e[maxm*];
int first[maxm],iscut[maxm],dfn[maxm],low[maxm],bccno[maxm],bcc_cnt,dfsnum,tot,n,m,kase;
vector<int>bcc[maxm];
stack<int>s;
void insert(int u,int v)
{tot++;e[tot].u=u;e[tot].v=v;e[tot].from=first[u];first[u]=tot;}
void tarjan(int x,int fa)
{
dfn[x]=low[x]=++dfsnum;
int child=;
for(int i=first[x];i;i=e[i].from)
if(e[i].v!=fa)
{
int y=e[i].v;
// s.push(i);
if(!dfn[y])
{
s.push(i);//
child++; //important
tarjan(y,x);
low[x]=min(low[x],low[y]);
if(low[y]>=dfn[x])
{
iscut[x]=;
bcc_cnt++;
bcc[bcc_cnt].clear();
for(;;)
{
int now=s.top();s.pop();
int u=e[now].u,v=e[now].v;
if(bccno[u]!=bcc_cnt){bcc[bcc_cnt].push_back(u);bccno[u]=bcc_cnt;}
if(bccno[v]!=bcc_cnt){bcc[bcc_cnt].push_back(v);bccno[v]=bcc_cnt;}
// printf("%d\n",bcc_cnt);
if(u==x&&v==y)break;
}
}
}
else if(dfn[y]<dfn[x])s.push(i),low[x]=min(low[x],dfn[y]);//important
}
if(fa<&&child==)iscut[x]=;
}
int main()
{
scanf("%d",&m);
while(m!=)
{
memset(first,,sizeof(first));
n=;tot=;bcc_cnt=;
for(int i=;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
n=max(n,max(u,v));
insert(u,v);
insert(v,u);
}
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(iscut,,sizeof(iscut));
memset(bccno,,sizeof(bccno));
dfsnum=;
while(!s.empty())s.pop();
for(int i=;i<=n;i++)if(!dfn[i])tarjan(i,-);
long long ans1=,ans2=;
for(int i=;i<=bcc_cnt;i++)
{
int cut_cnt=;
for(int j=;j<bcc[i].size();j++)
if(iscut[bcc[i][j]])cut_cnt++;
if(cut_cnt==)
{
ans1++;
ans2*=(long long)(bcc[i].size()-cut_cnt);
}
}
if(bcc_cnt==)
{
ans1=;
ans2=bcc[].size()*(bcc[].size()-)/;
}
printf("Case %d: %lld %lld\n",++kase,ans1,ans2);
scanf("%d",&m);
}
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. 【BIEE】01_下载安装BIEE(Business Intelligence)11g 11.1.1.9.0

    环境准备 安装文件 如果操作系统是64位,则下载64位版本,我安装的系统是64位的 1.下载所有安装文件 1.1 Oracle Database 11g R2 下载地址: http://www.ora ...

  3. LA 5135 Mining Your Own Business

    求出 bcc 后再……根据大白书上的思路即可. 然后我用的是自定义的 stack 类模板: #include<cstdio> #include<cstring> #includ ...

  4. 【337】Text Mining Using Twitter Streaming API and Python

    Reference: An Introduction to Text Mining using Twitter Streaming API and Python Reference: How to R ...

  5. UVALive - 5135 Mining Your Own Business

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

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

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

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

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

  8. 【贪心】【POJ3154】墓地雕塑(Graveyard, NEERC 2006, LA 3708)需要稍稍加工的(先贪心,再确保能这样贪(可行性&&如果可行必定最优&&非证明最优性)的题)(K)

    例题4  墓地雕塑(Graveyard, NEERC 2006, LA 3708) 在一个周长为10000的圆上等距分布着n个雕塑.现在又有m个新雕塑加入(位置可以随意放),希望所有n+m个雕塑在圆周 ...

  9. 【33.20%】【LA 4320】【Ping pong】

    [Description] N (3 ≤ N ≤ 20000) ping pong players live along a west-east street(consider the street ...

随机推荐

  1. # ML学习小笔记—Classification

    关于本课程的相关资料http://speech.ee.ntu.edu.tw/~tlkagk/courses_ML17.html 通过模型可以分类输入,此时根据分类结果的正确与否会有一个Loss函数.找 ...

  2. 3dContactPointAnnotationTool开发日志(十四)

      貌似每次让用户手动输入文件路径太不人道了,于是参考Unity 实用教程 之 调用系统窗口选择文件或路径增加了让用户浏览文件的功能,点击输入框旁边的+就可以找到文件并加载进来:   貌似调整位置再计 ...

  3. 父类与子类的转换as,is

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  4. BZOJ 1066 蜥蜴(网络流)

    很普通的拆点网络流,把每个柱子拆成两个点(i,j,0)和(i,j,1).对于柱子的高度限制则加边((i,j,0),(i,j,1),height). 两个柱子能互相到达则加边((i,j,1),(i1,j ...

  5. Python 源码剖析(一)【python对象】

    处于研究python内存释放问题,在阅读部分python源码,顺便记录下所得.(基于<python源码剖析>(v2.4.1)与 python源码(v2.7.6)) 先列下总结:      ...

  6. C++中Map的使用 (个人简单的对于String的使用)

    #include<map> #include<iostream> #include<string> using namespace std; int main() ...

  7. 【刷题】BZOJ 3510 首都

    Description 在X星球上有N个国家,每个国家占据着X星球的一座城市.由于国家之间是敌对关系,所以不同国家的两个城市是不会有公路相连的. X星球上战乱频发,如果A国打败了B国,那么B国将永远从 ...

  8. [洛谷P4781]【模板】拉格朗日插值

    题目大意:给你$n(n\leqslant2000)$个点,要你求$n-1$次经过这$n$个点的多项式在$k$处的值 题解:$Lagrange$插值:$$f_x=\sum\limits_{i=1}^ky ...

  9. 【国家集训队】聪聪可可 ——树形DP

    感觉是一道很妙的树形DP题,充分利用到了树的性质(虽然说点分治也可以做,,,,但是本蒟蒻不会啊) 然而某Twilight_Sx大佬表示这道题真的非常水,,,本蒟蒻也只能瑟瑟发抖了 本蒟蒻表示还是要经过 ...

  10. Android 数据库升级中数据保持和导入已有数据库

    一.数据库升级: 在我们的程序中,或多或少都会涉及到数据库,使用数据库必定会涉及到数据库的升级,数据库升级带来的一些问题,如旧版本数据库的数据记录的保持,对新表的字段的添加等等一系列问题,还记得当我来 ...