树的直径
作者:互联网
inline void dfs(int u, int f, int & tar)
{
int v;
for(int e = hd[u]; e; e = nt[e])
if((v = to[e]) ^ f)
{
dis[v] = dis[u] + w[e];
if(dis[v] > dis[tar]) tar = v;
dfs(v, u, tar);
}
}
主函数里:
dfs(1, 0, p);
dis[p] = 0;
dfs(p, 0, q);
标签:tar,int,void,dfs,inline,直径,dis 来源: https://www.cnblogs.com/fakeryu/p/16410726.html