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= ...
随机推荐
- layer.msg(msg, time, parme)设置图标问题
layer.msg只提到3个参数,实际上有第4个参数,第4个参数就是msg关闭后执行的回调. layer.msg('提示', 2, 1, function(){}) 第一个参数:提示 第二个参 ...
- 从轻测到上线,WeTest与《一起来捉妖》测试方案大公开
从2016年Pokémon GO引发的AR游戏热潮开始,国内就一直在期待新的一款具备代表性的AR游戏的头部作品. 4月11日的腾讯首款AR探索手游<一起来捉妖>不仅为国内市场注入了新的活力 ...
- 3.PO如何给开发团队讲好故事
敏捷开发系列文章目录 讲出符合开发团队味口的故事. 上一章说了敏捷开发团队的构成与迭代过程,本章重点说一下迭代第一天的计划会议.熟话说“好的开始就成功了一半”,一个迭代的计划会议做得好不好确实直接注定 ...
- .Net Core Linux centos7行—jenkins linux 构建.net core web app
1.安装jdk.jenkins 是一个java web程序.所以必然需要jdk. yum install java 或者 yum install java-1.8.0-openjdk 2.下载jenk ...
- python中面向对象_类_对象的概念与定义
1. 面向对象的概念,面向对象是一种编程思想. 是对现实世界中一类事物的抽象,在编程中可以理解为是一种建立现实世界事物的模型 2. 面向对象和面向过程的区别: 面向过程关注的是完成工作的步骤. 面向 ...
- ruby安装卸载
1.用命令yum install ruby安装,是2.0以下的版本.不建议使用 2.2.2以上 下载地址:https://www.ruby-lang.org/en/news/2018/03/28/r ...
- 用人工智能学习,凡亿推出PCB问题解答智能搜索机器人:pcb助手
对于学习者,你是不是经常遇到这样的问题:在我们狠狠下定决心学习PCB技术的时候,我们常常遇到很多大大小小的问题,遗憾的是身边没有一个能及时给自己解答问题的高手指点,通过论坛.群等方式询问可能半天也得不 ...
- 通过扩展方法简化UnityAPI调用
通过扩展方法简化UnityAPI调用 扩展方法unity apiapi简化 通过扩展方法简化UnityAPI调用 能省一秒是一秒,时间就是金钱,没人愿意把时间花在冗长的coding上
- jmeter阶梯加压线程组
添加阶梯加压线程组路径为鼠标捕获测试计划后,点击鼠标右键->添加->Threads(Users)->jp@gc – Stepping Thread Group(deprecated) ...
- 买卖股票的最佳时机 II
int maxProfit(int* prices, int pricesSize) { ; ; i < pricesSize - ; i++) { ]) { continue; } else ...