Cod sursa(job #1883229)

Utilizator cristinamateiCristina Matei cristinamatei Data 17 februarie 2017 20:22:00
Problema Zvon Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.93 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

ifstream in("zvon.in");
ofstream out("zvon.out");

const int N = 100003;
int t[N], n, test;
vector <int> a[N];

bool cmp( int x, int y )
{
    return t[x] > t[y];
}

void dfs( int x )
{
    int i, y;
    for ( i = 0; i < a[x].size(); i++ )
    {
        y = a[x][i];
        dfs(y);
    }
    sort( a[x].begin(), a[x].end(), cmp);
    t[x] = 0;
    for ( i = 0; i < a[x].size(); i++ )
    {
        y = a[x][i];
        t[x] = max(t[x], 1 + i + t[y]);
    }
}


int main()
{
    int i, j, x, y;
    in >> test;
    for ( i = 1; i <= test; i++ )
    {
        in >> n;
        for ( j = 1; j < n; j++ )
        {
            in >> x >> y;
            a[x].push_back(y);
        }

        dfs(1);
        out << t[1]<<'\n';

        for ( j = 1; j <= n; j++ )
        {
            a[j].clear();
            t[j] = 0;
        }
    }
    return 0;
}