简体   繁体   English

在无向树中找到最长的路径

[英]Finding longest path in an undirected tree

I was trying to find the longest path in an undirected tree ( http://www.spoj.com/problems/PT07Z/ ) and made the following code. 我试图在无向树( http://www.spoj.com/problems/PT07Z/ )中找到最长的路径,并编写了以下代码。

#include <iostream>
#include <vector>

using namespace std;

vector< vector<int> > graph;
int q=0, m = -1;                                // m is for largest path and q is one of the end vertex of that path

void dfs(int i, int count, vector<bool>& v)
{
    v[i] = true;
    for(int j=0;j<graph[i].size();j++)
    {
        if(!v[graph[i][j]])
        {
            count++;                            // count is the length of the path from the starting vertex to current vertex
            dfs(graph[i][j], count, v);
        }
    }
    if(count>m)
    {
        m= count;
        q=i;
    }
    count--;                                    // decreasing count since the method return to its calling function
}


int main()
{
    int n, x, i, y;
    cin>>n;
    graph = vector< vector<int> >(n);
    vector<bool> visited(n);
    vector<bool> v(n);
    for(i=0;i<n-1;i++)
    {
        cin>>x>>y;
        x--, y--;
        graph[x].push_back(y);
        graph[y].push_back(x);
    }
    dfs(0, 0, visited);
    m=-1;
    cout<<q<<endl;
    dfs(q, 0, v);
    cout<<q<<endl;
    cout<<m<<endl;
}

Can anybody tell me what is wrong here because I am getting wrong value of maximum length of the path (m) though the end vertices of the longest path is correct (atleast on the test cases that i have tried). 谁能告诉我这是什么问题,因为尽管最长路径的最终顶点正确(我尝试过的测试用例至少),但我得到的最大路径长度(m)值错误。 I have tried to implement the following algorithm here: 我试图在这里实现以下算法:

Algorithm: 算法:

  1. Run DFS from any node to find the farthest leaf node. 从任何节点运行DFS以查找最远的叶节点。 Label that node T. 标记该节点T。
  2. Run another DFS to find the farthest node from T. 运行另一个DFS以查找距T最远的节点。
  3. The path you found in step 2 is the longest path in the tree. 您在步骤2中找到的路径是树中的最长路径。

Some Test Cases: First: 一些测试用例:首先:

17
1 2
1 3
2 4
2 5
3 6
3 7
6 8
6 9
8 10
9 11
7 12
7 13
13 14
13 15
15 16
15 17

Correct Answer for this test case is 7. 该测试用例的正确答案是7。

Second: 第二:

7
1 2
1 3
2 4
2 5
3 6
3 7

Correct Answer for this test case is 4. 该测试用例的正确答案是4。

One problem according to me is that you should decrement count immediately on return from the called recursive function. 根据我的一个问题是,您应该在从调用的递归函数返回时立即减少计数。

    for(int j=0;j<graph[i].size();j++)
    {
        if(!v[graph[i][j]])
        {
            count++;                           
            dfs(graph[i][j], count, v);
            count--;
        }

Or simply: 或者简单地:

    for(int j=0;j<graph[i].size();j++)
    {
        if(!v[graph[i][j]])           
            dfs(graph[i][j], count+1, v);

This is because the count should not keep incrementing for each neighbour of graph[i] . 这是因为对于graph[i]每个邻居,计数不应保持递增。

Just remove that count++ from 'for' loop and remove that count--; 只需从“ for”循环中删除该count ++,然后删除该count--;

And keep 'count++' at the start of fucntion. 并且在功能开始时保持“ count ++”。

void dfs(int i, int count, vector<bool>& v)
{
    count++;
    v[i] = true;
    for(int j=0;j<graph[i].size();j++)
    {
       if(!v[graph[i][j]])
        {
            dfs(graph[i][j], count, v);
        }
    }
    if(count>m)
    {
        m= count;
        q=i;
    } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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