USACO 2020 December Contest, Silver Problem 1. Cowntagion

So I was solving the Cowntagion from the Silver Contest in December. I keep getting test cases 4,7 and 10 wrong. They are all in different sub-tasks so I can’t use that to help me. I looked at the solution and my solution has the same idea, except it was implemented differently.

My solution
#include <bits/stdc++.h>
using namespace std;

const int mxn=1e5;
int n, ans=0;
vector<int> adj[mxn+1];

void dfs(int node, int prev){
    int track=1;
    while(track<(int)adj[node].size())
        track*=2,ans++;
    for(int i : adj[node])
        if(i!=prev)
            dfs(i,node),ans++;
}

int main(){
    cin.tie(0)->sync_with_stdio(0);
    cin >> n;
    for(int i=0,u,v;i<n-1;i++){
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    dfs(1,0);
    cout << ans << "\n";
}

Test case 4 should be easy to debug- it’s just a bunch of nodes connected to node 1.

I figured it out. I forgot to add one for the starting root.