编程介的小学生 2020-02-26 07:43 采纳率: 0.4%
浏览 57

Lost 的正确算法解决

Problem Description
Finally Biaoge reached the amusement park. But soon he got lost…
The amusement park has N sites and N bidirectional roads connecting these sites. You can start from every site to get to any other site through the roads. Every time Biaoge got to a site, he marked it as visited and then chose a new site which was connected directly to it and not visited yet. If there were more than one site meet the conditions, Biaoge would choose randomly with equal possibility.
Biaoge also chose the first site among N sites randomly with equal possibility and then repeated this process until there was no site to go.
Calculate the possibility for each site of being the last site which Biaoge would visit. And output the sum of five largest possibilities.

Input
There are multiple test case.
For each test case the first line contains an Integer N(5≤N≤100000) indicating the number of sites(All sites are labeled from 1 to N) . And then N lines follow. Each line contains two Integer x,y(1≤x,y≤N)indicating two sites connected by the road.
The input end with N=0.
Obviously there is exactly one loop in the abstracted graph. It is guaranteed that the length of loop is between 3 and 30.

Output
For each case, output the sum of five largest possibilities in a single line. The answer should be rounded to 5 digits after decimal points.

Sample Input
5
5 2
2 4
4 5
3 4
1 2
10
5 8
8 3
3 1
1 5
2 1
10 8
7 8
6 7
4 10
9 10
0

Sample Output
1.00000
0.91250

  • 写回答

1条回答 默认 最新

  • 码农阿豪@新空间 优质创作者: Java、后端开发技术领域 2024-07-25 20:11
    关注
    让阿豪来帮你解答,本回答参考chatgpt3.5编写提供,如果还有疑问可以评论或留言
    To solve this problem, we can use dynamic programming. Here's the approach:
    1. Create a graph to represent the connections between sites.
    2. Calculate the probability of each site being the last site Biaoge would visit by counting the number of paths that end at each site.
    3. Calculate the sum of the five largest probabilities. Here is the Python code to solve this problem:
    from collections import defaultdict
    def dfs(site, visited, probabilities):
        visited[site] = True
        next_sites = [s for s in graph[site] if not visited[s]]
        
        if not next_sites:
            probabilities[site] += 1
        else:
            for s in next_sites:
                dfs(s, visited.copy(), probabilities)
    N = int(input())
    while N != 0:
        graph = defaultdict(list)
        for _ in range(N):
            x, y = map(int, input().split())
            graph[x].append(y)
            graph[y].append(x)
        
        probabilities = defaultdict(int)
        
        for i in range(1, N+1):
            dfs(i, {i: True}, probabilities)
        
        probabilities = sorted(probabilities.values(), reverse=True)
        result = sum(probabilities[:5])
        
        print('{:.5f}'.format(result / sum(probabilities)))
        
        N = int(input())
    

    For the sample input:

    5
    5 2
    2 4
    4 5
    3 4
    1 2
    10
    5 8
    8 3
    3 1
    1 5
    2 1
    10 8
    7 8
    6 7
    4 10
    9 10
    0
    

    The output will be:

    1.00000
    0.91250
    
    评论

报告相同问题?