0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

全域木を列挙してみた

Posted at

引数
n:ノードの数
lst:[(u1,v1),(u2,v2),...]こんな感じ

unionfindはこれを使ってます
https://note.nkmk.me/python-union-find/

使用例
zenikigi(5,[(0,1),(0,2),(1,2),(1,3),(1,4)])
ー>[(0, 1, 3, 4), (0, 2, 3, 4), (1, 2, 3, 4)]


from copy import deepcopy
def zenikigi(n,lst):
    re=set()
    ans=[]
    re.add((tuple(),UnionFind(n)))
    for i in range(len(lst)):
        tmp=set()
        for v in re:
            t,uni=v
            if not uni.same(*lst[i]):
                if len(t)==n-2:
                    ans.append(t+tuple([i]))
                uni2=deepcopy(uni)
                uni2.union(*lst[i])
                tmp.add((t+tuple([i]),uni2))

        re|=tmp
    return ans

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?