結果

問題 No.1473 おでぶなおばけさん
ユーザー 👑 KazunKazun
提出日時 2021-04-09 23:51:42
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 6,956 bytes
コンパイル時間 1,413 ms
コンパイル使用メモリ 87,064 KB
実行使用メモリ 148,760 KB
最終ジャッジ日時 2023-09-07 18:50:10
合計ジャッジ時間 27,662 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 99 ms
71,724 KB
testcase_01 AC 98 ms
71,844 KB
testcase_02 AC 963 ms
147,144 KB
testcase_03 AC 701 ms
128,452 KB
testcase_04 AC 592 ms
116,580 KB
testcase_05 AC 336 ms
89,704 KB
testcase_06 AC 649 ms
127,552 KB
testcase_07 AC 942 ms
146,396 KB
testcase_08 AC 1,008 ms
148,760 KB
testcase_09 AC 918 ms
145,412 KB
testcase_10 AC 278 ms
91,044 KB
testcase_11 AC 281 ms
90,700 KB
testcase_12 AC 276 ms
90,640 KB
testcase_13 AC 209 ms
86,816 KB
testcase_14 AC 186 ms
85,516 KB
testcase_15 AC 269 ms
92,308 KB
testcase_16 AC 272 ms
90,456 KB
testcase_17 AC 121 ms
80,372 KB
testcase_18 AC 126 ms
79,584 KB
testcase_19 AC 419 ms
96,180 KB
testcase_20 AC 671 ms
123,124 KB
testcase_21 AC 559 ms
106,296 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 AC 821 ms
146,812 KB
testcase_29 WA -
testcase_30 WA -
testcase_31 AC 788 ms
142,600 KB
testcase_32 AC 559 ms
129,252 KB
testcase_33 AC 586 ms
121,548 KB
testcase_34 AC 466 ms
103,004 KB
testcase_35 AC 364 ms
96,912 KB
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 AC 278 ms
90,780 KB
testcase_40 AC 263 ms
90,264 KB
testcase_41 AC 261 ms
90,860 KB
testcase_42 AC 262 ms
91,052 KB
testcase_43 AC 287 ms
119,408 KB
testcase_44 AC 289 ms
118,936 KB
testcase_45 AC 291 ms
118,920 KB
testcase_46 AC 632 ms
120,008 KB
testcase_47 AC 741 ms
126,932 KB
testcase_48 AC 671 ms
125,740 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class Union_Find():
    def __init__(self,N):
        """0,1,...,n-1を要素として初期化する.

        N:要素数
        """
        self.n=N
        self.parents=[-1]*N
        self.rank=[0]*N

    def find(self, x):
        """要素xの属している族を調べる.

        x:要素
        """
        V=[]
        while self.parents[x]>=0:
            V.append(x)
            x=self.parents[x]

        for v in V:
            self.parents[v]=x
        return x

    def union(self, x, y):
        """要素x,yを同一視する.

        x,y:要素
        """
        x=self.find(x)
        y=self.find(y)

        if x==y:
            return

        if self.rank[x]<self.rank[y]:
            x,y=y,x

        self.parents[x]+=self.parents[y]
        self.parents[y]=x

        if self.rank[x]==self.rank[y]:
            self.rank[x]+=1

    def size(self, x):
        """要素xの属している要素の数.

        x:要素
        """
        return -self.parents[self.find(x)]

    def same(self, x, y):
        """要素x,yは同一視されているか?

        x,y:要素
        """
        return self.find(x) == self.find(y)

    def members(self, x):
        """要素xが属している族の要素.
        ※族の要素の個数が欲しいときはsizeを使うこと!!

        x:要素
        """
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        """族の名前のリスト
        """
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        """族の個数
        """
        return len(self.roots())

    def all_group_members(self):
        """全ての族の出力
        """
        X={r:[] for r in self.roots()}
        for k in range(self.n):
            X[self.find(k)].append(k)
        return X

    def refresh(self):
        for i in range(self.n):
            _=self.find(i)

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

    def __repr__(self):
        return self.__str__()

class Graph:
    #入力定義
    def __init__(self,vertex=[]):
        self.vertex=set(vertex)

        self.edge_number=0
        self.adjacent={v:set() for v in vertex}

    #頂点の追加
    def add_vertex(self,*adder):
        for v in adder:
            if not self.vertex_exist(v):
                self.adjacent[v]=set()
                self.vertex.add(v)

    #辺の追加
    def add_edge(self,u,v):
        self.add_vertex(u)
        self.add_vertex(v)

        if not self.edge_exist(u,v):
            self.adjacent[u].add(v)
            self.adjacent[v].add(u)
            self.edge_number+=1

    #辺を除く
    def remove_edge(self,u,v):
        self.add_vertex(u)
        self.add_vertex(v)

        if self.edge_exist(u,v):
            self.adjacent[u].discard(v)
            self.adjacent[v].discard(u)
            self.edge_number-=1

    #頂点を除く
    def remove_vertex(self,*vertexes):
        for v in vertexes:
            if self.vertex_exist(v):
                U=self.neighbohood(v)
                for u in U:
                    self.edge_number-=1
                    self.adjacent[u].discard(v)

                del self.adjacent[v]
                self.vertex.discard(v)

    #Walkの追加
    def add_walk(self,*walk):
        n=len(walk)
        for i in range(n-1):
            self.add_edge(walk[i],walk[i+1])

    #Cycleの追加
    def add_cycle(self,*cycle):
        self.add_walk(*cycle)
        self.add_edge(cycle[-1],cycle[0])

    #頂点の交換
    def __vertex_swap(self,p,q):
        self.vertex.sort()

    #グラフに頂点が存在するか否か
    def vertex_exist(self,v):
        return v in self.vertex

    #グラフに辺が存在するか否か
    def edge_exist(self,u,v):
        if not(self.vertex_exist(u) and self.vertex_exist(v)):
            return False
        return v in self.adjacent[u]

    #近傍
    def neighbohood(self,v):
        if not self.vertex_exist(v):
            return []
        return list(self.adjacent[v])

    #次数
    def degree(self,v):
        if not self.vertex_exist(v):
            return 0

        return len(self.adjacent[v])

    #頂点数
    def vertex_count(self):
        return len(self.vertex)

    #辺数
    def edge_count(self):
        return self.edge_number

    #頂点vを含む連結成分
    def connected_component(self,v):
        if v not in self.adjacent:
            return []

        from collections import deque
        T={u:0 for u in self.vertex}
        T[v]=1
        Q=deque([v])
        while Q:
            u=Q.popleft()
            for w in self.adjacent[u]:
                if not T[w]:
                    T[w]=1
                    Q.append(w)
        return [x for x in self.adjacent if T[x]]

    #距離
    def distance(self,u,v):
        from collections import deque
        inf=float("inf")
        T={v:inf  for v in self.vertex}

        if u==v:
            return 0
        Q=deque([u])
        T[u]=0
        while Q:
            w=Q.popleft()
            for x in self.adjacent[w]:
                if T[x]==inf:
                    T[x]=T[w]+1
                    Q.append(x)
                    if x==v:
                        return T[x]
        return inf

    #ある1点からの距離
    def distance_all(self,u):
        from collections import deque
        inf=float("inf")
        T={v:inf  for v in self.vertex}

        Q=deque([u])
        T[u]=0
        while Q:
            w=Q.popleft()
            for x in self.adjacent[w]:
                if T[x]==inf:
                    T[x]=T[w]+1
                    Q.append(x)
        return T

    #最短路
    def shortest_path(self,u,v):
        from collections import deque
        inf=float("inf")
        T={v:None for v in self.vertex}

        if u==v:
            return [u]

        Q=deque([u])
        T[u]=u
        while Q:
            w=Q.popleft()
            for x in self.adjacent[w]:
                if not T[x]:
                    T[x]=w
                    Q.append(x)
                    if x==v:
                        P=[v]
                        a=v
                        while a!=u:
                            a=T[a]
                            P.append(a)
                        return P[::-1]
        return None

    #何かしらの頂点を選ぶ.
    def poping_vertex(self):
        v=self.vertex.pop()
        self.vertex.add(v)
        return v
#================================================
import sys
input=sys.stdin.readline
N,M=map(int,input().split())
E=[]
for _ in range(M):
    s,t,d=map(int,input().split())
    E.append((s,t,d))

E.sort(key=lambda x:x[-1],reverse=True)
U=Union_Find(N+1)
G=Graph(list(range(1,N+1)))
for s,t,d in E:
    U.union(s,t)
    G.add_edge(s,t)
    if U.same(1,N):
        w=d
        break

print(w,G.distance(1,N))
0