UVAlive5135_Mining Your Own Business
好题。给一个无向图,求最少染黑多少个点后,使得任意删除一个点,每一个点都有与至少一个黑点联通。
一开始的确不知道做。看白书,对于一个联通分量,如果它有两个或以上的割点,那么这个分量中间的任何一个点都是不需要染色的。如果这个联通分量恰好有一个割点,那么这个分量需要对其中任何一个非割点染色,如果分量没有割点,那么任意取两个染色即可。
理解也不难,因为最多只是删除一个点,所以假如删除的不是割点,分量里面是绝对联通的,同时还可以通过割点对外面进行联通,如果删除的是割点,那么外面的与里面的不联通,那么这时就需要里面至少有一个黑点了。如果有两个割点,显然,无论你去掉哪一个点,这个分量都是与外面联通的。
有了这个思路题目就简单了。
召唤代码君:
解法一:找出所有的联通分量,判断每一个连通分量的割点数,更新答案。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#define maxn 201000
typedef long long ll;
using namespace std; int first[maxn],next[maxn],to[maxn],edge;//graph
vector<int> bcc[maxn];
int N,bccnum;//the bcc data
int iscut[maxn],belong[maxn],d[maxn],low[maxn],child;
int U[maxn],V[maxn],stack[maxn],top;//stack
int n,m,cas=,T;
ll ans,tot; void _init()
{
ans=,tot=,edge=-,child=bccnum=,top=;
for (int i=; i<=n; i++) first[i]=-,d[i]=low[i]=iscut[i]=belong[i]=;
} void addedge(int uu,int vv)
{
edge++;
to[edge]=vv,next[edge]=first[uu],first[uu]=edge;
edge++;
to[edge]=uu,next[edge]=first[vv],first[vv]=edge;
} void dfs(int cur,int fa)
{
d[cur]=low[cur]=d[fa]+;
for (int i=first[cur]; i!=-; i=next[i])
{
if (to[i]==fa) continue;
if (!d[to[i]])
{
if (fa==) child++;
top++; U[top]=cur,V[top]=to[i];
dfs(to[i],cur);
low[cur]=min(low[cur],low[to[i]]);
if (low[to[i]]>=d[cur])
{
iscut[cur]=;
bccnum++,bcc[bccnum].clear();
for (;;top--)
{
if (belong[U[top]]!=bccnum) belong[U[top]]=bccnum,bcc[bccnum].push_back(U[top]);
if (belong[V[top]]!=bccnum) belong[V[top]]=bccnum,bcc[bccnum].push_back(V[top]);
if (U[top]==cur && V[top]==to[i])
{
top--;
break;
}
}
}
}
else low[cur]=min(low[cur],d[to[i]]);
}
if (fa== && child==) iscut[cur]=;
} int main()
{
while (scanf("%d",&m) && (m))
{
n=-;
for (int i=; i<=m; i++)
{
scanf("%d%d",&U[i],&V[i]);
n=max(n,max(U[i],V[i]));
}
_init();
for (int i=; i<=m; i++) addedge(U[i],V[i]);
for (int i=; i<=n; i++)
if (!d[i])
{
if (first[i]==-)
{
tot++;
continue;
}
child=;
dfs(i,);
}
for (int i=; i<=bccnum; i++)
{
int cutnum=;
for (unsigned j=; j<bcc[i].size(); j++)
if (iscut[bcc[i][j]]) cutnum++;
if (cutnum==)
{
tot++;
ans*=bcc[i].size()-;
}
else if (cutnum==)
{
tot+=;
ll tmp=bcc[i].size();
ans*=tmp*(tmp-)/;
}
}
printf("Case %d: %lld %lld\n",++cas,tot,ans);
}
return ;
解法二:标记所有割点,每次从非割点出发,看能走到多少非割点,而且总共与多少割点相邻,(其实也就是遍历了一遍联通分量,不过实现简单一些)。
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 200200
typedef long long ll;
using namespace std; int first[maxn],to[maxn],next[maxn],edge;
int U[maxn],V[maxn];
int d[maxn],low[maxn],tag[maxn];
bool iscut[maxn],vis[maxn];
int n,m,tot,child,sum,cut;
ll ans; void _init()
{
tot=,ans=,edge=-;
for (int i=; i<=n; i++)
first[i]=-,iscut[i]=vis[i]=false,tag[i]=low[i]=d[i]=;
} void addedge(int uu,int vv)
{
edge++;
to[edge]=vv,next[edge]=first[uu],first[uu]=edge;
edge++;
to[edge]=uu,next[edge]=first[vv],first[vv]=edge;
} void dfs(int cur,int fa)
{
d[cur]=low[cur]=d[fa]+;
for (int i=first[cur]; i!=-; i=next[i])
{
if (to[i]==fa) continue;
if (!d[to[i]])
{
if (fa==) child++;
dfs(to[i],cur);
low[cur]=min(low[cur],low[to[i]]);
if (low[to[i]]>=d[cur]) iscut[cur]=true;
}
else low[cur]=min(low[cur],d[to[i]]);
}
if (fa== && child==) iscut[cur]=false;
} void visit(int cur,int TAG)
{
sum++,vis[cur]=true;
for (int i=first[cur]; i!=-; i=next[i])
{
if (iscut[to[i]] && tag[to[i]]!=TAG) cut++,tag[to[i]]=TAG;
else if (!iscut[to[i]] && !vis[to[i]]) visit(to[i],TAG);
}
} int main()
{
int cas=;
while (scanf("%d",&m) && (m))
{
n=;
for (int i=; i<=m; i++) scanf("%d%d",&U[i],&V[i]),n=max(n,max(U[i],V[i]));
_init();
for (int i=; i<=m; i++) addedge(U[i],V[i]); for (int i=; i<=n; i++)
if (!d[i])
{
child=;
dfs(i,);
}
for (int i=; i<=n; i++)
if (!vis[i] && !iscut[i])
{
cut=sum=;
visit(i,i);
if (cut==) tot+=,ans*=(ll)sum*(sum-)/;
else if (cut==) tot++,ans*=sum;
}
printf("Case %d: %d %lld\n",++cas,tot,ans);
}
return ;
}
UVAlive5135_Mining Your Own Business的更多相关文章
- 在 SharePoint Server 2016 本地环境中设置 OneDrive for Business
建议补丁 建议在sharepoint2016打上KB3127940补丁,补丁下载地址 https://support.microsoft.com/zh-cn/kb/3127940 当然不打,也可以用O ...
- Java Business Process Management(业务流程管理) 初识环境搭建
一.简介 (一)什么是jbpm JBPM,全称是Java Business Process Management(业务流程管理),它是覆盖了业务流程管理.工作流.服务协作等领域的一个开源的.灵活的.易 ...
- Tips for Planning Your Business Startup
原文链接:http://domaintree.me/?p=1037 By Robert Thibodeau – Starting a business can be a very daunting ...
- 10 Biggest Business Mistakes That Every Entrepreneur Should Avoid
原文链接:http://www.huffingtonpost.com/syed-balkhi/10-biggest-business-mista_b_7626978.html When I start ...
- 7 COMPELLING REASONS YOU NEED TO START THE BUSINESS YOU’VE ALWAYS WANTED
原文链接:http://lesseesadvocate.com/7-compelling-reasons-need-start-business-youve-always-wanted/ Don’t ...
- business knowledge
Finance knowledge Trading---At the core of our business model is Trading, which involves the buying ...
- Business Unit Lookup in Form
Just add the below code in lookup() of StringEdit control in Form to get the Business Unit Lookup: p ...
- Office 365 系列一 ------- 如何单个安装Office 客户端和Skype for business
当我们注册好或者购买好 Office 365后,我们的单个用户如何进行在线的.流式的方式安装好我们的客户端,特别是对于我们非IT部门来说,这是一个比较为难的事情, 经常需要我们的IT去到同事的电脑旁边 ...
- 更改 Skype for Business Online 的 Sip 地址以匹配UPN
var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...
随机推荐
- Unity在OpenGL模式下Shader编译报错
报错信息 GLSL compilation failed: 0(21) : error C7528: OpenGL reserves names containing '__' 双击报错VS自动打开V ...
- UNITY_资源路径与加载外部文件
UNITY_资源路径与加载外部文件 https://www.tuicool.com/articles/qMNnmm6https://blog.csdn.net/appppppen/article/de ...
- 电梯调度 结对项目开发(郭林林&胡潇丹)
(一)需求分析: 上升,下降,开门,关门: 超过负载以后发出警报,下去乘客: 电梯出现故障后,电梯停止: 电梯楼层的输入框可以同时指定所要到的楼层,也是楼层的显示框: 电梯同时记录多个状态,即为到达多 ...
- Netty源码分析第5章(ByteBuf)---->第10节: SocketChannel读取数据过程
Netty源码分析第五章: ByteBuf 第十节: SocketChannel读取数据过程 我们第三章分析过客户端接入的流程, 这一小节带大家剖析客户端发送数据, Server读取数据的流程: 首先 ...
- yocto-sumo源码解析(七): BitBakeServer
1. 创建域套接字,管道以及锁: self.configuration = configuration self.featureset = featureset self.sockname = soc ...
- CS224n学习笔记1——深度自然语言处理
一.什么是自然语言处理呢? 自然语言处理是计算机科学家提出的名字,本质上与计算机语言学是同义的,它跨越了计算机学.语言学以及人工智能学科. 自然语言处理是人工智能的一个分支,在计算机研究领域中,也有其 ...
- Annotation 使用备忘
title: Annotation 使用备忘 date: 2016-11-16 23:16:43 tags: [Annotation] categories: [Programming,Java] - ...
- Python数据分析工具库-Numpy 数组支持库(一)
1 Numpy数组 在Python中有类似数组功能的数据结构,比如list,但在数据量大时,list的运行速度便不尽如意,Numpy(Numerical Python)提供了真正的数组功能,以及对数据 ...
- Vue 实例详解与生命周期
Vue 实例详解与生命周期 Vue 的实例是 Vue 框架的入口,其实也就是前端的 ViewModel,它包含了页面中的业务逻辑处理.数据模型等,当然它也有自己的一系列的生命周期的事件钩子,辅助我们进 ...
- Scrum Meeting 11.1
成员 今日任务 明日计划 用时 徐越 学习利用servlet上传下载文件 代码迁移 4h 赵庶宏 数据库的连接及代码学习 数据库连接 2h 武鑫 设计界面;尝试写一些初步的代码,独立完成一些简单界面 ...