Submission #10342911


Source Code Expand

#https://note.nkmk.me/python-union-find/
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

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

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

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        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 len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

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

def main():
    N,M = list(map(int,input().split()))
    ab = [list(map(int,input().split())) for i in range(M)]
    
    uf = UnionFind(N+1)
    for i in range(M):
        uf.union(ab[i][0],ab[i][1])
    base = uf.group_count()
    
    ans = 0
    for skip in range(M):
        uf = UnionFind(N+1)
        for i in range(M):
            if skip == i:
                continue
            else:
                uf.union(ab[i][0],ab[i][1])
        if uf.group_count()-base == 1:
            ans += 1
    print(ans)
    
if __name__ == '__main__':
    main()

Submission Info

Submission Time
Task C - Bridge
User nishigake
Language Python (3.4.3)
Score 300
Code Size 1817 Byte
Status AC
Exec Time 21 ms
Memory 3188 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 300 / 300
Status
AC × 3
AC × 20
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All sample_01.txt, sample_02.txt, sample_03.txt, subtask_1_1.txt, subtask_1_10.txt, subtask_1_11.txt, subtask_1_12.txt, subtask_1_13.txt, subtask_1_14.txt, subtask_1_15.txt, subtask_1_16.txt, subtask_1_17.txt, subtask_1_2.txt, subtask_1_3.txt, subtask_1_4.txt, subtask_1_5.txt, subtask_1_6.txt, subtask_1_7.txt, subtask_1_8.txt, subtask_1_9.txt
Case Name Status Exec Time Memory
sample_01.txt AC 18 ms 3188 KB
sample_02.txt AC 18 ms 3064 KB
sample_03.txt AC 18 ms 3064 KB
subtask_1_1.txt AC 21 ms 3064 KB
subtask_1_10.txt AC 18 ms 3188 KB
subtask_1_11.txt AC 20 ms 3188 KB
subtask_1_12.txt AC 20 ms 3188 KB
subtask_1_13.txt AC 21 ms 3064 KB
subtask_1_14.txt AC 21 ms 3188 KB
subtask_1_15.txt AC 21 ms 3188 KB
subtask_1_16.txt AC 21 ms 3188 KB
subtask_1_17.txt AC 21 ms 3188 KB
subtask_1_2.txt AC 21 ms 3064 KB
subtask_1_3.txt AC 19 ms 3064 KB
subtask_1_4.txt AC 21 ms 3064 KB
subtask_1_5.txt AC 19 ms 3188 KB
subtask_1_6.txt AC 20 ms 3064 KB
subtask_1_7.txt AC 21 ms 3064 KB
subtask_1_8.txt AC 20 ms 3064 KB
subtask_1_9.txt AC 18 ms 3064 KB