結果

問題 No.1473 おでぶなおばけさん
ユーザー 👑 KazunKazun
提出日時 2021-04-09 21:47:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,113 ms / 2,000 ms
コード長 7,906 bytes
コンパイル時間 430 ms
コンパイル使用メモリ 87,084 KB
実行使用メモリ 185,296 KB
最終ジャッジ日時 2023-09-07 10:46:48
合計ジャッジ時間 28,864 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 100 ms
72,308 KB
testcase_01 AC 101 ms
72,488 KB
testcase_02 AC 1,012 ms
177,860 KB
testcase_03 AC 721 ms
154,980 KB
testcase_04 AC 632 ms
135,148 KB
testcase_05 AC 320 ms
91,804 KB
testcase_06 AC 768 ms
155,588 KB
testcase_07 AC 1,089 ms
185,296 KB
testcase_08 AC 1,113 ms
184,412 KB
testcase_09 AC 1,071 ms
184,640 KB
testcase_10 AC 325 ms
90,488 KB
testcase_11 AC 310 ms
90,452 KB
testcase_12 AC 317 ms
90,556 KB
testcase_13 AC 222 ms
86,140 KB
testcase_14 AC 205 ms
86,116 KB
testcase_15 AC 270 ms
90,232 KB
testcase_16 AC 289 ms
90,388 KB
testcase_17 AC 143 ms
78,688 KB
testcase_18 AC 157 ms
80,560 KB
testcase_19 AC 349 ms
95,164 KB
testcase_20 AC 545 ms
131,016 KB
testcase_21 AC 526 ms
110,368 KB
testcase_22 AC 624 ms
127,508 KB
testcase_23 AC 561 ms
127,580 KB
testcase_24 AC 555 ms
120,080 KB
testcase_25 AC 486 ms
124,836 KB
testcase_26 AC 478 ms
121,740 KB
testcase_27 AC 280 ms
89,460 KB
testcase_28 AC 833 ms
164,244 KB
testcase_29 AC 600 ms
114,300 KB
testcase_30 AC 831 ms
125,076 KB
testcase_31 AC 890 ms
164,212 KB
testcase_32 AC 632 ms
143,752 KB
testcase_33 AC 617 ms
133,460 KB
testcase_34 AC 414 ms
105,876 KB
testcase_35 AC 380 ms
97,556 KB
testcase_36 AC 742 ms
106,180 KB
testcase_37 AC 636 ms
137,120 KB
testcase_38 AC 295 ms
84,072 KB
testcase_39 AC 305 ms
90,520 KB
testcase_40 AC 292 ms
90,344 KB
testcase_41 AC 260 ms
90,848 KB
testcase_42 AC 245 ms
90,984 KB
testcase_43 AC 512 ms
141,260 KB
testcase_44 AC 515 ms
141,188 KB
testcase_45 AC 515 ms
141,136 KB
testcase_46 AC 875 ms
142,760 KB
testcase_47 AC 1,024 ms
156,788 KB
testcase_48 AC 926 ms
156,716 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

def General_Binary_Decrease_Search(L,R,cond,Integer=True,ep=1/(1<<20),Times=50):
    """条件式が単調減少であるとき,一般的な二部探索を行う.
    L:解の下限
    R:解の上限
    cond:条件(1変数関数,広義単調減少 or 広義単調減少を満たす)
    Integer:解を整数に制限するか?
    ep:Integer=Falseのとき,解の許容する誤差
    """

    if not(cond(L)):
        return None

    if cond(R):
        return R

    if Integer:
        L-=1
        while R-L>1:
            C=L+(R-L)//2
            if cond(C):
                L=C
            else:
                R=C
        return L
    else:
        while (R-L)>=ep and Times:
            Times-=1
            C=L+(R-L)/2
            if cond(C):
                L=C
            else:
                R=C
        return L
#================================================
def check(w):
    U=Union_Find(N+1)
    for s,t,d in E:
        if w<=d:
            U.union(s,t)
    return U.same(1,N)
#================================================
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))


W=General_Binary_Decrease_Search(0,10**9,check)

G=Graph(list(range(1,N+1)))
for s,t,d in E:
    if W<=d:
        G.add_edge(s,t)

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