We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
  • Hackerrank Home
  • Prepare
    NEW
  • Certify
  • Compete
  • Career Fair
  • Hiring developers?
  1. Prepare
  2. Algorithms
  3. Game Theory
  4. Deforestation
  5. Discussions

Deforestation

Problem
Submissions
Leaderboard
Discussions
Editorial

    You are viewing a single comment's thread. Return to all comments →

  • dkhos17
    3 years ago+ 1 comment

    cbuff's link and dingma129's post is really helpful, thnx^^ but if u need code:

    int gr[501];
    
    int dfs(int u, int p, vector<vector<int>>& G){
        for(auto ch : G[u]){
            if(ch != p) gr[u] ^= dfs(ch,u,G);
        }
        return u == 1 ? gr[u] : ++gr[u];
    }
    
    
    string deforestation(int n, vector<vector<int>> tree) {
        memset(gr, 0, sizeof(gr));
        vector<vector<int>> G(n+1);
        for(auto p : tree){
            G[p[0]].push_back(p[1]);
            G[p[1]].push_back(p[0]);
        }
        return dfs(1,-1,G) ? "Alice" : "Bob";
    }
    
    2|
    Permalink
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature