提出 #37357012


ソースコード 拡げる

from collections import defaultdict
from copy import deepcopy
class DSU():
    def __init__(self):
        self.dsu = defaultdict(lambda: -1)
    
    def leader(self, i):
        if self.dsu[i] < 0: return i
        self.dsu[i] = self.leader(self.dsu[i])
        return self.dsu[i]

    def merge(self, i, j):
        '''サイズの大きい方(x)に小さい方(y)をマージ'''
        if i == j: return
        x = self.leader(i)
        y = self.leader(j)
        if x == y: return

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

        self.dsu[x] += self.dsu[y]
        self.dsu[y] = x
    
    def members(self, i):
        l = self.leader(i)
        return [k for k, v in self.dsu.items() if self.leader(k) == l]

    
def D(N, M, uv):
    dsu = DSU()
    g = DSU()
    edge = defaultdict(list)
    for u, v in uv:
        dsu.merge(u, N + v)
        dsu.merge(N + u, v)
        g.merge(u, v)
        edge[u].append(v)
        edge[v].append(u)
    
    leaders = dict()
    for i in range(1, N + 1):
        l = g.leader(i)
        if l in leaders: continue
        
        for n in g.members(l):
            if dsu.leader(n) == dsu.leader(n + N):
                leaders[l] = False
                break
        else:
            leaders[l] = True
        
    ans = 0
    for i in range(1, N + 1):
        e = edge[i]
        for j in range(i + 1, N + 1):
            if j in e: continue
            
            li = g.leader(i)
            lj = g.leader(j)
            
            if li != lj:
                if leaders[li] and leaders[lj]:
                    ans += 1
                    continue
                else:
                    continue
            
            if not leaders[li]:
                continue
            
            target = deepcopy(dsu)
            target.merge(i, N + j)
            target.merge(N + i, j)
            for k in [i, j]:
                if target.leader(k) == target.leader(k + N): break
            else:
                ans += 1
    
    return ans

N, M = map(int, input().split())
uv = list(list(map(int, input().split())) for _ in range(M))
print(D(N, M, uv))

提出情報

提出日時
問題 D - Make Bipartite 2
ユーザ arakaki_tokyo
言語 PyPy3 (7.3.0)
得点 0
コード長 2216 Byte
結果 TLE
実行時間 2217 ms
メモリ 315512 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 0 / 400
結果
AC × 3
AC × 12
TLE × 35
セット名 テストケース
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
ケース名 結果 実行時間 メモリ
000.txt AC 81 ms 67564 KiB
001.txt AC 63 ms 67804 KiB
002.txt TLE 2208 ms 76392 KiB
003.txt TLE 2208 ms 76508 KiB
004.txt TLE 2217 ms 315512 KiB
005.txt TLE 2214 ms 244372 KiB
006.txt TLE 2214 ms 236120 KiB
007.txt TLE 2213 ms 216508 KiB
008.txt AC 520 ms 111520 KiB
009.txt TLE 2211 ms 171064 KiB
010.txt TLE 2211 ms 171288 KiB
011.txt TLE 2210 ms 131292 KiB
012.txt TLE 2210 ms 123628 KiB
013.txt TLE 2210 ms 135656 KiB
014.txt TLE 2212 ms 182788 KiB
015.txt TLE 2208 ms 86084 KiB
016.txt TLE 2208 ms 74984 KiB
017.txt TLE 2208 ms 75060 KiB
018.txt TLE 2208 ms 73656 KiB
019.txt TLE 2212 ms 173712 KiB
020.txt AC 256 ms 82752 KiB
021.txt TLE 2210 ms 123460 KiB
022.txt AC 277 ms 84572 KiB
023.txt AC 268 ms 85076 KiB
024.txt TLE 2210 ms 131100 KiB
025.txt TLE 2208 ms 91336 KiB
026.txt TLE 2208 ms 73396 KiB
027.txt TLE 2208 ms 73656 KiB
028.txt TLE 2208 ms 74156 KiB
029.txt TLE 2213 ms 189340 KiB
030.txt TLE 2212 ms 184072 KiB
031.txt TLE 2212 ms 180900 KiB
032.txt TLE 2212 ms 173056 KiB
033.txt AC 1662 ms 97364 KiB
034.txt TLE 2211 ms 178592 KiB
035.txt TLE 2208 ms 76812 KiB
036.txt TLE 2208 ms 83192 KiB
037.txt TLE 2208 ms 75124 KiB
038.txt TLE 2208 ms 75488 KiB
039.txt TLE 2212 ms 186560 KiB
040.txt TLE 2213 ms 198812 KiB
041.txt AC 1775 ms 123064 KiB
042.txt TLE 2212 ms 174828 KiB
043.txt AC 149 ms 71284 KiB
example0.txt AC 66 ms 67912 KiB
example1.txt AC 66 ms 67568 KiB
example2.txt AC 70 ms 67900 KiB