有向图邻接表存储 + 树和图的深度优先遍历

news/2024/7/15 20:16:38 标签: 深度优先, 算法, 图论
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>

using namespace std;

const int N  = 100010, M = 2*N;

int h[N], e[M], ne[M], idx;

bool st[N];// dfs只搜索一遍

// 有向图邻接表存储
void add(int a, int b){
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx++;
}

// 树和图的深度优先搜索
void dfs(int u){
    
    st[u] = true; //表示已经搜索过当前点
    
    // 遍历当前点的出边
    for(i = h[u]; i != -1; i = ne[i]){
        // i 是索引  e[i] 代表当前边
        int j = e[i];
        if(!j) dfs(j);
    }
    
    
}

int main(){
    // 初始化头部 
    memset(h, -1 ,sizeof h);
    
    dfs(1); //从哪个点开始搜
    
    
    
    return 0;
}

ACWing 846 树的重心

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>

using namespace std;

const int N  = 100010, M = 2*N;

int h[N], e[M], ne[M], idx;

bool st[N];// dfs只搜索一遍

int ans = N;
int n;

// 有向图邻接表存储
void add(int a, int b){
    e[idx] = b;
    ne[idx] = h[a];
    h[a] = idx++;
}

// 树和图的深度优先搜索
//返回从当前节点为根的子树的点的数量
int dfs(int u){
    
    st[u] = true; //表示已经搜索过当前点
    
    int res = 0, sum = 1;
    
    // 遍历当前点的出边
    for(int i = h[u]; i != -1; i = ne[i]){
        // i 是索引  e[i] 代表当前边
        int j = e[i];
        if(!st[j]) {
            int s = dfs(j); // 记录以当前边为根的子树的点的数量 
            res = max(res,s);
            sum += s;
        }
    }
    
    res = max(res,n-sum);
    
    ans = min(ans,res);
    
    return sum;
    

}

int main(){
    // 初始化头部 
    memset(h, -1 ,sizeof h);
    int a,b;
    cin>>n;
    for(int i = 0; i < n-1; i++ ){
        cin>>a>>b;
        add(a,b),add(b,a);
    }
    
    dfs(1); //从哪个点开始搜
    cout<<ans<<'\n';
    
    
    return 0;
}

http://www.niftyadmin.cn/n/4981988.html

相关文章

jumpserver堡垒机添加资产配置

目录 jumpserver堡垒机添加资产配置 1、创建jumpserver管理用户&#xff0c;登录jumpserver堡垒机 2、创建普通用户&#xff0c;管理资源服务器 3、创建特权用户&#xff0c;登录资源服务器 4、添加资源 5、资产授权 6、登录jumpserver&#xff0c;创建的jumpserver用户 7、…

Python-matplotlib画图时标题中的指数表示

1.示例 2.核心代码 # 修改横轴的刻度 # 生成刻度的位置和标签 total_steps 1000000 # 总共100万步 num_segments 10 # 分成10段 segment_length total_steps // num_segments # 每段的步数# 生成刻度的位置 custom_ticks np.arange(0, total_steps 1, segment_length…

驱动 - 20230829

练习 基于platform实现 在根节点下&#xff0c;增加设备树 myplatform {compatible"hqyj,myplatform";interrupts-extended<&gpiof 9 0>, <&gpiof 7 0>, <&gpiof 8 0>;led1-gpio<&gpioe 10 0>;reg<0x12345678 59>;}…

Matlab(变量与文本读取)

目录 1.变量&#xff08;数据&#xff09;类型转换 1.1 字符 1.2 字符串 1.3 逻辑操作与赋值 2.Struct结构体数组 2.1函数的详细介绍&#xff1a; 2.1.1 cell2struct 2.1.1.1 垂直维度转换 2.1.1.2 水平维度转换 2.1.1.3 部分进行转换 2.1.2 rmfield 2.1.3 fieldnames(查…

【STM32】学习笔记(OLED)-江科大

调试方式 OLED简介 硬件电路 驱动函数 OLED.H #ifndef __OLED_H #define __OLED_Hvoid OLED_Init(void); void OLED_Clear(void); void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char); void OLED_ShowString(uint8_t Line, uint8_t Column, char *String); void OL…

linux使用不同的工具和命令来查看和放通允许访问的IP地址

在Linux系统中&#xff0c;您可以使用不同的工具和命令来查看和放通允许访问的IP地址。下面是一些常用的命令和方法&#xff1a; 查看当前开放的端口和规则&#xff1a; 使用 netstat 命令查看当前打开的端口和连接&#xff1a; Copy code netstat -tuln 使用 ss 命令也可以查…

SAP_ABAP_FUNCTION_ALV案例

SAP ABAP顾问能力模型梳理_企业数字化建设者的博客-CSDN博客SAP Abap顾问能力模型https://blog.csdn.net/java_zhong1990/article/details/132469977 一、Function ALV 1.1 基于退货采购订单创建&#xff0c;解释 FUNCTION_ALV开发的程序结构与代码模板参考 1.2 程序结构 to…

vue3+ts+tinynce在富文本编辑器菜单栏实现下拉框选择

实现效果 代码&#xff1a; <script lang"ts" setup> import Editor from tinymce/tinymce-vue import tinymce from tinymce; import { getIndicator } from /api/data-assets/data-dictoryimport {computed, ref} from "vue"; const props defin…