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= ...
随机推荐
- [Luogu4921]情侣?给我烧了![错位排列]
题意 题意很清楚 \滑稽 分析 对于每一个询问 \(k\) ,记 \(g(x)\) 表示 \(x\) 对情侣都错开的方案总数,那么答案可以写成如下形式: \[ {ans}_k= \binom{n}{k ...
- SQL Server Management Studio 评估期已过
SQL2008破解: (1)将SQL安装光盘(或者ISO)放进去运行,进入安装界面. (2)选择“维护”中的“版本升级”,如图: (3)按照版本升级的向导,先输入产品密钥,也就是正式企业版的序列号: ...
- 微信小程序——手把手教你写一个微信小程序
前言 微信小程序年前的跳一跳确实是火了一把,然后呢一直没有时间去实践项目,一直想搞但是工作上不需要所以,嗯嗯嗯嗯嗯emmmmm..... 需求 小程序语音识别,全景图片观看,登录授权,获取个人基本信息 ...
- 英文样式教师求职简历免费word模板
10款英文样式教师求职简历免费word模板,也可用于其他专业和职业,个人免费简历模板,个人简历表免费,个人简历表格. 声明:该简历模板仅用于个人欣赏使用,请勿用于商业用途,谢谢. 下载地址:百度网盘, ...
- 一个简单的获取RGB值方式
操作系统内置了许多小工具,有时候这些小工具也挺有用的,省去了安装一些复杂的软件, 截图 通过键盘PrtSc获取到要取色的图片,然后用画图工具打开 查看 通过画图工具的取色工具,取到你需要的颜色,然后点 ...
- win10家庭版没有组策略怎么办?(win10管理员已阻止你运行此应用”解决方法)
把下面代码复制到TXT文本中,把文本再改成 .cmd 格式保存后以管理员身份运行 @echo off pushd "%~dp0" dir /b C:\Windows\serv ...
- allure2 report+ jenkins 使用
物色了一个挺漂亮的报告生成插件 ——allure. 下面介绍一下这个报告的使用. 1. 添加依赖 <dependencies> <!-- https://mvnrepository. ...
- git解决代码提交冲突
树冲突文件名修改造成的冲突,称为树冲突.比如,A同事把文件改名为A.C,B同事把同一个文件改名为B.C,那么B同事将这两个commit合并时,会产生冲突.如果最终确定用B同事的文件名,那么解决办法如下 ...
- 【坚持】Selenium+Python学习记录 DAY9
2018/05/29 [来源:菜鸟教程](http://www.runoob.com/python3/python3-examples.html) 运算符重载 https://segmentfault ...
- GitHub笔记(二)——远程仓库的操作
二 远程仓库 1 创建联系 第1步:创建SSH Key.在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一 ...