結果

問題 No.1103 Directed Length Sum
ユーザー tyawanmusi
提出日時 2020-07-03 22:11:31
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,467 ms / 3,000 ms
コード長 414 bytes
コンパイル時間 150 ms
コンパイル使用メモリ 81,864 KB
実行使用メモリ 340,108 KB
最終ジャッジ日時 2024-09-17 02:32:45
合計ジャッジ時間 13,481 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 22
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=sys.stdin.readline
n=int(input())
edge=[[]for _ in range(n)]
to=[0]*n
p=[0]*n
for _ in range(n-1):
  a,b=map(int,input().split())
  a-=1
  b-=1
  edge[a].append(b)
  to[b]=1
stack=[to.index(0)]
visited=set(stack)
mod=10**9+7
ans=0
for i in stack:
  for j in edge[i]:
    if j in visited:continue
    visited.add(j)
    stack.append(j)
    p[j]+=p[i]+1
  ans+=p[i]*(p[i]+1)//2
  ans%=mod
print(ans)
0