Facebook
From Bobi, 3 Years ago, written in C++.
Embed
Download Paste or View Raw
Hits: 41
  1. #include <cstdio>
  2. #include <vector>
  3. using namespace std;
  4. const int MAXN = 100001;
  5. int n, m, rez, c;
  6. int d[MAXN];
  7. vector <int> graph[MAXN];
  8.  
  9. void dis(int x) {
  10.         m-=d[x];
  11.         for (int j = 0; j < graph[x].size(); j++) {
  12.                 if (d[graph[x][j]] > 0) {
  13.                         d[graph[x][j]]--;
  14.                 }
  15.         }
  16.         d[x] = 0;
  17. }
  18.  
  19. int main() {
  20.         scanf("%d", &n);
  21.         m = n - 1;
  22.         rez = 0;
  23.         for (int i = 0; i < m; i++) {
  24.                 int a, b;
  25.                 scanf("%d%d", &a, &b);
  26.                 d[a]++;
  27.                 d[b]++;
  28.                 graph[a].push_back(b);
  29.                 graph[b].push_back(a);
  30.         }
  31.         while (m > 0) {
  32.                 for (int i = 1; i <= n; i++) {
  33.                         if (d[i] == 1) {
  34.                                 c = i;
  35.                                 break;
  36.                         }
  37.                 }
  38.                 for (int i = 0; i < graph[c].size(); i++) {
  39.                         if (d[graph[c][i]] > 0) {
  40.                                 dis(graph[c][i]);
  41.                                 rez++;
  42.                                 break;
  43.                         }
  44.                 }
  45.         }
  46.         printf("%d", rez);
  47. }