[英]How to print MaxPath of graph using dfs O(n)?
We want to find the farthest vertex to the i-th vertex, 我们想要找到最远的顶点到第i个顶点,
farthest vertex to the current vertex of graph, gives the maximum Path to us. 到图的当前顶点的最远顶点,为我们提供了最大路径。
Please help me to get this right: 请帮助我解决这个问题:
vector<int> v[100];
bool mark[100];
int v1;
inline int max_path(int k)
{
int result = -1;
mark[k] = true;
for(int i=0; i<v[k].size(); i++)
if(!mark[v[k][i]])
{
int x = max_path(v[k][i]);
if(x > result)
{
result = x;
v1 = v[k][i];
}
}
return result+1;
}
v1 must be the farthest vertex to the current vertex (k), and result must be the length of the path. v1必须是距离当前顶点(k)最远的顶点,并且结果必须是路径的长度。
您可以使用int result[n]
,然后打印该int result[n]
的最大变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.