結果

問題 No.2642 Don't cut line!
ユーザー rikein12rikein12
提出日時 2024-02-19 22:43:14
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 2,997 bytes
コンパイル時間 139 ms
コンパイル使用メモリ 82,340 KB
実行使用メモリ 116,916 KB
最終ジャッジ日時 2024-09-29 02:34:10
合計ジャッジ時間 18,324 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 AC 497 ms
113,060 KB
testcase_07 AC 503 ms
111,988 KB
testcase_08 AC 519 ms
113,196 KB
testcase_09 AC 502 ms
111,472 KB
testcase_10 AC 512 ms
112,116 KB
testcase_11 AC 526 ms
113,292 KB
testcase_12 AC 507 ms
112,368 KB
testcase_13 AC 515 ms
113,296 KB
testcase_14 AC 497 ms
111,596 KB
testcase_15 AC 529 ms
112,116 KB
testcase_16 AC 575 ms
111,440 KB
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 AC 445 ms
106,860 KB
testcase_22 AC 436 ms
102,592 KB
testcase_23 RE -
testcase_24 AC 522 ms
112,740 KB
testcase_25 AC 546 ms
112,276 KB
testcase_26 AC 611 ms
112,056 KB
testcase_27 RE -
testcase_28 RE -
testcase_29 AC 594 ms
111,524 KB
testcase_30 AC 559 ms
111,968 KB
testcase_31 RE -
testcase_32 AC 567 ms
111,740 KB
testcase_33 AC 42 ms
55,416 KB
testcase_34 AC 41 ms
55,344 KB
testcase_35 AC 42 ms
55,652 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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_min(u,v):
        ret = 10**9
        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 = min(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 = min(DP2[i][u], DP2[i][v], ret)
                u = DP1[i][u]
                v = DP1[i][v]
        ret = min(DP2[0][u], DP2[0][v], ret)
        return ret
    
    for u,v,w,p in rest:
        if ret + w - lca_min(u,v) <= C:
            ans = max(p, ans)
    print(ans)
0