提出 #6370852


ソースコード 拡げる

import sys
input = sys.stdin.readline

MOD = 10 ** 9 + 7

N,M = map(int,input().split())
X = int(input())
UVW = [[int(x) for x in input().split()] for _ in range(M)]
UVW.sort(key = lambda x: x[2])

root = list(range(N+1))
def find_root(x):
    y = root[x]
    if x == y:
        return x
    z = find_root(y)
    root[x] = z
    return z

# min spanning tree
tree = [set() for _ in range(N+1)]
min_tree_size = 0
for u,v,w in UVW:
    ru = find_root(u)
    rv = find_root(v)
    if ru == rv:
        continue
    root[ru] = rv
    min_tree_size += w
    tree[u].add((v,w))
    tree[v].add((u,w))

# treeにおける2頂点の間の辺の最大値を求めたい
# LCA。2^n個手前およびそこまでの最大値を覚える

parent = [[0] * 12 for _ in range(N+1)]
max_wt = [[0] * 12 for _ in range(N+1)]
depth = [0] * (N+1)

def dfs(v=1,p=0,dep=0,w=0):
    parent[v][0] = p
    depth[v] = dep
    max_wt[v][0] = w
    for n in range(1,11):
        parent[v][n] = parent[parent[v][n-1]][n-1]
        max_wt[v][n] = max(max_wt[v][n-1], max_wt[parent[v][n-1]][n-1])
    for u,w in tree[v]:
        if u == p:
            continue
        dfs(u,v,dep+1,w)

dfs()

def max_wt_between(x,y):
    # LCA しながら重みの最大値を得る
    wt = 0
    dx,dy = depth[x], depth[y]
    if dx > dy:
        x,y = y,x
        dx,dy = dy,dx
    while dy > dx:
        diff = dy - dx
        step = diff & (-diff)
        n = step.bit_length() - 1
        wt = max(wt, max_wt[y][n])
        y = parent[y][n]
        dy -= step
    if x == y:
        return wt
    step = 1 << 11
    while step:
        n = step.bit_length() - 1
        rx,ry = parent[x][n], parent[y][n]
        if rx != ry:
            wt = max(wt, max_wt[x][n], max_wt[y][n])
            x,y = rx,ry
        step >>= 1
    return max(wt, max_wt[x][0], max_wt[y][0])

# 各edgeに対して、その辺を含む最小の全域木の大きさを求める
min_size = []
for u,v,w in UVW:
    if (v,w) in tree[u]:
        min_size.append(min_tree_size)
    else:
        x = max_wt_between(u,v)
        min_size.append(min_tree_size + w - x)

sm = sum(1 if s < X else 0 for s in min_size)
eq = sum(1 if s == X else 0 for s in min_size)
gr = sum(1 if s > X else 0 for s in min_size)

if eq == 0:
    answer = 0
elif sm == 0:
    # eq 内の辺が完全同色でなければよい
    answer = (pow(2,eq,MOD) - 2) * pow(2,gr,MOD) % MOD
else:
    # sm 内が完全同色でなければならない。
    # eq 内は、smの色と異なる色を持っていれば何でもよい
    answer = 2 * (pow(2,eq,MOD) - 1) * pow(2,gr,MOD) % MOD

print(answer)

提出情報

提出日時
問題 E - Bichrome Spanning Tree
ユーザ maspy
言語 Python (3.4.3)
得点 900
コード長 2715 Byte
結果 AC
実行時間 41 ms
メモリ 5108 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 900 / 900
結果
AC × 4
AC × 52
セット名 テストケース
Sample sample-01.txt, sample-02.txt, sample-03.txt, sample-04.txt
All 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, 20.txt, 21.txt, 22.txt, 23.txt, 24.txt, 25.txt, 26.txt, 27.txt, 28.txt, 29.txt, 30.txt, 31.txt, 32.txt, 33.txt, 34.txt, 35.txt, 36.txt, 37.txt, 38.txt, 39.txt, 40.txt, 41.txt, 42.txt, 43.txt, 44.txt, 45.txt, 46.txt, 47.txt, 48.txt, sample-01.txt, sample-02.txt, sample-03.txt, sample-04.txt
ケース名 結果 実行時間 メモリ
01.txt AC 39 ms 4212 KiB
02.txt AC 39 ms 4212 KiB
03.txt AC 38 ms 4340 KiB
04.txt AC 39 ms 4724 KiB
05.txt AC 40 ms 4852 KiB
06.txt AC 40 ms 4212 KiB
07.txt AC 40 ms 4596 KiB
08.txt AC 40 ms 4212 KiB
09.txt AC 40 ms 4724 KiB
10.txt AC 40 ms 4852 KiB
11.txt AC 38 ms 4212 KiB
12.txt AC 31 ms 4340 KiB
13.txt AC 31 ms 4596 KiB
14.txt AC 31 ms 3956 KiB
15.txt AC 38 ms 4212 KiB
16.txt AC 38 ms 4212 KiB
17.txt AC 40 ms 4212 KiB
18.txt AC 40 ms 4212 KiB
19.txt AC 39 ms 5108 KiB
20.txt AC 40 ms 4596 KiB
21.txt AC 40 ms 4212 KiB
22.txt AC 40 ms 4852 KiB
23.txt AC 40 ms 4596 KiB
24.txt AC 31 ms 4340 KiB
25.txt AC 31 ms 4588 KiB
26.txt AC 38 ms 4212 KiB
27.txt AC 40 ms 4212 KiB
28.txt AC 40 ms 4340 KiB
29.txt AC 41 ms 4340 KiB
30.txt AC 40 ms 4212 KiB
31.txt AC 40 ms 4468 KiB
32.txt AC 40 ms 4212 KiB
33.txt AC 39 ms 4084 KiB
34.txt AC 38 ms 4212 KiB
35.txt AC 32 ms 3444 KiB
36.txt AC 33 ms 3444 KiB
37.txt AC 33 ms 3444 KiB
38.txt AC 40 ms 4212 KiB
39.txt AC 40 ms 4340 KiB
40.txt AC 41 ms 4468 KiB
41.txt AC 39 ms 4212 KiB
42.txt AC 32 ms 3956 KiB
43.txt AC 32 ms 4084 KiB
44.txt AC 31 ms 4596 KiB
45.txt AC 33 ms 4340 KiB
46.txt AC 36 ms 4340 KiB
47.txt AC 31 ms 4340 KiB
48.txt AC 31 ms 4084 KiB
sample-01.txt AC 18 ms 3316 KiB
sample-02.txt AC 18 ms 3316 KiB
sample-03.txt AC 18 ms 3316 KiB
sample-04.txt AC 18 ms 3316 KiB