day56-graph theory-part06-8.26

tasks for today:

1. 108.冗余连接

2. 109.冗余连接II

---------------------------------------------------------------------------------

1. 108.冗余连接

in this practice, there is a rule needed to be identified: if u & v have been connected to a same root, then if a new couple come up, and isSame is true, there would be a circle showing up, which means this edge should be removed.

def find(u, father):
    if u == father[u]: return u
    else: return find(father[u], father)
    
def isSame(u, v, father):
    u = find(u, father)
    v = find(v, father)
    return u == v

def join(u, v, father):
    u = find(u, father)
    v = find(v, father)
    if u == v: return
    father[v] = u

def main():
    n = int(input())
    father = list(range(0,n+1))
    for _ in range(n):
        s, t = map(int, input().split())
        if isSame(s, t, father):
            print(' '.join([str(s),str(t)]))
            return 0
        else:
            join(s, t, father)

if __name__ == "__main__":
    main()

2. 109.冗余连接II

the key difference in this practice is the direct requirement for the edge, the introduce more complicated operation in the main function. 

def find(u, father):
    if father[u] == u:
        return u
    else:
        return find(father[u], father)

def isSame(u, v, father):
    u = find(u,father)
    v = find(v,father)
    return u == v

def join(u, v, father):
    u = find(u,father)
    v = find(v,father)
    if u == v: return
    father[v] = u
    
def isTreeAfterRemove(edges, deleteedge, father):
    # print(type(edges))
    # print(edges)
    # for i, j in edges:
    #     print(i,j)
    for s, t in edges:
        if s == edges[deleteedge][0] and t == edges[deleteedge][1]: continue
        if isSame(s, t, father):
            return False            
        join(s, t, father)
    return True

def getRemoveEdge(edges, father):
    for s, t in edges:
        if isSame(s, t, father):
            print(' '.join([str(s), str(t)]))
            return
        join(s, t, father)
    

def main():
    n = int(input())
    edges = []
    isDegree = [0] * (n+1)
    father = list(range(0, n+1))
    for _ in range(n):
        s, t = map(int, input().split())
        isDegree[t] += 1
        edges.append([s,t])
    
    suspect = []
    for i in range(n-1, -1, -1):
        if isDegree[edges[i][1]] == 2:
            suspect.append(i)
    # print(type(edges))
    # isTreeAfterRemove(edges, suspect[0], father)
    if len(suspect)>0:
        if isTreeAfterRemove(edges, suspect[0], father):
            print(' '.join(list(map(str, edges[suspect[0]]))))
            return
        else:
            print(' '.join(list(map(str, edges[suspect[1]]))))
            return
    
    getRemoveEdge(edges, father)

if __name__ == "__main__":
    main()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值