結果

問題 No.1507 Road Blocked
ユーザー marroncastlemarroncastle
提出日時 2021-05-14 22:49:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 503 ms / 2,000 ms
コード長 1,723 bytes
コンパイル時間 211 ms
コンパイル使用メモリ 82,464 KB
実行使用メモリ 162,880 KB
最終ジャッジ日時 2024-04-10 01:57:40
合計ジャッジ時間 15,673 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
54,464 KB
testcase_01 AC 42 ms
55,524 KB
testcase_02 AC 39 ms
54,204 KB
testcase_03 AC 234 ms
162,576 KB
testcase_04 AC 489 ms
162,556 KB
testcase_05 AC 456 ms
162,156 KB
testcase_06 AC 453 ms
159,184 KB
testcase_07 AC 466 ms
161,640 KB
testcase_08 AC 459 ms
162,308 KB
testcase_09 AC 471 ms
162,660 KB
testcase_10 AC 450 ms
162,384 KB
testcase_11 AC 461 ms
161,560 KB
testcase_12 AC 470 ms
161,656 KB
testcase_13 AC 472 ms
161,872 KB
testcase_14 AC 472 ms
161,904 KB
testcase_15 AC 477 ms
162,372 KB
testcase_16 AC 461 ms
162,400 KB
testcase_17 AC 475 ms
161,680 KB
testcase_18 AC 485 ms
162,880 KB
testcase_19 AC 496 ms
162,520 KB
testcase_20 AC 491 ms
161,908 KB
testcase_21 AC 503 ms
162,124 KB
testcase_22 AC 484 ms
162,556 KB
testcase_23 AC 487 ms
161,752 KB
testcase_24 AC 469 ms
161,636 KB
testcase_25 AC 480 ms
162,184 KB
testcase_26 AC 464 ms
161,936 KB
testcase_27 AC 472 ms
162,408 KB
testcase_28 AC 471 ms
162,268 KB
testcase_29 AC 492 ms
162,448 KB
testcase_30 AC 460 ms
161,888 KB
testcase_31 AC 472 ms
162,140 KB
testcase_32 AC 486 ms
161,892 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
from collections import deque
class Tree:
  def __init__(self, N):
    self.V = N
    self.edge2 = [[] for _ in range(N)]
    self.edge = [deque([]) for _ in range(N)]
    self.order = []
  
  def add_edges(self, ind=1, bi=True):
    for i in range(self.V-1):
      a,b = map(int, input().split())
      a -= ind; b -= ind
      self.edge[a].append(b)
      self.edge2[a].append(b)
      if bi:
        self.edge[b].append(a)
        self.edge2[b].append(a)

  def add_edge(self, a, b, bi=True):
    self.edge[a].append(b)
    if bi: self.edge[b].append(a)

  def dp(self, start):
    stack = deque([start])
    self.parent = [self.V]*self.V; self.parent[start] = -1
    self.order.append(start)
    #記録したい値の配列を定義
    self.dp = [1]*self.V
    while stack:
      v = stack[-1]
      while len(self.edge[v]):
        u = self.edge[v].popleft()
        if u==self.parent[v]: continue
        self.parent[u]=v
        stack.append(u); self.order.append(u)
        break
      else:
        stack.pop()
    for v in self.order[::-1]:
      for u in self.edge2[v]:
        if u==self.parent[v]: continue
        self.dp[v] += self.dp[u] #帰りがけ処理
  
  def bfs(self, start):
    d = deque([start])
    self.min_cost = [-1]*self.V; self.min_cost[start]=0
    while len(d)>0:
      v = d.popleft()
      for w in self.edge[v]:
        if self.min_cost[w]==-1:
          self.min_cost[w]=self.min_cost[v]+1
          d.append(w)
    return

N = int(input())
G = Tree(N)
G.add_edges(ind=1, bi=True)
G.dp(0)
ans = 0
MOD = 998244353
for i in range(1,N):
  num = G.dp[i]
  ans += num*(num-1) + (N-num)*(N-num-1)
ans *= pow(N*(N-1)*(N-1), MOD-2, MOD)
print(ans%MOD)
0