Cod sursa(job #1483974)

Utilizator justsomedudePalade Thomas-Emanuel justsomedude Data 10 septembrie 2015 11:27:39
Problema Zvon Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.19 kb
#include<iostream>
#include<fstream>
#include<queue>
 
using namespace std;
 
ifstream fin ("zvon.in");
ofstream fout ("zvon.out");

vector <int> L[100003]; 
int n,cost[100003],cnt;
bool viz[100003];

void Citire()
{
  int x,y;
  fin >> n;
  for (int i=1; i<n; i++)
   {
      fin >> x >> y;     
      L[x].push_back(y);
      L[y].push_back(x);
      cost[i]=0;
      viz[i]=0;
   }
   viz[n]=cost[n]=0;
}
 
void BFS()
{
  int i,j,k;
  queue <int> q;
  
  q.push(1);
  viz[1]=1;
  cost[1]=0;
   
  while (!q.empty())
 {
   k = q.front();
   q.pop();
   cnt=1;
   
   for (j=0; j<L[k].size(); j++)
       {
            i = L[k][j]; 
             
			if (!viz[i])
               {
                   cost[i] = cost[k] + cnt;
                   cnt++;
                   viz[i]=1;
                   q.push(i);
               }
       }
 }  
}
 
void Afisare()
{
 int maxim = 0;
 
 cout << cost[5];
 
 for (int i=1; i<=n; i++)
   maxim = max(cost[i],maxim);
 fout << maxim << "\n";
}
 
int main ()
{
 int i,T;
 fin>> T;
 for (i=1; i<=T; i++)
 {   Citire();
     BFS();
     Afisare();
 }
 fin.close();
 fout.close();
 return 0;
}