Problem p02822 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<double, double> pd;
typedef pair<ll, ll> pl;

vector<int> adj[200001];
int childs[200001];

ll res;
int n;

int modpow(int a, int b) {
  ll powres = 1;

  for (int i = 0; i < b; i++) {
    powres *= a;
    powres %= mod;
  }

  return powres;
}

int inv(int a) { return modpow(a, mod - 2); }

int dfs(int cur, int pre) {
  vector<int> vcounts;
  childs[cur] = 1;

  for (int i = 0; i < adj[cur].size(); i++)
    if (adj[cur][i] != pre) {
      vcounts.push_back(dfs(adj[cur][i], cur));
      childs[cur] += vcounts.back();
    }

  if (childs[cur] < n)
    vcounts.push_back(n - childs[cur]);

  ll p[3] = {(mod + 1) / 2, 0, 0};

  for (int i = 0; i < vcounts.size(); i++) {
    ll q[3] = {};
    int w = modpow((mod + 1) / 2, vcounts[i]);
    int b = (mod + 1 - w) % mod;

    q[0] = (p[0] * w) % mod;
    q[1] = ((p[0] * b) % mod + (p[1] * w) % mod) % mod;
    q[2] = (p[2] + p[1] * b) % mod;

    swap(p, q);

    /*if(cur == 2)
      printf("%llda\n", w);*/
  }
  // printf("%lld\n", p[2]);
  res += p[2];
  res %= mod;

  return childs[cur];
}

int main() {
  scanf("%d", &n);

  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  dfs(1, -1);

  // printf("%lld\n", (((ll)mod+1)/2 * ((mod+1)/2))%mod);

  printf("%lld\n", res);

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <typeinfo>
#include <utility>
#include <vector>
#define mod 1000000007
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<double, double> pd;
typedef pair<ll, ll> pl;

vector<int> adj[200001];
int childs[200001];

ll res;
int n;

ll modpow(int a, int b) {
  if (b == 1)
    return a;
  else if (b % 2 == 0) {
    ll temp = modpow(a, b / 2);
    return temp * temp % mod;
  } else
    return modpow(a, b - 1) * a % mod;
}

int inv(int a) { return modpow(a, mod - 2); }

int dfs(int cur, int pre) {
  vector<int> vcounts;
  childs[cur] = 1;

  for (int i = 0; i < adj[cur].size(); i++)
    if (adj[cur][i] != pre) {
      vcounts.push_back(dfs(adj[cur][i], cur));
      childs[cur] += vcounts.back();
    }

  if (childs[cur] < n)
    vcounts.push_back(n - childs[cur]);

  ll p[3] = {(mod + 1) / 2, 0, 0};

  for (int i = 0; i < vcounts.size(); i++) {
    ll q[3] = {};
    int w = modpow((mod + 1) / 2, vcounts[i]);
    int b = (mod + 1 - w) % mod;

    q[0] = (p[0] * w) % mod;
    q[1] = ((p[0] * b) % mod + (p[1] * w) % mod) % mod;
    q[2] = (p[2] + p[1] * b) % mod;

    swap(p, q);

    /*if(cur == 2)
      printf("%llda\n", w);*/
  }
  // printf("%lld\n", p[2]);
  res += p[2];
  res %= mod;

  return childs[cur];
}

int main() {
  scanf("%d", &n);

  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    adj[a].push_back(b);
    adj[b].push_back(a);
  }

  dfs(1, -1);

  // printf("%lld\n", (((ll)mod+1)/2 * ((mod+1)/2))%mod);

  printf("%lld\n", res);

  return 0;
}