Cod sursa(job #1888443)

Utilizator tiberiumunteanMuntean Tiberiu tiberiumuntean Data 22 februarie 2017 09:25:09
Problema Zvon Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.17 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");

int t, n, viz[100005], dp[100005], tata[100005];
vector <int> A[100005];
bool cmp(int a, int b)
{
    return dp[a]>dp[b];
}
void dfs(int nod)
{
    viz[nod]=1;
    for(int i=0; i<A[nod].size(); ++i)
        if(!viz[A[nod][i]])
        {
            tata[ A[nod][i] ]=nod;
            dfs(A[nod][i]);
        }
    sort(A[nod].begin(), A[nod].end(), cmp);
    for(int i=0, nr=1; i<A[nod].size(); ++i)
        if(A[nod][i]!=tata[nod])
        {
            dp[nod]=maxi(dp[nod], dp[A[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;
            A[x].push_back(y);
            A[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)
            A[i].clear();
    }
    return 0;
}