Can you find any runtime error in this code..plz..kpth query problm

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
#define ll long long
vector<ll> vec[N];
ll up[N][19];
vector<bool> vis(N,false);
ll depth[N];

void dfs(ll node,ll level,ll par)
{
    vis[node]=true;
    up[node][0]=par;
    depth[node]=level;
    for(auto it:vec[node])
    {
        if(!vis[it])
        {
            dfs(it,level+1,node);
        }
    }
}
ll lca(ll a,ll b)
{
    if(depth[a]<depth[b])
    {
        swap(a,b);
    }
    ll k=depth[a]-depth[b];
    for(int i=18;i>=0;i--)
    {
        if(k&(1<<i))
        {
            a=up[a][i];
        }
    }
    if(a==b)
        return a;
    for(int i=18;i>=0;i--)
    {
        if(up[a][i]!=up[b][i] && up[a][i]!=-1)
        {
            a=up[a][i];
            b=up[b][i];
        }
    }
    return up[a][0];
}
ll dist(ll a,ll b)
{   ll k=lca(a,b);
    return (depth[a]+depth[b]-2*depth[k]);
}

int main()
{
    #ifndef ONLINE_JUDGE
        freopen("input.txt","r",stdin);
        freopen("output.txt","w",stdout);
    #endif
ios_base::sync_with_stdio(false); cin.tie(0);cout.tie(0);
ll t;
cin>>t;
while(t--)
{
    ll n;
    cin>>n;
    for(int i=1;i<n;i++)
    {
        ll u,v;
        cin>>u>>v;
        vec[u].push_back(v);
        vec[v].push_back(u);
    }
    memset(up,-1,sizeof(up));
    dfs(1,0,-1);
    for(int i=1;i<n;i++)
    {
        for(int j=18;j>=1;j--)
        {
            if(up[i][j-1]!=-1)
            {
                up[i][j]=up[up[i][j-1]][j-1];
            }
        }
    }
    ll q;
    cin>>q;
    while(q--)
    {
        ll k;
        cin>>k;
        ll arr[k];
        for(auto &x:arr)
        {
            cin>>x;
        }
        ll node=arr[0];
        ll node1;
        ll d=-1;
        for(int i=0;i<k;i++)
        {
            if(dist(node,arr[i])>d)
            {
                d=dist(node,arr[i]);
                node1=arr[i];
            }
        }
        d=-1;
        ll node2;
        for(int i=0;i<k;i++)
        {
            if(dist(node1,arr[i])>d)
            {
                d=dist(node1,arr[i]);
                node2=arr[i];
            }
        }
         d=dist(node1,node2);
        bool ans=true;
        for(int i=0;i<k;i++)
        {
            if(dist(node1,arr[i])+dist(node2,arr[i])!=d)
            {
                ans=false;
                break;
            }
        }
        if(ans)
            cout<<"yes"<<endl;
        else
            cout<<"no"<<endl;
    }

}
return 0;
}

Please either format your code or (better!) link to your submission - the forum software has mangled it and it won’t compile! :slight_smile:

is it ok now…

Better, thanks, but missing the #includes.

You should address the final two warnings before continuing:

simon@simon-laptop][16:35:45]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling imran_02-KPATHQRY.cpp
+ g++ -std=c++14 imran_02-KPATHQRY.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv -fno-sanitize-recover
imran_02-KPATHQRY.cpp: In function ‘int main()’:
imran_02-KPATHQRY.cpp:104:23: warning: conversion to ‘int’ from ‘long long int’ may alter its value [-Wconversion]
                 d=dist(node,arr[i]);
                   ~~~~^~~~~~~~~~~~~
imran_02-KPATHQRY.cpp:114:23: warning: conversion to ‘int’ from ‘long long int’ may alter its value [-Wconversion]
                 d=dist(node1,arr[i]);
                   ~~~~^~~~~~~~~~~~~~
imran_02-KPATHQRY.cpp:118:16: warning: conversion to ‘int’ from ‘long long int’ may alter its value [-Wconversion]
          d=dist(node1,node2);
            ~~~~^~~~~~~~~~~~~
imran_02-KPATHQRY.cpp:109:13: warning: ‘node2’ may be used uninitialized in this function [-Wmaybe-uninitialized]
         int node2;
             ^~~~~
imran_02-KPATHQRY.cpp:98:13: warning: ‘node1’ may be used uninitialized in this function [-Wmaybe-uninitialized]
         int node1;

give it a try now…hope it works…

He had already mentioned.

fed up fixing this code…plz give me the correct code after fixing it…

It’s the “may be used uninitialized” warnings that are important. They might be false alarms, but definitely worth addressing.

the code is running fine on my local machine but don’t know what happens when i submit…it doesn’t even pass 1 test case.
problem link
https://www.codechef.com/JULY21C/problems/KPATHQRY