首页 > 其他分享 >图的遍历

图的遍历

时间:2023-01-31 13:16:39浏览次数:36  
标签:nxt 遍历 int dfs add go hd

n=0
N=10000
all=0
go=[0]*N
hd=[0]*N
nxt=[0]*N

def add_(x,y):
    global all
    all+=1
    nxt[all]=hd[int(x)]
    go[all]=y
    hd[x]=all

def dfs(x):
    print("%d "%(x))
    i=hd[x]
    while(i):
        dfs(go[i])
        i=nxt[i]

n=input()
for i in range(int(n)-1):
    x,y= map(int, input().strip().split())
    add_(x,y)

dfs(1)

 

标签:nxt,遍历,int,dfs,add,go,hd
From: https://www.cnblogs.com/towboa/p/17078627.html

相关文章