結果
問題 | No.1227 I hate ThREE |
ユーザー | chineristAC |
提出日時 | 2020-09-11 22:57:33 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 162 ms / 2,000 ms |
コード長 | 2,846 bytes |
コンパイル時間 | 346 ms |
コンパイル使用メモリ | 82,080 KB |
実行使用メモリ | 102,948 KB |
最終ジャッジ日時 | 2025-01-02 05:44:33 |
合計ジャッジ時間 | 4,216 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 33 |
ソースコード
import sys input = sys.stdin.readline sys.setrecursionlimit(2*10**5) mod = 10**9+7 N,C = map(int,input().split()) edge = [[] for i in range(N)] for i in range(N-1): a,b = map(int,input().split()) edge[a-1].append(b-1) edge[b-1].append(a-1) depth = [-1 for i in range(N)] parent = [-1 for i in range(N)] size = [1 for i in range(N)] depth[0] = 0 def dfs(v,pv): for nv in edge[v]: if nv!=pv: parent[nv] = v depth[nv] = depth[v] + 1 dfs(nv,v) size[v] += size[nv] dfs(0,-1) D = max(depth) def solve(M): if M<=2*D+1: dp = [[1 for i in range(M)] for j in range(N)] def dp_dfs(v,pv): for nv in edge[v]: if nv!=pv: dp_dfs(nv,v) for i in range(M): tmp = 0 if i!=0: tmp += dp[nv][i-1] if i!=M-1: tmp += dp[nv][i+1] tmp %= mod dp[v][i] *= tmp dp[v][i] %= mod dp_dfs(0,-1) res = 0 for i in range(M): res += dp[0][i] res %= mod return res else: dp_low = [[1 for i in range(D+1)] for j in range(N)] dp_high = [[1 for i in range(D+1)] for j in range(N)] def dp_dfs(v,pv): for nv in edge[v]: if nv!=pv: dp_dfs(nv,v) for i in range(D+1): tmp = 0 if i!=0: tmp += dp_low[nv][i-1] if i!=D: tmp += dp_low[nv][i+1] else: tmp += pow(2,size[nv]-1,mod) tmp %= mod dp_low[v][i] *= tmp dp_low[v][i] %= mod tmp = 0 if i!=0: tmp += dp_high[nv][i-1] if i!=D: tmp += dp_high[nv][i+1] else: tmp += pow(2,size[nv]-1,mod) tmp %= mod dp_high[v][i] *= tmp dp_high[v][i] %= mod dp_dfs(0,-1) res = 0 for i in range(D+1): res += dp_low[0][i] res %= mod res += dp_high[0][i] res %= mod tmp = pow(2,N-1,mod) * (M-2*D-2) %mod res += tmp res %= mod return res ans = 0 c = C//3 if C%3==1: ans += solve(c+1) + 2*solve(c) ans %= mod elif C%3==2: ans += 2*solve(c+1) + solve(c) ans %= mod else: ans = 3*solve(c) ans %= mod print(ans)