Cod sursa(job #627788)

Utilizator vlad2901Vlad Berindei vlad2901 Data 30 octombrie 2011 18:15:42
Problema Zvon Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#define NMAX 100001

using namespace std;

vector< vector<int> > v(NMAX);

int cost[NMAX], viz[NMAX];
int N, T;

bool cmp(int a, int b)
{
    if(cost[a] > cost[b])
    {
        return 1;
    }
    return 0;
}

void mark(int nod)
{
    int max, c, s;
    vector<int>::iterator it;
    if(v[nod].empty())
    {
        cost[nod] = 0;
        return;
    }
    //s = v[nod].size();
    //c = 0;
    for(it=v[nod].begin();it!=v[nod].end();it++)
    //for(it=v[nod].begin();c<s;it++)
    {
        if(!viz[*it])
        {
            viz[*it] = 1;
            mark(*it);
        }
        ++c;
    }
    sort(v[nod].begin(), v[nod].end(), cmp);
    max = 0;
    c = 1;
    for(it=v[nod].begin();it!=v[nod].end();it++)
    {
        if(max < c + cost[*it])
        {
            max = c + cost[*it];
        }
        c++;
    }
    cost[nod] = max;
}


int main()
{
    int i, x, y;

    freopen("zvon.in", "r", stdin);
    freopen("zvon.out", "w", stdout);



    scanf("%d", &T);

    while(T--)
    {
        memset(cost, 0, sizeof(cost));
        memset(viz, 0, sizeof(viz));

        for(i=0;i<N;++i)
        {
            v[i].clear();
        }

        scanf("%d", &N);
        for(i=0;i<N-1;++i)
        {
            scanf("%d %d", &x, &y);
            v[x].push_back(y);
        }

        mark(1);
        printf("%d\n", cost[1]);
    }


    return 0;
}