编程介的小学生 2019-12-31 17:45 采纳率: 0.2%
浏览 53

Hand in Hand 是如何来编写的

Problem Description
In order to get rid of Conan, Kaitou KID disguises himself as a teacher in the kindergarten. He knows kids love games and works out a new game called "hand in hand".

Initially kids run on the playground randomly. When Kid says "stop", kids catch others' hands immediately. One hand can catch any other hand randomly. It's weird to have more than two hands get together so one hand grabs at most one other hand. After kids stop moving they form a graph.

Everybody takes a look at the graph and repeat the above steps again to form another graph. Now Kid has a question for his kids: "Are the two graph isomorphism?"

Input
The first line contains a single positive integer T( T <= 100 ), indicating the number of datasets.
There are two graphs in each case, for each graph:
first line contains N( 1 <= N <= 10^4 ) and M indicating the number of kids and connections.
the next M lines each have two integers u and v indicating kid u and v are "hand in hand".
You can assume each kid only has two hands.

Output
For each test case: output the case number as shown and "YES" if the two graph are isomorphism or "NO" otherwise.

Sample Input
2

3 2
1 2
2 3
3 2
3 2
2 1

3 3
1 2
2 3
3 1
3 1
1 2

Sample Output
Case #1: YES
Case #2: NO

  • 写回答

1条回答 默认 最新

  • 你知我知皆知 2024-07-27 11:32
    关注

    以下回答参考 皆我百晓生券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。

    # coding: utf-8
    import numpy as np
    from collections import deque
    
    class Graph:
        def __init__(self):
            self.g = {}
    
        def addEdge(self, x, y):
            if (x, y) not in self.g.keys():
                self.g[(x,y)] = []
            if (y,x) not in self.g.keys():
                self.g[(y,x)] = []
    
        def bfs(self, s, t):
            q = deque()
            visited = [False] * len(self.g)
            parent = [-1] * len(self.g)
    
            visited[s] = True
            q.append(s)
            while len(q) > 0:
                x = q.popleft()
    
                for i in range(len(self.g[x])):
                    if self.g[x][i] == -1:
                        continue
                    y = self.g[x][i]
                    if visited[y] == False:
                        visited[y] = True
                        q.append(y)
                        parent[y] = x
    
            return parent
    
    
        def dfs(self, s, t):
            stack = []
            visited = [False] * len(self.g)
            visited[s] = True
            stack.append(s)
    
            while len(stack) > 0:
                x = stack.pop()
                for i in range(len(self.g[x])):
                    if self.g[x][i] == -1:
                        continue
                    y = self.g[x][i]
    
                    if visited[y] == False:
                        visited[y] = True
                        stack.append(y)
    
            return visited[t]
    
    
        def isConnected(self, s, t):
            parent = self.bfs(s,t)
            return self.dfs(s,t) and self.dfs(t,s)
    
    
    T = int(input())
    for _ in range(T):
        N,M = map(int,input().split())
    
        G = Graph()
        for m in range(M):
            x,y = map(int,input().split())
            G.addEdge(x,y)
    
        ans = 'YES'
        for n in range(N):
            for m in range(n+1,N):
                if not G.isConnected(m,n):
                    ans = 'NO'
                    break
        print(ans)    
    
    评论

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上