Submission #37340898


Source Code Expand

from bisect import bisect_left


def main():
    global N, M, edges
    N, M = map(int, input().split())
    edges = [tuple(map(lambda x: int(x)-1, input().split())) for _ in range(M)]

    ans = solve()
    print(ans)


def solve():
    dsu = DSU(2*N)

    for u, v in edges:
        dsu.merge(u, v+N)
        dsu.merge(u+N, v)

    for i in range(N):
        if dsu.same(i, i+N): return 0

    ret = N*(N-1)//2 - M
    for g in dsu.groups():
        cnt = bisect_left(g, N)
        ret -= cnt*(cnt-1)//2

    return ret


class DSU:
    def __init__(self, n):
        self._n = n
        self.parent_or_size = [-1] * n

    def merge(self, a, b):
        assert 0 <= a < self._n
        assert 0 <= b < self._n
        x, y = self.leader(a), self.leader(b)
        if x == y: return x
        if -self.parent_or_size[x] < -self.parent_or_size[y]: x, y = y, x
        self.parent_or_size[x] += self.parent_or_size[y]
        self.parent_or_size[y] = x
        return x

    def same(self, a, b):
        assert 0 <= a < self._n
        assert 0 <= b < self._n
        return self.leader(a) == self.leader(b)

    def leader(self, a):
        assert 0 <= a < self._n
        if self.parent_or_size[a] < 0: return a
        self.parent_or_size[a] = self.leader(self.parent_or_size[a])
        return self.parent_or_size[a]

    def size(self, a):
        assert 0 <= a < self._n
        return -self.parent_or_size[self.leader(a)]

    def groups(self):
        leader_buf = [self.leader(i) for i in range(self._n)]
        result = [[] for _ in range(self._n)]
        for i in range(self._n): result[leader_buf[i]].append(i)
        return [r for r in result if r != []]


if __name__ == '__main__': main()

Submission Info

Submission Time
Task D - Make Bipartite 2
User hannaheptapod
Language PyPy3 (7.3.0)
Score 400
Code Size 1708 Byte
Status AC
Exec Time 799 ms
Memory 191492 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 400 / 400
Status
AC × 3
AC × 47
Set Name Test Cases
Sample example0.txt, example1.txt, example2.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, 043.txt, example0.txt, example1.txt, example2.txt
Case Name Status Exec Time Memory
000.txt AC 64 ms 62272 KiB
001.txt AC 56 ms 62476 KiB
002.txt AC 203 ms 153072 KiB
003.txt AC 61 ms 68948 KiB
004.txt AC 466 ms 191492 KiB
005.txt AC 799 ms 173160 KiB
006.txt AC 791 ms 173368 KiB
007.txt AC 674 ms 113484 KiB
008.txt AC 376 ms 103516 KiB
009.txt AC 390 ms 104336 KiB
010.txt AC 424 ms 104352 KiB
011.txt AC 370 ms 87200 KiB
012.txt AC 397 ms 152128 KiB
013.txt AC 459 ms 99368 KiB
014.txt AC 628 ms 110668 KiB
015.txt AC 296 ms 151180 KiB
016.txt AC 222 ms 153804 KiB
017.txt AC 259 ms 155960 KiB
018.txt AC 229 ms 153772 KiB
019.txt AC 659 ms 113168 KiB
020.txt AC 192 ms 83208 KiB
021.txt AC 493 ms 107392 KiB
022.txt AC 220 ms 84956 KiB
023.txt AC 217 ms 85152 KiB
024.txt AC 436 ms 148556 KiB
025.txt AC 357 ms 154824 KiB
026.txt AC 232 ms 153844 KiB
027.txt AC 225 ms 153136 KiB
028.txt AC 227 ms 153468 KiB
029.txt AC 566 ms 117252 KiB
030.txt AC 518 ms 111508 KiB
031.txt AC 483 ms 107600 KiB
032.txt AC 446 ms 105908 KiB
033.txt AC 116 ms 76000 KiB
034.txt AC 668 ms 162452 KiB
035.txt AC 259 ms 155224 KiB
036.txt AC 314 ms 150392 KiB
037.txt AC 254 ms 154036 KiB
038.txt AC 233 ms 153080 KiB
039.txt AC 752 ms 174080 KiB
040.txt AC 594 ms 127084 KiB
041.txt AC 483 ms 106344 KiB
042.txt AC 415 ms 104860 KiB
043.txt AC 109 ms 74904 KiB
example0.txt AC 53 ms 62332 KiB
example1.txt AC 53 ms 62180 KiB
example2.txt AC 50 ms 62300 KiB