当前位置:网站首页>AcWing 396. 矿场搭建 题解(tarjan割点)
AcWing 396. 矿场搭建 题解(tarjan割点)
2022-07-16 18:59:00 【乔大先生】
AcWing 396. 矿场搭建
懒得写了,直接照搬大佬的题解
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ULL;
const int N = 1010, M = 510;
int h[N], e[M], ne[M], idx;
int n ,m;
int low[N], dfn[N], timep;
vector<int>dcc[N]; //储存分割块内都有那些点
bool cut[N]; //记录是不是割点
int root;
int dcc_cnt; //记录分割块数量
int stk[N];
int top;
void add(int a, int b){
e[idx] = b;
ne[idx] = h[a];
h[a] = idx ++ ;
}
void tarjan(int u){
low[u] = dfn[u] = ++ timep;
stk[ ++ top] = u;
//如果是孤点
if(u == root && h[u] == -1){
dcc_cnt ++ ; //分割块数量++
dcc[dcc_cnt].push_back(u);
return ;
}
//如果不是孤点
int cnt = 0;
for(int i = h[u]; ~i; i = ne[i]){
int j = e[i];
if(!dfn[j]){
tarjan(j);
low[u] = min(low[u], low[j]);
if(dfn[u] <= low[j]){
cnt ++ ;
if(u != root || cnt > 1) cut[u] = true; //是个割点
++ dcc_cnt;
int y;
do{
y = stk[top -- ];
dcc[dcc_cnt].push_back(y);
}while(y != j); //因为要保留u给其他包含u的分割块,所以不将u弹出
dcc[dcc_cnt].push_back(u); //这个分割块内还是要加入u节点
}
}
else low[u] = min(low[u], dfn[j]);
}
}
int main()
{
int T = 1;
while(scanf("%d", &m), m){
for(int i = 1; i <= dcc_cnt; i ++ ) dcc[i].clear(); //分割块的数量是从1开始用的
n = idx = timep = dcc_cnt = top = 0;
memset(h, -1, sizeof h);
memset(dfn, 0, sizeof dfn);
memset(cut, 0, sizeof cut);
while(m -- ){
int a, b;
cin>>a>>b;
n = max(n, max(a, b));
add(a, b);
add(b, a);
}
for(root = 1; root <= n; root ++ ){
if(!dfn[root])
tarjan(root);
}
//遍历每一个分割块
int res = 0;
ULL num = 1; //记录方案数
for(int i = 1 ;i <= dcc_cnt; i ++ ){
int cnt = 0; //记录分割点数
for(int j = 0; j < dcc[i].size(); j ++ ){
if(cut[dcc[i][j]]) cnt ++ ;
}
if(cnt == 0){
//如果没有割点
if(dcc[i].size() > 1) res += 2, num *= dcc[i].size() * (dcc[i].size() - 1) / 2; //这不是个孤点
else res ++ ;
}
else if(cnt == 1) res ++ , num *= dcc[i].size() - 1;
}
printf("Case %d: %d %llu\n", T ++, res, num);
}
return 0;
}

边栏推荐
- [original] migration of rm500u-cn module driver
- Machine learning BP (back propagation) neural network
- 词云图,词频图,专门统计某些关键词的词云词频
- SQL notes
- MySQL index
- Introduction to sap appgyver
- 专属资源池使用最佳实践-Notebook与训练任务联动
- R language ggplot2 visualization: use the gghistogram function of ggpubr package to visualize the histogram, use the add parameter to add the mean dotted line, vertical line and horizontal axis to the
- Sword finger offer 53 - I. find the number I in the sorted array
- MySQL - multi table query - relationship between tables
猜你喜欢

scrapy 快速下载

What is the event delegation in JS?

【PHP代码审计】Pikachu靶场漏洞入门分析

Analysis of redis and MySQL double write consistency scheme

MySQL -- string function

About SQL: orcale SQL, why is the foreign key inventory ID statement of the merchant table invalid

不同的图像patch由不同的专家模型来处理!南洋理工&Mila稀疏融合混合专家模型SF-MoE,具有超强泛化能力!代码已开源!...

Current mirror automatic layout symmetry: quantification and application to eliminate nonlinear process gradients

Mysql——字符串函数

Daily question brushing record (XXV)
随机推荐
True question of CCF (anger takes 100 faints)
R language uses GLM function to build Poisson logarithm linear regression model, processes three-dimensional contingency table data to build saturation model, and uses step function to realize stepwis
基于单片机倾角检测仪设计分享
[dynamic programming]dp21 regular expression matching - difficult
[the pro test is valid]npm warn config global ` --global`, `--local` are deprecated Use `--location=global` instead.
Phabricator Conduit API介绍
Hystrix deployment
Technology cloud report: what is the core ability to build observability?
[hero planet July training leetcode problem solving daily] 16th queue
Best practices for exclusive resource pool use -notebook and training task linkage
浅学js中的关系运算符
FreeSwitch的限流配置
MySQL -- string function
Configuration actuelle de freeswitch
Best practices for the use of exclusive resource pools - Introduction to the use of dependent services
R language uses LM function to build regression model and BoxCox function of mass package to find the best power transformation to improve the fitting degree of the model (determine the best λ Paramet
为什么网络安全在工厂中很重要?
Duplicate disk: how does the backpropagation process of pooling layer, average pooling and maximum pooling backpropagate
Recommend a well written article on "I2C protocol explanation"
Linux服务器装mysql数据库(详细教程)