Cod sursa(job #1584443)

Utilizator armandpredaPreda Armand armandpreda Data 30 ianuarie 2016 09:32:18
Problema Zvon Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.21 kb
#include <fstream>
#include <vector>
#include <algorithm>
#include <cstring>
#define maxi(a, b) ((a)>(b)?(a):(b))

using namespace std;

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

const int LIM=100005;
int t, n, viz[LIM], dp[LIM], tata[LIM];
vector <int> arb[LIM];
bool cmp(int a, int b)
{
    return dp[a]>dp[b];
}
void dfs(int nod)
{
    viz[nod]=1;
    for(int i=0; i<arb[nod].size(); ++i)
        if(!viz[ arb[nod][i] ])
        {
            tata[ arb[nod][i] ]=nod;
            dfs(arb[nod][i]);
        }
    sort(arb[nod].begin(), arb[nod].end(), cmp);
    for(int i=0, nr=1; i<arb[nod].size(); ++i)
        if(arb[nod][i]!=tata[nod])
        {
            dp[nod]=maxi(dp[nod], dp[ arb[nod][i] ]+nr);
            nr++;
        }
}
int main()
{

    cin>>t;
    while(t--)
    {
        cin>>n;
        for(int i=1; i<n; ++i)
        {
            int x, y;
            cin>>x>>y;
            arb[x].push_back(y);
            arb[y].push_back(x);
        }
        dfs(1);
        cout<<dp[1]<<'\n';
        memset(viz, 0, (n+1)*sizeof(int));
        memset(dp, 0, (n+1)*sizeof(int));
        for(int i=1; i<=n; ++i)
            arb[i].clear();
    }
    return 0;
}