結果

問題 No.2642 Don't cut line!
ユーザー rikein12rikein12
提出日時 2024-02-20 15:07:41
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,069 ms / 4,000 ms
コード長 3,108 bytes
コンパイル時間 312 ms
コンパイル使用メモリ 82,528 KB
実行使用メモリ 338,404 KB
最終ジャッジ日時 2024-09-29 03:43:56
合計ジャッジ時間 19,906 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
55,724 KB
testcase_01 AC 911 ms
274,588 KB
testcase_02 AC 745 ms
244,104 KB
testcase_03 AC 792 ms
257,228 KB
testcase_04 AC 799 ms
259,060 KB
testcase_05 AC 1,050 ms
313,936 KB
testcase_06 AC 369 ms
110,712 KB
testcase_07 AC 376 ms
111,260 KB
testcase_08 AC 372 ms
110,764 KB
testcase_09 AC 373 ms
111,260 KB
testcase_10 AC 384 ms
111,648 KB
testcase_11 AC 365 ms
110,708 KB
testcase_12 AC 376 ms
111,432 KB
testcase_13 AC 365 ms
110,892 KB
testcase_14 AC 371 ms
111,204 KB
testcase_15 AC 384 ms
112,068 KB
testcase_16 AC 395 ms
110,696 KB
testcase_17 AC 1,069 ms
338,404 KB
testcase_18 AC 777 ms
256,428 KB
testcase_19 AC 551 ms
190,264 KB
testcase_20 AC 453 ms
129,392 KB
testcase_21 AC 325 ms
105,568 KB
testcase_22 AC 323 ms
101,860 KB
testcase_23 AC 835 ms
248,180 KB
testcase_24 AC 394 ms
112,628 KB
testcase_25 AC 438 ms
111,888 KB
testcase_26 AC 486 ms
111,924 KB
testcase_27 AC 705 ms
238,336 KB
testcase_28 AC 821 ms
237,328 KB
testcase_29 AC 456 ms
112,136 KB
testcase_30 AC 438 ms
111,992 KB
testcase_31 AC 713 ms
225,100 KB
testcase_32 AC 441 ms
111,992 KB
testcase_33 AC 37 ms
54,712 KB
testcase_34 AC 37 ms
56,212 KB
testcase_35 AC 36 ms
55,744 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys

sys.setrecursionlimit(500000)
def input():
    return sys.stdin.readline()[:-1]



from collections import deque

class UnionFind():
    def __init__(self,size):
        self.table = [-1 for _  in range(size)]
        self.member_num = [1 for _ in range(size)]
 
    #representative
    #O(a(N))
    def find(self,x):
        Q = deque()
        while self.table[x] >= 0:
            Q.append(x)
            x = self.table[x]
        while len(Q)>0:
            y = Q.pop()
            self.table[y] = x
        return x

    def union(self,x,y):
        s1 = self.find(x)
        s2 = self.find(y)
        m1=self.member_num[s1]
        m2=self.member_num[s2]
        if s1 != s2:
            if m1 != m2:
                if m1 < m2:
                    self.table[s1] = s2
                    self.member_num[s2]=m1+m2
                    return [m1,m2]
                else:
                    self.table[s2] = s1
                    self.member_num[s1]=m1+m2
                    return [m1,m2]
            else:
                self.table[s1] = -1
                self.table[s2] = s1
                self.member_num[s1] = m1+m2
                return [m1,m2]
        return [0,0]

N,K,C = list(map(int,input().split()))
edges = []
for i in range(K):
    u,v,w,p = list(map(int,input().split()))
    u -= 1
    v -= 1
    edges.append((u,v,w,p))
edges.sort(key=lambda x:x[2])
ans = 0

UF = UnionFind(N)
ret = 0
count = 0
e_list = [[] for i in range(N)]
rest = []
for u,v,w,p in edges:
    if UF.find(u) != UF.find(v):
        UF.union(u,v)
        e_list[u].append((v, w))
        e_list[v].append((u, w))
        ret += w
        count += 1
        ans = max(ans, p)
    else:
        rest.append((u,v,w,p))
if ret > C:
    print(-1)
else:
    par = [-1]*N
    depth = [0]*N
    mn = [10**9]*N
    def dfs(v):
        for v1, w in e_list[v]:
            if v1 != par[v]:
                par[v1] = v
                mn[v1] = w
                depth[v1] = depth[v] + 1                
                dfs(v1)
    dfs(0)
    DP1 = [[0]*N for i in range(20)]
    DP2 = [[0]*N for i in range(20)]
    for i in range(N):
        DP1[0][i] = par[i]
        DP2[0][i] = mn[i]
    for i in range(19):
        for j in range(N):
            DP1[i+1][j] = DP1[i][DP1[i][j]]
            DP2[i+1][j] = min(DP2[i][j], DP2[i][DP1[i][j]])
    def lca_max(u,v):
        ret = 0
        if depth[u] > depth[v]:
            u,v = v,u
        d1 = depth[u]
        d2 = depth[v]
        diff = d2 - d1
        b = 1
        count = 0
        while b < 2*diff:
            if (b&diff) > 0:
                v = DP1[count][v]
                ret = max(DP2[count][v], ret)
            b *= 2
            count += 1
        l = 0
        for i in range(19, -1, -1):
            if DP1[i][u] != DP1[i][v]:
                ret = max(DP2[i][u], DP2[i][v], ret)
                u = DP1[i][u]
                v = DP1[i][v]
        if u != v:
	        ret = max(DP2[0][u], DP2[0][v], ret)
        return ret
    
    for u,v,w,p in rest:
        if ret + w - lca_max(u,v) <= C:
            ans = max(p, ans)
    print(ans)
0