prithivMLmods/Coder-Stat huggingface dataset:
Problem p03174 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define ll long long
#define ff first
#define ss second
#define pb push_back

using namespace std;

const int N = 1e6 + 69, INF = 1e9, mod = 1e9 + 7;

const ll INFLL = 1e18;

int n, K;
ll dp[405][405], A[30][30], sum[405][405];

int main() {

  cin >> n;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> A[i][j];
    }
  }

  for (int i = 0; i < n; i++) {
    if (A[0][i] == 1) {
      dp[0][(1 << i)] = 1;
    }
  }

  for (int i = 0; i < n; i++) {
    for (int mask = 0; mask < (1 << n); mask++) {
      if (!dp[i][mask])
        continue;
      for (int j = 0; j < n; j++) {
        if (mask & (1 << j) || !A[i + 1][j])
          continue;
        dp[i + 1][mask ^ (1 << j)] += dp[i][mask];
        dp[i + 1][mask ^ (1 << j)] %= mod;
      }
    }
  }

  cout << dp[n - 1][(1 << n) - 1] << endl;
}


Fixed submission:
#include <bits/stdc++.h>

#define ll long long
#define ff first
#define ss second
#define pb push_back

using namespace std;

const int N = 1e6 + 69, INF = 1e9, mod = 1e9 + 7;

const ll INFLL = 1e18;

int n, K;
ll dp[22][(1 << 22)], A[30][30], sum[405][405];

int main() {

  cin >> n;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> A[i][j];
    }
  }

  for (int i = 0; i < n; i++) {
    if (A[0][i] == 1) {
      dp[0][(1 << i)] = 1;
    }
  }

  for (int i = 0; i < n; i++) {
    for (int mask = 0; mask < (1 << n); mask++) {
      if (!dp[i][mask])
        continue;
      for (int j = 0; j < n; j++) {
        if (mask & (1 << j) || !A[i + 1][j])
          continue;
        dp[i + 1][mask ^ (1 << j)] += dp[i][mask];
        dp[i + 1][mask ^ (1 << j)] %= mod;
      }
    }
  }

  cout << dp[n - 1][(1 << n) - 1] << endl;
}
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

template <typename T> void printv(const vector<T> &v) {
  int sz = v.size();
  for (int i = 0; i < sz; i++) {
    cout << v[i] << " \n"[i == sz - 1];
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<vector<int>> a(N, vector<int>(N));
  vector<vector<ll>> dp(N, vector<ll>(1 << N, 0));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> a[i][j];
      if (i == 0 && a[i][j]) {
        dp[i][1 << j] = 1;
      }
    }
  }
  for (int i = 1; i < N; i++) {
    for (int j = 0; j < (1 << N); j++) {
      for (int k = 0; k < N; k++) {
        if (!a[i][k])
          continue;
        if (j & (1 << k)) {
          dp[i][j] += dp[i - 1][j ^ (1 << k)];
          dp[i][j] %= MOD;
        }
      }
    }
  }
  cout << dp[N - 1][(1 << N) - 1] << endl;
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;
using P = pair<int, int>;
constexpr ld EPS = 1e-12;
constexpr int INF = numeric_limits<int>::max() / 2;
constexpr int MOD = 1e9 + 7;

template <typename T> void printv(const vector<T> &v) {
  int sz = v.size();
  for (int i = 0; i < sz; i++) {
    cout << v[i] << " \n"[i == sz - 1];
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N;
  cin >> N;
  vector<vector<int>> a(N, vector<int>(N));
  vector<vector<ll>> dp(N, vector<ll>(1 << N, 0));
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      cin >> a[i][j];
      if (i == 0 && a[i][j]) {
        dp[i][1 << j] = 1;
      }
    }
  }
  for (int i = 1; i < N; i++) {
    for (int j = 0; j < N; j++) {
      if (!a[i][j])
        continue;
      for (int k = 0; k < (1 << N); k++) {
        if (k & (1 << j)) {
          dp[i][k] += dp[i - 1][k ^ (1 << j)];
          dp[i][k] %= MOD;
        }
      }
    }
  }
  cout << dp[N - 1][(1 << N) - 1] << endl;
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef int64_t LL;
using namespace std;

const int N = 25;
const int mod = 1e9 + 7;
bool A[N][N];
int n;
int dp[N][1 << 21];
LL solve(int k, int used) {
  if (k == n)
    return 1LL;
  if (dp[k][used] >= 0)
    return dp[k][used];
  LL res = 0;
  for (int i = 0; i < n; i++) {
    if (A[k][i] && !((used >> i) & 1)) {
      used ^= (1 << i);
      res += solve(k + 1, used);
      res %= mod;
      used ^= (1 << i);
    }
  }
  return dp[k][used] = res;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      scanf("%d", &A[i][j]);
    }
  }
  memset(dp, sizeof(dp), -1);
  int ans = solve(0, 0);
  printf("%d\n", ans);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
typedef int64_t LL;
using namespace std;

const int N = 25;
const int mod = 1e9 + 7;
bool A[N][N];
int n;
int dp[N][1 << 21];
LL solve(int k, int used) {
  if (k == n)
    return 1LL;
  if (dp[k][used] >= 0)
    return dp[k][used];
  LL res = 0;
  for (int i = 0; i < n; i++) {
    if (A[k][i] && !((used >> i) & 1)) {
      used ^= (1 << i);
      res += solve(k + 1, used);
      res %= mod;
      used ^= (1 << i);
    }
  }
  return dp[k][used] = res;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      scanf("%d", &A[i][j]);
    }
  }
  memset(dp, -1, sizeof(dp));
  int ans = solve(0, 0);
  printf("%d\n", ans);
  return 0;
}
Problem p03174 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

#define int long long

typedef pair<int, int> ii;

const int mod = 1e9 + 7;

const int N = 21;

int n, a[N][N], dp[N][(1 << N)];

signed main() {
  cin.tie(0), ios::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      cin >> a[i][j];
  }
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int mask = 0; mask < (1 << n); mask++) {
      if (!dp[i][mask])
        continue;
      for (int k = 0; k < n; k++) {
        if (a[i][k] && (mask & (1 << k)) == 0)
          dp[i + 1][(mask ^ (1 << k))] =
              (dp[i + 1][(mask ^ (1 << k))] + dp[i][mask]) % mod;
      }
    }
  }
  cout << dp[n][(1 << n) - 1];
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define mp make_pair

#define int long long

typedef pair<int, int> ii;

const int mod = 1e9 + 7;

const int N = 22;

int n, a[N][N], dp[N][(1 << N)];

signed main() {
  cin.tie(0), ios::sync_with_stdio(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++)
      cin >> a[i][j];
  }
  dp[0][0] = 1;
  for (int i = 0; i < n; i++) {
    for (int mask = 0; mask < (1 << n); mask++) {
      if (!dp[i][mask])
        continue;
      for (int k = 0; k < n; k++) {
        if (a[i][k] && (mask & (1 << k)) == 0)
          dp[i + 1][(mask ^ (1 << k))] =
              (dp[i + 1][(mask ^ (1 << k))] + dp[i][mask]) % mod;
      }
    }
  }
  cout << dp[n][(1 << n) - 1];
}
Problem p03174 (Python)

Buggy submission (status: Time Limit Exceeded):
import numpy as np

N = int(input())
A = [np.flatnonzero(np.array(list(map(int, input().split())))) for _ in range(N)]

MOD = 10**9 + 7
dp = np.zeros(1 << N, dtype=np.int64)
dp[0] = 1
for a in A:
    new_dp = np.zeros_like(dp)
    for i in a:
        x = 1 << i
        new_dp[x:] += dp[:-x]
    dp = new_dp
    dp %= MOD
print(dp[-1])


Fixed submission:
import numpy as np

N = int(input())
A = [np.array(list(map(int, input().split()))).nonzero()[0] for _ in range(N)]

MOD = 10**9 + 7
dp = np.zeros(1 << N, dtype=np.int64)
dp[0] = 1
for a in A:
    new_dp = np.zeros_like(dp)
    for i in a:
        x = 1 << i
        new_dp[x:] += dp[:-x]
    dp = new_dp
    dp %= MOD
print(dp[-1])
Problem p03174 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define ll long long
template <typename T, typename U> static inline void amin(T &x, U y) {
  if (x > y)
    x = y;
}
template <typename T, typename U> static inline void amax(T &x, U y) {
  if (x < y)
    x = y;
}
#define rep(i, n) for (int i = 0; i < n; ++i)
#define irep(i, k, n) for (int i = k; i <= n; ++i)
#define irepr(i, k, n) for (int i = k; i >= n; --i)

int inf = 2e9;
ll INF = 2e18;
const int mod = 1e9 + 7;
const int N = 22;
int n;

vvi dp(N, vi(1 << N, -1));
vvi a(N, vi(N));
int solve(int r, int mask) {
  if (r == n) {
    return mask == 0;
  }
  if (dp[r][mask] == -1) {
    dp[r][mask] = 0;
    for (int i = 0; i < n; ++i) {
      if (a[r][n - 1 - i] == 1) {
        dp[r][mask] += solve(r + 1, mask ^ (1 << i));
        dp[r][mask] %= mod;
      }
    }
  }
  return dp[r][mask];
}

int main() {
  cin >> n;
  rep(i, n) {
    rep(j, n) { cin >> a[i][j]; }
  }
  cout << solve(0, (1 << n) - 1) << endl;
  return 0;
}

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

#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define ll long long
template <typename T, typename U> static inline void amin(T &x, U y) {
  if (x > y)
    x = y;
}
template <typename T, typename U> static inline void amax(T &x, U y) {
  if (x < y)
    x = y;
}
#define rep(i, n) for (int i = 0; i < n; ++i)
#define irep(i, k, n) for (int i = k; i <= n; ++i)
#define irepr(i, k, n) for (int i = k; i >= n; --i)

int inf = 2e9;
ll INF = 2e18;
const int mod = 1e9 + 7;
const int N = 22;
int n;

vvi dp(N, vi(1 << N, -1));
vvi a(N, vi(N));
int solve(int r, int mask) {
  if (r == n) {
    return mask == 0;
  }
  if (dp[r][mask] == -1) {
    dp[r][mask] = 0;
    for (int i = 0; i < n; ++i) {
      if (a[r][n - 1 - i] == 1 && mask & (1 << i)) {
        dp[r][mask] += solve(r + 1, mask ^ (1 << i));
        dp[r][mask] %= mod;
      }
    }
  }
  return dp[r][mask];
}

int main() {
  cin >> n;
  rep(i, n) {
    rep(j, n) { cin >> a[i][j]; }
  }
  cout << solve(0, (1 << n) - 1) << endl;
  return 0;
}
Problem p03175 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define ppb pop_back
#define ppf pop_front
#define umax(x, y) x = max(x, y)
#define umin(x, y) x = min(x, y)
#define ll long long
#define ii pair<int, int>
#define iii pair<ii, int>
#define iiii pair<ii, ii>
#define sz(x) ((int)x.size())
#define orta ((bas + son) >> 1)
#define all(x) x.begin(), x.end()
#define dbgs(x) cerr << (#x) << " --> " << (x) << " "
#define dbg(x)                                                                 \
  cerr << (#x) << " --> " << (x) << endl;                                      \
  getchar()
#define pw(x) (1 << (x))
#define inf 1000000000000005
#define MOD 1000000007
#define N 10005
#define M 27000
#define LOG 1000000
#define KOK 650
#define EPS 0.000000001
using namespace std;

int n, x, y;
int dp[N][2];
vector<int> v[N];

void dfs(int node, int ata) {

  for (int i : v[node]) {

    if (i == ata)
      continue;

    dfs(i, node);
  }

  int z = 1, o = 1;

  for (int i : v[node]) {

    if (i == ata)
      continue;

    z = 1ll * z * dp[i][0] % MOD;
    o = 1ll * o * dp[i][1] % MOD;
  }

  dp[node][0] = (z + o) % MOD;
  dp[node][1] = z;
}

int main() {

  // freopen("input.txt","r",stdin);

  scanf("%d", &n);

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

    scanf("%d %d", &x, &y);

    v[x].pb(y);
    v[y].pb(x);
  }

  dfs(1, 0);

  printf("%d", dp[1][0]);
}


Fixed submission:
#include <bits/stdc++.h>
#define st first
#define nd second
#define pb push_back
#define ppb pop_back
#define ppf pop_front
#define umax(x, y) x = max(x, y)
#define umin(x, y) x = min(x, y)
#define ll long long
#define ii pair<int, int>
#define iii pair<ii, int>
#define iiii pair<ii, ii>
#define sz(x) ((int)x.size())
#define orta ((bas + son) >> 1)
#define all(x) x.begin(), x.end()
#define dbgs(x) cerr << (#x) << " --> " << (x) << " "
#define dbg(x)                                                                 \
  cerr << (#x) << " --> " << (x) << endl;                                      \
  getchar()
#define pw(x) (1 << (x))
#define inf 1000000000000005
#define MOD 1000000007
#define N 100005
#define M 27000
#define LOG 1000000
#define KOK 650
#define EPS 0.000000001
using namespace std;

int n, x, y;
int dp[N][2];
vector<int> v[N];

void dfs(int node, int ata) {

  for (int i : v[node]) {

    if (i == ata)
      continue;

    dfs(i, node);
  }

  int z = 1, o = 1;

  for (int i : v[node]) {

    if (i == ata)
      continue;

    z = 1ll * z * dp[i][0] % MOD;
    o = 1ll * o * dp[i][1] % MOD;
  }

  dp[node][0] = (z + o) % MOD;
  dp[node][1] = z;
}

int main() {

  // freopen("input.txt","r",stdin);

  scanf("%d", &n);

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

    scanf("%d %d", &x, &y);

    v[x].pb(y);
    v[y].pb(x);
  }

  dfs(1, 0);

  printf("%d", dp[1][0]);
}
Problem p03175 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
pair<ll, ll> func(vector<vector<int>> &v, bool b[], int ind, ll &mod) {
  b[ind] = 1;
  ll t = 1, t1 = 1;
  for (int i = 0; i < v[ind].size(); i++) {
    if (b[v[ind][i]] == 0) {
      pair<ll, ll> p;
      p = func(v, b, v[ind][i], mod);
      t = (t * ((p.first + p.second) % mod)) % mod;
      t1 = (t1 * p.second) % mod;
    }
  }
  return {t1, t};
}
int main() {
  int n;
  cin >> n;
  if (n == 1)
    return 2;
  vector<vector<int>> v(n + 1);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  bool b[n + 1];
  ll mod = 1e9 + 7;
  for (int i = 0; i <= n; i++)
    b[i] = 0;
  pair<ll, ll> ans[n + 1];
  for (int i = 0; i <= n; i++)
    ans[i] = {0, 0};
  ans[1] = func(v, b, 1, mod);
  ll k = (ans[1].first + ans[1].second) % mod;
  cout << k;
  return 0;
}

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

typedef long long ll;
pair<ll, ll> func(vector<vector<int>> &v, bool b[], int ind, ll &mod) {
  b[ind] = 1;
  ll t = 1, t1 = 1;
  for (int i = 0; i < v[ind].size(); i++) {
    if (b[v[ind][i]] == 0) {
      pair<ll, ll> p;
      p = func(v, b, v[ind][i], mod);
      t = (t * ((p.first + p.second) % mod)) % mod;
      t1 = (t1 * p.second) % mod;
    }
  }
  return {t1, t};
}
int main() {
  int n;
  cin >> n;
  if (n == 1) {
    cout << "2";
    return 0;
  }
  vector<vector<int>> v(n + 1);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  bool b[n + 1];
  ll mod = 1e9 + 7;
  for (int i = 0; i <= n; i++)
    b[i] = 0;
  pair<ll, ll> ans[n + 1];
  for (int i = 0; i <= n; i++)
    ans[i] = {0, 0};
  ans[1] = func(v, b, 1, mod);
  ll k = (ans[1].first + ans[1].second) % mod;
  cout << k;
  return 0;
}
Problem p03175 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
ll mod = 1e9 + 7;
pair<ll, ll> dfs(int s, vector<int> graph[], vector<bool> visited) {
  visited[s] = 1;
  pair<ll, ll> pr = {1, 1};
  for (int v : graph[s]) {
    if (!visited[v]) {
      pair<ll, ll> pr1 = dfs(v, graph, visited);
      pr.first *= pr1.second;
      pr.first %= mod;
      pr.second *= (pr1.second + pr1.first);
      pr.second %= mod;
    }
  }
  return pr;
}
int main() {
  int n;
  cin >> n;
  vector<int> graph[n + 1];
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  vector<bool> visited(n + 1, false);
  pair<ll, ll> pr = dfs(1, graph, visited);
  cout << (pr.first + pr.second) % mod;
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
using ll = long long;
ll mod = 1e9 + 7;
pair<ll, ll> dfs(int s, vector<int> graph[], vector<bool> &visited) {
  visited[s] = 1;
  pair<ll, ll> pr = {1, 1};
  for (int v : graph[s]) {
    if (!visited[v]) {
      pair<ll, ll> pr1 = dfs(v, graph, visited);
      pr.first *= pr1.second;
      pr.first %= mod;
      pr.second *= (pr1.second + pr1.first);
      pr.second %= mod;
    }
  }
  return pr;
}
int main() {
  int n;
  cin >> n;
  vector<int> graph[n + 1];
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  vector<bool> visited(n + 1, false);
  pair<ll, ll> pr = dfs(1, graph, visited);
  cout << (pr.first + pr.second) % mod;
}
Problem p03175 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
#define pb emplace_back

using namespace std;

const int MAXN = 1e5 + 10, MD = 1e9 + 7;
int n;
ll dp[MAXN][2];
vector<int> g[MAXN];

void Dfs(int v = 1, int p = -1) {
  dp[v][0] = 1;
  dp[v][1] = 1;
  for (auto i : g[v]) {
    if (p == i)
      continue;
    Dfs(i, v);
    dp[v][0] = dp[v][0] * (dp[i][0] + dp[i][1]) % MD;
    dp[v][1] = dp[v][1] * dp[i][0] % MD;
  }
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cout.tie(0);

  cin >> n;
  for (int i = 0; i < n; ++i) {
    int a, b;
    cin >> a >> b;
    g[a].pb(b);
    g[b].pb(a);
  }
  Dfs();
  ll ans = dp[1][0] + dp[1][1];
  cout << (ans + MD) % MD << '\n';

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
#define pb emplace_back

using namespace std;

const int MAXN = 1e5 + 10, MD = 1e9 + 7;
int n;
ll dp[MAXN][2];
vector<int> g[MAXN];

void Dfs(int v = 1, int p = -1) {
  dp[v][0] = 1;
  dp[v][1] = 1;
  for (auto i : g[v]) {
    if (p == i)
      continue;
    Dfs(i, v);
    dp[v][0] = dp[v][0] * (dp[i][0] + dp[i][1]) % MD;
    dp[v][1] = dp[v][1] * dp[i][0] % MD;
  }
}

int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cout.tie(0);

  cin >> n;
  for (int i = 0; i < n - 1; ++i) {
    int a, b;
    cin >> a >> b;
    g[a].pb(b);
    g[b].pb(a);
  }
  Dfs();
  ll ans = dp[1][0] + dp[1][1];
  cout << (ans + MD) % MD << '\n';

  return 0;
}
Problem p03175 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
int f[100001][2];
int head[100001], to[100001], nxt[100001];
int tot;
int n;
void addedge(int u, int v) {
  tot++;
  nxt[tot] = head[u];
  to[tot] = v;
  head[u] = tot;
}
void dfs(int index, int p) {
  f[index][0] = f[index][1] = 1;
  for (int i = head[index]; i; i = nxt[i]) {
    int v = to[i];
    if (p == v)
      continue;
    dfs(v, index);
    f[index][0] = f[index][0] * ((long long)f[v][0] + f[v][1]) % 1000000007;
    f[index][1] = f[index][1] * (long long)f[v][0] % 1000000007;
  }
  return;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    addedge(u, v);
    addedge(v, u);
  }
  dfs(1, 0);
  printf("%lld", ((long long)f[1][0] + f[1][1]) % 1000000007);
}

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
int f[100001][2];
int head[100001], to[200001], nxt[200001];
int tot;
int n;
void addedge(int u, int v) {
  tot++;
  nxt[tot] = head[u];
  to[tot] = v;
  head[u] = tot;
}
void dfs(int index, int p) {
  f[index][0] = f[index][1] = 1;
  for (int i = head[index]; i; i = nxt[i]) {
    int v = to[i];
    if (p == v)
      continue;
    dfs(v, index);
    f[index][0] = f[index][0] * ((long long)f[v][0] + f[v][1]) % 1000000007;
    f[index][1] = f[index][1] * (long long)f[v][0] % 1000000007;
  }
  return;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d%d", &u, &v);
    addedge(u, v);
    addedge(v, u);
  }
  dfs(1, 0);
  printf("%lld", ((long long)f[1][0] + f[1][1]) % 1000000007);
}
Problem p03175 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>

using namespace std;

// dp[i][j]=(頂点iを(j = 1黒く: j =
// 0白く)塗ったとき、iを親とする部分木の塗り方の場合の数) int dp[100100][2];
int dp[100][2];

vector<vector<int>> g;
const int MOD = 1e9 + 7;

void dfs(int v, int p) {
  dp[v][0] = 1;
  dp[v][1] = 1;

  for (auto next_n : g[v]) {
    if (next_n == p)
      continue;
    dfs(next_n, v);
    // 自分が白なら白黒どっちでもok
    dp[v][0] = dp[v][0] * (dp[next_n][0] + dp[next_n][1]);
    dp[v][0] %= MOD;
    // 黒ならその前は絶対に白
    dp[v][1] = dp[v][1] * dp[next_n][0];
    dp[v][1] %= MOD;
  }
}

int main() {
  int N;
  cin >> N;

  g.resize(N);

  for (int i = 0; i < N - 1; ++i) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;

    g[x].push_back(y);
    g[y].push_back(x);
  }

  dfs(0, -1);
  cout << (dp[0][0] + dp[0][1]) % MOD << endl;
}

Fixed submission:
#include <iostream>
#include <vector>

using namespace std;

// dp[i][j]=(頂点iを(j = 1黒く: j =
// 0白く)塗ったとき、iを親とする部分木の塗り方の場合の数)
long long dp[100100][2];
// long long dp[100][2];

vector<vector<int>> g;
const int MOD = 1e9 + 7;

void dfs(int v, int p) {
  dp[v][0] = 1;
  dp[v][1] = 1;

  for (auto next_n : g[v]) {
    if (next_n == p)
      continue;
    dfs(next_n, v);
    // 自分が白なら白黒どっちでもok
    dp[v][0] = dp[v][0] * (dp[next_n][0] + dp[next_n][1]);
    dp[v][0] %= MOD;
    // 黒ならその前は絶対に白
    dp[v][1] = dp[v][1] * dp[next_n][0];
    dp[v][1] %= MOD;
  }
}

int main() {
  int N;
  cin >> N;

  g.resize(N);

  for (int i = 0; i < N - 1; ++i) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;

    g[x].push_back(y);
    g[y].push_back(x);
  }

  dfs(0, -1);
  cout << (dp[0][0] + dp[0][1]) % MOD << endl;
}
Problem p03175 (C++)

Buggy submission (status: Runtime Error):
//                                      KiSmAt
#include "bits/stdc++.h"
using namespace std;

using ll = long long;

const ll inf = 1e18;
const int mod = 1e9 + 7;
const int N = 2 * 1e5 + 10;

ll res;
ll a[N], dp[N][2];

vector<ll> v[N];

inline ll add(ll x, ll y) { return (x % mod + y % mod) % mod; }
inline ll sub(ll x, ll y) { return (x % mod - y % mod + mod) % mod; }
inline ll mul(ll x, ll y) {
  x %= mod;
  y %= mod;
  return 1LL * x * y % mod;
}

void dfs(ll u, ll par) {
  dp[u][0] = dp[u][1] = 1;
  for (auto i : v[u]) {
    if (i != par) {
      dfs(i, par);
      dp[u][0] = mul(dp[u][0], dp[i][1]);
      dp[u][1] = mul(dp[u][1], add(dp[i][0], dp[i][1]));
    }
  }
}

void solve() {
  ll n, x, y;
  cin >> n;
  for (int i = 0; i < n - 1; ++i) {
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  dfs(1, 1);
  cout << add(dp[1][0], dp[1][1]);
}

int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}
// nEro

Fixed submission:
//                                      KiSmAt
#include "bits/stdc++.h"
using namespace std;

using ll = long long;

const ll inf = 1e18;
const int mod = 1e9 + 7;
const int N = 2 * 1e5 + 10;

ll res;
ll a[N], dp[N][2];

vector<ll> v[N];

inline ll add(ll x, ll y) { return (x % mod + y % mod) % mod; }
inline ll sub(ll x, ll y) { return (x % mod - y % mod + mod) % mod; }
inline ll mul(ll x, ll y) {
  x %= mod;
  y %= mod;
  return 1LL * x * y % mod;
}

void dfs(ll u, ll par) {
  dp[u][0] = dp[u][1] = 1;
  for (auto i : v[u]) {
    if (i != par) {
      dfs(i, u);
      dp[u][0] = mul(dp[u][0], dp[i][1]);
      dp[u][1] = mul(dp[u][1], add(dp[i][0], dp[i][1]));
    }
  }
}

void solve() {
  ll n, x, y;
  cin >> n;
  for (int i = 0; i < n - 1; ++i) {
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  dfs(1, 1);
  cout << add(dp[1][0], dp[1][1]);
}

int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  ll t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
}
// nEro
Problem p03175 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define ALL(x) x.begin(), x.end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;

ll mod = 1e9 + 7;

vector<vi> memo(10123, vi(2));
vector<vi> adj(10123);

void dfs(int u, int p) {
  if (memo[u][0]) {
    return;
  }

  ll black = 1, white = 1;
  for (int v : adj[u]) {
    if (v != p) {
      dfs(v, u);
      black *= memo[v][1];
      black %= mod;
      white *= memo[v][1] + memo[v][0];
      white %= mod;
    }
  }

  memo[u][0] = black;
  memo[u][1] = white;
}

void solve(void) {

  int n;
  cin >> n;

  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  dfs(1, 0);
  ll ans = (memo[1][1] + memo[1][0]) % mod;
  cout << ans << '\n';
}

int main(void) {
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define ALL(x) x.begin(), x.end()

using namespace std;

typedef long long ll;
typedef vector<int> vi;

ll mod = 1e9 + 7;

vector<vi> memo(100123, vi(2));
vector<vi> adj(100123);

void dfs(int u, int p) {
  if (memo[u][0]) {
    return;
  }

  ll black = 1, white = 1;
  for (int v : adj[u]) {
    if (v != p) {
      dfs(v, u);
      black *= memo[v][1];
      black %= mod;
      white *= memo[v][1] + memo[v][0];
      white %= mod;
    }
  }

  memo[u][0] = black;
  memo[u][1] = white;
}

void solve(void) {

  int n;
  cin >> n;

  for (int i = 0; i < n - 1; i++) {
    int u, v;
    cin >> u >> v;
    adj[u].push_back(v);
    adj[v].push_back(u);
  }

  dfs(1, 0);
  ll ans = (memo[1][1] + memo[1][0]) % mod;
  cout << ans << '\n';
}

int main(void) {
  solve();
  return 0;
}
Problem p03175 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
pair<long long, long long> dfs(vector<vector<int>> c, int v) {
  long long ans0 = 1, ans1 = 1;
  for (int w : c[v]) {
    pair<long long, long long> P = dfs(c, w);
    ans0 *= (P.first + P.second);
    ans0 %= MOD;
    ans1 *= P.first;
    ans1 %= MOD;
  }
  return make_pair(ans0, ans1);
}
int main() {
  int N;
  cin >> N;
  vector<vector<int>> E(N);
  for (int i = 0; i < N - 1; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    E[x].push_back(y);
    E[y].push_back(x);
  }
  vector<int> p(N, -1);
  queue<int> Q;
  Q.push(0);
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop();
    for (int w : E[v]) {
      if (p[w] == -1) {
        p[w] = v;
        Q.push(w);
      }
    }
  }
  vector<vector<int>> c(N);
  for (int i = 1; i < N; i++) {
    c[p[i]].push_back(i);
  }
  pair<long long, long long> P = dfs(c, 0);
  cout << (P.first + P.second) % MOD << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
pair<long long, long long> dfs(vector<vector<int>> &c, int v) {
  long long ans0 = 1, ans1 = 1;
  for (int w : c[v]) {
    pair<long long, long long> P = dfs(c, w);
    ans0 *= (P.first + P.second);
    ans0 %= MOD;
    ans1 *= P.first;
    ans1 %= MOD;
  }
  return make_pair(ans0, ans1);
}
int main() {
  int N;
  cin >> N;
  vector<vector<int>> E(N);
  for (int i = 0; i < N - 1; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    E[x].push_back(y);
    E[y].push_back(x);
  }
  vector<int> p(N, -1);
  queue<int> Q;
  Q.push(0);
  while (!Q.empty()) {
    int v = Q.front();
    Q.pop();
    for (int w : E[v]) {
      if (p[w] == -1) {
        p[w] = v;
        Q.push(w);
      }
    }
  }
  vector<vector<int>> c(N);
  for (int i = 1; i < N; i++) {
    c[p[i]].push_back(i);
  }
  pair<long long, long long> P = dfs(c, 0);
  cout << (P.first + P.second) % MOD << endl;
}
Problem p03175 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
#define sz 100100

ll dp[sz][2];

inline ll mul(ll a, ll b) {
  a = ((a % MOD) + MOD) % MOD;
  b = ((b % MOD) + MOD) % MOD;
  return (a * b) % MOD;
}

inline ll add(ll a, ll b) {
  a = ((a % MOD) + MOD) % MOD;
  b = ((b % MOD) + MOD) % MOD;
  return (a + b) % MOD;
}

vector<int> adj[sz];

ll dfs(int rt, bool st, int par) {

  if (adj[rt].size() == 1 && adj[rt][0] == par) {
    if (st)
      return 2;
    return 1;
  }

  ll ans = dp[rt][st];
  if (ans != -1)
    return ans;

  ans = 1;

  for (auto it : adj[rt]) {
    if (it == par)
      continue;
    ans = mul(ans, dfs(it, 1, rt));
  }

  if (st) {
    ll ans2 = 1;
    for (auto it : adj[rt]) {
      if (it == par)
        continue;
      ans2 = mul(ans2, dfs(it, 0, rt));
    }
    ans = add(ans, ans2);
  }

  return ans;
}

int main() {

  memset(dp, -1, sizeof(dp));

  int n, a, b;
  scanf("%d", &n);

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

  cout << dfs(1, 1, -1);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define MOD 1000000007
#define sz 100100

ll dp[sz][2];

inline ll mul(ll a, ll b) {
  a = ((a % MOD) + MOD) % MOD;
  b = ((b % MOD) + MOD) % MOD;
  return (a * b) % MOD;
}

inline ll add(ll a, ll b) {
  a = ((a % MOD) + MOD) % MOD;
  b = ((b % MOD) + MOD) % MOD;
  return (a + b) % MOD;
}

vector<int> adj[sz];

ll dfs(int rt, bool st, int par) {

  if (adj[rt].size() == 1 && adj[rt][0] == par) {
    if (st)
      return 2;
    return 1;
  }

  ll &ans = dp[rt][st];
  if (ans != -1)
    return ans;

  ans = 1;

  for (auto it : adj[rt]) {
    if (it == par)
      continue;
    ans = mul(ans, dfs(it, 1, rt));
  }

  if (st) {
    ll ans2 = 1;
    for (auto it : adj[rt]) {
      if (it == par)
        continue;
      ans2 = mul(ans2, dfs(it, 0, rt));
    }
    ans = add(ans, ans2);
  }

  return ans;
}

int main() {

  memset(dp, -1, sizeof(dp));

  int n, a, b;
  scanf("%d", &n);

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

  cout << dfs(1, 1, -1);

  return 0;
}
Problem p03175 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>

const long long INF = 1000000007;

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<pii, pii> piiii;

int n;
vector<int> adj[10011];
bool visit[10011];
ll db[10011];
ll dw[10011];

ll dp(int x) {
  if (visit[x])
    return (db[x] + dw[x]) % INF;
  ll ansb = 1, answ = 1;
  visit[x] = true;
  for (int v : adj[x]) {
    if (visit[v])
      continue;
    answ = (answ * dp(v)) % INF;
    ansb = (ansb * dw[v]) % INF;
  }
  dw[x] = answ;
  db[x] = ansb;
  return (answ + ansb) % INF;
}

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

  fill(visit + 1, visit + n + 1, false);

  printf("%d\n", dp(1));
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <string>
#include <vector>

const long long INF = 1000000007;

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
typedef pair<pii, pii> piiii;

int n;
vector<int> adj[100011];
bool visit[100011];
ll db[100011];
ll dw[100011];

ll dp(int x) {
  if (visit[x])
    return (db[x] + dw[x]) % INF;
  ll ansb = 1, answ = 1;
  visit[x] = true;
  for (int v : adj[x]) {
    if (visit[v])
      continue;
    answ = (answ * dp(v)) % INF;
    ansb = (ansb * dw[v]) % INF;
  }
  dw[x] = answ;
  db[x] = ansb;
  return (answ + ansb) % INF;
}

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

  fill(visit + 1, visit + n + 1, false);

  printf("%d\n", dp(1));
}
Problem p03175 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 3e5 + 5;
const ll M = 1e9 + 7;
vector<int> v[N];
ll dp[N][2];
void dfs(int s, int p) {
  dp[s][0] = dp[s][1] = 1;
  for (auto i : v[s]) {

    dfs(i, s);
    dp[s][0] = (dp[s][0] * dp[i][1]) % M;
    dp[s][1] = (dp[s][1] * ((dp[i][0] + dp[i][1]) % M)) % M;
  }
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  dfs(1, 1);
  cout << (dp[1][0] + dp[1][1]) % M;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N = 3e5 + 5;
const ll M = 1e9 + 7;
vector<int> v[N];
ll dp[N][2];
void dfs(int s, int p) {
  dp[s][0] = dp[s][1] = 1;
  for (auto i : v[s]) {
    if (i != p) {
      dfs(i, s);
      dp[s][0] = (dp[s][0] * dp[i][1]) % M;
      dp[s][1] = (dp[s][1] * ((dp[i][0] + dp[i][1]) % M)) % M;
    }
  }
}
int main() {
  ios_base ::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int x, y;
    cin >> x >> y;
    v[x].push_back(y);
    v[y].push_back(x);
  }
  dfs(1, 1);
  cout << (dp[1][0] + dp[1][1]) % M;
  return 0;
}
Problem p03175 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(c) (c).rbegin(), (c).rend()
#define mp make_pair
#define double long double
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll inf = 1e9 + 7;
const ll mod = 1e9 + 7;
ll n;
vector<vector<ll>> G(100010);
vector<vector<ll>> dp(100010, vector<ll>(2, -1));
ll dfs(ll i, ll p, ll c) {
  if (dp[i][c] != -1)
    return dp[i][c];
  ll res = 1;
  for (auto e : G[i]) {
    if (e == p)
      continue;
    ll a = dfs(e, i, 0);
    ll b = dfs(e, i, 1);
    if (c)
      res = res % inf * a % inf;
    else
      res = res % inf * (a + b) % inf;
  }
  return res;
}
int main() {
  cin >> n;
  rep(i, n - 1) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    G[x].pb(y);
    G[y].pb(x);
  }
  cout << (dfs(0, -1, 0) + dfs(0, -1, 1)) % inf << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
#define REP(i, m, n) for (int i = (m); i < (n); i++)
#define rep(i, n) REP(i, 0, n)
#define pb push_back
#define all(a) a.begin(), a.end()
#define rall(c) (c).rbegin(), (c).rend()
#define mp make_pair
#define double long double
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll inf = 1e9 + 7;
const ll mod = 1e9 + 7;
ll n;
vector<vector<ll>> G(100010);
vector<vector<ll>> dp(100010, vector<ll>(2, -1));
ll dfs(ll i, ll p, ll c) {
  if (dp[i][c] != -1)
    return dp[i][c];
  ll res = 1;
  for (auto e : G[i]) {
    if (e == p)
      continue;
    ll a = dfs(e, i, 0);
    ll b = dfs(e, i, 1);
    if (c)
      res = res % inf * a % inf;
    else
      res = res % inf * (a + b) % inf;
  }
  dp[i][c] = res;
  return res;
}
int main() {
  cin >> n;
  rep(i, n - 1) {
    ll x, y;
    cin >> x >> y;
    x--;
    y--;
    G[x].pb(y);
    G[y].pb(x);
  }
  cout << (dfs(0, -1, 0) + dfs(0, -1, 1)) % inf << endl;
}
Problem p03175 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;

#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)

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

template <typename T> void cmpmax(T &reference, T value) {
  reference = max(reference, value);
}

template <typename T> void cmpmin(T &reference, T value) {
  reference = min(reference, value);
}

const ll mod = 1000000007;

static const ll INF = 1e15;

int n;
vector<vector<int>> graph;
vector<vector<ll>> dp;
const int white = 0;
const int black = 1;

void dfs(int c, int from) {
  ll w = 1;
  ll b = 1;
  for (auto p : graph[c]) {
    if (p == from)
      continue;
    dfs(p, c);
    w *= (dp[p][white] + dp[p][black]);
    b *= dp[p][white];
    w %= mod;
    b %= mod;
  }
  dp[c][white] = w;
  dp[c][black] = b;
}

int main() {
  cin >> n;
  graph.resize(n);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  int leaf = -1;
  rep(i, n) {
    if (graph[i].size() == 1) {
      leaf = i;
      break;
    }
  }
  dp.resize(n, vector<ll>(2, 0));
  dfs(leaf, -1);
  auto sum = dp[leaf][white] + dp[leaf][black];
  sum %= mod;
  cout << sum << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;

#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)

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

template <typename T> void cmpmax(T &reference, T value) {
  reference = max(reference, value);
}

template <typename T> void cmpmin(T &reference, T value) {
  reference = min(reference, value);
}

const ll mod = 1000000007;

static const ll INF = 1e15;

int n;
vector<vector<int>> graph;
vector<vector<ll>> dp;
const int white = 0;
const int black = 1;

void dfs(int c, int from) {
  ll w = 1;
  ll b = 1;
  for (auto p : graph[c]) {
    if (p == from)
      continue;
    dfs(p, c);
    w *= (dp[p][white] + dp[p][black]);
    b *= dp[p][white];
    w %= mod;
    b %= mod;
  }
  dp[c][white] = w;
  dp[c][black] = b;
}

int main() {
  cin >> n;
  graph.resize(n);
  rep(i, n - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    graph[a].push_back(b);
    graph[b].push_back(a);
  }
  int leaf = 0;
  rep(i, n) {
    if (graph[i].size() == 1) {
      leaf = i;
      break;
    }
  }
  dp.resize(n, vector<ll>(2, 0));
  dfs(leaf, -1);
  auto sum = dp[leaf][white] + dp[leaf][black];
  sum %= mod;
  cout << sum << endl;
  return 0;
}
Problem p03175 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define md 1000000007
#define mx 1e18
#define pb push_back
#define endl '\n'
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define pii pair<ll int, ll int>
#define ff first
#define ss second

ll int n;
map<ll int, vector<ll int>> g;
ll int dp[100001][2];

void dfs(ll int u = 1, ll int p = 0) {
  for (auto v : g[u]) {
    if (v == p)
      continue;
    dfs(v, u);

    dp[u][1] *= (dp[v][0] + dp[v][1]);
    dp[u][1] %= md;
    dp[u][0] *= (dp[v][1]);
    dp[u][0] %= md;
  }
}

int main() {
  fastio
#ifndef ONLINE_JUDGE
      freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  cin >> n;
  for (ll int i = 1; i < n; i++) {
    ll int u, v;
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
  }
  for (ll int i = 1; i <= n; i++)
    for (ll int j = 0; j <= 1; j++)
      dp[i][j] = 1;

  dfs();

  cout << (dp[1][0] + dp[1][1]) % md << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define md 1000000007
#define mx 1e18
#define pb push_back
#define endl '\n'
#define fastio                                                                 \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cout.tie(NULL);
#define pii pair<ll int, ll int>
#define ff first
#define ss second

ll int n;
map<ll int, vector<ll int>> g;
ll int dp[100001][2];

void dfs(ll int u = 1, ll int p = 0) {
  for (auto v : g[u]) {
    if (v == p)
      continue;
    dfs(v, u);

    dp[u][1] *= (dp[v][0] + dp[v][1]);
    dp[u][1] %= md;
    dp[u][0] *= (dp[v][1]);
    dp[u][0] %= md;
  }
}

int main() {
  // fastio
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt" , "r" , stdin);
  // freopen("output.txt" , "w" , stdout);
  // #endif

  cin >> n;
  for (ll int i = 1; i < n; i++) {
    ll int u, v;
    cin >> u >> v;
    g[u].pb(v);
    g[v].pb(u);
  }
  for (ll int i = 1; i <= n; i++)
    for (ll int j = 0; j <= 1; j++)
      dp[i][j] = 1;

  dfs();

  cout << (dp[1][0] + dp[1][1]) % md << endl;

  return 0;
}
Problem p03175 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define _overload3(_1, _2, _3, _4, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n, s) for (int i = a; s > 0 ? i < n : i > n; i += s)
#define rep(...) _overload3(__VA_ARGS__, per, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmax(T &a, const T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int inf = 1e9 + 10, mod = 1e9 + 7;
const long long llinf = 1e18;
// head
int black[10001];
int white[10001];
int visited[10001];
vector<int> G[10001];

void dfs(int node) {
  visited[node] = 1;
  long long b = 1, w = 1;
  for (auto v : G[node]) {
    if (!visited[v]) {
      dfs(v);
      b = (b * white[v]) % mod;
      w = (w * (white[v] + black[v]) % mod) % mod;
    }
  }
  black[node] = b;
  white[node] = w;
}

int main() {
  int n;
  cin >> n;
  rep(i, n - 1) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  dfs(0);
  cout << (black[0] + white[0]) % mod << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define _overload3(_1, _2, _3, _4, name, ...) name
#define _rep(i, n) repi(i, 0, n)
#define repi(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n, s) for (int i = a; s > 0 ? i < n : i > n; i += s)
#define rep(...) _overload3(__VA_ARGS__, per, repi, _rep, )(__VA_ARGS__)
#define all(x) (x).begin(), (x).end()
template <class T> inline bool chmax(T &a, const T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, const T b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int inf = 1e9 + 10, mod = 1e9 + 7;
const long long llinf = 1e18;
// head
int black[100001];
int white[100001];
int visited[100001];
vector<int> G[100001];

void dfs(int node) {
  visited[node] = 1;
  long long b = 1, w = 1;
  for (auto v : G[node]) {
    if (!visited[v]) {
      dfs(v);
      b = (b * white[v]) % mod;
      w = (w * (white[v] + black[v]) % mod) % mod;
    }
  }
  black[node] = b;
  white[node] = w;
}

int main() {
  int n;
  cin >> n;
  rep(i, n - 1) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    G[x].push_back(y);
    G[y].push_back(x);
  }
  dfs(0);
  cout << (black[0] + white[0]) % mod << endl;
}
Problem p03175 (C++)

Buggy submission (status: Runtime Error):
// #pragma GCC optimize "trapv"
#include <bits/stdc++.h>
#define ll long long int
#define fab(a, b, i) for (int i = a; i < b; i++)
#define pb push_back
#define db double
#define mp make_pair
#define endl "\n"
#define f first
#define se second
#define all(x) x.begin(), x.end()
#define MOD 1000000007
#define quick                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
using namespace std;
vector<int> v[100005];
ll dp[100005][2];
void dfs(int src, int par)

{
  // cout<<"src:"<<src<<" "<<pr<<endl;

  dp[src][0] = dp[src][1] = 1;

  for (auto i : v[src]) {
    if (i == par)
      continue;
    dfs(i, src);

    dp[src][0] = ((dp[i][1] + dp[i][0]) * dp[src][0]) % MOD;

    dp[src][1] = (dp[src][1] * dp[i][0]) % MOD;
  }
  // cout<<"dp:"<<dp[src][pr]<<endl;
}

int main() {
  quick;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  int n;
  cin >> n;
  fab(0, n + 1, i) dp[i][0] = dp[i][1] = -1;
  fab(1, n, i) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    v[x].pb(y);
    v[y].pb(x);
  }
  dfs(0, -1);
  cout << (dp[0][1] + dp[0][0]) % MOD << endl;

  return 0;
}

Fixed submission:
// #pragma GCC optimize "trapv"
#include <bits/stdc++.h>
#define ll long long int
#define fab(a, b, i) for (int i = a; i < b; i++)
#define pb push_back
#define db double
#define mp make_pair
#define endl "\n"
#define f first
#define se second
#define all(x) x.begin(), x.end()
#define MOD 1000000007
#define quick                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL)
using namespace std;
vector<int> v[100005];
ll dp[100005][2];
void dfs(int src, int par)

{
  // cout<<"src:"<<src<<" "<<pr<<endl;

  dp[src][0] = dp[src][1] = 1;

  for (auto i : v[src]) {
    if (i == par)
      continue;
    dfs(i, src);

    dp[src][0] = ((dp[i][1] + dp[i][0]) * dp[src][0]) % MOD;

    dp[src][1] = (dp[src][1] * dp[i][0]) % MOD;
  }
  // cout<<"dp:"<<dp[src][pr]<<endl;
}

int main() {
  quick;

  int n;
  cin >> n;
  fab(0, n + 1, i) dp[i][0] = dp[i][1] = -1;
  fab(1, n, i) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    v[x].pb(y);
    v[y].pb(x);
  }
  dfs(0, -1);
  cout << (dp[0][1] + dp[0][0]) % MOD << endl;

  return 0;
}
Problem p03175 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const int MOD = 1e9 + 7;
int N;
vector<int> adj[11000];
ll dp[11000][2];

void dfs(int nd, int prv = -1) {
  dp[nd][0] = dp[nd][1] = 1;
  for (int nxt : adj[nd]) {
    if (nxt == prv)
      continue;
    dfs(nxt, nd);
    dp[nd][1] *= dp[nxt][0];
    dp[nd][0] *= dp[nxt][0] + dp[nxt][1];
    dp[nd][1] %= MOD;
    dp[nd][0] %= MOD;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  dfs(0);
  cout << (dp[0][0] + dp[0][1]) % MOD << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;

const int MOD = 1e9 + 7;
int N;
vector<int> adj[110000];
ll dp[110000][2];

void dfs(int nd, int prv = -1) {
  dp[nd][0] = dp[nd][1] = 1;
  for (int nxt : adj[nd]) {
    if (nxt == prv)
      continue;
    dfs(nxt, nd);
    dp[nd][1] *= dp[nxt][0];
    dp[nd][0] *= dp[nxt][0] + dp[nxt][1];
    dp[nd][1] %= MOD;
    dp[nd][0] %= MOD;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> N;
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    adj[a].push_back(b);
    adj[b].push_back(a);
  }
  dfs(0);
  cout << (dp[0][0] + dp[0][1]) % MOD << '\n';
  return 0;
}
Problem p03175 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const long long mod = 1000000000 + 7;
vector<int> g[100000];
long long f[100000][2];
int dd[100000], d2[100000][2];
int n;
long long qhd(int u, int tt) {
  dd[u] = 1;
  long long sum = 1;
  if (d2[u][tt] == 0) {
    for (auto i = g[u].begin(); i != g[u].end(); ++i) {
      sum = 1;
      if (dd[*i] == 0) {
        sum = qhd(*i, 0);
        if (tt == 0)
          sum += qhd(*i, 1);
      }
      f[u][tt] = f[u][tt] * sum;
      f[u][tt] = f[u][tt] % mod;
    }
    d2[u][tt] = 1;
  }
  dd[u] = 0;
  return f[u][tt];
}
int main() {
  cin >> n;
  int u, v;
  for (int i = 0; i < n - 1; ++i) {
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i) {
    f[i][0] = 1;
    f[i][1] = 1;
  }
  cout << (qhd(1, 0) + qhd(1, 1)) % mod;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
const long long mod = 1000000000 + 7;
vector<int> g[100001];
long long f[100001][2];
int dd[100001], d2[100001][2];
int n;
long long qhd(int u, int tt) {
  dd[u] = 1;
  long long sum = 1;
  if (d2[u][tt] == 0) {
    for (auto i = g[u].begin(); i != g[u].end(); ++i) {
      sum = 1;
      if (dd[*i] == 0) {
        sum = qhd(*i, 0);
        if (tt == 0)
          sum += qhd(*i, 1);
      }
      f[u][tt] = f[u][tt] * sum;
      f[u][tt] = f[u][tt] % mod;
    }
    d2[u][tt] = 1;
  }
  dd[u] = 0;
  return f[u][tt];
}
int main() {
  cin >> n;
  int u, v;
  for (int i = 0; i < n - 1; ++i) {
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  for (int i = 1; i <= n; ++i) {
    f[i][0] = 1;
    f[i][1] = 1;
  }
  cout << (qhd(1, 0) + qhd(1, 1)) % mod;
}