Advertisement
adnan_dipta89

SubtreeNode

Oct 22nd, 2019
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define MAX 100000
  3. using namespace std;
  4. typedef long long ll;
  5. enum color {WHITE,GRAY,BLACK};
  6. vector<vector<ll>> adjList;
  7. color vertex_color[MAX+1];
  8. vector<ll> node_count;
  9. ll parent[MAX+1];
  10. ll N,M;
  11. void addEdge(ll u, ll v)
  12. {
  13.     adjList[u].push_back(v);
  14. }
  15. void DFS_VISIT(ll u)
  16. {
  17.     vertex_color[u] = GRAY;
  18.     for(ll i = 0; i < adjList[u].size(); i++)
  19.     {
  20.         ll v = adjList[u][i];
  21.         if(vertex_color[v] == WHITE)
  22.         {
  23.             parent[v] = u;
  24.             DFS_VISIT(v);
  25.             node_count[u] += node_count[v];
  26.         }
  27.     }
  28.     node_count[u] += 1;
  29.     vertex_color[u] = BLACK;
  30. }
  31. void DFS()
  32. {
  33.     for(ll i = 1; i <= N; i++)
  34.     {
  35.         vertex_color[i] = WHITE;
  36.         parent[i] = 0;
  37.     }
  38.     for(ll i = 1; i <= N; i++)
  39.     {
  40.         if(vertex_color[i] == WHITE)
  41.         {
  42.             DFS_VISIT(i);
  43.         }
  44.     }
  45. }
  46. int main()
  47. {
  48.     ll edges = 0;
  49.     cin >> N >> M;
  50.     adjList.resize(N+1);
  51.     for(ll i = 1; i <= M; i++)
  52.     {
  53.         ll u,v;
  54.         cin >> u >> v;
  55.         addEdge(u,v);
  56.         addEdge(v,u);
  57.     }
  58.     node_count.resize(N+1);
  59.     for(ll i = 1; i <= N; i++)
  60.     {
  61.         node_count[i] = 0;
  62.     }
  63.     DFS();
  64.     for(ll i = 1; i <= N; i++)
  65.     {
  66.         cout << i << ": " << node_count[i] << endl;
  67.     }
  68.     for(ll i = 2; i <= N; i++)
  69.     {
  70.         ll x = node_count[i];
  71.         if(x%2 == 0)
  72.             edges++;
  73.  
  74.     }
  75.     cout << edges << endl;
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement