Lightoj 1094 - Farthest Nodes in a Tree

/*
Lionel Messi is such a player that you may catch him, you may touch him, you may feel him
and definitely you may Love him.
Lionel Messi is Messi. A little Magician in this World.

*/

// Algorithm: DFS
// Accepted

#include<bits/stdc++.h>

using namespace std;

#define fast ios_base::sync_with_stdio(0)
#define bfast cin.tie(0)
#define outs(x) cout << x << " "
#define outn(x) cout << x << "\n"
#define sf scanf
#define pf printf
#define nl puts("")
#define psb push_back
#define mset(c,v) memset(c , v , sizeof c)
#define loop0(n) for(int i=0; i<n; i++)
#define loop1(n) for(int i=1; i<=n; i++)
#define mpair(x , y) make_pair(x , y)
#define all(x) x.begin(), x.end()
#define pi acos(-1.0)
#define psb push_back

typedef unsigned long long ull;
typedef long long LL;
typedef vector<int>vii;
typedef vector<LL>vll;
typedef vector<string>vs;
typedef map<int, int>mpii;
typedef map<string, int>mpsi;
typedef map<char, int>mpci;
typedef map<LL, LL>mpll;

const int mod = 1000007;
const int high = 30003;

vector<list<pair<int, int> > >adj(high);
bool visited[high];
LL total=0 , root=0 , maxi = 0;
int Right[high], Left[high];

int DFS(int u)
{
    int v=0 , cost=0;

    visited[u] = true; // every Root will be colored not child

    list<pair<int, int> >::iterator it;

    for(it=adj[u].begin(); it!=adj[u].end(); ++it)
    {
        v = (*it).first;
        cost = (*it).second;

        if(!visited[v])
        {
            total = cost + DFS(v);

            if(total >= Left[u])
            {
                Right[u] = Left[u]; // Left and Right child of Root u, just think like LCA
                Left[u] = total;
            }

            else if(total >= Right[u]) // Right child of Root u, just think like LCA
            {
                Right[u] = total;
            }
        }
    }

    //cout << "total = " << total << "; ";

    maxi = max((LL)Left[u] + Right[u] , maxi);

    return Left[u];
}

void CLR(int n)
{
    for(int i=0; i<n; i++)
    {
        adj[i].clear();
    }
}

int main()
{
    //fast;
    //freopen("in.txt", "r", stdin);

    int n , i , j , u , v, w, test , tc=0 , ans=0;
    sf("%d", &test);
    while(test--)
    {
        mset(visited , false);
        mset(Left , 0);
        mset(Right , 0);
        CLR(n);

        total = root = maxi = 0;

        sf("%d", &n);

        sf("%d %d %d", &u, &v , &w);

        adj[u].psb(make_pair(v , w));
        adj[v].psb(make_pair(u , w));

        root = u;
        //cout << "root = " << root;
        //cout << "\n";

        for(i=1; i<n-1; i++)
        {
            sf("%d %d %d", &u , &v , &w);

            adj[u].psb(make_pair(v , w));
            adj[v].psb(make_pair(u , w));
        }

//        for(i=0; i<n-1; i++)
//        {
//            cout << i << "->";
//
//            list<pair<int, int> >::iterator it;
//
//            for(it=adj[i].begin(); it != adj[i].end(); ++it)
//            {
//                cout << (*it).first << " " << (*it).second << "; ";
//            }
//
//            cout << "\n";
//        }

        DFS(root);

//        for(i=0; i<n-1; i++)
//        {
//            cout << i << "-> left child = " << Left[i] << " right child = " << Right[i] << "\n";
//        }

        pf("Case %d: %lld\n" , ++tc, maxi);

    }

    return 0;
}

Comments

Popular posts from this blog

SPOJ-CMG - Collecting Mango

LightOJ 1009 - Back to Underworld

LeetCode Palindrome Number