Submission #64781262


Source Code Expand

Copy
n, m = map(int, input().split())
edg = [[] for _ in range(n)]
rev = [[] for _ in range(n)]
class DSU:
def __init__(self, n):
self.parent = list(range(n)) #
self.rank = [0] * n #
def find(self, x):
#
if self.parent[x] != x:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def unite(self, x, y):
# xy
x_root = self.find(x)
y_root = self.find(y)
if x_root == y_root:
return #
 
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
n, m = map(int, input().split())
edg = [[] for _ in range(n)]
rev = [[] for _ in range(n)]
class DSU:
    def __init__(self, n):
        self.parent = list(range(n))  # 各要素の親(初期状態では自分自身)
        self.rank = [0] * n           # ランク(経路圧縮用)

    def find(self, x):
        # 経路圧縮を行いながら親を探す
        if self.parent[x] != x:
            self.parent[x] = self.find(self.parent[x])
        return self.parent[x]

    def unite(self, x, y):
        # xとyの代表を見つける
        x_root = self.find(x)
        y_root = self.find(y)

        if x_root == y_root:
            return  # すでに同じグループ

        # 小さい方の番号を代表とする
        if x_root < y_root:
            self.parent[y_root] = x_root
        else:
            self.parent[x_root] = y_root

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

    def groups(self):
        # 各グループのメンバーをリスト化(確認用)
        from collections import defaultdict
        group_map = defaultdict(list)
        for i in range(len(self.parent)):
            root = self.find(i)
            group_map[root].append(i)
        return list(group_map.values())

for _ in range(m):
    u, v = map(int, input().split())
    u -= 1
    v -= 1
    if u > v:
        u, v = v, u
    edg[u].append(v)
    rev[v].append(u)
ans = []
uf = DSU(n)
length = set()
gg=[1 for _ in range(n)]
for i in range(n):
    if i:
        for v in rev[i]:
            ul=uf.find(i)
            uk=uf.find(v)
            if ul>uk:
                ul,uk=uk,ul
            if ul!=uk:
                gg[ul]+=gg[uk]
                gg[uk]=0
            uf.unite(i, v)
            if v in length:
                length.discard(v)
            if i in length:
              length.discard(i)
              
    for v in edg[i]:
        length.add(v)
    if gg[0]==i+1:
        #print(length)
        ans.append(len(length))
    else:
        ans.append(-1)
print(*ans, sep="\n")

Submission Info

Submission Time
Task E - Reachable Set
User juntenbanana
Language Python (PyPy 3.10-v7.3.12)
Score 450
Code Size 2131 Byte
Status AC
Exec Time 563 ms
Memory 174148 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 450 / 450
Status
AC × 4
AC × 49
Set Name Test Cases
Sample 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt
All 00_sample_00.txt, 00_sample_01.txt, 00_sample_02.txt, 00_sample_03.txt, 01_random_04.txt, 01_random_05.txt, 01_random_06.txt, 01_random_07.txt, 01_random_08.txt, 01_random_09.txt, 01_random_10.txt, 01_random_11.txt, 01_random_12.txt, 01_random_13.txt, 01_random_14.txt, 01_random_15.txt, 01_random_16.txt, 01_random_17.txt, 01_random_18.txt, 01_random_19.txt, 01_random_20.txt, 01_random_21.txt, 01_random_22.txt, 01_random_23.txt, 01_random_24.txt, 01_random_25.txt, 01_random_26.txt, 01_random_27.txt, 01_random_28.txt, 01_random_29.txt, 01_random_30.txt, 01_random_31.txt, 01_random_32.txt, 01_random_33.txt, 01_random_34.txt, 01_random_35.txt, 01_random_36.txt, 01_random_37.txt, 01_random_38.txt, 01_random_39.txt, 01_random_40.txt, 01_random_41.txt, 01_random_42.txt, 01_random_43.txt, 01_random_44.txt, 01_random_45.txt, 01_random_46.txt, 01_random_47.txt, 01_random_48.txt
Case Name Status Exec Time Memory
00_sample_00.txt AC 56 ms 76724 KB
00_sample_01.txt AC 56 ms 76264 KB
00_sample_02.txt AC 55 ms 76472 KB
00_sample_03.txt AC 56 ms 76532 KB
01_random_04.txt AC 299 ms 141944 KB
01_random_05.txt AC 271 ms 174148 KB
01_random_06.txt AC 334 ms 161396 KB
01_random_07.txt AC 287 ms 155912 KB
01_random_08.txt AC 563 ms 144556 KB
01_random_09.txt AC 454 ms 143360 KB
01_random_10.txt AC 548 ms 146924 KB
01_random_11.txt AC 461 ms 151480 KB
01_random_12.txt AC 500 ms 148016 KB
01_random_13.txt AC 543 ms 150664 KB
01_random_14.txt AC 514 ms 142788 KB
01_random_15.txt AC 235 ms 98352 KB
01_random_16.txt AC 168 ms 107392 KB
01_random_17.txt AC 96 ms 116152 KB
01_random_18.txt AC 67 ms 84952 KB
01_random_19.txt AC 68 ms 84808 KB
01_random_20.txt AC 155 ms 90972 KB
01_random_21.txt AC 129 ms 88300 KB
01_random_22.txt AC 95 ms 83500 KB
01_random_23.txt AC 176 ms 99096 KB
01_random_24.txt AC 176 ms 98448 KB
01_random_25.txt AC 138 ms 88720 KB
01_random_26.txt AC 462 ms 146776 KB
01_random_27.txt AC 523 ms 152308 KB
01_random_28.txt AC 420 ms 152184 KB
01_random_29.txt AC 511 ms 157824 KB
01_random_30.txt AC 446 ms 144532 KB
01_random_31.txt AC 513 ms 150212 KB
01_random_32.txt AC 417 ms 154256 KB
01_random_33.txt AC 527 ms 149192 KB
01_random_34.txt AC 466 ms 148016 KB
01_random_35.txt AC 517 ms 151160 KB
01_random_36.txt AC 426 ms 156604 KB
01_random_37.txt AC 518 ms 155876 KB
01_random_38.txt AC 56 ms 76372 KB
01_random_39.txt AC 56 ms 76472 KB
01_random_40.txt AC 56 ms 76444 KB
01_random_41.txt AC 56 ms 76468 KB
01_random_42.txt AC 55 ms 76456 KB
01_random_43.txt AC 55 ms 76584 KB
01_random_44.txt AC 56 ms 76244 KB
01_random_45.txt AC 56 ms 76280 KB
01_random_46.txt AC 56 ms 76436 KB
01_random_47.txt AC 56 ms 76500 KB
01_random_48.txt AC 56 ms 76532 KB


2025-04-26 (Sat)
20:44:30 +09:00