結果

問題 No.1507 Road Blocked
ユーザー marroncastlemarroncastle
提出日時 2021-05-14 22:49:48
言語 PyPy3
(7.3.8)
結果
AC  
実行時間 517 ms / 2,000 ms
コード長 1,723 bytes
コンパイル時間 319 ms
使用メモリ 169,184 KB
最終ジャッジ日時 2022-11-25 20:33:37
合計ジャッジ時間 16,594 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
使用メモリ
testcase_00 AC 91 ms
76,416 KB
testcase_01 AC 88 ms
76,720 KB
testcase_02 AC 90 ms
76,572 KB
testcase_03 AC 258 ms
167,496 KB
testcase_04 AC 464 ms
168,820 KB
testcase_05 AC 456 ms
168,668 KB
testcase_06 AC 459 ms
168,612 KB
testcase_07 AC 467 ms
168,560 KB
testcase_08 AC 465 ms
169,080 KB
testcase_09 AC 476 ms
168,724 KB
testcase_10 AC 471 ms
168,708 KB
testcase_11 AC 479 ms
168,552 KB
testcase_12 AC 470 ms
168,992 KB
testcase_13 AC 476 ms
168,736 KB
testcase_14 AC 471 ms
169,184 KB
testcase_15 AC 493 ms
169,036 KB
testcase_16 AC 517 ms
168,720 KB
testcase_17 AC 493 ms
168,612 KB
testcase_18 AC 480 ms
167,116 KB
testcase_19 AC 477 ms
168,892 KB
testcase_20 AC 468 ms
168,740 KB
testcase_21 AC 474 ms
169,092 KB
testcase_22 AC 472 ms
168,596 KB
testcase_23 AC 490 ms
168,984 KB
testcase_24 AC 472 ms
168,828 KB
testcase_25 AC 466 ms
168,792 KB
testcase_26 AC 491 ms
168,708 KB
testcase_27 AC 488 ms
165,888 KB
testcase_28 AC 485 ms
168,544 KB
testcase_29 AC 502 ms
168,704 KB
testcase_30 AC 484 ms
168,540 KB
testcase_31 AC 474 ms
168,800 KB
testcase_32 AC 485 ms
169,056 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