当前位置: 首页 > news >正文

【电力——tarjan割点,求连通块】

题目

分析

这是割点的板子 

代码

#include <bits/stdc++.h>
using namespace std;

const int N = 1e4+10;
const int M = 3e4+10;

int h[N], e[M], ne[M], idx;
int dfn[N], low[N], tot;
int root, ans;

void add(int a, int b)  // 添加一条边a->b
{
    e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}

void tarjan(int u)
{
    dfn[u] = low[u] = ++tot;
    
    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(low[j] >= dfn[u])
                cnt++;
        }
        else low[u] = min(low[u], dfn[j]);
    }
    
    if(u != root) cnt++;
    ans = max(ans, cnt);
}
int main()
{
    int n, m;
    while(scanf("%d%d", &n, &m), n || m)
    {
        idx = tot = 0;
        memset(h, -1, sizeof h);
        memset(dfn, 0, sizeof dfn);
        for(int i = 1; i <= m; i++)
        {
            int a, b;
            scanf("%d%d", &a, &b);
            add(a, b), add(b, a);
        }
        
        ans = 0; //删掉结点的最大贡献
        int cnt = 0; //连通块数目
        for(root = 0; root < n; root++)
            if(!dfn[root])
                tarjan(root), cnt++;
                
        printf("%d\n", cnt + ans - 1);
    }
}


文章转载自:

http://8ZdeZiaX.drspc.cn
http://GHQXWMQE.drspc.cn
http://6qpZDvoV.drspc.cn
http://hCQywLYw.drspc.cn
http://z2OSQP8V.drspc.cn
http://9DI7lC8u.drspc.cn
http://KIBPuhsS.drspc.cn
http://bUkdlYjw.drspc.cn
http://GCouXrUc.drspc.cn
http://9fOBdUrw.drspc.cn
http://56uy4IXu.drspc.cn
http://jQXeE85v.drspc.cn
http://hHvtYFz1.drspc.cn
http://IkrR7yr6.drspc.cn
http://OHNXHWXd.drspc.cn
http://leBvVmDt.drspc.cn
http://EEGEt3jv.drspc.cn
http://ZDZbMnlm.drspc.cn
http://mcf2ceps.drspc.cn
http://zQAjYcrb.drspc.cn
http://XPyAkG94.drspc.cn
http://NndY7RNJ.drspc.cn
http://JUufyQss.drspc.cn
http://miBSwsRG.drspc.cn
http://51xutWm6.drspc.cn
http://sZV5Yd2b.drspc.cn
http://pNwE6mQQ.drspc.cn
http://aQxSpfib.drspc.cn
http://t1L2tJCF.drspc.cn
http://o7uvZtlK.drspc.cn
http://www.dtcms.com/a/45997.html

相关文章:

  • Leetcode132:分割回文串 II
  • 【领域】百度OCR识别
  • 请AI帮我们进行JAVA ORM 框架的选型, Hibernate, Mybatis, Bee,如何选择
  • 类和对象进一步了解c++
  • ​SMPP 协议在短信网关中的应用与迁移指南​附上文档地址(另外有个包内的方法,我使用的版本是又问题的,文章已经附上解决方式)
  • cellphonedb v5受配体多组比较气泡图(原创函数)
  • Ubuntu系统中测试硬盘速度
  • java作业
  • [转盘寿司]
  • ES批量查询
  • 网络分层 DNS CDN简单概念梳理
  • vcpkg 与 CMake 集成的步骤
  • 从零搭建微服务项目Pro(第1-3章——Quartz定时任务模块整合)
  • Perl 调用 DeepSeek API 脚本
  • 云原生监控篇——全链路可观测性与AIOps实战
  • 安装即是高级版!专业版软件,
  • Hadoop之01:HDFS分布式文件系统
  • Xshell及Xftp v8.0安装与使用-生信工具050
  • 【AI学习从零至壹】pytorch基础
  • Linux安装Apache2.4.54操作步骤
  • 前端js搭建(搭建后包含cookie,弹窗,禁用f12)
  • onerror事件的理解与用法
  • 【人工智能】GPT-4 vs DeepSeek-R1:谁主导了2025年的AI技术竞争?
  • 对大模型输出的 logits 进行处理,从而控制文本的生成
  • Java---入门基础篇(下)---方法与数组
  • C++类和对象:匿名对象及连续构造拷贝编译器的优化
  • Windows下git疑难:有文件无法被跟踪
  • FPGA开发,使用Deepseek V3还是R1(1):应用场景
  • openssl下aes128算法CFB模式加解密运算实例
  • 【自学笔记】大数据基础知识点总览-持续更新