結果

問題 No.2642 Don't cut line!
ユーザー hato336hato336
提出日時 2024-02-19 22:35:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,308 ms / 4,000 ms
コード長 4,346 bytes
コンパイル時間 327 ms
コンパイル使用メモリ 82,648 KB
実行使用メモリ 182,420 KB
最終ジャッジ日時 2024-09-29 03:38:37
合計ジャッジ時間 29,692 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 140 ms
89,380 KB
testcase_01 AC 1,143 ms
181,828 KB
testcase_02 AC 1,145 ms
181,112 KB
testcase_03 AC 1,162 ms
181,288 KB
testcase_04 AC 1,160 ms
181,284 KB
testcase_05 AC 1,174 ms
181,344 KB
testcase_06 AC 595 ms
114,280 KB
testcase_07 AC 611 ms
115,140 KB
testcase_08 AC 608 ms
115,112 KB
testcase_09 AC 602 ms
114,780 KB
testcase_10 AC 609 ms
114,924 KB
testcase_11 AC 614 ms
115,408 KB
testcase_12 AC 614 ms
115,392 KB
testcase_13 AC 604 ms
114,948 KB
testcase_14 AC 616 ms
115,140 KB
testcase_15 AC 622 ms
115,200 KB
testcase_16 AC 758 ms
136,848 KB
testcase_17 AC 1,041 ms
170,080 KB
testcase_18 AC 1,113 ms
174,180 KB
testcase_19 AC 868 ms
153,732 KB
testcase_20 AC 843 ms
125,948 KB
testcase_21 AC 520 ms
113,316 KB
testcase_22 AC 623 ms
124,912 KB
testcase_23 AC 1,308 ms
182,420 KB
testcase_24 AC 800 ms
134,040 KB
testcase_25 AC 815 ms
131,556 KB
testcase_26 AC 832 ms
120,896 KB
testcase_27 AC 889 ms
149,268 KB
testcase_28 AC 1,193 ms
168,800 KB
testcase_29 AC 845 ms
123,400 KB
testcase_30 AC 802 ms
127,492 KB
testcase_31 AC 1,034 ms
161,008 KB
testcase_32 AC 831 ms
126,992 KB
testcase_33 AC 138 ms
89,056 KB
testcase_34 AC 137 ms
88,940 KB
testcase_35 AC 139 ms
89,104 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import collections,sys,math,functools,operator,itertools,bisect,heapq,decimal,string,time,random
class lca():
    def __init__(self,n,edge):
        self.n = n
        self.edge = edge
        self.dist = [10**20] * n
        self.k = math.floor(math.log2(n))
        self.ansestor = [[-1 for i in range(n)] for j in range(self.k + 1)]
        self.mini = [[-10**18 for i in range(n)] for j in range(self.k + 1)]
        self.bfs()
        for i in range(self.k):
            for j in range(n):
               if self.ansestor[i][j] == -1:
                   self.ansestor[i+1][j] = -1
               else:
                   self.ansestor[i+1][j] = self.ansestor[i][self.ansestor[i][j]]
        for i in range(self.k):
            for j in range(n):
                self.mini[i+1][j] = max(self.mini[i+1][j],self.mini[i][j],self.mini[i][self.ansestor[i][j]])
       
    def bfs(self):
        d = collections.deque()
        d.append(0)
        mou = set()
        mou.add(0)
        self.dist[0] = 0
        while d:
            now = d.popleft()
            for i,j in self.edge[now]:
                if i not in mou:
                    self.ansestor[0][i] = now
                    self.mini[0][i] = j
                    d.append(i)
                    mou.add(i)
                    self.dist[i] = self.dist[now] + 1
    
    def ans(self,u,a):
        for i in reversed(range(self.k + 1)):
            if u == -1:
                break
            if (a >> i) & 1:
                u = self.ansestor[i][u]
        return u
    
    def ans2(self,u,a):
        res = -10**18
        for i in reversed(range(self.k + 1)):
            if u == -1:
                break
            if (a >> i) & 1:
                res = max(res,self.mini[i][u])
                u = self.ansestor[i][u]
        return res
    
    def calc(self,s,t):
        if self.dist[s] > self.dist[t]:
            s = self.ans(s,self.dist[s] - self.dist[t])
        if self.dist[s] < self.dist[t]:
            t = self.ans(t,-self.dist[s] + self.dist[t])
        for i in reversed(range(self.k)):
            ns = self.ansestor[i][s]
            nt = self.ansestor[i][t]
            if ns != nt:
                s = ns
                t = nt
        x = (0 if self.ansestor[0][s] == -1 else s if s == t else self.ansestor[0][s])
        return x
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

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

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, 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):
        group_members = collections.defaultdict(list)
        for member in range(self.n):
            group_members[self.find(member)].append(member)
        return group_members

    def __str__(self):
        return ''.join(f'{r}: {m}' for r, m in self.all_group_members().items())
#sys.setrecursionlimit(10**9)
#sys.set_int_max_str_digits(0)
input = sys.stdin.readline
n,k,c = map(int,input().split())
edge = [[] for i in range(n)]
e = []
for i in range(k):
    u,v,w,p = map(int,input().split())
    u-=1
    v-=1
    edge[u].append((v,w))
    edge[v].append((u,w))
    e.append((p,w,u,v))
uf = UnionFind(n)
e.sort(key=lambda x:x[1])
ans = 0
temp = 0
ed = [[] for i in range(n)]
for p,w,u,v in e:
    if uf.same(u,v) == False:
        ans += w 
        uf.union(u,v)
        ed[u].append((v,w))
        ed[v].append((u,w))

if ans > c:
    exit(print(-1))
l = lca(n,ed)
for p,w,u,v in e:
    x = l.calc(u,v)
    if ans - max(l.ans2(u,l.dist[u]-l.dist[x]),l.ans2(v,l.dist[v]-l.dist[x])) + w <= c:
        temp = max(temp,p)
print(temp)
    
0