Help needed in tree house

need help...i wrote the tree house code (using greedy...map based approach) but getting wrong and in 0,1 and 6....can anyone plz check this code and let me know what I missed..

#include <iostream>

#include <bits/stdc++.h>

using namespace std;

int main()

{

  long long int t;

  cin >> t;

  long long int mod = 1e9 + 7;

  while (t > 0)

  {

    map<int, vector<long long int>> m;

    int n;

    long long int x;

    cin >> n >> x;

    for (int i = 1; i < n; i++)

    {

      int a, b;

      cin >> a >> b;

      m[a].push_back(b);

    }

    long long int sum = x;

    multimap<long long int, long long int, greater<>> m1;

    unordered_map<long long int, long long int> m2;

    m2[1] = x;

    for (auto el1 : m)

    {

      for (auto el2 : el1.second)

      {

        // m1[m[el2].size()]=el2;

        m1.insert(make_pair(m[el2].size(), el2));

        //cout<<m[el2].size()<<" ";

      }

      // cout<<endl;

      int i = 1;

      x = m2[el1.first];

      for (auto el3 : m1)

      {

        long long int g = i * x;

        // cout<<"heee "<<" ";

        sum = (sum + g + mod) % mod;

        m2[el3.second] = g;

        i++;

      }

      m1.clear();

    }

    cout << sum << endl;

    t--;

  }

  return 0;

}

Hi!
My approach is very similar to that of yours.
I went through your code a couple of times, and I found this.
It fails this kind of test cases.
Test case 1:-

I/O:-
1
9 1
1 2
1 3
2 4
2 5
3 6
6 7
6 8
6 9

For this input, the actual output should be:-
17
Your code prints:-
21

Also…
The inputs taken u,v does not always mean that u is the parent node of v.
Example:-
1
9 1
1 2
3 1
2 4
2 5
3 6
6 7
6 8
6 9
The above test case should also print the same output i.e 17.

I have not written my code yet.

I believe, it would give AC after making all the above changes.
Have a great day!! :smile:

2 Likes