Why is my solution for "Cowntagion" slow?

title. Here is my code, which returns TLE for almost every case:

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

int dfs(int curr, vector<vector<int>> adj) {
  int res = ceil(log((int)adj[curr].size()+1));
  for (int neighbor: adj[curr]) {
    res += 1 + dfs(neighbor, adj);
  }
  return res;
}

int main() {
  int n;
  cin >> n;
  vector<vector<int>> adj(n);
  for (int i = 0 ; i < n-1 ; i++) {
    int a, b;
    cin >> a >> b;
    adj[a-1].push_back(b-1);
  }
  int res = dfs(0, adj);
  cout << res;
  return 0;
}

If it TLEs even on the first, sample test case, then you might be running into an infinite loop.

no, it works on the sample case but fails everything else. it is quite similar to the dfs solution on usaco guide.

You still might be running into an infinite loop. Try downloading the test data and see what happens.

You need to check if (!used[next node]) then dfs(next node)

But my program creates a directed tree, so the child node doesn’t point back to the parent.

Have you tried downloading the test cases?
Also, I took a closer look at your code, and I think you’re assuming that all edges point away from the root; that isn’t necessarily the case.