Can somebody plz help me to find the bug in this last starters 39 problem named MINBLOCK.

#include <iostream>
#include <bits/stdc++.h>
#include <cstring>
using namespace std;
#define fast_io ios_base::sync_with_stdio(false); cin.tie(NULL)
#define int long long
  map<int,bool> visited;

int cc=0;

class graph
{  public:
    
    map<int,int> blk;
    
    map<int,vector<int>> adj;
    void makegraph(int u,int v)
    {
       adj[u].push_back(v);
       adj[v].push_back(u);
       
    }

    int DFS(int v,vector<int>&ans, bool block)
    {  int nodes=1;
        visited[v]=true;
           
        vector<int>::iterator it;

        for (auto it =adj[v].begin() ; it!=adj[v].end(); it++)
        {   
            if(!visited[*it])
            {
               int x=DFS(*it,ans,block|blk[*it]);
                nodes+=x;
                if(!block&&(blk[*it]==1))
                {
                    ans.push_back(x);
                }
            }
        }
      return nodes;


    }


};
void solve()
 {    int n,k;
     cin>>n>>k;
    //  vector<pair<int,int>> vect;
int arr[n],brr[n],crr[n];
graph g;

for(int i = 0; i < (n-1); i++)
{
        cin>>arr[i]>>brr[i]>>crr[i];
    // vect.push_back(make_pair(arr[i],brr[i]));
        g.makegraph(arr[i],brr[i]);
        g.blk[brr[i]]=crr[i];
}

 vector<int> ans;
 g.DFS(1,ans,0); 
 
 sort(ans.begin(),ans.end());
//  for(int i=0;i<ans.size();i++)
//  cout<<ans[i]<<" ";
 int p=n,gc=0;
 for (int i = ans.size()-1; (p>k)&&(i>=0); i--)
 {
     p=p-ans[i];
    gc++;

 }
   if(p>k)
   {
       cout<<"-1\n";
   }
   else
   {
       cout<<gc<<"\n";
   }

   
       }
signed main()
{  fast_io;
int t;
cin>>t;
while(t--)
{
    solve();
}
 }