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= ...
随机推荐
- Python抓取歌词自制FreeStyle
故事的起因是上周六看<中国好声音>,一个周杰伦战队的学员用人工智能写的歌词,于是乎,我也有了这个想法,代码的主题思路是看Crossin先生的文章,虽然最后不能写出一首歌,但是押韵脚这事情分 ...
- node.js学习笔记——前序
一.什么是node.js 简单的说 Node.js 就是运行在服务端的 JavaScript. Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台. Node.js是一 ...
- Macaca上手体验
在研究了一段时间Appium后,尝试对另一个框架做实验——Macaca,阿里的开源测试框架,该框架不只适合移动端,同样适用于web端,可谓是方便的很啊~ 同时支持js.java.python.封装好的 ...
- 旧的 .NET Core 项目重新打包出现提示版本不对问题
错误提示 当电脑更新 VS2017 版本后,如果同时有新的 .NET Core SDK 更新,打开旧的项目重新打包,可能会报这样的错误 NETSDK1061: 项目是使用 Microsoft.NETC ...
- TPO-21 C2 Which elective courses to take
/* 加粗:语音部分 * 红色:单词部分 * 斜体:语法部分 * 下划线:信号词/句 */ 第 1 段 1.Listen to a conversation between a student and ...
- 2.2 Oracle之DML的SQL语句之多表查询以及组函数
一.SQL的多表查询: 1.左连接和右连接(不重要一方加(+)) SELECT e.empno,e.ename,d.deptno,d.dname,d.loc FROM emp e,dept d WHE ...
- 【坚持】Selenium+Python学习之从读懂代码开始 DAY6
2018/05/23 Python内置的@property装饰器 [@property](https://www.programiz.com/python-programming/property) ...
- 新手Python第二天(存储)
Python 列表的创建 创建一个空列表 例如:fruit=[] 创建一个有元素的列表 例如:fruit=['apple','banana','cherry'] 创建嵌套列表 例如:fruit=[[ ...
- 虚拟机中安装MAC OS X教程(适用所有电脑方法,特别是cpu不支持硬件虚拟化的电脑)
前言 之前写了一篇在Windows上搭建Object-C开发环境,并且写了一个HelloWorld程序.但真正开发苹果软件是在MAC OS X系统中(以下简称OSX)中.买不起MacBook,也没有O ...
- telnet命令详解
基础命令学习目录 原文链接:https://www.cnblogs.com/PatrickLiu/p/8556762.html telnet命令用于登录远程主机,对远程主机进行管理.telnet因为采 ...