結果
問題 | No.1817 Reversed Edges |
ユーザー |
|
提出日時 | 2022-01-21 21:27:37 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 260 ms / 2,000 ms |
コード長 | 1,573 bytes |
コンパイル時間 | 439 ms |
コンパイル使用メモリ | 82,340 KB |
実行使用メモリ | 105,536 KB |
最終ジャッジ日時 | 2024-11-25 22:38:33 |
合計ジャッジ時間 | 5,738 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 23 |
ソースコード
import sysinput = lambda : sys.stdin.readline().rstrip()write = lambda x: sys.stdout.write(x+"\n"); writef = lambda x: print("{:.12f}".format(x))debug = lambda x: sys.stderr.write(x+"\n")YES="Yes"; NO="No"; pans = lambda v: print(YES if v else NO)LI = lambda : list(map(int, input().split()))# sys.setrecursionlimit(3*10**5+10)### 木の読み込み yomikomin = int(input())ns = [[] for _ in range(n)]for _ in range(n-1):u,v = map(int, input().split())u -= 1v -= 1ns[u].append(v)ns[v].append(u)# 深さ優先探索 (巻き戻しあり) dfs treeq = [(0, -1)]vs = [0]*nwhile q:u,prv = q.pop()if u<0:# 返るときの処理u = ~ufor v in ns[u]:if v==prv:continuepasselse:q.append((~u,prv))for v in ns[u]:# 進むときの処理if v==prv:continueq.append((v,u))if u>v:vs[0] += 1# 深さ優先探索 (巻き戻しあり) dfs tree# print(vs)q = [(0, -1)]while q:u,prv = q.pop()if u<0:# 返るときの処理u = ~ufor v in ns[u]:if v==prv:continuepasselse:q.append((~u,prv))for v in ns[u]:# 進むときの処理if v==prv:continueq.append((v,u))if u>v:vs[v] = vs[u] - 1else:vs[v] = vs[u] + 1write("\n".join(map(str, vs)))