Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <queue>
  5.  
  6. using namespace std;
  7.  
  8. ifstream fin("statiuni.in");
  9. ofstream fout("statiuni.out");
  10.  
  11. #define nmax 100010
  12. #define mp make_pair
  13. #define f first
  14. #define s second
  15.  
  16. vector <int> v[nmax];
  17. int viz[nmax];
  18. int length[nmax];
  19. int n,k,ans;
  20.  
  21. void bf()
  22. {
  23.     queue <pair<int,pair<int,int> > >q;
  24.     int i;
  25.     for(i=1; i<=n; ++i)
  26.     {
  27.         if(v[i].size()==1)
  28.         {
  29.             q.push(mp(i,mp(0,i)));
  30.             viz[i]=1;
  31.         }
  32.     }
  33.     int nod,l,f;
  34.     while(!q.empty())
  35.     {
  36.         nod = q.front().f;
  37.         l = q.front().s.f;
  38.         f = q.front().s.s;
  39.         q.pop();
  40.         if(l==k)
  41.             continue;
  42.         for(auto it:v[nod])
  43.             if(((viz[it]==2 && length[it]>l+1) || viz[it]<2) && it!=f)
  44.             {
  45.                 ++viz[it];
  46.                 length[it]=l+1;
  47.                 q.push(mp(it,mp(l+1,nod)));
  48.             }
  49.     }
  50. }
  51.  
  52. int main()
  53. {
  54.     fin>>n>>k;
  55.     for(int i=1; i<n; ++i)
  56.     {
  57.         int x,y;
  58.         fin>>x>>y;
  59.         v[x].push_back(y);
  60.         v[y].push_back(x);
  61.     }
  62.     bf();
  63.     for(int i=1; i<=n; ++i)
  64.         if(viz[i]==2)
  65.             ++ans;
  66.     fout<<ans;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement