結果

問題 No.2642 Don't cut line!
ユーザー hato336hato336
提出日時 2024-02-19 22:35:42
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,111 ms / 4,000 ms
コード長 4,346 bytes
コンパイル時間 134 ms
コンパイル使用メモリ 81,700 KB
実行使用メモリ 181,952 KB
最終ジャッジ日時 2024-02-20 12:48:26
合計ジャッジ時間 26,026 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 134 ms
88,164 KB
testcase_01 AC 990 ms
180,792 KB
testcase_02 AC 994 ms
180,776 KB
testcase_03 AC 1,021 ms
180,808 KB
testcase_04 AC 1,035 ms
180,636 KB
testcase_05 AC 1,029 ms
181,240 KB
testcase_06 AC 534 ms
113,892 KB
testcase_07 AC 542 ms
114,660 KB
testcase_08 AC 534 ms
114,148 KB
testcase_09 AC 532 ms
114,404 KB
testcase_10 AC 549 ms
114,532 KB
testcase_11 AC 535 ms
114,532 KB
testcase_12 AC 545 ms
114,916 KB
testcase_13 AC 536 ms
114,404 KB
testcase_14 AC 534 ms
114,660 KB
testcase_15 AC 549 ms
114,916 KB
testcase_16 AC 652 ms
138,412 KB
testcase_17 AC 908 ms
169,716 KB
testcase_18 AC 962 ms
172,768 KB
testcase_19 AC 753 ms
153,320 KB
testcase_20 AC 734 ms
125,440 KB
testcase_21 AC 475 ms
113,712 KB
testcase_22 AC 549 ms
124,644 KB
testcase_23 AC 1,111 ms
181,952 KB
testcase_24 AC 705 ms
133,516 KB
testcase_25 AC 716 ms
131,036 KB
testcase_26 AC 711 ms
120,460 KB
testcase_27 AC 755 ms
148,480 KB
testcase_28 AC 1,014 ms
168,316 KB
testcase_29 AC 716 ms
122,868 KB
testcase_30 AC 700 ms
127,204 KB
testcase_31 AC 901 ms
160,772 KB
testcase_32 AC 732 ms
125,540 KB
testcase_33 AC 133 ms
88,164 KB
testcase_34 AC 132 ms
88,164 KB
testcase_35 AC 132 ms
88,164 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