【随手记】python大规模数据读取

题目是MT3055 交换排列
python大规模数据读取用这个sys.stdin.read。

import sys
input = sys.stdin.read
data = input().split()

这个是题解。

import heapq

class UnionFind:
    def __init__(self, size):
        self.parent = list(range(size))

    def find(self, x):
        if self.parent[x] == x:
            return x
        self.parent[x] = self.find(self.parent[x])
        return self.parent[x]

    def union(self, x, y):
        rootX = self.find(x)
        rootY = self.find(y)
        if rootX != rootY:
            self.parent[rootX] = rootY

def main():
    # python大规模数据读取用这个sys.stdin.read,具体读啥仿照这个示例
    import sys
    input = sys.stdin.read
    data = input().split()
    
    index = 0
    n = int(data[index])
    m = int(data[index + 1])
    index += 2

    num = [0] * (n + 1)
    fa = list(range(n + 1))
    q = [[] for _ in range(n + 1)]

    uf = UnionFind(n + 1)

    for i in range(1, n + 1):
        num[i] = int(data[index])
        index += 1

    for _ in range(m):
        x = int(data[index])
        y = int(data[index + 1])
        index += 2
        uf.union(x, y)

    for i in range(1, n + 1):
        root = uf.find(i)
        heapq.heappush(q[root], -num[i])

    result = []
    for i in range(1, n + 1):
        root = uf.find(i)
        result.append(-heapq.heappop(q[root]))

    print(" ".join(map(str, result)))

if __name__ == "__main__":
    main()

相关推荐

  1. 随手python大规模数据读取

    2024-07-15 08:40:02       27 阅读
  2. 随手python中的nonlocal关键字

    2024-07-15 08:40:02       46 阅读
  3. python读取kafka数据

    2024-07-15 08:40:02       37 阅读
  4. python 读取 hdfs 数据

    2024-07-15 08:40:02       20 阅读
  5. python/pytorch读取数据

    2024-07-15 08:40:02       52 阅读
  6. 蓝桥杯备考随手: 数位分解

    2024-07-15 08:40:02       39 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-15 08:40:02       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-15 08:40:02       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-15 08:40:02       58 阅读
  4. Python语言-面向对象

    2024-07-15 08:40:02       69 阅读

热门阅读

  1. django之 annotate,aggrate

    2024-07-15 08:40:02       24 阅读
  2. Linux shell自动交互之expect实践案例

    2024-07-15 08:40:02       21 阅读
  3. 代码改进,深度学习,强化学习

    2024-07-15 08:40:02       17 阅读
  4. Macos R安装xlsx ld: library not found for -lpcre2-8

    2024-07-15 08:40:02       19 阅读
  5. GitHub备份代码的学习笔记

    2024-07-15 08:40:02       23 阅读
  6. UF_add_callback_function

    2024-07-15 08:40:02       25 阅读
  7. 根服务器上市公司概览

    2024-07-15 08:40:02       23 阅读
  8. 【Go】如何使用 Go 连接 MySQL 数据库

    2024-07-15 08:40:02       20 阅读
  9. 职场新人感受

    2024-07-15 08:40:02       22 阅读