简体   繁体   中英

How to print MaxPath of graph using dfs O(n)?

We want to find the farthest vertex to the i-th vertex,

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.

您可以使用int result[n] ,然后打印该int result[n]的最大变量。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM