Advertisement
Guest User

Untitled

a guest
Jun 12th, 2014
481
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.56 KB | None | 0 0
  1. #pragma comment(linker, "/STACK:167177216")
  2.  
  3. #include <stdio.h>
  4. #include <stack>
  5. #include <math.h>
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <string.h>
  9. #include <string>
  10. #include <memory.h>
  11. #include <vector>
  12. #include <map>
  13. #include <queue>
  14. #include <set>
  15. #include <time.h>
  16. #include <cassert>
  17. #include <cstring>
  18. //#include <unordered_set>
  19.  
  20. using namespace std;
  21.  
  22. #define mp make_pair
  23. #define pb push_back
  24. #define pii pair<int, int>
  25. #define forn(i, n) for(int i = 0; i < (int)(n); i++)
  26. #define x first
  27. #define y second
  28.  
  29. typedef long long li;
  30. typedef long double ld;
  31. typedef unsigned long long uli;
  32.  
  33. const int INF = 1e9;
  34. const ld eps = 1e-9;
  35. const li MOD = (li)(4e6 + 37);
  36. const li INF64 = (li)(INF) * (li)(INF);
  37.  
  38. const int ddx[] = {-1, 1, 1, -1};
  39. const int ddy[] = {1, 1, -1, -1};
  40. const int dx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
  41. const int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
  42. const int dx4[] = {-1, 0, 1, 0};
  43. const int dy4[] = {0, 1, 0, -1};
  44. const int dxh[] = {-1, -1, -1, 1, 1, 1, 1, -1};
  45. const int dyh[] = {1, -1, -1, -1, -1, 1, 1, 1};
  46. const string dirs[] = {"RIGHT", "UP", "LEFT", "DOWN"};
  47.  
  48. const int N = 1000;
  49.  
  50. vector<int> g[N]; //spiski smezhnosti dereva
  51. bool used[N]; //poseshali vershinu ili net
  52. int depth[N]; //massiv glubin depth[i] == glubina vershini i
  53. int max_deep = 0; // max glubina v dereve
  54. int n; //kolichestvo vershin v dereve i kolichestvo reber
  55.  
  56.  
  57. //poisk v glubinu: prinimaet vershinu i ee glubinu
  58. void dfs(int v, int deep)
  59. {
  60.     used[v] = true;
  61.     depth[v] = deep;
  62.     max_deep = max(max_deep, deep);
  63.    
  64.     for(int i = 0; i < g[v].size(); i++)
  65.     {
  66.         int to = g[v][i];
  67.         if(!used[to])
  68.         {
  69.             used[to] = true;
  70.             dfs(to, deep + 1);
  71.         }
  72.     }
  73. }
  74.  
  75. //sama funkciya
  76. //type idet po vsem vershinam i esli glubina vershini ravna trebuemoy i vershina list(u nee tolko 1 rebro) to uvelichivaet otvet
  77.  
  78. int cnt(int deep)
  79. {
  80.     if(deep < 1 || deep > max_deep)
  81.         return 0;
  82.     if(deep == 1)
  83.         return 0;
  84.    
  85.     int res = 0;
  86.  
  87.     for(int i = 1; i <= n; i++)
  88.         if(depth[i] == deep && g[i].size() == 1)
  89.             res++;
  90.  
  91.     return res;
  92. }
  93.  
  94. int main()
  95. {
  96.     //freopen("input.txt", "r", stdin);
  97.     //freopen("output.txt", "w", stdout);
  98.     //freopen("errors.txt", "w", stderr);
  99.     //ios_base::sync_with_stdio(false);
  100.    
  101.  
  102.     cin >> n;
  103.     for(int i = 0; i < n - 1; i++)
  104.     {
  105.         int a, b;
  106.         cin >> a >> b;
  107.         g[a].pb(b);
  108.         g[b].pb(a);
  109.     }
  110.  
  111.     //cout << "777" << endl;
  112.  
  113.     dfs(1, 1);
  114.  
  115.     //cout << "WTF" << endl;
  116.  
  117.     for(int i = 1; i <= max_deep; i++)
  118.     {
  119.         cout << "kolichestvo list'ev na glubine " << i << " == " << cnt(i) << endl;
  120.     }
  121.  
  122.     return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement