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 ...
随机推荐
- 【嵌入式硬件Esp32】Ubuntu18.04 更换阿里云软件源
使用Ubuntu 的apt-get来安装软件是总是因为官方源的速度太慢而抓狂. 但是用阿里云的源就很快,下面总结一下如何更换Ubuntu的软件源. 一.备份sudo cp /etc/apt/sourc ...
- ROW_NUMBER()函数使用详解
原文地址:https://blog.csdn.net/qq_30908543/article/details/74108348 注:mysql貌似不适用,本人测试未成功,mysql实现方式可参考:ht ...
- mongodb4.0 安装
下载: wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-4.0.9.tgz 解压缩 tar -zxvf mongodb-linux ...
- 使用Matlab绘制三维图的几种方法
以下六个函数都可以实现绘制三维图像: surf(xx,yy,zz); surfc(xx,yy,zz); mesh(xx,yy,zz); meshc(xx,yy,zz); meshz(xx,yy,zz) ...
- 高级UI-自定义动画框架
有的时候会需要做一些自定义的动画效果,在会反复用到的动画效果可以考虑做成动画框架,方便使用,做成框架的话就需要考虑很多的问题,最典型的问题就是属性和方法必须要是可配置的,这里就来聊一聊自定义动画框架的 ...
- (十六)springMvc 补充
文章目录 数据回显 `@ModelAttribute` && `@SessionAttributes` 注解 数据回显 对 pojo 数据回显的支持 ,springMvc 会默认的将传 ...
- 利用Python进行数据分析 第8章 数据规整:聚合、合并和重塑.md
学习时间:2019/11/03 周日晚上23点半开始,计划1110学完 学习目标:Page218-249,共32页:目标6天学完(按每页20min.每天1小时/每天3页,需10天) 实际反馈:实际XX ...
- 00 Python的变量
变量分类 a.全局变量:在模块内.在所有函数外面.在class外面,这就是全局变量. b.局部变量:在函数内.在class的方法(构造.类方法.静态方法.实例方法)内(变量未加self修饰),这就是局 ...
- springboot 的启动流程
1.我们springboot 项目的启动类如下. 方式1 @SpringBootApplicationpublic class SpringbootZkLockApplication { public ...
- jQueryUI的widget的Hello World
为了看懂jQuery-File-Upload里面的代码,所以学习到这里 //main.js //实践自定义jquery widget,风格1 (function($){ //$.widget('命名空 ...