博客
关于我
【 POJ - 1611 】 C - The Suspects(简单并查集)求集合中元素个数
阅读量:276 次
发布时间:2019-03-01

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

为了解决这个问题,我们需要找出所有被怀疑的学生。学生0被怀疑后,所有与他同一群组的学生也会被怀疑,并且这些群组中的其他群组也会被怀疑。我们可以使用广度优先搜索(BFS)来处理这些群组,以确保所有相关的学生都被怀疑。

方法思路

  • 初始化:将学生0标记为怀疑。
  • 读取输入:读取学生数和群组数,然后读取每个群组的成员列表。
  • 建立映射:为每个学生记录他所在的所有群组。
  • BFS处理:使用队列进行BFS,处理每个群组及其成员,标记所有相关的学生为怀疑。
  • 统计结果:统计所有被怀疑的学生数量。
  • 解决代码

    import sysfrom collections import dequedef main():    while True:        n, m = map(int, sys.stdin.readline().split())        if n == 0 and m == 0:            break                groups = []        student_groups = [[] for _ in range(n)]  # student_groups[i] stores the groups that student i belongs to                for h in range(m):            parts = list(map(int, sys.stdin.readline().split()))            k = parts[0]            members = parts[1:]            groups.append(members)            for m_i in members:                student_groups[m_i].append(h)                suspicious = [False] * n        suspicious[0] = True                queue = deque()        for h in range(m):            if 0 in groups[h]:                queue.append(h)                processed = [False] * m                while queue:            current_group = queue.popleft()            if processed[current_group]:                continue            processed[current_group] = True            for member in groups[current_group]:                if not suspicious[member]:                    suspicious[member] = True                    for prime_group in student_groups[member]:                        if not processed[prime_group]:                            queue.append(prime_group)                            processed[prime_group] = True                count = sum(suspicious)        print(count)if __name__ == "__main__":    main()

    代码解释

  • 读取输入:从标准输入读取数据,直到遇到n=0且m=0时结束。
  • 初始化数据结构:groups存储每个群组的成员,student_groups记录每个学生所在的群组。
  • 标记怀疑状态:使用布尔数组Suspicious记录每个学生是否被怀疑。
  • 队列初始化:将所有包含学生0的群组加入队列。
  • BFS处理:处理每个群组及其成员,标记所有相关的学生为怀疑,并将这些群组的其他群组加入队列。
  • 统计结果:计算并输出被怀疑的学生数量。
  • 该方法确保了所有相关的学生都被正确标记为怀疑,时间复杂度为O(m + n + 总成员数),在给定的约束条件下是高效的。

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

    你可能感兴趣的文章
    Python3:setup.py:完成所有任务的PIP安装(Build_ext+Install)
    查看>>
    python4.0 gil_Python 并发编程系列--1、浅谈GIL历史及对多线程的影响
    查看>>
    python60行绘图程序_天底下最简单的QT画图板,就一个类,60行代码
    查看>>
    Python: AES加密与解密
    查看>>
    Python: Range() 最大尺寸;动态的还是静态的?
    查看>>
    Python: Windows下用 multiprocessing 的深坑
    查看>>
    python:Could not find platform independent libraries <prefix>解决方法
    查看>>
    Python:datetime tzinfo 时区名称文档
    查看>>
    Python:deepcopy(list) vs new_list = old_list[:]
    查看>>
    Python:Google API - 从消息中获取 mimeTypes
    查看>>
    Python:Google API - 从消息中获取 mimeTypes
    查看>>
    Python:subprocess.call,stdout to file,stderr to file,在屏幕上实时显示stderr
    查看>>
    Python:TypeError:无法连接“str“和“int“对象
    查看>>
    Python:XPath 在 ElementTree 中不可用
    查看>>
    Python:从 FTP 服务器下载文件
    查看>>
    python:从列表中创建元组列表
    查看>>
    Python:从嵌套列表中删除重复项
    查看>>
    Python:从间隔映射到值
    查看>>
    Python:使用 shell=False 的子进程调用不起作用
    查看>>
    Python:修改 PATH 环境变量的平台无关方式
    查看>>