LOJ-1308-Ant network(蚂蚁的网络)-求割点分隔开的子图个数及乘积
网上的题解大都模糊,我可能写的也比较模糊吧,讲究看看。
大致题意:
原图没有一个割点时,特殊考虑,至少ans1=2个通风井,方案数n*(n-1)/2;
原图上有多个割点时,每个(由割点限制成几部分的)联通块个数即为ans1;需要dfs进行vis标记和iscut区分,不重不漏;
ans2,建设时避开在割点上建设通风井(通风井数量可最小化,以免通风井损毁后还需再建一个以备万一);求解时:当一个颜色块有两个割点时,摧毁一个蚂蚁们总可以通过另一个割点紧急转移;当一个颜色块有仅一个割点时,摧毁割点后就必须在颜色块内部建设一个。
//五点双环一割点,(发现这个样例:颜色块为1割点不为1,用颜色块来计算就不对头了!)
12
5 6 0 1 1 2 0 2 2 3 2 4 3 4
AC题解:
//头文件都私奔了!
#include<set>
using namespace std;
#define N 10100
#define inf 0x3f3f3f3f
typedef unsigned long long ULL;
int n,m,Time; //单指向边的共m条;
vector<int>G[N]; ///一次存贮1个内存!!
int dfn[N],low[N];
int color_num,in[N];
stack<int>st;
int father[N];
int cas;
int color[N]; ///表示i在的颜色块的编号,本题无用!
bool iscut[N]; ///表示i 是否为割点
bool vis[N]; ///dfs中有用
int test_num; ///dfs中从某u点(非割点)出发可以遇见的
set<int>ss; ///从某u点(非割点)出发dfs中的遇见割点(方便去重用set)
ULL mod;///模数 void init()
{
for(int i=;i<=n;i++){
G[i].clear();
}
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
Time=;
color_num=;
memset(in,,sizeof(in));memset(color,,sizeof(color));
memset(iscut,false,sizeof(iscut));
memset(father,-,sizeof(father));
while(!st.empty())st.pop();
}
void tarjan(int u,int fa)
{
dfn[u]=low[u]=++Time;
st.push(u);in[u]=;
father[u]=fa; ///别忘记!
for(int i=;i<(int)G[u].size();i++){
int v=G[u][i];
if(!dfn[v]){
tarjan(v,u);
low[u]=min(low[u],low[v]);
}
else if(v!=fa&&in[u]==){ ///子节点在队列中并且不是父节点
low[u]=min(low[u],dfn[v]);
}
}
if(dfn[u]==low[u]){
++color_num;
int top;
do{
top=st.top();st.pop();
in[top]=;
color[top]=color_num; //细节!
}while(top!=u);
}
}
ULL fact_mod(ULL x){ ///对2的64次方取模(mod只有一半) while(x/(ULL)>=mod)
x=x-mod-mod;
return x;
}
void dfs(int u)
{
vis[u]=;
test_num++; ///从u点出发的由割点分隔开的联通图中的非割点数目
for(int i=;i<(int)G[u].size();i++){
int v=G[u][i];
if(iscut[v]){
ss.insert(v);continue;
}
if(iscut[v]||vis[v])continue; ///vis防重复,iscut防dfs到切点
dfs(v);
}
} void solve() //缩点
{
tarjan(,-); ///单源点图,求DAG
int ans1=;
ULL ans2=;
int rootsons=,u,cnt=; ///cnt表示割点个数
for(int i=;i<n;i++){ ///求割点,分类特判源点0是否为割点
if(father[i]==)
rootsons++;
else{
u=father[i]; ///父节点
if(dfn[u]<=low[i]&&iscut[u]==false){
iscut[u]=true;cnt++;
}
}
}
if(rootsons>){ ///特判源点
iscut[]=true;cnt++;
}
if(cnt==){ ///没有割点的图
ans1=;
ans2=(ULL)n*(n-)/;
ans2=fact_mod(ans2);
}
else{
memset(vis,false,sizeof(vis));
for(int i=;i<n;i++){ ///多个割点的图
if(iscut[i]||vis[i])
continue;
ss.clear();test_num=;
dfs(i);
if(ss.size()<=){
ans1++;ans2=ans2*(ULL)test_num;
}
}
}
ans2=fact_mod(ans2);
printf("Case %d: %d %llu\n",++cas,ans1,ans2);
}
int main()
{
int T;
scanf("%d",&T);
cas=;
mod=(ULL); ///求模数
for(int i=;i<=;i++){
mod=mod*(ULL);
} while(T--){
scanf("%d%d",&n,&m);
init();
int x,y;
for(int i=;i<=m;i++){
scanf("%d%d",&x,&y);
G[x].push_back(y);
G[y].push_back(x);
}
solve();
}
return ;
}
LOJ-1308-Ant network(蚂蚁的网络)-求割点分隔开的子图个数及乘积的更多相关文章
- poj 1144 Network【双连通分量求割点总数】
Network Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11042 Accepted: 5100 Descript ...
- B - Network - uva 315(求割点)
题意:给一个无向连通图,求出割点的数量. 首先输入一个N(多实例,0结束),下面有不超过N行的数,每行的第一个数字代表后面的都和它存在边,0表示行输入的结束(很蛋疼的输入方式). 分析:割点的模板题 ...
- Network UVA - 315(求割点)
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...
- POJ 1144 Network(Tarjan求割点)
Network Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 12707 Accepted: 5835 Descript ...
- uva 315 Network(无向图求割点)
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- NFS - Network File System网络文件系统
NFS(Network File System/网络文件系统): 设置Linux系统之间的文件共享(Linux与Windows中间文件共享采用SAMBA服务): NFS只是一种文件系统,本身没有传输功 ...
- POJ 1144 Network(tarjan 求割点个数)
Network Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17016 Accepted: 7635 Descript ...
- poj 1144 Network 无向图求割点
Network Description A Telephone Line Company (TLC) is establishing a new telephone cable network. Th ...
- UVA315:Network(求割点)
Network 题目链接:https://vjudge.net/problem/UVA-315 Description: A Telephone Line Company (TLC) is estab ...
随机推荐
- Yii2性能优化
https://www.yiiframework.com/doc/guide/2.0/zh-cn/tutorial-performance-tuning 性能优化 有许多因素影响你的 Web 应用程序 ...
- Framework7 介绍
Framework7 - is a free and open source framework to develop mobile, desktop or web apps with native ...
- JS Maximum call stack size exceeded
一.问题描述 Maximum call stack size exceeded 翻译为:超过最大调用堆栈大小 二.效果截图 三.问题解决方案 出现该问题,说明程序出现了死循环了.所以要去检查出错的程 ...
- windows下连接mysql提示1044-access denied for root''@'localhost' to database
ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mydb'. 原因是因为mysql数据库的user表里,存 ...
- CentOS 安装tab命令补全
CentOS 安装tab命令补全 1. 安装epel 源 yum -y install epel-release 2. 加快yum速度 yum -y install yum-plugin-fastes ...
- 025 Android 带进度条的对话框(ProgressDialog)
1.ProgressDialog介绍 ProgressDialog可以在当前界面弹出一个置顶于所有界面元素的对话框,同样具有屏蔽其他控件的交互能力,用于提示用户当前操作正在运行,让用户等待: 2.应用 ...
- 剑指offer41:所有和为S的连续正数序列,例如,有多少种连续的正数序列的和为100
1 题目描述 小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100.但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数).没多久 ...
- ArcGIS SOE开发异常之 ClassFactory cannot supply requested class
最近SOE开发一个功能,辛辛苦苦写完, 异常: ClassFactory cannot supply requested class 辛苦解决: 百度一下,描述这个问题的帖子很多,不过内容基本一致.大 ...
- Eclipse RCP难点:给Command传递参数(Object)
这个问题网络上没有答案,国外网站上也没有,本人研究了一天,终于搞明白如何实现,实际上是Eclipse RCP的ICommandService本身就已经提供的方法,只是网络上教的都是使用IHandler ...
- mock打桩测试
pom依赖: <!-- https://mvnrepository.com/artifact/org.jmockit/jmockit --> <dependency> < ...