Cod sursa(job #695483)

Utilizator hiticas_abelhiticasabel hiticas_abel Data 28 februarie 2012 12:39:48
Problema Zvon Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.65 kb
#include <fstream>
#include <vector>
#include <bitset>
using namespace std;
const int maxn=100005;
int c[maxn],k,n,m,i,x,y;
long long cost[maxn], timp;
vector <int> A[maxn];

bitset <maxn>viz;

ifstream f("zvon.in");
ofstream g("zvon.out");
void citire()
{
     int x,y,i;
    // f>>n>>m>>k;
     
     f>>n;
    
     for(i=1;i<n;i++)
     {
                      f>>x>>y;
                      A[x].push_back(y);
                      //A[y].push_back(x);
                      }
    f.close();
     }
void bfs(int k)
{
    timp=0;
     int li,ls,i,nod,NV;
     li=1;
     ls=1;
     c[li]=k;
     viz[k]=1;
     while(li<=ls)
     {
                  nod=c[li];
                  NV=A[nod].size();
               
                  for(i=0;i<NV;i++)
                  
                  if(!viz[A[nod][i]])
                  {
                                      ls++;
                                      c[ls]=A[nod][i];
                                      viz[A[nod][i]]=1;
                                      cost[A[nod][i]]=cost[nod]+1;
                                      
                                     
                                    
                                       
                  }
                                       
                  li++;
                  }
                  
                  }
                  
int main()
{
    int i;
    citire();
    bfs(1);
   
   g<<cost[n-1]+viz[n-1];
   
   //g<<timp<<"\n";
/*
    for(i=1;i<=n;i++)
    if(cost[i]!=0||i==k)
    g<<cost[i]<<" ";
    else
    g<<-1<<" ";
    
    */
    g.close();
    return 0;
}