結果

問題 No.2642 Don't cut line!
ユーザー rikein12rikein12
提出日時 2024-02-19 22:43:52
言語 PyPy3
(7.3.15)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 3,092 bytes
コンパイル時間 134 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 383,064 KB
最終ジャッジ日時 2024-02-20 12:47:55
合計ジャッジ時間 21,487 ms
ジャッジサーバーID
(参考情報)
judge14 / judge16
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 1,004 ms
298,116 KB
testcase_02 AC 870 ms
264,604 KB
testcase_03 AC 845 ms
261,972 KB
testcase_04 AC 913 ms
283,668 KB
testcase_05 AC 1,292 ms
358,796 KB
testcase_06 AC 411 ms
110,040 KB
testcase_07 AC 408 ms
110,680 KB
testcase_08 AC 412 ms
110,168 KB
testcase_09 AC 411 ms
110,296 KB
testcase_10 AC 410 ms
110,680 KB
testcase_11 AC 408 ms
110,168 KB
testcase_12 AC 422 ms
110,808 KB
testcase_13 AC 402 ms
110,168 KB
testcase_14 AC 406 ms
110,424 KB
testcase_15 AC 419 ms
111,192 KB
testcase_16 AC 443 ms
110,168 KB
testcase_17 AC 1,246 ms
383,064 KB
testcase_18 AC 897 ms
278,936 KB
testcase_19 AC 614 ms
203,504 KB
testcase_20 AC 503 ms
131,092 KB
testcase_21 AC 357 ms
105,304 KB
testcase_22 AC 345 ms
101,592 KB
testcase_23 AC 893 ms
257,280 KB
testcase_24 AC 451 ms
111,704 KB
testcase_25 AC 461 ms
111,320 KB
testcase_26 AC 515 ms
110,936 KB
testcase_27 AC 794 ms
266,624 KB
testcase_28 AC 837 ms
240,732 KB
testcase_29 AC 493 ms
110,808 KB
testcase_30 AC 485 ms
111,448 KB
testcase_31 AC 827 ms
247,656 KB
testcase_32 AC 489 ms
111,448 KB
testcase_33 AC 39 ms
55,604 KB
testcase_34 AC 39 ms
55,604 KB
testcase_35 AC 39 ms
55,604 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_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