結果
問題 | No.1817 Reversed Edges |
ユーザー |
|
提出日時 | 2022-01-22 00:06:18 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 399 ms / 2,000 ms |
コード長 | 1,783 bytes |
コンパイル時間 | 187 ms |
コンパイル使用メモリ | 82,580 KB |
実行使用メモリ | 184,596 KB |
最終ジャッジ日時 | 2024-11-26 07:10:18 |
合計ジャッジ時間 | 8,104 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 23 |
ソースコード
from collections import defaultdict, deque, Counterfrom heapq import heapify, heappop, heappushimport mathfrom copy import deepcopyfrom itertools import combinations, permutations, product, combinations_with_replacementfrom bisect import bisect_left, bisect_rightimport sysdef input():return sys.stdin.readline().rstrip()def getN():return int(input())def getNM():return map(int, input().split())def getList():return list(map(int, input().split()))def getListGraph():return list(map(lambda x:int(x) - 1, input().split()))def getArray(intn):return [int(input()) for i in range(intn)]mod = 10 ** 9 + 7MOD = 998244353sys.setrecursionlimit(10000000)inf = float('inf')eps = 10 ** (-15)dy = [0, 1, 0, -1]dx = [1, 0, -1, 0]############## Main Code ##############"""ある辺についてx1-1-2-x2x2の方の頂点からスタートする場合はその辺は逆張りになるx2の頂点にそれぞれ+1していけばいいdfsする 子の方が大きかったら+1する累積木dp"""N = getN()E = [[] for i in range(N)]for _ in range(N - 1):a, b = getNM()E[a - 1].append(b - 1)E[b - 1].append(a - 1)fore = [0] * Nback = [0] * Ndef dfs1(u, p):global fore, backfor v in E[u]:if v != p:dfs1(v, u)# 帰りがけif u > v:back[u] += 1back[u] += back[v]dfs1(0, -1)# 親の分 - 自分の分 + (親 < 自分)def dfs2(u, p):global backfor v in E[u]:if v != p:back[v] = back[u] # 引き継ぐ# 行きがけif u < v:back[v] += 1else:back[v] -= 1dfs2(v, u)dfs2(0, -1)for a in back:print(a)