結果

問題 No.2200 Weird Shortest Path
ユーザー vwxyzvwxyz
提出日時 2023-12-01 14:39:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,168 ms / 2,000 ms
コード長 4,081 bytes
コンパイル時間 3,307 ms
コンパイル使用メモリ 81,572 KB
実行使用メモリ 120,212 KB
最終ジャッジ日時 2023-12-01 14:40:19
合計ジャッジ時間 29,505 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
55,608 KB
testcase_01 AC 40 ms
55,608 KB
testcase_02 AC 40 ms
55,608 KB
testcase_03 AC 49 ms
63,716 KB
testcase_04 AC 61 ms
72,728 KB
testcase_05 AC 47 ms
55,608 KB
testcase_06 AC 41 ms
55,608 KB
testcase_07 AC 46 ms
63,644 KB
testcase_08 AC 59 ms
70,680 KB
testcase_09 AC 58 ms
68,572 KB
testcase_10 AC 41 ms
55,608 KB
testcase_11 AC 40 ms
55,608 KB
testcase_12 AC 42 ms
55,608 KB
testcase_13 AC 40 ms
55,608 KB
testcase_14 AC 40 ms
55,608 KB
testcase_15 AC 59 ms
68,572 KB
testcase_16 AC 42 ms
55,608 KB
testcase_17 AC 381 ms
89,016 KB
testcase_18 AC 570 ms
102,356 KB
testcase_19 AC 972 ms
108,376 KB
testcase_20 AC 945 ms
114,600 KB
testcase_21 AC 414 ms
97,336 KB
testcase_22 AC 530 ms
97,632 KB
testcase_23 AC 974 ms
114,740 KB
testcase_24 AC 254 ms
84,152 KB
testcase_25 AC 934 ms
111,680 KB
testcase_26 AC 984 ms
113,456 KB
testcase_27 AC 392 ms
94,776 KB
testcase_28 AC 785 ms
106,280 KB
testcase_29 AC 994 ms
115,628 KB
testcase_30 AC 1,004 ms
112,572 KB
testcase_31 AC 624 ms
101,336 KB
testcase_32 AC 1,012 ms
119,584 KB
testcase_33 AC 1,013 ms
119,592 KB
testcase_34 AC 1,037 ms
119,588 KB
testcase_35 AC 1,015 ms
119,972 KB
testcase_36 AC 1,036 ms
119,592 KB
testcase_37 AC 1,001 ms
119,584 KB
testcase_38 AC 1,033 ms
120,212 KB
testcase_39 AC 589 ms
105,548 KB
testcase_40 AC 607 ms
117,984 KB
testcase_41 AC 433 ms
104,360 KB
testcase_42 AC 617 ms
119,804 KB
testcase_43 AC 1,168 ms
116,548 KB
testcase_44 AC 1,159 ms
116,420 KB
testcase_45 AC 1,065 ms
116,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline=sys.stdin.readline
from collections import defaultdict

class UnionFind:
    def __init__(self,N,label=None,f=None,weighted=False,rollback=False):
        self.N=N
        self.parents=[None]*self.N
        self.size=[1]*self.N
        self.roots={i for i in range(self.N)}
        self.label=label
        if self.label!=None:
            self.label=[x for x in label]
        self.f=f
        self.weighted=weighted
        if self.weighted:
            self.weight=[0]*self.N
        self.rollback=rollback
        if self.rollback:
            self.operate_list=[]
            self.operate_set=[]

    def Find(self,x):
        stack=[]
        while self.parents[x]!=None:
            stack.append(x)
            x=self.parents[x]
        if not self.rollback:
            if self.weighted:
                w=0
                for y in stack[::-1]:
                    self.parents[y]=x
                    w+=self.weight[y]
                    self.weight[y]=w
            else:
                for y in stack[::-1]:
                    self.parents[y]=x
        return x

    def Union(self,x,y,w=None):
        root_x=self.Find(x)
        root_y=self.Find(y)
        if self.rollback:
            self.operate_list.append([])
            self.operate_set.append([])
        if root_x==root_y:
            if self.weighted:
                if self.weight[y]-self.weight[x]==w:
                    return True
                else:
                    return False
        else:
            if self.size[root_x]<self.size[root_y]:
                x,y=y,x
                root_x,root_y=root_y,root_x
                if self.weighted:
                    w=-w
            if self.rollback:
                self.operate_list[-1].append((self.parents,root_y,self.parents[root_y]))
                self.operate_list[-1].append((self.size,root_x,self.size[root_x]))
                self.operate_set[-1].append(root_y)
                if self.label!=None:
                    self.operate_list[-1]((self.label,root_x,self.label[root_x]))
                if self.weighted:
                    self.operate_list[-1].append((self.weight,root_y,self.weight[root_y]))
            self.parents[root_y]=root_x
            self.size[root_x]+=self.size[root_y]
            self.roots.remove(root_y)
            if self.label!=None:
                self.label[root_x]=self.f(self.label[root_x],self.label[root_y])
            if self.weighted:
                self.weight[root_y]=w+self.weight[x]-self.weight[y]

    def Size(self,x):
        return self.size[self.Find(x)]

    def Same(self,x,y):
        return self.Find(x)==self.Find(y)

    def Label(self,x):
        return self.label[self.Find(x)]

    def Weight(self,x,y):
        root_x=self.Find(x)
        root_y=self.Find(y)
        if root_x!=root_y:
            return None
        return self.weight[y]-self.weight[x]

    def Roots(self):
        return list(self.roots)

    def Linked_Components_Count(self):
        return len(self.roots)

    def Linked_Components(self):
        linked_components=defaultdict(list)
        for x in range(self.N):
            linked_components[self.Find(x)].append(x)
        return linked_components
    
    def Rollback(self):
        assert self.rollback
        if self.operate_list:
            for lst,x,v in self.operate_list.pop():
                lst[x]=v
            for x in self.operate_set.pop():
                self.roots.add(x)            
            return True
        else:
            return False

    def __str__(self):
        linked_components=defaultdict(list)
        for x in range(self.N):
            linked_components[self.Find(x)].append(x)
        return "\n".join(f"{r}: {linked_components[r]}" for r in sorted(list(linked_components.keys())))

N,M=map(int,readline().split())
edges=[]
for m in range(M):
    a,b,w=map(int,readline().split())
    a-=1;b-=1
    edges.append((w,a,b))
edges.sort()
ans=0
UF=UnionFind(N)
for w,a,b in edges:
    if not UF.Same(a,b):
        ans+=UF.Size(a)*UF.Size(b)*w
        UF.Union(a,b)
print(ans)
0