結果

問題 No.1451 集団登校
ユーザー ansainansain
提出日時 2021-03-31 18:47:24
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 409 ms / 2,000 ms
コード長 3,243 bytes
コンパイル時間 321 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 17,152 KB
最終ジャッジ日時 2024-12-15 06:19:06
合計ジャッジ時間 6,222 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 28 ms
11,008 KB
testcase_01 AC 29 ms
10,880 KB
testcase_02 AC 28 ms
10,880 KB
testcase_03 AC 29 ms
11,136 KB
testcase_04 AC 27 ms
11,136 KB
testcase_05 AC 29 ms
10,880 KB
testcase_06 AC 30 ms
11,136 KB
testcase_07 AC 30 ms
11,008 KB
testcase_08 AC 409 ms
17,152 KB
testcase_09 AC 30 ms
11,008 KB
testcase_10 AC 175 ms
13,824 KB
testcase_11 AC 120 ms
12,544 KB
testcase_12 AC 169 ms
13,568 KB
testcase_13 AC 284 ms
15,488 KB
testcase_14 AC 397 ms
15,616 KB
testcase_15 AC 187 ms
13,696 KB
testcase_16 AC 265 ms
14,976 KB
testcase_17 AC 252 ms
11,520 KB
testcase_18 AC 120 ms
12,544 KB
testcase_19 AC 79 ms
12,160 KB
testcase_20 AC 394 ms
15,744 KB
testcase_21 AC 177 ms
11,392 KB
testcase_22 AC 111 ms
12,032 KB
testcase_23 AC 67 ms
10,880 KB
testcase_24 AC 160 ms
13,568 KB
testcase_25 AC 182 ms
13,440 KB
testcase_26 AC 257 ms
14,976 KB
testcase_27 AC 312 ms
13,568 KB
testcase_28 AC 122 ms
12,288 KB
testcase_29 AC 199 ms
13,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict
import sys
from collections import defaultdict, Counter, deque
from itertools import permutations, combinations, product, combinations_with_replacement, groupby, accumulate
import operator
from math import sqrt, gcd, factorial
# from math import isqrt, prod,comb  # python3.8用(notpypy)
#from bisect import bisect_left,bisect_right
#from functools import lru_cache,reduce
from heapq import heappush, heappop, heapify, heappushpop, heapreplace
#import numpy as np
#import networkx as nx
#from networkx.utils import UnionFind
#from numba import njit, b1, i1, i4, i8, f8
#from scipy.sparse import csr_matrix
#from scipy.sparse.csgraph import shortest_path, floyd_warshall, dijkstra, bellman_ford, johnson, NegativeCycleError
# numba例 @njit(i1(i4[:], i8[:, :]),cache=True) 引数i4配列、i8 2次元配列,戻り値i1
def input(): return sys.stdin.readline().rstrip()
def divceil(n, k): return 1+(n-1)//k  # n/kの切り上げを返す
def yn(hantei, yes='Yes', no='No'): print(yes if hantei else no)


# https://note.nkmk.me/python-union-find/から一部改変


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.group_num = n
        self.tournament=[0]*n

    def find(self, x, y=0):
        if self.parents[x] < 0:
            return x, y+self.tournament[x]
        else:
            return self.find(self.parents[x], y+self.tournament[x])

    def union(self, x, y):
        x,_ = self.find(x)
        y,_ = self.find(y)

        if x == y:
            return

        self.group_num -= 1
        if self.parents[x] > self.parents[y]:
            x, y = y, x
        if self.parents[x] == self.parents[y]:
            self.tournament[x] = 1
            self.tournament[y] = 1
            self.tournament.append(0)
            self.parents.append(self.parents[x]+self.parents[y])
            self.parents[x] = len(self.parents)-1
            self.parents[y] = len(self.parents)-1

        else:
            self.tournament[y] -= 10**9
            self.parents[x] += self.parents[y]
            self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return self.group_num

    def all_group_members(self):
        self.all_group_member = defaultdict(list)
        for i in range(self.n):
            self.all_group_member[self.find(i)].append(i)
        return self.all_group_member

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def main():
    mod = 10**9+7
    mod2 = 998244353
    n,m=map(int, input().split())
    uf=UnionFind(n)
    for i in range(m):
        a,b=map(lambda x: int(x)-1, input().split())
        uf.union(a,b)
    #print(uf.tournament)
    for i in range(n):
        root,t=uf.find(i)
        if t>=0:
            print(pow(2,-t,mod))
        else:
            print(0)



if __name__ == '__main__':
    main()
0