Submission #7477532


Source Code Expand

N, M = map(int, input().split())
L = {}
for i in range(N):
    A = list(map(int, input().split()))
    for j, a in enumerate(A):
        if j == 0:
            continue
        if not a-1 in L.keys():
            L[a-1] = [i]
        else:
            L[a-1].append(i)

class UnionFind():
    # 作りたい要素数nで初期化
    # 使用するインスタンス変数の初期化
    def __init__(self, n):
        self.n = n
        # root[x]<0ならそのノードが根かつその値が木の要素数
        # rootノードでその木の要素数を記録する
        self.root = [-1]*(n+1)
        # 木をくっつける時にアンバランスにならないように調整する
        self.rnk = [0]*(n+1)

    # ノードxのrootノードを見つける
    def Find_Root(self, x):
        if(self.root[x] < 0):
            return x
        else:
            # ここで代入しておくことで、後の繰り返しを避ける
            self.root[x] = self.Find_Root(self.root[x])
            return self.root[x]
    # 木の併合、入力は併合したい各ノード
    def Unite(self, x, y):
        # 入力ノードのrootノードを見つける
        x = self.Find_Root(x)
        y = self.Find_Root(y)
        # すでに同じ木に属していた場合
        if(x == y):
            return 
        # 違う木に属していた場合rnkを見てくっつける方を決める
        elif(self.rnk[x] > self.rnk[y]):
            self.root[x] += self.root[y]
            self.root[y] = x

        else:
            self.root[y] += self.root[x]
            self.root[x] = y
            # rnkが同じ(深さに差がない場合)は1増やす
            if(self.rnk[x] == self.rnk[y]):
                self.rnk[y] += 1
    # xとyが同じグループに属するか判断
    def isSameGroup(self, x, y):
        return self.Find_Root(x) == self.Find_Root(y)

    # ノードxが属する木のサイズを返す
    def Count(self, x):
        return -self.root[self.Find_Root(x)]

union = UnionFind(N)

for list_l in L.values():
    for l in list_l:
        union.Unite(l, list_l[0])

ans = "YES"
for i in range(N):
    if not union.isSameGroup(0, i):
        ans = "NO"
print(ans)

Submission Info

Submission Time
Task C - Interpretation
User wattaihei
Language Python (3.4.3)
Score 400
Code Size 2300 Byte
Status AC
Exec Time 705 ms
Memory 16308 KB

Judge Result

Set Name sample dataset1 dataset2
Score / Max Score 0 / 0 200 / 200 200 / 200
Status
AC × 2
AC × 12
AC × 27
Set Name Test Cases
sample sample-01.txt, sample-02.txt
dataset1 sample-01.txt, sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt
dataset2 sample-01.txt, sample-02.txt, 01-01.txt, 01-02.txt, 01-03.txt, 01-04.txt, 01-05.txt, 01-06.txt, 01-07.txt, 01-08.txt, 01-09.txt, 01-10.txt, 02-01.txt, 02-02.txt, 02-03.txt, 02-04.txt, 02-05.txt, 02-06.txt, 02-07.txt, 02-08.txt, 02-09.txt, 02-10.txt, 02-11.txt, 02-12.txt, 02-13.txt, sample-01.txt, sample-02.txt
Case Name Status Exec Time Memory
01-01.txt AC 18 ms 3064 KB
01-02.txt AC 20 ms 3188 KB
01-03.txt AC 23 ms 3188 KB
01-04.txt AC 23 ms 3188 KB
01-05.txt AC 24 ms 3188 KB
01-06.txt AC 22 ms 3188 KB
01-07.txt AC 22 ms 3188 KB
01-08.txt AC 22 ms 3188 KB
01-09.txt AC 23 ms 3188 KB
01-10.txt AC 24 ms 3064 KB
02-01.txt AC 285 ms 14712 KB
02-02.txt AC 701 ms 8744 KB
02-03.txt AC 300 ms 11028 KB
02-04.txt AC 536 ms 14280 KB
02-05.txt AC 613 ms 9568 KB
02-06.txt AC 568 ms 14260 KB
02-07.txt AC 705 ms 10100 KB
02-08.txt AC 627 ms 8780 KB
02-09.txt AC 611 ms 8748 KB
02-10.txt AC 423 ms 15784 KB
02-11.txt AC 413 ms 16308 KB
02-12.txt AC 462 ms 15124 KB
02-13.txt AC 462 ms 15264 KB
sample-01.txt AC 18 ms 3064 KB
sample-02.txt AC 18 ms 3064 KB