【LA】5135 Mining Your Own Business
【算法】点双连通分量
【题解】详见《算法竞赛入门竞赛入门经典训练指南》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的更多相关文章
- UVALive - 5135 - Mining Your Own Business(双连通分量+思维)
Problem UVALive - 5135 - Mining Your Own Business Time Limit: 5000 mSec Problem Description John D ...
- 【BIEE】01_下载安装BIEE(Business Intelligence)11g 11.1.1.9.0
环境准备 安装文件 如果操作系统是64位,则下载64位版本,我安装的系统是64位的 1.下载所有安装文件 1.1 Oracle Database 11g R2 下载地址: http://www.ora ...
- LA 5135 Mining Your Own Business
求出 bcc 后再……根据大白书上的思路即可. 然后我用的是自定义的 stack 类模板: #include<cstdio> #include<cstring> #includ ...
- 【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 ...
- 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 ...
- 【贪心】【POJ3154】墓地雕塑(Graveyard, NEERC 2006, LA 3708)需要稍稍加工的(先贪心,再确保能这样贪(可行性&&如果可行必定最优&&非证明最优性)的题)(K)
例题4 墓地雕塑(Graveyard, NEERC 2006, LA 3708) 在一个周长为10000的圆上等距分布着n个雕塑.现在又有m个新雕塑加入(位置可以随意放),希望所有n+m个雕塑在圆周 ...
- 【33.20%】【LA 4320】【Ping pong】
[Description] N (3 ≤ N ≤ 20000) ping pong players live along a west-east street(consider the street ...
随机推荐
- servlet转发和重定向
1 转发 RequestDispatcher getRequestDispatcher(java.lang.String path) 1)地址栏不会改变 2)转发只能转发到当前web应用内的资源 3) ...
- lintcode-149-买卖股票的最佳时机
149-买卖股票的最佳时机 假设有一个数组,它的第i个元素是一支给定的股票在第i天的价格.如果你最多只允许完成一次交易(例如,一次买卖股票),设计一个算法来找出最大利润. 样例 给出一个数组样例 [3 ...
- week1 技术随笔
类别c 内容c 开始时间s 结束时间e 被打断时间I 总计(min) 9.5 随笔 构建之法福后感 22:00 24:00 7 113 9.6 分析 需求分析 9:00 9:30 2 28 编码 词频 ...
- nginx的平滑升级,不间断服务
nginx的平滑升级,不间断服务 Nginx更新真的很快,最近nginx的1.0.5稳定版,nginx的0.8.55和nginx的0.7.69旧的稳定版本已经发布.我一项比较喜欢使用新版本的软件, ...
- jquery animate() stop() finish() 方法使用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- BZOJ4011 HNOI2015落忆枫音(动态规划+拓扑排序)
DAG中每个点选一条入边就可以构成一棵有向树,所以如果没有环答案就是∏degreei. 考虑去掉含环的答案.可以看做把环缩点,剩下的点仍然可以任意选入边.于是去除的方案数即为∏degreei/∏deg ...
- [luogu2783] 有机化学之神偶尔会做作弊
题目链接 洛谷. Solution 边双缩点然后\(lca\)跑\(dis\)就好了. 注意这里是边双,不知道为啥所有题解都说的是点双. 边双是定义在点上的,即每个点只属于一个边双:点双是定义在边上的 ...
- Android <Android应用开发实战> 学习总结杂项
1.系统相册默认保存地址:android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Ca ...
- BZOJ5338:[TJOI2018]异或——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=5338 现在有一颗以1为根节点的由n个节点组成的树,树上每个节点上都有一个权值vi. 现在有Q 次操 ...
- BZOJ3669:[NOI2014]魔法森林——题解
http://www.lydsy.com/JudgeOnline/problem.php?id=3669 https://www.luogu.org/problemnew/show/P2387 为了得 ...