2021-10-12leetcode

2021-11-02  本文已影响0人  Cipolee

快速加

def fast_multi(a,b):
    ans=0
    while b:
        if b&1:
            ans+=a
        a*=2
        b>>=1
    return ans
         

快速幂

def faster_power(a,b):
    ans=1
    while b:
        if b&1:
            ans*=a
        a*=a
        b>>=1
    return ans

二分图的最大匹配

import collections
def dfs_hungary(i, isvisited, map_):
    for j in range(1, m + 1):
        if edge[i][j] and not isvisited[j]:
            isvisited[j] = 1
            if map_[j] == 0 or dfs_hungary(map_[j], isvisited, map_):
                map_[j] = i
                return True
    return False


if __name__ == '__main__':
    n, m, e_num = [int(i) for i in input().split()]
    edge = [[0] * (m + 1) for _ in range(n + 1)]
    for _ in range(e_num):
        u, v = [int(i) for i in input().split()]
        #edge[u][v], edge[v][u] = 1, 1
        edge[u][v]=1
    ans = 0
    # isvisited=[False]*m
    map_ = collections.defaultdict(int)
    for i in range(1, n + 1):
        isvisited = [False] * (m + 1)
        if dfs_hungary(i, isvisited, map_):
            ans += 1
    print(ans)


上一篇下一篇

猜你喜欢

热点阅读