結果

問題 No.2427 Tree Distance Two
ユーザー bug maker / ばぐめいかー@Python学習者bug maker / ばぐめいかー@Python学習者
提出日時 2023-08-18 22:08:14
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 918 bytes
コンパイル時間 198 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 68,608 KB
最終ジャッジ日時 2024-05-06 04:12:18
合計ジャッジ時間 4,272 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 26 ms
16,000 KB
testcase_01 AC 25 ms
10,624 KB
testcase_02 AC 27 ms
10,624 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = lambda :sys.stdin.readline()[:-1]

from collections import deque, Counter, defaultdict

def main():
    N = int(input())

    graph = [[] for _ in range(N)]
    for _ in range(N-1):
        a, b = map(lambda x: int(x) - 1, input().split())
        graph[a].append(b)
        graph[b].append(a)  # 有向グラフなら消す
    # print(graph) 

    def bfs(s):
        d = deque()
        d.append(s)
    
        dist = [-1] * N
        dist[s] = 0
    
        while d:
            v = d.popleft()
            for i in graph[v]:
                if dist[i] != -1:
                    continue
                dist[i] = dist[v] + 1
                if dist[i] >= 3:
                    break
                d.append(i)
            else:
                continue
            break
    
        return dist.count(2)

    for i in range(N):
        print(bfs(i))

if __name__ == '__main__':
    main()
0