Tree and Distance | Practice | GeeksforGeeks

Given a tree with N nodes rooted at 1, the task is to find the maximum distance of node X from any other node

Note: All the nodes are numbered from 1 to N. 
Input: 
1. The first line of the input contains a single integer  T denoting the number of test cases. The description of T test cases follows.
2. The first line of each test case contains two space-separated single integers N and X.
3. Next N-1 lines contain two space-separated integers u and v, represents an edge in between them

Output: For each test case, print the answer

Constraints:
1. 1 <= T <= 5
2. 1 <= X <= N <= 105


Example:
Input:
2
4 2
1 2
3 4
4 1
5 3
1 2
2 3
3 4
4 5
Output:
3
2
0
chaitanyasai3213 months ago
#include <bits/stdc++.h>using namespace std;
int main() {int t;cin>>t;while(t--){    int n,x;    cin>>n>>x;    vector<int> adj[n+1];    for(int i=0;i<n-1;i++)    {        int u,v;        cin>>u>>v;        adj[u].push_back(v);        adj[v].push_back(u);    }    queue<pair<int,int> >q;    q.push({x,0});    int ma=0,vis[n+1]={0};    while(!q.empty())    {        auto get=q.front();        int child=get.first;        int cou=get.second;        q.pop();        vis[child]=1;        ma=max(ma,cou);        for(auto it:adj[child])        {            if(!vis[it])            {                q.push({it,cou+1});            }        }    }    cout<<ma<<"\n";    }}
0
neznajko6 months ago
Yeeh! 0.2/2.2
////////////////////////////////////////////////////////
#include <iostream>
#include <vector>
#include <algorithm> // max
using namespace std;
////////////////////////////////////////////////////////
class solutn {
private:
    int d = -1; // distance
    int m = 0;  // max distance
    const vector<vector<int>>& g; // adjacency list
    void xp( int v, int u); // explore v <- u
public:
    solutn( const vector<vector<int>>& g): g( g) {}
    int maxdist( int x);
};
void solutn::xp( int v, int u)
{ // explore v comming from u
    d++; // previsit
    if( g[ v].size() == 1 and u > 0) {
        m = max( m, d); // update m if v is leaf node
    } else {
        for( int w: g[ v]) {
            if( w != u) xp( w, v);
        }
    }
    d--; // postvisit
}
int solutn::maxdist( int x)
{
    xp( x, 0);
    return m;
}
////////////////////////////////////////////////////////
int main()
{
    int tc;
    std::cin >> tc;
    while( tc--) {
        int n, x;
        std::cin >> n >> x;
        vector<vector<int>> g( n + 1);
        for( int j = 0; j < n - 1; ++j) {
            int u, v;
            std::cin >> u >> v;
            g[ u].push_back( v);
            g[ v].push_back( u);
        }
        solutn s( g);
        cout << s.maxdist( x) << endl;
    }
}
////////////////////////////////////////////////////////
// log:
////////////////////////////////////////////////////////
0
imranwahid6 months ago
Easy C++ solution using DFS
https://ide.geeksforgeeks.org/EfZctZjVNj
#include<bits/stdc++.h>
using namespace std;
// function for dfs traversal
void dfs(unordered_map<int,vector<int>>&m,int curr,vector<bool>&vis,int dis,int &ans)
{
    vis[curr]=true;
    // take the maximum of the current distance and the ans encountered so far
    ans=max(ans,dis);
    for(auto it:m[curr])
    {
        if(!vis[it])
        {
            dfs(m,it,vis,dis+1,ans);
        }
    }
}
int maxDistance(unordered_map<int,vector<int>>&m,int n,int x)
{
    vector<bool>vis(n+1,false);
    int ans=0;
    dfs(m,x,vis,0,ans);
    return ans;
}
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	    int n,x;
	    unordered_map<int,vector<int>>m;
	    cin>>n>>x;
	    for(int i=0;i<n-1;i++)
	    {
	        int u,v;
	        cin>>u>>v;
	        m[u].push_back(v);
	        m[v].push_back(u);
	    }
	    cout<<maxDistance(m,n,x)<<endl;
	}
	return 0;
}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.