博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
BZOJ2730: [HNOI2012]矿场搭建
阅读量:4465 次
发布时间:2019-06-08

本文共 2503 字,大约阅读时间需要 8 分钟。

【传送门:】


简要题意:

  给出m条无向边,每个点有人,有时候会出现一个点崩塌,使得这个点和与这个点相连的边都不能经过

  可以在某些点设置安全出口,其它可以到达这些点的点上的人可以逃出去

  请问至少设置多少个安全出口和求出最少安全出口的情况下有多少种设置的方案


题解:

  Tarjan求割点例题

  设n为点数,n直接等于所有边中找出最大的点

  题目中只会出现一个点崩塌

  先找出所有割点,然后求出删除了割点之后的所有连通块与多少个割点相连

  设num为安全出口数,ans为方案数,tot为当前连通块的点数

  对于一个连通块

  如果不与其它割点相连,num+=2,ans*=(tot-1)*tot/2

  如果只与一个割点相连,num+=1,ans*=tot

  如果与两个及以上割点相连,则不处理,因为即使其中一个割点崩塌,这个连通块的点依旧可以通过其它割点走向其它连通块的出口

  特判一下,如果整个图都没有割点,则num=2,ans=n*(n-1)/2


参考代码:

#include
#include
#include
#include
#include
using namespace std;typedef long long LL;struct node{ int x,y,next;}a[1100];int len,last[510];void ins(int x,int y){ len++; a[len].x=x;a[len].y=y; a[len].next=last[x];last[x]=len;}int dfn[510],low[510],id;bool v[510];int rt,cnt;void findcut(int x){ dfn[x]=low[x]=++id; int t=0; for(int k=last[x];k;k=a[k].next) { int y=a[k].y; if(dfn[y]==0) { t++; findcut(y); if(low[y]>=dfn[x]) v[x]=true,cnt++; } low[x]=min(low[x],low[y]); } if(x==rt&&t==1) v[x]=false,cnt--;}int tot;void dfs(int x,int t){ dfn[x]=t; if(v[x]==true){cnt++;return ;} tot++; for(int k=last[x];k;k=a[k].next) { int y=a[k].y; if(dfn[y]!=t) { dfs(y,t); } }}int main(){ int n,m;int T=0; while(scanf("%d",&m)!=EOF) { if(m==0) break;T++; len=0;memset(last,0,sizeof(last)); n=0; for(int i=1;i<=m;i++) { int x,y; scanf("%d%d",&x,&y); ins(x,y);ins(y,x); n=max(n,max(x,y)); } id=cnt=0; memset(dfn,0,sizeof(dfn)); memset(low,0,sizeof(low)); memset(v,false,sizeof(v)); for(int i=1;i<=n;i++) { rt=i; if(dfn[i]==0) findcut(i); } if(cnt==0) printf("Case %d: 2 %d\n",T,n*(n-1)/2); else { id=0; memset(dfn,0,sizeof(dfn)); int num=0;LL ans=1; for(int i=1;i<=n;i++) { if(dfn[i]==0&&v[i]==false) { tot=cnt=0; dfs(i,i); if(cnt==0) num+=2,ans*=LL((tot-1)*tot/2); if(cnt==1) num+=1,ans*=LL(tot); } } printf("Case %d: %d %lld\n",T,num,ans); } } return 0;}

 

转载于:https://www.cnblogs.com/Never-mind/p/8966597.html

你可能感兴趣的文章
java8 按对象属性值排序
查看>>
【转帖】国产x86处理器KX-6000发布
查看>>
04-js的运算符
查看>>
第三天 while循环 及其用法
查看>>
Delphi 10 seattle 去掉自带的代码连接线
查看>>
构建高并发高可用的电商平台架构实践(转)
查看>>
Geometry Imager Viewport Filter
查看>>
九度oj 题目1025:最大报销额
查看>>
数字及字符串
查看>>
【转载】OmniGraffle (二)基础绘图和模具
查看>>
一些提高开发效率的 Category
查看>>
拓扑排序基础题——排序
查看>>
转:iphone 申请证书
查看>>
Python就业方向
查看>>
一步步学习SPD2010--第二章节--处理SP网站(3)--创建网站层次架构
查看>>
TCP
查看>>
Excel常用函数大全
查看>>
团队-团队编程项目中国象棋-模块测试过程
查看>>
10个经典的C语言面试基础算法及代码
查看>>
普通的java Ftp客户端的文件上传
查看>>