HIM_MRY 2021-07-15 14:11 采纳率: 75%
浏览 29

USACO Open 2008 Bronze

总时间限制: 10000ms 单个测试点时间限制: 1000ms 内存限制: 65536kB
描述
The steamy, sweltering summers of Wisconsin's dairy district stimulate the cows to slake their thirst. Farmer John pipes clear cold water into a set of N (3 <= N <= 99999; N odd) branching pipes conveniently numbered 1..N from a pump located at the barn. As the water flows through the pipes, the summer heat warms it. Bessie wants to find the coldest water so she can enjoy the weather more than any other cow.

She has mapped the entire set of branching pipes and noted that they form a tree with its root at the farm and furthermore that every branch point has exactly two pipes exiting from it. Surprisingly, every pipe is exactly one unit in length; of course, all N pipes connect up in one way or another to the pipe-tree.

Given the map of all the pipe connctions, make a list of the distance from the barn for every branching point and endpoint.Bessie will use the list to find the very coldest water.

The endpoint of a pipe, which might enter a branching point or might be a spigot, is named by the pipe's number. The map contains C (1 <= C <= N) connections, each of which specifies three integers: the endpoint E_i (1 <= E_i <= N) of a pipe and two branching pipes B1_i and B2_i (2 <= B1_i <= N; 2 <= B2_i <= N). Pipe number 1 connects to the barn; the distance from its endpoint to the barn is 1.
输入

  • Line 1: Two space-separated integers: N and C

  • Lines 2..C+1: Line i+1 describes branching point i with three space-separated integers: E_i, B1_i, and B2_i
    输出

  • Lines 1..N: Line i of the output contains a single integer that is the distance from the barn to the endpoint of pipe i
    样例输入
    5 2
    3 5 4
    1 2 3
    样例输出
    1
    2
    2
    3
    3
    提示
    INPUT DETAILS:

The input above describes this pipe map:

                +------+
                | Barn |
                +------+
                   | 1
                   *
                2 / \ 3
                     *
                  4 / \ 5
  • 写回答

1条回答 默认 最新

  • Taylor 淡定哥 2023-02-10 20:20
    关注

    这个问题可以使用(BFS)来解决。从谷仓(管道1)开始,访问每个分支点,标记从谷仓到每个管道端点的距离。继续访问连接到当前分支点的所有管道,直到访问完所有管道。从谷仓到管道端点的距离存储在一个数组中,通过打印数组中的值来获得输出。

    from collections import deque
    
    def bfs(graph, start, n):
        distances = [0] * (n + 1)
        queue = deque([start])
        while queue:
            node = queue.popleft()
            for neighbor in graph[node]:
                if distances[neighbor] == 0:
                    distances[neighbor] = distances[node] + 1
                    queue.append(neighbor)
        return distances
    
    n, c = map(int, input().split())
    graph = {i: [] for i in range(1, n + 1)}
    for _ in range(c):
        e, b1, b2 = map(int, input().split())
        graph[e].append(b1)
        graph[e].append(b2)
    
    distances = bfs(graph, 1, n)
    for i in range(1, n + 1):
        print(distances[i])
    
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 7月15日

悬赏问题

  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集