結果

問題 No.1817 Reversed Edges
ユーザー shotoyooshotoyoo
提出日時 2022-01-21 21:27:37
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 333 ms / 2,000 ms
コード長 1,573 bytes
コンパイル時間 462 ms
コンパイル使用メモリ 87,180 KB
実行使用メモリ 107,708 KB
最終ジャッジ日時 2023-08-17 04:57:11
合計ジャッジ時間 7,750 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 73 ms
71,400 KB
testcase_01 AC 73 ms
71,460 KB
testcase_02 AC 74 ms
71,472 KB
testcase_03 AC 75 ms
71,320 KB
testcase_04 AC 75 ms
71,400 KB
testcase_05 AC 77 ms
71,236 KB
testcase_06 AC 75 ms
71,396 KB
testcase_07 AC 333 ms
90,808 KB
testcase_08 AC 199 ms
84,064 KB
testcase_09 AC 285 ms
90,000 KB
testcase_10 AC 207 ms
84,904 KB
testcase_11 AC 237 ms
86,820 KB
testcase_12 AC 326 ms
92,360 KB
testcase_13 AC 321 ms
92,528 KB
testcase_14 AC 327 ms
92,448 KB
testcase_15 AC 314 ms
92,292 KB
testcase_16 AC 331 ms
92,524 KB
testcase_17 AC 318 ms
92,740 KB
testcase_18 AC 329 ms
92,328 KB
testcase_19 AC 327 ms
92,408 KB
testcase_20 AC 326 ms
92,368 KB
testcase_21 AC 321 ms
92,408 KB
testcase_22 AC 184 ms
107,708 KB
testcase_23 AC 178 ms
100,852 KB
testcase_24 AC 184 ms
98,464 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = 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)

### 木の読み込み yomikomi
n = int(input())
ns = [[] for _ in range(n)]
for _ in range(n-1):
    u,v = map(int, input().split())
    u -= 1
    v -= 1
    ns[u].append(v)
    ns[v].append(u)
    
# 深さ優先探索 (巻き戻しあり) dfs tree
q = [(0, -1)]
vs = [0]*n
while q:
    u,prv = q.pop()
    if u<0:
        # 返るときの処理
        u = ~u
        for v in ns[u]:
            if v==prv:
                continue
            pass
    else:
        q.append((~u,prv))
        for v in ns[u]:
            # 進むときの処理
            if v==prv:
                continue
            q.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 = ~u
        for v in ns[u]:
            if v==prv:
                continue
            pass
    else:
        q.append((~u,prv))
        for v in ns[u]:
            # 進むときの処理
            if v==prv:
                continue
            q.append((v,u))
            if u>v:
                vs[v] = vs[u] - 1
            else:
                vs[v] = vs[u] + 1
write("\n".join(map(str, vs)))
0