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= ...
随机推荐
- Security1:登录和用户
授予权限的思路,可以用一句话来概括,那就是:授予 Principal 操作 Securable 的 Permission,在多数文档中,把 Principal 翻译为安全主体,Securable翻译为 ...
- kali 2018.1安装教程
00x01 摘要 Kali-linux系统,渗透测试人员的利器,其官网自称 OurMost Advanced Penetration Testing Distribution, Ever. 永远是最 ...
- allure2 report+ jenkins 使用
物色了一个挺漂亮的报告生成插件 ——allure. 下面介绍一下这个报告的使用. 1. 添加依赖 <dependencies> <!-- https://mvnrepository. ...
- 接口测试中抓包工具Charles的使用
在被测接口并没有明确的接口文档给出时,我们需要借助抓包工具来帮助测试,利用抓包工具我们几乎可以获得接口文档中能给你的一切.常见的抓包工具有Charles和Fiddler, Fiddler只能用在Win ...
- c语言数字图像处理(六):二维离散傅里叶变换
基础知识 复数表示 C = R + jI 极坐标:C = |C|(cosθ + jsinθ) 欧拉公式:C = |C|ejθ 有关更多的时域与复频域的知识可以学习复变函数与积分变换,本篇文章只给出DF ...
- python3 拼接字符串的7种方法
1.直接通过(+)操作符拼接 1 2 >>> 'Hello' + ' ' + 'World' + '!' 'Hello World!' 使用这种方式进行字符串连接的操作效率低下,因为 ...
- Hyperledger Fabric 中channel配置相关数据结构
channel Configuration Transaction Hyperledger Fabric区块链网络中的配置存储在一个configuration-transaction的集合中,每个ch ...
- 第十次ScrumMeeting博客
第十次ScrumMeeting博客 本次会议于11月5日(日)22时整在新主楼G座2楼召开,持续20分钟. 与会人员:刘畅.辛德泰.窦鑫泽.张安澜.赵奕.方科栋. 特邀嘉宾:陈彦吉学长. 1. 每个人 ...
- python爬虫项目(新手教程)之知乎(requests方式)
-前言 之前一直用scrapy与urllib姿势爬取数据,最近使用requests感觉还不错,这次希望通过对知乎数据的爬取为 各位爬虫爱好者和初学者更好的了解爬虫制作的准备过程以及requests请求 ...
- Python数据分析工具库-Numpy 数组支持库(二)
1 shape变化及转置 >>> a = np.floor(10*np.random.random((3,4))) >>> a array([[ 2., 8., 0 ...