結果
| 問題 |
No.1207 グラフX
|
| コンテスト | |
| ユーザー |
双六
|
| 提出日時 | 2020-08-31 23:36:04 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 1,809 bytes |
| コンパイル時間 | 395 ms |
| コンパイル使用メモリ | 82,304 KB |
| 実行使用メモリ | 357,236 KB |
| 最終ジャッジ日時 | 2024-11-17 02:49:48 |
| 合計ジャッジ時間 | 39,022 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 43 TLE * 3 |
ソースコード
import sys; input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
from collections import defaultdict
con = 10 ** 9 + 7; INF = float("inf")
def getlist():
return list(map(int, input().split()))
class UnionFind:
def __init__(self, n):
self.par = [i for i in range(n + 1)]
self.rank = [0] * (n + 1)
self.size = [1] * (n + 1)
def find(self, x):
if self.par[x] == x:
return x
else:
self.par[x] = self.find(self.par[x])
return self.par[x]
def same_check(self, x, y):
return self.find(x) == self.find(y)
def union(self, x, y):
x = self.find(x); y = self.find(y)
if self.rank[x] < self.rank[y]:
if self.same_check(x, y) != True:
self.size[y] += self.size[x]
self.size[x] = 0
self.par[x] = y
else:
if self.same_check(x, y) != True:
self.size[x] += self.size[y]
self.size[y] = 0
self.par[y] = x
if self.rank[x] == self.rank[y]:
self.rank[x] += 1
class Graph(object):
def __init__(self):
self.graph = defaultdict(list)
def __len__(self):
return len(self.graph)
def add_edge(self, a, b):
self.graph[a].append(b)
ans = 0
def DFS(N, G, W, visit, node, D, X):
for i in G.graph[node]:
if visit[i] != 1:
visit[i] = 1
DFS(N, G, W, visit, i, D, X)
W[node] += W[i]
a, b = list(sorted([node, i]))
global ans
ans += (N - W[i]) * W[i] * pow(X, D[(10 ** 6) * a + b], con)
ans %= con
#処理内容
def main():
N, M, X = getlist()
UF = UnionFind(N)
D = defaultdict(int)
G = Graph()
for i in range(M):
x, y, z = getlist()
x -= 1; y -= 1
x, y = list(sorted([x, y]))
if not UF.same_check(x, y):
UF.union(x, y)
G.add_edge(x, y)
G.add_edge(y, x)
D[(10 ** 6) * x + y] = z
W = [1] * N
visit = [0] * N
visit[0] = 1
DFS(N, G, W, visit, 0, D, X)
print(ans)
if __name__ == '__main__':
main()
双六