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

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

typedef long long LL;

const int maxn = 1e5 + 7;
const int inf = 2e9;
const int mod = 1e9 + 7;

int dis[maxn], head[maxn];
bool vis[maxn];
int n, cnt = 0, cnt_Fen = 0, cnt_Su = 0;
struct s {
  int u;
  int v;
  int next;
} edge[maxn];
void add(int u, int v) {
  edge[cnt].u = u;
  edge[cnt].v = v;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}
void dfs_Fen(int pos, int len) {
  dis[pos] = len;
  for (int i = head[pos]; i != -1; i = edge[i].next)
    if (!vis[edge[i].v]) {
      vis[edge[i].v] = true;
      dfs_Fen(edge[i].v, len + 1);
    }
}
void dfs_Su(int pos, int len) {
  if (len < dis[pos])
    cnt_Su++;
  else
    cnt_Fen++;
  for (int i = head[pos]; i != -1; i = edge[i].next)
    if (!vis[edge[i].v]) {
      vis[edge[i].v] = true;
      dfs_Su(edge[i].v, len + 1);
    }
}
int main() {
#ifdef LOCAL
  freopen("sample.in", "r", stdin);
  // freopen("nico.out","w",stdout);
#endif
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  memset(head, -1, sizeof(head));
  memset(vis, false, sizeof(vis));
  memset(dis, 0, sizeof(dis));
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    add(a, b);
    add(b, a);
  }
  vis[1] = true;
  dfs_Fen(1, 0);
  memset(vis, false, sizeof(vis));
  vis[n] = true;
  dfs_Su(n, 0);
  // cout<<cnt_Su<<endl<<cnt_Fen<<endl;
  if (cnt_Su < cnt_Fen)
    cout << "Fennec" << endl;
  else
    cout << "Snuke" << endl;
  return 0;
}


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

typedef long long LL;

const int maxn = 2e5 + 7;
const int inf = 2e9;
const int mod = 1e9 + 7;

int dis[maxn], head[maxn];
bool vis[maxn];
int n, cnt = 0, cnt_Fen = 0, cnt_Su = 0;
struct s {
  int u;
  int v;
  int next;
} edge[maxn];
void add(int u, int v) {
  edge[cnt].u = u;
  edge[cnt].v = v;
  edge[cnt].next = head[u];
  head[u] = cnt++;
}
void dfs_Fen(int pos, int len) {
  dis[pos] = len;
  for (int i = head[pos]; i != -1; i = edge[i].next)
    if (!vis[edge[i].v]) {
      vis[edge[i].v] = true;
      dfs_Fen(edge[i].v, len + 1);
    }
}
void dfs_Su(int pos, int len) {
  if (len < dis[pos])
    cnt_Su++;
  else
    cnt_Fen++;
  for (int i = head[pos]; i != -1; i = edge[i].next)
    if (!vis[edge[i].v]) {
      vis[edge[i].v] = true;
      dfs_Su(edge[i].v, len + 1);
    }
}
int main() {
#ifdef LOCAL
  freopen("sample.in", "r", stdin);
  // freopen("nico.out","w",stdout);
#endif
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  memset(head, -1, sizeof(head));
  memset(vis, false, sizeof(vis));
  memset(dis, 0, sizeof(dis));
  for (int i = 1; i < n; i++) {
    int a, b;
    cin >> a >> b;
    add(a, b);
    add(b, a);
  }
  vis[1] = true;
  dfs_Fen(1, 0);
  memset(vis, false, sizeof(vis));
  vis[n] = true;
  dfs_Su(n, 0);
  // cout<<cnt_Su<<endl<<cnt_Fen<<endl;
  if (cnt_Su < cnt_Fen)
    cout << "Fennec" << endl;
  else
    cout << "Snuke" << endl;
  return 0;
}
Problem p03662 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1e9;
typedef pair<ll, ll> P;
const int xn = 2001;
vector<int> v[xn];
int pr[xn];
int ply[xn];
int ct[2];
int dfs(int from, int to) {
  pr[to] = from;
  for (int nx : v[to]) {
    if (from == nx)
      continue;
    dfs(to, nx);
  }
}
int dfs2(int from, int to, int py, int en) {
  ct[py]++;
  for (int nx : v[to]) {
    if (from == nx)
      continue;
    else if (ply[nx] == en)
      continue;
    dfs2(to, nx, py, en);
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  dfs(-1, 0);

  // for(int i=0;i<n;i++) cout<<pr[i]<<" "; cout<<endl;

  int pv = n - 1;
  vector<int> v2;
  v2.push_back(pv);
  while (true) {
    pv = pr[pv];
    if (pv == -1)
      break;
    v2.push_back(pv);
  }

  // for(int x:v2) cout<<x<<" "; cout<<endl;
  reverse(v2.begin(), v2.end());

  fill(ply, ply + n, -1);
  ct[0] = ct[1] = 0;
  int ii = 0, jj = v2.size() - 1;
  while (ii < jj) {
    ply[v2[ii]] = 0;
    ii++;
    ply[v2[jj]] = 1;
    jj--;
  }
  // for(int i=0;i<n;i++) cout<<ply[i]<<" "; cout<<endl;
  dfs2(-1, 0, 0, 1);
  ct[1] = n - ct[0];
  // cout<<ct[0]<<" "<<ct[1]<<endl;
  if (ct[0] <= ct[1])
    cout << "Snuke" << endl;
  else
    cout << "Fennec" << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
const int INF = 1e9;
typedef pair<ll, ll> P;
const int xn = 100001;
vector<int> v[xn];
int pr[xn];
int ply[xn];
int ct[2];
int dfs(int from, int to) {
  pr[to] = from;
  for (int nx : v[to]) {
    if (from == nx)
      continue;
    dfs(to, nx);
  }
}
int dfs2(int from, int to, int py, int en) {
  ct[py]++;
  for (int nx : v[to]) {
    if (from == nx)
      continue;
    else if (ply[nx] == en)
      continue;
    dfs2(to, nx, py, en);
  }
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  dfs(-1, 0);

  // for(int i=0;i<n;i++) cout<<pr[i]<<" "; cout<<endl;

  int pv = n - 1;
  vector<int> v2;
  v2.push_back(pv);
  while (true) {
    pv = pr[pv];
    if (pv == -1)
      break;
    v2.push_back(pv);
  }

  // for(int x:v2) cout<<x<<" "; cout<<endl;
  reverse(v2.begin(), v2.end());

  fill(ply, ply + n, -1);
  ct[0] = ct[1] = 0;
  int ii = 0, jj = v2.size() - 1;
  while (ii < jj) {
    ply[v2[ii]] = 0;
    ii++;
    ply[v2[jj]] = 1;
    jj--;
  }
  // for(int i=0;i<n;i++) cout<<ply[i]<<" "; cout<<endl;
  dfs2(-1, 0, 0, 1);
  ct[1] = n - ct[0];
  // cout<<ct[0]<<" "<<ct[1]<<endl;
  if (ct[0] <= ct[1])
    cout << "Snuke" << endl;
  else
    cout << "Fennec" << endl;
}
Problem p03662 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)

using namespace std;

vector<int> G[10005];

int main() {
  int N;
  cin >> N;
  REP(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<bool> used(N, false);
  queue<int> black[2], white[2];
  black[0].push(0);
  white[0].push(N - 1);
  int bct = 0, wct = 0;
  while (true) {
    int index = -1;
    bool haveblack = true;
    if (black[0].size()) {
      index = 0;
    } else if (black[1].size()) {
      index = 1;
    } else
      haveblack = false;
    while (haveblack && black[index].size()) {
      int now_p = black[index].front();
      black[index].pop();
      bct++;
      for (int next : G[now_p]) {
        if (used[next])
          continue;
        black[!index].push(next);
        used[next] = true;
      }
    }
    bool havewhite = true;
    if (white[0].size()) {
      index = 0;
    } else if (white[1].size()) {
      index = 1;
    } else
      havewhite = false;
    while (havewhite && white[index].size()) {
      int now_p = white[index].front();
      white[index].pop();
      wct++;
      for (int next : G[now_p]) {
        if (used[next])
          continue;
        white[!index].push(next);
        used[next] = true;
      }
    }
    if (!havewhite && !haveblack)
      break;
  }
  cout << (bct - wct > 0 ? "Fennec" : "Snuke") << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (n); i++)

using namespace std;

vector<int> G[100005];

int main() {
  int N;
  cin >> N;
  REP(i, N - 1) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  vector<bool> used(N, false);
  queue<int> black[2], white[2];
  black[0].push(0);
  white[0].push(N - 1);
  int bct = 0, wct = 0;
  while (true) {
    int index = -1;
    bool haveblack = true;
    if (black[0].size()) {
      index = 0;
    } else if (black[1].size()) {
      index = 1;
    } else
      haveblack = false;
    while (haveblack && black[index].size()) {
      int now_p = black[index].front();
      black[index].pop();
      bct++;
      for (int next : G[now_p]) {
        if (used[next])
          continue;
        black[!index].push(next);
        used[next] = true;
      }
    }
    bool havewhite = true;
    if (white[0].size()) {
      index = 0;
    } else if (white[1].size()) {
      index = 1;
    } else
      havewhite = false;
    while (havewhite && white[index].size()) {
      int now_p = white[index].front();
      white[index].pop();
      wct++;
      for (int next : G[now_p]) {
        if (used[next])
          continue;
        white[!index].push(next);
        used[next] = true;
      }
    }
    if (!havewhite && !haveblack)
      break;
  }
  cout << (bct - wct > 0 ? "Fennec" : "Snuke") << endl;
  return 0;
}
Problem p03662 (C++)

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

using namespace std;

const int N = 10010;

int n;
vector<int> a[N];
int b[N], cnt;
int f = 0, dead = 0;

void dfs(int u, int p) {
  b[++cnt] = u;
  if (u == n) {
    f = 1;
    return;
  }
  for (int v : a[u]) {
    if (v == p) {
      continue;
    }
    dfs(v, u);
    if (f) {
      return;
    }
  }
  cnt--;
}

int go(int u, int p) {
  int res = 1;
  for (int v : a[u]) {
    if (v == p || dead == v) {
      continue;
    }
    res += go(v, u);
  }
  return res;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    a[u].push_back(v);
    a[v].push_back(u);
  }
  dfs(1, 0);
  dead = b[(cnt + 1) / 2 + 1];
  int foo = go(1, 0);
  if (foo * 2 <= n) {
    puts("Snuke");
  } else {
    puts("Fennec");
  }
  return 0;
}


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

using namespace std;

const int N = 100010;

int n;
vector<int> a[N];
int b[N], cnt;
int f = 0, dead = 0;

void dfs(int u, int p) {
  b[++cnt] = u;
  if (u == n) {
    f = 1;
    return;
  }
  for (int v : a[u]) {
    if (v == p) {
      continue;
    }
    dfs(v, u);
    if (f) {
      return;
    }
  }
  cnt--;
}

int go(int u, int p) {
  int res = 1;
  for (int v : a[u]) {
    if (v == p || dead == v) {
      continue;
    }
    res += go(v, u);
  }
  return res;
}

int main() {
  scanf("%d", &n);
  for (int i = 1; i < n; i++) {
    int u, v;
    scanf("%d %d", &u, &v);
    a[u].push_back(v);
    a[v].push_back(u);
  }
  dfs(1, 0);
  dead = b[(cnt + 1) / 2 + 1];
  int foo = go(1, 0);
  if (foo * 2 <= n) {
    puts("Snuke");
  } else {
    puts("Fennec");
  }
  return 0;
}
Problem p03662 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> graph[102];
int dis[2][102];
void dfs(int d, int v, int p) {
  for (int i = 0; i < graph[v].size(); i++) {
    int y = graph[v][i];
    if (y != p) {
      dis[d][y] = dis[d][v] + 1;
      dfs(d, y, v);
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  dis[0][1] = 0;
  dfs(0, 1, 0);
  dis[1][n] = 0;
  dfs(1, n, 0);
  int cnt[2] = {0};
  for (int i = 1; i <= n; i++) {
    cnt[dis[0][i] > dis[1][i]]++;
  }
  string ans[2] = {"Fennec", "Snuke"};
  cout << ans[cnt[0] <= cnt[1]] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n;
vector<int> graph[100002];
int dis[2][100002];
void dfs(int d, int v, int p) {
  for (int i = 0; i < graph[v].size(); i++) {
    int y = graph[v][i];
    if (y != p) {
      dis[d][y] = dis[d][v] + 1;
      dfs(d, y, v);
    }
  }
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    graph[x].push_back(y);
    graph[y].push_back(x);
  }
  dis[0][1] = 0;
  dfs(0, 1, 0);
  dis[1][n] = 0;
  dfs(1, n, 0);
  int cnt[2] = {0};
  for (int i = 1; i <= n; i++) {
    cnt[dis[0][i] > dis[1][i]]++;
  }
  string ans[2] = {"Fennec", "Snuke"};
  cout << ans[cnt[0] <= cnt[1]] << endl;
  return 0;
}
Problem p03662 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstring>
using namespace std;
int e;
int v[100010 * 2], first[100010], next[100010 * 2];
void AddEdge(int U, int V) {
  v[++e] = V;
  next[e] = first[U];
  first[U] = e;
}
int n, fa[100010], dep[100010];
void dfs(int U) {
  for (int i = first[U]; i; i = next[i]) {
    if (!fa[v[i]]) {
      fa[v[i]] = U;
      dep[v[i]] = dep[U] + 1;
      dfs(v[i]);
    }
  }
}
bool vis[100010];
int X, Y, ans[2];
void dfs(int U, int op) {
  vis[U] = 1;
  ++ans[op];
  for (int i = first[U]; i; i = next[i]) {
    if (!vis[v[i]] && !(U == X && v[i] == Y) && !(U == Y && v[i] == X)) {
      dfs(v[i], op);
    }
  }
}
int main() {
  int x, y;
  scanf("%d", &n);
  for (int i = 1; i < n; ++i) {
    scanf("%d%d", &x, &y);
    AddEdge(x, y);
    AddEdge(y, x);
  }
  fa[1] = -1;
  dfs(1);
  int U = n;
  while (dep[U] != dep[n] / 2 + 1) {
    U = fa[n];
  }
  X = fa[U], Y = U;
  dfs(X, 0);
  memset(vis, 0, sizeof(vis));
  dfs(Y, 1);
  if (ans[0] <= ans[1]) {
    puts("Snuke");
  } else {
    puts("Fennec");
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
using namespace std;
int e;
int v[100010 * 2], first[100010], next[100010 * 2];
void AddEdge(int U, int V) {
  v[++e] = V;
  next[e] = first[U];
  first[U] = e;
}
int n, fa[100010], dep[100010];
void dfs(int U) {
  for (int i = first[U]; i; i = next[i]) {
    if (!fa[v[i]]) {
      fa[v[i]] = U;
      dep[v[i]] = dep[U] + 1;
      dfs(v[i]);
    }
  }
}
bool vis[100010];
int X, Y, ans[2];
void dfs(int U, int op) {
  vis[U] = 1;
  ++ans[op];
  for (int i = first[U]; i; i = next[i]) {
    if (!vis[v[i]] && !(U == X && v[i] == Y) && !(U == Y && v[i] == X)) {
      dfs(v[i], op);
    }
  }
}
int main() {
  int x, y;
  scanf("%d", &n);
  for (int i = 1; i < n; ++i) {
    scanf("%d%d", &x, &y);
    AddEdge(x, y);
    AddEdge(y, x);
  }
  fa[1] = -1;
  dfs(1);
  int U = n;
  while (dep[U] != dep[n] / 2 + 1) {
    U = fa[U];
  }
  X = fa[U], Y = U;
  dfs(X, 0);
  memset(vis, 0, sizeof(vis));
  dfs(Y, 1);
  if (ans[0] <= ans[1]) {
    puts("Snuke");
  } else {
    puts("Fennec");
  }
  return 0;
}
Problem p03662 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
vector<int> contact[300000];
int cnt1[100000];
int cnt2[100000];
bool u[100000] = {false};
void dfs(int i, int father, bool is_Fennec) {
  u[i] = true;
  for (int k = 0; k < contact[i].size(); k++) {
    if (is_Fennec == true) {
      cnt1[i] = father + 1;
    } else {
      cnt2[i] = father + 1;
    }
    if (!u[contact[i][k]]) {
      dfs(contact[i][k], father + 1, is_Fennec);
    }
  }
  return;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    contact[a].push_back(b);
    contact[b].push_back(a);
  }
  /*cout<<endl;
  for(int j=1;j<=n;j++)
  {
          cout<<j<<':';
          for(int k=0;k<contact[j].size();k++)
          {
                  cout<<contact[j][k]<<",";
          }
          cout<<endl;
  }
  cout<<endl;*/
  dfs(1, -1, true);
  memset(u, false, sizeof(u));
  dfs(n, -1, false);
  int f = 0, s = 0;
  for (int j = 1; j <= n; j++) {
    if (cnt1[j] <= cnt2[j]) {
      f++;
    } else {
      s++;
    }
  }
  if (f > s) {
    cout << "Fennec";
  } else {
    cout << "Snuke";
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> contact[300000];
int cnt1[100010];
int cnt2[100010];
bool u[100010] = {false};
void dfs(int i, int father, bool is_Fennec) {
  u[i] = true;
  for (int k = 0; k < contact[i].size(); k++) {
    if (is_Fennec == true) {
      cnt1[i] = father + 1;
    } else {
      cnt2[i] = father + 1;
    }
    if (!u[contact[i][k]]) {
      dfs(contact[i][k], father + 1, is_Fennec);
    }
  }
  return;
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    contact[a].push_back(b);
    contact[b].push_back(a);
  }
  /*cout<<endl;
  for(int j=1;j<=n;j++)
  {
          cout<<j<<':';
          for(int k=0;k<contact[j].size();k++)
          {
                  cout<<contact[j][k]<<",";
          }
          cout<<endl;
  }
  cout<<endl;*/
  dfs(1, -1, true);
  memset(u, false, sizeof(u));
  dfs(n, -1, false);
  int f = 0, s = 0;
  for (int j = 1; j <= n; j++) {
    if (cnt1[j] <= cnt2[j]) {
      f++;
    } else {
      s++;
    }
  }
  if (f > s) {
    cout << "Fennec";
  } else {
    cout << "Snuke";
  }
  return 0;
}
Problem p03662 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string.h>
#define MN 100000
#define MM 200000
int n, a, b, c1 = 0, c2 = 0;
int p[MN + 5], pn = 0, col[MN + 5];
int hd[MN + 5], nxt[MM + 5], to[MM + 5], rn = 0;
void add(int u, int v) {
  ++rn;
  nxt[rn] = hd[u];
  hd[u] = rn;
  to[rn] = v;
}
bool dfs(int u) {
  p[++pn] = u;
  if (u == n)
    return true;
  for (int i = hd[u]; i != -1; i = nxt[i])
    if (p[pn - 1] != to[i])
      if (dfs(to[i]))
        return true;
  --pn;
  return false;
}
void spr(int u) {
  for (int i = hd[u]; i != -1; i = nxt[i])
    if (col[to[i]] == 0) {
      col[to[i]] = col[u];
      spr(to[i]);
    }
}
int main() {
  memset(hd, 0xff, sizeof(hd));
  memset(col, 0, sizeof(col));
  scanf("%d", &n);
  for (int i = 1; i <= n - 1; i++) {
    scanf("%d%d", &a, &b);
    add(a, b), add(b, a);
  }
  dfs(1);
  for (int i = 1; i <= pn; i++)
    if (i <= ((pn + 1) >> 1))
      col[p[i]] = 1;
    else
      col[p[i]] = 2;
  spr(1);
  spr(n);
  for (int i = 1; i <= n; i++) {
    if (col[i] == 0)
      return 1;
    if (col[i] == 1)
      c1++;
    if (col[i] == 2)
      c2++;
  }
  if (c1 <= c2)
    puts("Snuke");
  else
    puts("Fennec");
}

Fixed submission:
#include <stdio.h>
#include <string.h>
#define MN 100000
#define MM 200000
int n, a, b, c1 = 0, c2 = 0;
int p[MN + 5], pn = 0, col[MN + 5];
int hd[MN + 5], nxt[MM + 5], to[MM + 5], rn = 0;
void add(int u, int v) {
  ++rn;
  nxt[rn] = hd[u];
  hd[u] = rn;
  to[rn] = v;
}
bool dfs(int u) {
  p[++pn] = u;
  if (u == n)
    return true;
  for (int i = hd[u]; i != -1; i = nxt[i])
    if (p[pn - 1] != to[i])
      if (dfs(to[i]))
        return true;
  --pn;
  return false;
}
void spr(int u) {
  for (int i = hd[u]; i != -1; i = nxt[i])
    if (col[to[i]] == 0) {
      col[to[i]] = col[u];
      spr(to[i]);
    }
}
int main() {
  memset(hd, 0xff, sizeof(hd));
  memset(col, 0, sizeof(col));
  scanf("%d", &n);
  for (int i = 1; i <= n - 1; i++) {
    scanf("%d%d", &a, &b);
    add(a, b), add(b, a);
  }
  dfs(1);
  for (int i = 1; i <= pn; i++)
    if (i <= ((pn + 1) >> 1))
      col[p[i]] = 1;
    else
      col[p[i]] = 2;
  for (int i = 1; i <= pn; i++)
    spr(p[i]);
  for (int i = 1; i <= n; i++) {
    if (col[i] == 0)
      return 1;
    if (col[i] == 1)
      c1++;
    if (col[i] == 2)
      c2++;
  }
  if (c1 <= c2)
    puts("Snuke");
  else
    puts("Fennec");
}
Problem p03662 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
std::vector<int> vec[100005];
int ans1[15], ans2[15];
int vis[15];
int cnt1, cnt2;
void bfs(int begin, int ans[]) {
  std::queue<int> q;
  ans[begin] = 0;
  vis[begin] = 1;
  for (int i = 0; i < vec[begin].size(); i++) {
    q.push(vec[begin][i]);
    ans[vec[begin][i]] = 1;
    vis[vec[begin][i]] = 1;
  }
  while (!q.empty()) {
    int z = q.front();
    int tmp = ans[z];
    q.pop();
    vis[z] = 1;
    for (int i = 0; i < vec[z].size(); i++) {
      if (vis[vec[z][i]])
        continue;
      vis[vec[z][i]] = 1;
      q.push(vec[z][i]);
      ans[vec[z][i]] = tmp + 1;
    }
  }
}
int main() {
  //    freopen("in.txt", "r", stdin);
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    vec[a].push_back(b);
    vec[b].push_back(a);
  }
  bfs(1, ans1);
  memset(vis, 0, sizeof(vis));
  bfs(n, ans2);
  for (int i = 1; i <= n; i++) {
    if (ans1[i] <= ans2[i])
      cnt1++;
    else
      cnt2++;
  }
  if (cnt1 > cnt2)
    printf("Fennec\n");
  else
    printf("Snuke\n");
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
std::vector<int> vec[100005];
int ans1[100005], ans2[100005];
int vis[100005];
int cnt1, cnt2;
void bfs(int begin, int ans[]) {
  std::queue<int> q;
  ans[begin] = 0;
  vis[begin] = 1;
  for (int i = 0; i < vec[begin].size(); i++) {
    q.push(vec[begin][i]);
    ans[vec[begin][i]] = 1;
    vis[vec[begin][i]] = 1;
  }
  while (!q.empty()) {
    int z = q.front();
    int tmp = ans[z];
    q.pop();
    vis[z] = 1;
    for (int i = 0; i < vec[z].size(); i++) {
      if (vis[vec[z][i]])
        continue;
      vis[vec[z][i]] = 1;
      q.push(vec[z][i]);
      ans[vec[z][i]] = tmp + 1;
    }
  }
}
int main() {
  //    freopen("in.txt", "r", stdin);
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    vec[a].push_back(b);
    vec[b].push_back(a);
  }
  bfs(1, ans1);
  memset(vis, 0, sizeof(vis));
  bfs(n, ans2);
  for (int i = 1; i <= n; i++) {
    if (ans1[i] <= ans2[i])
      cnt1++;
    else
      cnt2++;
  }
  if (cnt1 > cnt2)
    printf("Fennec\n");
  else
    printf("Snuke\n");
  return 0;
}
Problem p03662 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
#define N 110
int dis1[N], dis2[N];
int n;
vector<int> g[N];
void dfs(int cur, int pre, int len, int dist[]) {
  int now;
  dist[cur] = len;
  for (int i = 0; i < g[cur].size(); ++i) {
    now = g[cur][i];
    if (now != pre)
      dfs(now, cur, len + 1, dist);
  }
}
int main() {
  int i, x, y;
  scanf("%d", &n);
  for (i = 0; i < n - 1; ++i) {
    scanf("%d%d", &x, &y);
    g[x].push_back(y);
    g[y].push_back(x);
  }
  dfs(1, -1, 0, dis1);
  dfs(n, -1, 0, dis2);
  int ans = 0;
  for (i = 2; i < n; ++i) {
    if (dis1[i] <= dis2[i])
      ans++;
    else
      ans--;
  }
  if (ans > 0)
    cout << "Fennec";
  else
    cout << "Snuke";
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <vector>
using namespace std;
#define N 100005
int dis1[N], dis2[N];
int n;
vector<int> g[N];
void dfs(int cur, int pre, int len, int dist[]) {
  int now;
  dist[cur] = len;
  for (int i = 0; i < g[cur].size(); ++i) {
    now = g[cur][i];
    if (now != pre)
      dfs(now, cur, len + 1, dist);
  }
}
int main() {
  int i, x, y;
  scanf("%d", &n);
  for (i = 0; i < n - 1; ++i) {
    scanf("%d%d", &x, &y);
    g[x].push_back(y);
    g[y].push_back(x);
  }
  dfs(1, -1, 0, dis1);
  dfs(n, -1, 0, dis2);
  int ans = 0;
  for (i = 2; i < n; ++i) {
    if (dis1[i] <= dis2[i])
      ans++;
    else
      ans--;
  }
  if (ans > 0)
    cout << "Fennec";
  else
    cout << "Snuke";
  return 0;
}
Problem p03662 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
#define INF 0x3f3f3f3f
#define ESP 1e-8
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
const double PI = acos(-1.0);

#define N 100005
struct Edge {
  int from, to, next;
} e[N];
int head[N], ecnt, n;

void add(int idx, int to) {
  e[ecnt].from = idx, e[ecnt].to = to;
  e[ecnt].next = head[idx], head[idx] = ecnt++;
}

int d[2][N], c;
void dfs(int idx, int pa, int len) {
  d[c][idx] = len;
  int i = head[idx];
  while (i != -1) {
    if (e[i].to != pa)
      dfs(e[i].to, idx, len + 1);
    i = e[i].next;
  }
}

int main() {
  // FILE *fp = freopen("out.txt", "w", stdout);
  // freopen("in.txt", "r", stdin);
  int i, x, y;
  while (scanf("%d", &n) != EOF) {
    ecnt = 0;
    memset(head, -1, sizeof(head));
    for (i = 1; i < n; i++) {
      scanf("%d%d", &x, &y);
      add(x, y), add(y, x);
    }
    c = 0, dfs(1, 0, 0);
    c = 1, dfs(n, 0, 0);
    x = 0;
    for (i = 1; i <= n; i++)
      if (d[0][i] <= d[1][i])
        x++;
    if (x > n - x)
      printf("Fennec\n");
    else
      printf("Snuke\n");
  }

  // fclose(fp);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <map>
#include <math.h>
#include <set>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;

typedef long long ll;
#define INF 0x3f3f3f3f
#define ESP 1e-8
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
const double PI = acos(-1.0);

#define N 100005
struct Edge {
  int from, to, next;
} e[N * 2];
int head[N], ecnt, n;

void add(int idx, int to) {
  e[ecnt].from = idx, e[ecnt].to = to;
  e[ecnt].next = head[idx], head[idx] = ecnt++;
}

int d[2][N], c;
void dfs(int idx, int pa, int len) {
  d[c][idx] = len;
  int i = head[idx];
  while (i != -1) {
    if (e[i].to != pa)
      dfs(e[i].to, idx, len + 1);
    i = e[i].next;
  }
}

int main() {
  // FILE *fp = freopen("out.txt", "w", stdout);
  // freopen("in.txt", "r", stdin);
  int i, x, y;
  while (scanf("%d", &n) != EOF) {
    ecnt = 0;
    memset(head, -1, sizeof(head));
    for (i = 1; i < n; i++) {
      scanf("%d%d", &x, &y);
      add(x, y), add(y, x);
    }
    c = 0, dfs(1, 0, 0);
    c = 1, dfs(n, 0, 0);
    x = 0;
    for (i = 1; i <= n; i++)
      if (d[0][i] <= d[1][i])
        x++;
    if (x > n - x)
      printf("Fennec\n");
    else
      printf("Snuke\n");
  }

  // fclose(fp);
  return 0;
}
Problem p03662 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
using namespace std;
typedef long long LL;

#define _CRT_SECURE_NO_WARNINGS
int N;
vector<int> l[10010];
template <class T> class Node {
public:
  Node() : parent(0), child(0){};
  Node *parent;
  vector<Node *> child;
  T para;
};
Node<int> nodes[100010];
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    l[a].push_back(b);
    l[b].push_back(a);
  }

  nodes[1].para = 0;
  stack<int> s;
  s.push(1);
  while (!s.empty()) {
    int n = s.top();
    s.pop();
    for (int i = 0; i < l[n].size(); i++) {
      if (nodes[n].parent != &nodes[l[n][i]]) {
        nodes[n].child.push_back(&nodes[l[n][i]]);
        nodes[l[n][i]].parent = &nodes[n];
        nodes[l[n][i]].para = nodes[n].para + 1;
        s.push(l[n][i]);
      }
    }
  }

  Node<int> *a = &nodes[N];
  for (int i = 0; i < (nodes[N].para - 1) / 2; i++) {
    a = a->parent;
  }
  int c = 1;
  stack<Node<int> *> ss;
  ss.push(a);
  while (!ss.empty()) {
    Node<int> *n = ss.top();
    ss.pop();
    if (n->child.size() != 0) {
      for (int i = 0; i < n->child.size(); i++) {
        c++;
        ss.push(n->child[i]);
      }
    }
  }

  if (c >= N - c) {
    printf("Snuke\n");
  } else {
    printf("Fennec\n");
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
using namespace std;
typedef long long LL;

#define _CRT_SECURE_NO_WARNINGS
int N;
vector<int> l[100010];
template <class T> class Node {
public:
  Node() : parent(0), child(0){};
  Node *parent;
  vector<Node *> child;
  T para;
};
Node<int> nodes[100010];
int main() {
  scanf("%d", &N);
  for (int i = 0; i < N - 1; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    l[a].push_back(b);
    l[b].push_back(a);
  }

  nodes[1].para = 0;
  stack<int> s;
  s.push(1);
  while (!s.empty()) {
    int n = s.top();
    s.pop();
    for (int i = 0; i < l[n].size(); i++) {
      if (nodes[n].parent != &nodes[l[n][i]]) {
        nodes[n].child.push_back(&nodes[l[n][i]]);
        nodes[l[n][i]].parent = &nodes[n];
        nodes[l[n][i]].para = nodes[n].para + 1;
        s.push(l[n][i]);
      }
    }
  }

  Node<int> *a = &nodes[N];
  for (int i = 0; i < (nodes[N].para - 1) / 2; i++) {
    a = a->parent;
  }
  int c = 1;
  stack<Node<int> *> ss;
  ss.push(a);
  while (!ss.empty()) {
    Node<int> *n = ss.top();
    ss.pop();
    if (n->child.size() != 0) {
      for (int i = 0; i < n->child.size(); i++) {
        c++;
        ss.push(n->child[i]);
      }
    }
  }

  if (c >= N - c) {
    printf("Snuke\n");
  } else {
    printf("Fennec\n");
  }
  return 0;
}
Problem p03662 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, dist1[10005], dist2[10005], resf, ress;
vector<int> son[10005];
void dfs1(int c, int f, int d) {
  dist1[c] = d;
  for (int i = 0; i < son[c].size(); i++)
    if (son[c][i] != f)
      dfs1(son[c][i], c, d + 1);
}
void dfs2(int c, int f, int d) {
  dist2[c] = d;
  for (int i = 0; i < son[c].size(); i++)
    if (son[c][i] != f)
      dfs2(son[c][i], c, d + 1);
}
int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    son[a].push_back(b);
    son[b].push_back(a);
  }
  dfs1(1, -1, 0);
  dfs2(n, -1, 0);
  for (int i = 1; i <= n; i++)
    if (dist1[i] <= dist2[i])
      resf++;
    else
      ress++;
  if (resf > ress)
    cout << "Fennec";
  else
    cout << "Snuke";
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, dist1[100005], dist2[100005], resf, ress;
vector<int> son[100005];
void dfs1(int c, int f, int d) {
  dist1[c] = d;
  for (int i = 0; i < son[c].size(); i++)
    if (son[c][i] != f)
      dfs1(son[c][i], c, d + 1);
}
void dfs2(int c, int f, int d) {
  dist2[c] = d;
  for (int i = 0; i < son[c].size(); i++)
    if (son[c][i] != f)
      dfs2(son[c][i], c, d + 1);
}
int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    son[a].push_back(b);
    son[b].push_back(a);
  }
  dfs1(1, -1, 0);
  dfs2(n, -1, 0);
  for (int i = 1; i <= n; i++)
    if (dist1[i] <= dist2[i])
      resf++;
    else
      ress++;
  if (resf > ress)
    cout << "Fennec";
  else
    cout << "Snuke";
  return 0;
}
Problem p03662 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1e9
int n;
int a[111111], b[111111];
vector<int> v[111111];
int f[111111], s[111111];
int F, S;
queue<int> q;
signed main() {
  cin >> n;
  rep(i, n - 1) {
    cin >> a[i] >> b[i];
    v[a[i]].push_back(b[i]);
    v[b[i]].push_back(a[i]);
  }
  fill(f, f + n + 1, INF);
  f[1] = 0;
  q.push(1);
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    for (int i : v[t]) {
      if (f[i] > f[t] + 1) {
        f[i] = f[t] + 1;
        q.push(i);
      }
    }
  }
  fill(s, s + n + 1, INF);
  s[n] = 0;
  q.push(n);
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    for (int i : v[t]) {
      if (f[i] > s[t] + 1) {
        s[i] = s[t] + 1;
        q.push(i);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (f[i] <= s[i])
      F++;
    else
      S++;
  }
  if (F > S)
    puts("Fennec");
  else
    puts("Snuke");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1e9
int n;
int a[111111], b[111111];
vector<int> v[111111];
int f[111111], s[111111];
int F, S;
queue<int> q;
signed main() {
  cin >> n;
  rep(i, n - 1) {
    cin >> a[i] >> b[i];
    v[a[i]].push_back(b[i]);
    v[b[i]].push_back(a[i]);
  }
  fill(f, f + n + 1, INF);
  f[1] = 0;
  q.push(1);
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    for (int i : v[t]) {
      if (f[i] > f[t] + 1) {
        f[i] = f[t] + 1;
        q.push(i);
      }
    }
  }
  fill(s, s + n + 1, INF);
  s[n] = 0;
  q.push(n);
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    for (int i : v[t]) {
      if (s[i] > s[t] + 1) {
        s[i] = s[t] + 1;
        q.push(i);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    if (f[i] <= s[i])
      F++;
    else
      S++;
  }
  if (F > S)
    puts("Fennec");
  else
    puts("Snuke");
  return 0;
}
Problem p03662 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stack>
#include <vector>
using namespace std;

int n;
vector<int> pos[100010];
stack<int> sta;
int cnt[100010], first;

inline void dfs(int v, int pr) {
  cnt[v] = 1;
  sta.push(v);

  if (v == n - 1) {
    stack<int> t = sta;
    for (int i = 0; i < (sta.size() + 3) / 2; i++)
      t.pop();
    first = t.top();
  }

  for (auto to : pos[v]) {
    if (to == pr)
      continue;
    dfs(to, v);
    cnt[v] += cnt[to];
  }
  sta.pop();
}

int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    pos[a].push_back(b);
    pos[b].push_back(a);
  }

  first = -1;
  dfs(0, 0);
  cout << (cnt[first] * 2 < n ? "Fennec" : "Snuke");

  return 0;
}

Fixed submission:
#include <iostream>
#include <stack>
#include <vector>
using namespace std;

int n;
vector<int> pos[100010];
stack<int> sta;
int cnt[100010], first;

inline void dfs(int v, int pr) {
  cnt[v] = 1;
  sta.push(v);

  if (v == n - 1) {
    stack<int> t = sta;
    for (int i = 0; i < sta.size() / 2 - 1; i++)
      t.pop();
    first = t.top();
  }

  for (auto to : pos[v]) {
    if (to == pr)
      continue;
    dfs(to, v);
    cnt[v] += cnt[to];
  }
  sta.pop();
}

int main() {
  cin >> n;
  for (int i = 0; i < n - 1; i++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    pos[a].push_back(b);
    pos[b].push_back(a);
  }

  first = -1;
  dfs(0, 0);
  cout << (cnt[first] * 2 < n ? "Fennec" : "Snuke");

  return 0;
}
Problem p03664 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, k, l, mp[25][25], all[1 << 16], dp[1 << 16][25], x, y, z, sum,
    ans;
int main() {
  cin >> n >> m;
  for (i = 1; i <= m; i++) {
    cin >> x >> y >> z;
    x--;
    y--;
    sum += z;
    mp[x][y] = mp[y][x] = z;
  }
  for (i = 0; i < (1 << n); i++) {
    for (j = 0; j < n; j++)
      if ((i >> j) & 1) {
        for (k = j + 1; k < n; k++)
          if ((i >> k) & 1) {
            all[i] += mp[j][k];
          }
      }
  }
  memset(dp, -1, sizeof(dp));
  dp[1][0] = 0;
  for (i = 1; i < (1 << n); i++) {
    for (j = 0; j < n; j++)
      if ((i >> j) & 1)
        if (dp[i][j] != -1) {
          for (k = ((((1 << n) - 1) ^ i) | (1 << j)); k;
               k = ((k - 1) & ((((1 << n) - 1) ^ i) | (1 << j)))) {
            dp[i | k][j] = max(dp[i | k][j], dp[i][j] + all[k]);
          }
          for (k = 0; k < n; k++)
            if (!((i >> k) & 1))
              if (mp[j][k]) {
                dp[i | (1 << k)][k] =
                    max(dp[i | (1 << k)][k], dp[i][j] + mp[j][k]);
              }
        }
    /*if (i>32766){
            system("pause");
    }*/
    if ((i >> (n - 1)) & 1)
      ans = max(ans, dp[i][n - 1]);
    if (ans > 1234567) {
      system("pause");
    }
  }
  cout << sum - ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, m, i, j, k, l, mp[25][25], all[1 << 16], dp[1 << 16][25], x, y, z, sum,
    ans;
int main() {
  cin >> n >> m;
  for (i = 1; i <= m; i++) {
    cin >> x >> y >> z;
    x--;
    y--;
    sum += z;
    mp[x][y] = mp[y][x] = z;
  }
  for (i = 0; i < (1 << n); i++) {
    for (j = 0; j < n; j++)
      if ((i >> j) & 1) {
        for (k = j + 1; k < n; k++)
          if ((i >> k) & 1) {
            all[i] += mp[j][k];
          }
      }
  }
  memset(dp, -1, sizeof(dp));
  dp[1][0] = 0;
  for (i = 1; i < (1 << n); i++) {
    for (j = 0; j < n; j++)
      if ((i >> j) & 1)
        if (dp[i][j] != -1) {
          for (k = ((((1 << n) - 1) ^ i) | (1 << j)); k;
               k = ((k - 1) & ((((1 << n) - 1) ^ i) | (1 << j)))) {
            dp[i | k][j] = max(dp[i | k][j], dp[i][j] + all[k]);
          }
          for (k = 0; k < n; k++)
            if (!((i >> k) & 1))
              if (mp[j][k]) {
                dp[i | (1 << k)][k] =
                    max(dp[i | (1 << k)][k], dp[i][j] + mp[j][k]);
              }
        }
    /*if (i>32766){
            system("pause");
    }*/
    if ((i >> (n - 1)) & 1)
      ans = max(ans, dp[i][n - 1]);
    /*if (ans>1234567){
            system("pause");
    }*/
  }
  cout << sum - ans << endl;
  return 0;
}
Problem p03664 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <memory.h>
#include <queue>
#include <stdio.h>

using namespace std;
typedef long long LL;
const int maxn = 20;

int e[maxn][maxn], n, m, bin[1 << 15];
LL dp[maxn][1 << 15];
LL cs[maxn][1 << 15];
LL cst[1 << 15], sum;
void upd(LL &x, LL y) {
  if (x < y)
    x = y;
}
int main() {
#ifdef LOCAL_JUDGE
  freopen("mine.in", "r", stdin);
  freopen("mine.out", "w", stdout);
#endif
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++)
    bin[1 << i] = i;
  for (int u, v, w, i = 1; i <= m; i++)
    scanf("%d %d %d", &u, &v, &w), u--, v--, e[u][v] = e[v][u] = w, sum += w;
  for (int i = 0; i < n; i++)
    for (int j = 1; j < (1 << n); j++)
      cs[j][i] = cs[j ^ (j & -j)][i] + e[bin[j & -j]][i];
  for (int i = 1; i < (1 << n); i++)
    cst[i] = cst[i ^ (i & -i)] + cs[i ^ (i & -i)][bin[i & -i]];
  memset(dp, -1, sizeof dp);
  dp[1][0] = 0;
  for (int s = 0; s < (1 << n); s++)
    for (int u = 0; u < n; u++)
      if ((s >> u & 1) && dp[s][u] != -1) {
        for (int t = 0; t < n; t++)
          if (!(s >> t & 1) && e[u][t])
            upd(dp[s ^ (1 << t)][t], e[u][t] + dp[s][u]);
        int o = ((1 << n) - 1) ^ s;
        for (int t = o; t; t = t - 1 & o)
          upd(dp[s ^ t][u], dp[s][u] + cst[t] + cs[t][u]);
      }
  printf("%lld", sum - dp[(1 << n) - 1][n - 1]);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <memory.h>
#include <queue>
#include <stdio.h>

using namespace std;
typedef long long LL;
const int maxn = 20;

int e[maxn][maxn], n, m, bin[1 << 15];
LL dp[1 << 15][maxn];
LL cs[1 << 15][maxn];
LL cst[1 << 15], sum;
void upd(LL &x, LL y) {
  if (x < y)
    x = y;
}
int main() {
#ifdef LOCAL_JUDGE
  freopen("mine.in", "r", stdin);
  freopen("mine.out", "w", stdout);
#endif
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++)
    bin[1 << i] = i;
  for (int u, v, w, i = 1; i <= m; i++)
    scanf("%d %d %d", &u, &v, &w), u--, v--, e[u][v] = e[v][u] = w, sum += w;
  for (int i = 0; i < n; i++)
    for (int j = 1; j < (1 << n); j++)
      cs[j][i] = cs[j ^ (j & -j)][i] + e[bin[j & -j]][i];
  for (int i = 1; i < (1 << n); i++)
    cst[i] = cst[i ^ (i & -i)] + cs[i ^ (i & -i)][bin[i & -i]];
  memset(dp, -1, sizeof dp);
  dp[1][0] = 0;
  for (int s = 0; s < (1 << n); s++)
    for (int u = 0; u < n; u++)
      if ((s >> u & 1) && dp[s][u] != -1) {
        for (int t = 0; t < n; t++)
          if (!(s >> t & 1) && e[u][t])
            upd(dp[s ^ (1 << t)][t], e[u][t] + dp[s][u]);
        int o = ((1 << n) - 1) ^ s;
        for (int t = o; t; t = t - 1 & o)
          upd(dp[s ^ t][u], dp[s][u] + cst[t] + cs[t][u]);
      }
  printf("%lld", sum - dp[(1 << n) - 1][n - 1]);
  return 0;
}
Problem p03664 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
int N, M, g[15][15], f[32768][15];
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 0, u, v, w; i < M; i++) {
    scanf("%d%d%d", &u, &v, &w);
    u--, v--;
    g[u][v] = g[v][u] = w;
  }
  for (int i = 0; i < 1 << N; i++)
    for (int j = 0; j < N; j++)
      f[i][j] = 2000000000;
  for (int i = 1; i < 1 << N; i += 2)
    f[i][0] = 0;
  for (int i = 1; i < 1 << N; i++)
    for (int j = 0; j < N; j++)
      if (f[i][j] < 2000000000) {
        static int s[15];
        for (int k = 0; k < N; k++) {
          s[k] = 0;
          for (int l = 0; l < N; l++)
            if (i >> l & 1)
              s[k] += g[k][l];
        }
        for (int k = 0; k < N; k++)
          if (!(i >> k & 1) && g[j][k])
            for (int mask = i | 1 << k, l = mask; l < 1 << N;
                 l = l + 1 | mask) {
              int S = -g[j][k];
              for (int m = 0; m < N; m++)
                if (l - i >> m & 1)
                  S += s[m];
              f[l][k] = std::min(f[l][k], f[i][j] + S);
            }
      }
  printf("%d\n", f[(1 << N) - 1][N - 1]);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstdio>
int N, M, g[15][15], f[32768][15];
int main() {
  scanf("%d%d", &N, &M);
  for (int i = 0, u, v, w; i < M; i++) {
    scanf("%d%d%d", &u, &v, &w);
    u--, v--;
    g[u][v] = g[v][u] = w;
  }
  for (int i = 0; i < 1 << N; i++)
    for (int j = 0; j < N; j++)
      f[i][j] = 2000000000;
  for (int i = 1; i < 1 << N; i += 2)
    f[i][0] = 0;
  for (int i = 1; i < 1 << N; i++)
    for (int j = 0; j < N; j++)
      if (f[i][j] < 2000000000) {
        static int s[15];
        for (int k = 0; k < N; k++) {
          s[k] = 0;
          for (int l = 0; l < N; l++)
            if (i >> l & 1)
              s[k] += g[k][l];
        }
        for (int k = i; k < 1 << N; k = k + 1 | i) {
          int S = 0;
          for (int l = 0; l < N; l++)
            if (k - i >> l & 1)
              S += s[l];
          for (int l = 0; l < N; l++)
            if (k - i >> l & 1 && g[j][l])
              f[k][l] = std::min(f[k][l], f[i][j] + S - g[j][l]);
        }
      }
  printf("%d\n", f[(1 << N) - 1][N - 1]);
  return 0;
}
Problem p03665 (C++)

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

using namespace std;

long long pascal[100][100];

void make_table() {
  pascal[0][0] = 1;
  pascal[1][0] = 1;
  pascal[1][1] = 1;
  for (int i = 2; i < 60; i++) {
    pascal[i][0] = 1;
    pascal[i][i] = 1;
    for (int j = 1; j < i; j++) {
      pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j];
    }
  }
}

int main() {
  long long en, on;
  long long n, p;
  make_table();
  cin >> n >> p;
  long long ensum = 1;
  long long onsum1 = 0;
  long long onsum2 = 0;
  for (long long i = 0; i < n; i++) {
    long long a;
    cin >> a;
    if (a % 2 == 0) {
      en++;
    } else {
      on++;
    }
  }
  for (int i = 1; i <= en; i++) {
    ensum += pascal[en][i];
  }

  for (int i = 1; i <= on; i += 2) {
    onsum1 += pascal[on][i];
  }
  for (int i = 0; i <= on; i += 2) {
    onsum2 += pascal[on][i];
  }
  long long ans = ensum * (p == 1 ? onsum1 : onsum2);

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;

long long pascal[100][100];

void make_table() {
  pascal[0][0] = 1;
  pascal[1][0] = 1;
  pascal[1][1] = 1;
  for (int i = 2; i < 60; i++) {
    pascal[i][0] = 1;
    pascal[i][i] = 1;
    for (int j = 1; j < i; j++) {
      pascal[i][j] = pascal[i - 1][j - 1] + pascal[i - 1][j];
    }
  }
}

int main() {
  long long en = 0, on = 0;
  long long n, p;
  make_table();
  cin >> n >> p;
  long long ensum = 1;
  long long onsum1 = 0;
  long long onsum2 = 0;
  for (long long i = 0; i < n; i++) {
    long long a;
    cin >> a;
    if (a % 2 == 0) {
      en++;
    } else {
      on++;
    }
  }
  for (int i = 1; i <= en; i++) {
    ensum += pascal[en][i];
  }

  for (int i = 1; i <= on; i += 2) {
    onsum1 += pascal[on][i];
  }
  for (int i = 0; i <= on; i += 2) {
    onsum2 += pascal[on][i];
  }
  long long ans = ensum * (p == 1 ? onsum1 : onsum2);

  cout << ans << endl;

  return 0;
}
Problem p03665 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;
int combination(int n, int r) {
  if (r == 0 || r == n)
    return (1);
  else if (r == 1)
    return (n);
  return (combination(n - 1, r - 1) + combination(n - 1, r));
}
int main() {
  int n, p;
  cin >> n >> p;
  int a[n];
  int b[n];
  int c[n];
  int j = 0;
  int k = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] % 2 == 0) {
      b[j] = a[i];
      j++;
    } else {
      c[k] = a[i];
      k++;
    }
  }
  long long int ans = 0;
  long long int g = 0;
  long long int o = 0;
  if (p == 0) {
    for (int i = 1; i <= j; i++) {
      g += combination(j, i);
    }
    for (int i = 1; i <= k; i++) {
      if ((i % 2) == 0) {
        o += combination(k, i);
      }
    }
    ans = (g + o + (g * o) + 1);
    cout << ans << endl;
  } else {
    for (int i = 1; i <= k; i++) {
      if ((i % 2) == 1)
        o += combination(k, i);
    }
    for (int i = 1; i <= j; i++) {
      g += combination(j, i);
    }
    ans = o + (g * o);
    cout << ans << endl;
  }
}


Fixed submission:
#include <iostream>
using namespace std;
long long int dp[1000][1000];
long long int combination(int n, int r) {
  if (n == r)
    return dp[n][r] = 1;
  if (r == 0)
    return dp[n][r] = 1;
  if (r == 1)
    return dp[n][r] = n;
  if (dp[n][r])
    return dp[n][r];
  return dp[n][r] = combination(n - 1, r) + combination(n - 1, r - 1);
}
int main() {
  int n, p;
  cin >> n >> p;
  int a[n];
  int b[n];
  int c[n];
  int j = 0;
  int k = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] % 2 == 0) {
      b[j] = a[i];
      j++;
    } else {
      c[k] = a[i];
      k++;
    }
  }
  long long int ans = 0;
  long long int g = 0;
  long long int o = 0;
  if (p == 0) {
    for (int i = 1; i <= j; i++) {
      g += combination(j, i);
    }
    for (int i = 1; i <= k; i++) {
      if ((i % 2) == 0) {
        o += combination(k, i);
      }
    }
    ans = (g + o + (g * o) + 1);
    cout << ans << endl;
  } else {
    for (int i = 1; i <= k; i++) {
      if ((i % 2) == 1)
        o += combination(k, i);
    }
    for (int i = 1; i <= j; i++) {
      g += combination(j, i);
    }
    ans = o + (g * o);
    cout << ans << endl;
  }
}
Problem p03665 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstring>
#include <iostream>
#include <stdio.h>
typedef long long ll;
const int maxn = 100010;
const ll inf = 10000000010;
#define cle(n) memset(n, 0, sizeof(n))
using namespace std;

ll C[110][110];

int n, m;
int p;
int a[110];

int main() {

  cle(C);
  for (int i = 0; i < 110; i++) {
    C[i][0] = 1;
    for (int j = 1; j <= i; j++)
      C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
  }

  while (scanf("%d%d", &n, &p)) {
    cle(a);
    int jishu = 0, oushu = 0;

    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
      if (a[i] % 2)
        jishu++;
      else
        oushu++;
    }

    // if(oushu==0)oushu++;
    ll ans = 0;
    if (p == 0) {
      int jishulen = jishu;
      if (jishu % 2)
        jishulen--;
      ll t1 = 0;
      t1++;
      for (int i = 2; i <= jishulen; i = i + 2) {
        t1 += C[jishu][i];
      }
      ll t2 = 0;
      for (int i = 0; i <= oushu; i++) {
        t2 += C[oushu][i];
      }
      // cout<<t2<<endl;
      //  cout<<t1<<' '<<t2<<endl;
      ans = t1 * t2;
    }

    else {
      // if(oushu==1)
      // oushu--;
      // if(jishu%2) jishulen--;
      ll t1 = 0;

      for (int i = 1; i <= jishu; i = i + 2) {
        t1 += C[jishu][i];
      }

      ll t2 = 0;

      for (int i = 0; i <= oushu; i++) {
        t2 += C[oushu][i];
      }
      ans = t1 * t2;
    }
    cout << ans << endl;
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cstring>
#include <iostream>
#include <stdio.h>
typedef long long ll;
const int maxn = 100010;
const ll inf = 10000000010;
#define cle(n) memset(n, 0, sizeof(n))
using namespace std;

ll C[110][110];

int n, m;
int p;
int a[110];

int main() {

  cle(C);
  for (int i = 0; i < 110; i++) {
    C[i][0] = 1;
    for (int j = 1; j <= i; j++)
      C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
  }

  scanf("%d%d", &n, &p);
  {
    cle(a);
    int jishu = 0, oushu = 0;

    for (int i = 0; i < n; i++) {
      scanf("%d", &a[i]);
      if (a[i] % 2)
        jishu++;
      else
        oushu++;
    }

    // if(oushu==0)oushu++;
    ll ans = 0;
    if (p == 0) {
      int jishulen = jishu;
      if (jishu % 2)
        jishulen--;
      ll t1 = 0;
      t1++;
      for (int i = 2; i <= jishulen; i = i + 2) {
        t1 += C[jishu][i];
      }
      ll t2 = 0;
      for (int i = 0; i <= oushu; i++) {
        t2 += C[oushu][i];
      }
      // cout<<t2<<endl;
      //  cout<<t1<<' '<<t2<<endl;
      ans = t1 * t2;
    }

    else {
      // if(oushu==1)
      // oushu--;
      // if(jishu%2) jishulen--;
      ll t1 = 0;

      for (int i = 1; i <= jishu; i = i + 2) {
        t1 += C[jishu][i];
      }

      ll t2 = 0;

      for (int i = 0; i <= oushu; i++) {
        t2 += C[oushu][i];
      }
      ans = t1 * t2;
    }
    cout << ans << endl;
  }

  return 0;
}