博客
关于我
POJ--3352 道路建设
阅读量:188 次
发布时间:2019-02-28

本文共 1870 字,大约阅读时间需要 6 分钟。

为了解决这个问题,我们需要确定在修一条路后,景点之间仍然连通的数量。这个问题可以通过图论中的双连通分量和叶子节点的概念来解决。

方法思路

  • 双连通分量识别:使用Tarjan算法来识别图中的双连通分量。双连通分量是指在移除任何一条边后,该子图仍然保持连通的结构。

  • 压缩图:将每个双连通分量压缩成一个点,这样问题就转化为在压缩后的图中统计叶子节点的数量。

  • 计算叶子节点:在压缩后的图中,统计叶子节点的数量。叶子节点是指度数为1的点。

  • 确定添加边数:根据公式(k + 1) / 2,其中k是叶子节点的数量,计算需要添加的边数。

  • 解决代码

    #include 
    #include
    #include
    #include
    using namespace std;struct Edge { int to, next;};int head[maxn], cnt;int low[maxn], dfn[maxn], degree[maxn], num;void add(int u, int v) { Edge e; e.to = v; e.next = head[u]; head[u] = ++cnt;}void tarjan(int u, int fa) { dfn[u] = low[u] = ++num; for (int i = head[u]; i; i = e[i].next) { int v = e[i].to; if (v == fa) continue; if (!dfn[v]) { tarjan(v, u); low[u] = min(low[u], low[v]); } else { low[u] = min(low[u], dfn[v]); } }}void init() { memset(head, 0, sizeof(head)); memset(low, 0, sizeof(low)); memset(dfn, 0, sizeof(dfn)); cnt = num = 0;}int main() { while (cin >> n >> m) { init(); int u, v; while (m--) { cin >> u >> v; add(u, v); add(v, u); } tarjan(1, 0); for (int u = 1; u <= n; u++) { for (int i = head[u]; i; i = e[i].next) { int v = e[i].to; if (low[u] != low[v]) { degree[low[u]]++; } } } int leaf = 0; for (int i = 1; i <= n; i++) { if (degree[i] == 1) { leaf++; } } cout << (leaf + 1) / 2 << endl; } return 0;}

    代码解释

  • 数据结构初始化:使用了边结构体来表示图中的边,并初始化了必要的数组来存储图的信息。

  • Tarjan算法:用于深度优先搜索,计算每个节点的低点值,识别双连通分量。

  • 压缩图:通过计算每个节点的低点值,识别出双连通分量,并将每个双连通分量压缩成一个点。

  • 统计叶子节点:遍历每个节点,统计压缩图中度数为1的叶子节点数量。

  • 计算边数:根据公式(k + 1) / 2,计算需要添加的边数,确保图的双连通性。

  • 通过以上步骤,我们可以有效地解决问题,并确定需要修建的道路数量。

    转载地址:http://aufj.baihongyu.com/

    你可能感兴趣的文章
    nmon_x86_64_centos7工具如何使用
    查看>>
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.7 Parameters vs Hyperparameters
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    nnU-Net 终极指南
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>