alexjercan/bugnet huggingface dataset:
Problem p00223 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
struct po {
  int x, y, a, b;
};
int w, h;
int mp[55][55];
int tx, ty, kx, ky;
int dis[55][55][55][55];

po mkp(int a, int b, int c, int d) {
  po res;
  res.x = a, res.y = b, res.a = c, res.b = d;
  return res;
}

int bfs() {
  int dx[] = {0, 0, -1, 1};
  int dy[] = {1, -1, 0, 0};
  memset(dis, 0, sizeof(dis));
  queue<po> Q;
  Q.push(mkp(tx, ty, kx, ky));

  while (!Q.empty()) {
    po t = Q.front();
    Q.pop();

    if (dis[t.y][t.x][t.b][t.a] >= 100)
      return -1;
    if (t.x == t.a && t.y == t.b)
      return dis[t.y][t.x][t.b][t.a];

    for (int i = 0; i < 4; i++) {
      int nx = t.x + dx[i], ny = t.y + dy[i];
      int na = t.a - dx[i], nb = t.b - dy[i];
      if (nx < 0 || ny < 0 || nx >= w || ny >= h || mp[ny][nx])
        nx = t.x, ny = t.y;
      if (na < 0 || nb < 0 || na >= w || nb >= h || mp[nb][na])
        na = t.a, nb = t.b;
      if (nx == t.x && ny == t.y && na == t.a && nb == t.b)
        continue;
      if (dis[ny][nx][nb][na])
        continue;
      dis[ny][nx][nb][na] = dis[t.y][t.x][t.b][t.a] + 1;
      Q.push(mkp(nx, ny, na, nb));
    }
  }
  return -1;
}

int main() {

  while (1) {
    cin >> w >> h;
    if (!w && !h)
      break;
    cin >> tx >> ty >> kx >> ky;
    tx--, ty--, kx--, ky--;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> mp[i][j];
    int res = bfs();
    if (res == -1)
      cout << "NA" << endl;
    else
      cout << res << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
struct po {
  int x, y, a, b;
};
int w, h;
int mp[51][51];
int tx, ty, kx, ky;
int dis[51][51][51][51];

po mkp(int a, int b, int c, int d) {
  po res;
  res.x = a, res.y = b, res.a = c, res.b = d;
  return res;
}

int bfs() {
  int dx[] = {0, 0, -1, 1};
  int dy[] = {1, -1, 0, 0};

  memset(dis, 0, sizeof(dis));
  queue<po> Q;
  Q.push(mkp(tx, ty, kx, ky));

  while (!Q.empty()) {
    po t = Q.front();
    Q.pop();
    if (dis[t.y][t.x][t.b][t.a] >= 100)
      return -1;
    if (t.x == t.a && t.y == t.b)
      return dis[t.y][t.x][t.b][t.a];

    for (int i = 0; i < 4; i++) {
      int nx = t.x + dx[i], ny = t.y + dy[i];
      int na = t.a - dx[i], nb = t.b - dy[i];
      if (nx < 0 || ny < 0 || nx >= w || ny >= h || mp[ny][nx])
        nx = t.x, ny = t.y;
      if (na < 0 || nb < 0 || na >= w || nb >= h || mp[nb][na])
        na = t.a, nb = t.b;
      if (dis[ny][nx][nb][na])
        continue;
      dis[ny][nx][nb][na] = dis[t.y][t.x][t.b][t.a] + 1;
      Q.push(mkp(nx, ny, na, nb));
    }
  }
  return -1;
}

int main() {

  while (1) {
    cin >> w >> h;
    cin >> tx >> ty >> kx >> ky;
    tx--, ty--, kx--, ky--;
    for (int i = 0; i < h; i++)
      for (int j = 0; j < w; j++)
        cin >> mp[i][j];
    int res = bfs();
    if (res == -1)
      cout << "NA" << endl;
    else
      cout << res << endl;
  }
}
Problem p00225 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <set>
#include <utility>
#include <vector>
using namespace std;

template <typename T> class UnionFind {
  std::map<T, T> parent;
  std::map<T, int> rank;

public:
  void unionSet(const T &a, const T &b) {
    T x = findSet(a), y = findSet(b);
    if (rank[x] < rank[y])
      std::swap(x, y);
    parent[y] = x;
    if (rank[x] == rank[y])
      rank[x] += 1;
  }
  const T findSet(const T &x) {
    if (!parent.count(x)) {
      rank[x] = 1;
      return parent[x] = x;
    }
    return (parent[x] == x) ? x : (parent[x] = findSet(parent[x]));
  }
};

int main() {
  int n;
  while (cin >> n, n) {
    vector<int> indegree(26);
    vector<int> outdegree(26);
    UnionFind<int> group;
    set<int> root;
    for (int i = 0; i < n; ++i) {
      string s;
      cin >> s;
      int head = s[0] - 'a';
      int tail = s[s.size() - 1] - 'a';
      ++indegree[head];
      ++outdegree[tail];
      group.unionSet(head, tail);
    }
    for (int i = 0; i < 26; ++i)
      if (indegree[i] != 0 || outdegree[i] != 0)
        root.insert(group.findSet(i));
    bool ok = true;
    for (int i = 0; i < 26; ++i)
      if (indegree[i] != outdegree[i])
        ok = false;
    if (root.size() != 1)
      ok = false;
    if (ok)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <set>
#include <tuple>
#include <utility>
#include <vector>
using namespace std;

template <typename T> class UnionFind {
  std::map<T, T> parent;
  std::map<T, int> rank;

public:
  void makeSet(const T &x) {
    parent[x] = x;
    rank[x] = 0;
  }
  void unionSet(const T &a, const T &b) {
    T x = findSet(a), y = findSet(b);
    if (rank[x] < rank[y])
      std::swap(x, y);
    parent[y] = x;
    if (rank[x] == rank[y])
      rank[x] += 1;
  }
  const T findSet(const T &x) {
    return (parent[x] == x) ? x : (parent[x] = findSet(parent[x]));
  }
};

typedef tuple<char, char> Word;
enum { H, T };

int main() {
  int n;
  while (cin >> n, n) {
    vector<Word> word;
    vector<int> degree(n);
    UnionFind<int> group;
    for (int i = 0; i < n; ++i)
      group.makeSet(i);
    set<int> root;
    for (int i = 0; i < n; ++i) {
      string s;
      cin >> s;
      word.push_back(Word(s[0], s[s.size() - 1]));
    }
    for (int i = 0; i < n; ++i)
      for (int j = i + 1; j < n; ++j) {
        if (get<H>(word[i]) == get<T>(word[j])) {
          ++degree[i];
          ++degree[j];
          group.unionSet(i, j);
        }
        if (get<T>(word[i]) == get<H>(word[j])) {
          ++degree[i];
          ++degree[j];
          group.unionSet(i, j);
        }
      }
    for (int i = 0; i < n; ++i)
      root.insert(group.findSet(i));
    int odd = 0;
    for (const auto &d : degree)
      if (d & 1)
        ++odd;
    if (root.size() == 1 && odd <= 2)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
}
Problem p00225 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <iostream>
#include <string>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
int n, in[26], out[26], G[26][26];
bool visited[26];
bool check() {
  rep(i, 26) if (in[i] != out[i]) return false;
  return true;
}
void dfs(int u) {
  visited[u] = true;
  rep(v, 26) if (!visited[v] && G[u][v]) dfs(v);
}
int getNComponent() {
  int nc = 0;
  rep(i, 26) {
    if (in[i] == 0 && out[i] == 0)
      continue;
    if (!visited[i]) {
      nc++;
      dfs(i);
    }
  }
  return nc;
}
int main() {
  while (cin >> n && n) {
    rep(i, 26) {
      in[i] = out[i] = 0;
      visited[i] = false;
      rep(j, 26) G[i][j] = 0;
    }
    string str;
    rep(i, n) {
      cin >> str;
      in[str[0] - 'a']++;
      out[str[str.size() - 1] - 'a']++;
      G[str[0] - 'a'][str[str.size() - 1] - 'a'] = 1;
      G[str[str.size() - 1] - 'a'][str[0] - 'a'] = 1;
    }
    cout << ((check() && getNComponent() == 1) ? "OK" : "NG") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

int adjacent[27][27];
short position[10001];
int m, n_edge, edge, solution;

void visit(int i) {
  int j;

  if (solution)
    return;

  position[edge] = i;
  if (edge == n_edge) {
    cout << "OK" << endl;
    solution++;
  } else {
    for (j = 1; j <= m; j++)
      if (adjacent[i][j]) {
        adjacent[i][j]--;
        adjacent[j][i]--;
        edge++;
        visit(j);
        edge--;
        adjacent[i][j]++;
        adjacent[j][i]++;
      }
  }
}

int main(int argc, char const *argv[]) {
  int n;
  string str;

  while (cin >> n && n) {
    n_edge = n;
    for (int i = 0; i < 27; i++) {
      for (int j = 0; j < 27; j++) {
        adjacent[i][j] = 0;
      }
    }
    for (int i = 0; i < 10001; i++)
      position[i] = 0;

    for (int i = 0; i < n; i++) {
      cin >> str;
      adjacent[str[0] - 'a' + 1][str[str.size() - 1] - 'a' + 1]++;
      adjacent[str[str.size() - 1] - 'a' + 1][str[0] - 'a' + 1]++;
    }
    m = 27;

    solution = edge = 0;
    visit(1);
    if (solution == 0)
      cout << "NG" << endl;
  }
  return 0;
}
Problem p00225 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int N;
string s[10000];

int par[26], sz[26];

void init() {
  for (int i = 0; i < 26; i++) {
    par[i] = i;
    sz[i] = 1;
  }
}

int find(int x) { return x == par[x] ? x : par[x] = find(par[x]); }

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (sz[x] < sz[y])
    swap(x, y);
  par[y] = x;
  sz[x] += sz[y];
}

int in[26] = {0};
int out[26] = {0};
vector<int> G[26];
void dfs(int p) {
  in[p]--;
  for (int i = 0; i < G[p].size(); i++) {
    int to = G[p][i];
    if ((!in[to]) || (!out[to]))
      continue;
    out[to]--;
    dfs(to);
  }
}
void solve() {
  for (int i = 0; i < 26; i++) {
    in[i] = out[i] = 0;
    G[i] = vector<int>();
  }
  int start;
  for (int i = 0; i < N; i++) {
    int o = s[i][0] - 'a';
    int e = s[i][s[i].size() - 1] - 'a';

    in[o]++;
    out[e]++;
    unite(o, e);

    start = o;
    G[o].push_back(e);
  }

  int cnt = 0;

  for (int i = 0; i < 26; i++) {
    if (in[i] != out[i]) {
      cout << "NG" << endl;
      return;
    }

    if (in[i] && i == par[i])
      cnt++;
  }

  dfs(start);

  for (int i = 0; i < 26; i++) {
    if (in[i]) {
      cout << "NG" << endl;
      return;
    }
  }

  cout << "OK" << endl;
}

int main() {
  while (cin >> N, N) {
    for (int i = 0; i < N; i++)
      cin >> s[i];
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
bool used[26];
vector<int> g[26];
bool dfs(int pos) {
  if (used[pos])
    return true;
  used[pos] = true;
  bool res = false;
  for (int i = 0; i < g[pos].size(); i++) {
    res |= dfs(g[pos][i]);
  }
  used[pos] = false;
  return res;
}
int main() {
  int n;
  while (cin >> n, n) {
    fill_n(g, 26, vector<int>(0));
    fill_n(used, 26, false);
    for (int i = 0; i < n; i++) {
      string str;
      cin >> str;
      int s = str[0] - 'a';
      int e = str[str.length() - 1] - 'a';
      g[s].push_back(e);
    }
    for (int i = 0; i < 26; i++) {
      sort(g[i].begin(), g[i].end());
      g[i].erase(unique(g[i].begin(), g[i].end()), g[i].end());
    }
    bool flag = false;
    for (int i = 0; i < 26; i++) {
      flag |= dfs(i);
    }
    cout << (flag ? "OK" : "NG") << endl;
  }
  return 0;
}
Problem p00225 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define reps(i, f, n) for (int i = f; i < int(n); i++)
#define rep(i, n) reps(i, 0, n)

using namespace std;

int in[32];
int out[32];

int main(void) {
  int n;
  char s[128];

  while (scanf("%d", &n), n) {
    vector<int> x[32];
    rep(i, 32) in[i] = out[i] = 0;

    rep(i, n) {
      scanf("%s", s);
      in[s[0] - 'a']++;
      out[s[strlen(s) - 1] - 'a']++;
      x[s[0] - 'a'].push_back(s[strlen(s) - 1] - 'a');
    }

    int d = 0;

    rep(i, 26) {
      if (in[i] != out[i]) {
        d++;
      }
    }

    if (d == 0) {
      int use[32];
      rep(i, 32) use[i] = -1;
      queue<char> q;

      rep(i, 26) {
        if (x[i].size() > 0) {
          use[i] = 0;
          rep(j, x[i].size()) { q.push(x[i][j]); }
          break;
        }
      }

      while (!q.empty()) {
        int f = q.front();
        q.pop();
        if (use[f] != -1)
          continue;
        use[f] = 0;
        rep(i, x[f].size()) { q.push(x[f][i]); }
      }
      bool flg = true;
      rep(i, 26) {
        if (use[i] == -1 && x[i].size())
          flg = false;
      }
      if (flg) {
        puts("OK");
      } else {
        puts("NG");
      }
    } else {
      puts("NG");
    }
    rep(i, 32) x[i].clear();
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
  int n, lg, i, j;
  bool flag;
  char str[10001][2], temp[32];

  while (1) {
    flag = 0;
    scanf("%d", &n);
    if (n == 0)
      break;
    for (i = 0; i < n; i++) {
      scanf("%s", temp);
      lg = strlen(temp);
      str[i][0] = temp[0];
      str[i][1] = temp[lg - 1];
    }
    for (i = 0; i < n; i++) {
      for (j = 0; j < n; j++) {
        if (i != j) {
          if (str[i][1] == str[j][0]) {
            str[i][1] = 'O';
            str[j][0] = 'O';
          }
        }
      }
    }
    for (i = 0; i < n; i++) {
      if (str[i][0] != 'O' || str[i][1] != 'O') {
        flag = 1;
      }
    }
    if (flag == 1)
      puts("NG");
    else
      puts("OK");
  }

  return 0;
}
Problem p00225 (C++) — buggy submission, verdict: Runtime Error

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

using ll = long long;

class union_find {
public:
  union_find(int n) : par(n, -1) {}

  int root(int x) { return par[x] < 0 ? x : par[x] = root(par[x]); }
  void unite(int x, int y) {
    x = root(x), y = root(y);
    if (x == y)
      return;
    if (par[x] > par[y])
      swap(x, y);
    par[x] += par[y];
    par[y] = x;
  }

  int size(int x) { return -par[root(x)]; }

private:
  vector<int> par;
};

int main() {
  int n;
  while (cin >> n, n) {
    vector<string> word(n);
    for (auto &s : word) {
      cin >> s;
    }

    vector<int> in(26), out(26);
    union_find uf(26);
    for (auto &s : word) {
      const auto first = s[0] - 'a', last = s.back() - 'a';
      in[first]++;
      out[last]++;
      uf.unite(first, last);
    }

    bool ok = true;
    int comp_num = 0;
    for (int i = 0; i < 26; ++i) {
      comp_num += in[i] && uf.root(i) == i;
      ok &= in[i] == out[i];
    }
    ok &= comp_num == 1;

    cout << (ok ? "OK" : "NG") << endl;
  }
}


Error: 0

Fixed submission:

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0225
// ユニオンファインド木
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// #define MAX_N 10001
int n;                                      // 単語の数
vector<vector<char>> v(n, vector<char>(2)); // wordの先頭と、末尾のcharを持つ

int par[26];
int r[26]; // rank

void init(int n) {
  for (int i = 0; i < n; i++) {
    r[i] = 0;
    par[i] = i;
  }
}

int root(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = root(par[x]);
  }
}

bool same(int x, int y) { return root(x) == root(y); }

void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y) {
    return;
  }
  if (r[x] < r[y]) {
    par[x] = y;
  } else if (r[x] > r[y]) {
    par[y] = x;
  } else if (r[x] == r[y]) {
    par[x] = y;
    r[y]++;
  }
}

int main() {

  while (cin >> n, n) {
    init(n);
    vector<string> word(n);
    for (auto &s : word) {
      cin >> s;
    }

    vector<int> in(26), out(26);
    for (auto &s : word) {
      const auto
          first = s[0] - 'a',
          last =
              s.back() -
              'a'; // 最初と最後の文字を取り出す　-'a'はa-zを0-25に対応させている
      in[first]++; // 入次数
      out[last]++; // 出次数
      unite(first, last); // 頂点(アルファべット)first,lastが同じ集合に属する
    }
    bool ok = true;
    int comp_num = 0; // 集合の数
    for (int i = 0; i < 26; i++) {
      comp_num += (in[i] && root(i) == i);
      ok &= in[i] == out[i];
    }
    ok &= (comp_num == 1);
    if (ok) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }

  return 0;
}
Problem p00225 (C++) — buggy submission, verdict: Runtime Error

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0225
// ユニオンファインド木
#include <iostream>
#include <string>
#include <vector>
using namespace std;

// int n;//単語の数

int par[26];
int r[26]; // rank

void init(int n) {
  for (int i = 0; i < n; i++) {
    r[i] = 0;
    par[i] = i;
  }
}

int root(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = root(par[x]);
  }
}

bool same(int x, int y) { return root(x) == root(y); }

void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y) {
    return;
  }
  if (r[x] < r[y]) {
    par[x] = y;
  } else if (r[x] > r[y]) {
    par[y] = x;
  } else if (r[x] == r[y]) {
    par[x] = y;
    r[y]++;
  }
}

int main() {
  int n;

  while (cin >> n, n) {
    init(26);
    vector<string> word(n);
    for (auto &s : word) {
      cin >> s;
    }

    vector<int> in(26), out(26);
    for (auto &s : word) {
      const auto
          first = s[0] - 'a',
          last =
              s.back() -
              'a'; // 最初と最後の文字を取り出す　-'a'はa-zを0-25に対応させている
      in[first]++; // 入次数
      out[last]++; // 出次数
      unite(first, last); // 頂点(アルファべット)first,lastが同じ集合に属する
    }
    bool ok = true;
    int comp_num = 0; // 集合の数
    for (int i = 0; i < 26; i++) {
      comp_num += (in[i] && root(i) == i);
      ok &= in[i] == out[i];
    }
    ok &= (comp_num == 1);
    if (ok) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }

  return 0;
}


Error: 0

Fixed submission:

// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=0225
// ユニオンファインド木
#include <iostream>
#include <string>
#include <vector>
using namespace std;

// int n;//単語の数

int par[26];
int r[26]; // rank

void init(int n) {
  for (int i = 0; i < n; i++) {
    r[i] = 0;
    par[i] = i;
  }
}

int root(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = root(par[x]);
  }
}

bool same(int x, int y) { return root(x) == root(y); }

void unite(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y) {
    return;
  }
  if (r[x] < r[y]) {
    par[x] = y;
  } else if (r[x] > r[y]) {
    par[y] = x;
  } else if (r[x] == r[y]) {
    par[x] = y;
    r[y]++;
  }
}

int main() {
  int n;

  while (cin >> n, n) {
    init(n);
    vector<string> word(n);
    for (auto &s : word) {
      cin >> s;
    }

    vector<int> in(26), out(26);
    for (auto &s : word) {
      const auto
          first = s[0] - 'a',
          last =
              s.back() -
              'a'; // 最初と最後の文字を取り出す　-'a'はa-zを0-25に対応させている
      in[first]++; // 入次数
      out[last]++; // 出次数
      unite(first, last); // 頂点(アルファべット)first,lastが同じ集合に属する
    }
    bool ok = true;
    int comp_num = 0; // 集合の数
    for (int i = 0; i < 26; i++) {
      comp_num += (in[i] && root(i) == i);
      ok &= in[i] == out[i];
    }
    ok &= (comp_num == 1);
    if (ok) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }

  return 0;
}
Problem p00225 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>
#include <vector>
#define REP(i, j) for (int i = 0; i < j; i++)
using namespace std;
const int N = 26;

vector<bool> visited(N, false);
void makeVisited(vector<vector<int>> &num, int now) {
  REP(i, N) {
    if (!visited[i] && num[now][i]) {
      visited[i] = true;
      makeVisited(num, i);
    }
  }
}

bool checkVisited(vector<bool> &used, vector<vector<int>> &num) {
  REP(i, N) {
    if (used[i]) {
      makeVisited(num, i);
      break;
    }
  }
  REP(i, N) if (used[i] && !visited[i]) return false;
  return true;
}

bool checkEuler(vector<vector<int>> &num, vector<bool> &used) {
  REP(alpha, N) {
    if (!used[alpha])
      continue;
    int diff = 0;
    REP(i, N) {
      diff += num[alpha][i];
      diff -= num[i][alpha];
    }
    if (diff)
      return false;
  }
  return true;
}

int main() {
  int n;
  while (cin >> n && n) {
    visited = vector<bool>(N, false);
    vector<vector<int>> num(N, vector<int>(N, 0));
    vector<bool> used(N, false);
    REP(i, n) {
      string str;
      cin >> str;
      int top = str[0] - 'a', tail = str[str.size() - 1] - 'a';
      used[top] = true;
      used[tail] = true;
      num[top][tail]++;
    }

    if (!checkVisited(used, num)) {
      cout << "NG" << endl;
      continue;
    }

    cout << ((checkEuler(num, used)) ? "OK" : "NG") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < j; i++)
#define FOR(i, j, k) for (int i = j; i < k; i++)
#define P pair<int, int>
const int INF = INT_MAX / 2;
const int ALPHA = 26;

int isConnected(int cnt, int c, vector<vector<bool>> word, vector<bool> used,
                vector<bool> find) {
  if (used[c])
    return cnt;
  used[c] = true;
  cnt++;
  REP(i, ALPHA) {
    if (word[c][i]) {
      cnt = isConnected(cnt, i, word, used, find);
    }
  }
  return cnt;
}

int main() {
  int n;
  while (cin >> n && n) {
    vector<int> in(ALPHA, 0), out(ALPHA, 0);
    vector<vector<bool>> word(ALPHA, vector<bool>(ALPHA, false));
    vector<bool> used(ALPHA, false), find(ALPHA, false);

    REP(i, n) {
      string str;
      cin >> str;
      int from = str[0] - 'a', to = str[str.size() - 1] - 'a';
      out[from]++;
      in[to]++;
      word[from][to] = true;
      word[to][from] = true;
      find[from] = true;
      find[to] = true;
    }
    int ok_flg = true;
    REP(i, ALPHA) {
      if (in[i] != out[i]) {
        ok_flg = false;
        break;
      }
    }
    bool all_connected = true;
    REP(i, ALPHA) {
      if (find[i]) {
        int tmp = isConnected(0, i, word, used, find);
        // cout <<tmp <<endl;
        if (tmp != (n * 2 - 1))
          ok_flg = false;
      }
    }
    if (ok_flg)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}
Problem p00225 (C++) — buggy submission, verdict: Runtime Error

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

struct UnionFind {
  vector<int> parent;

  UnionFind(int n) {
    parent.resize(n);
    for (int i = 0; i < n; i++) {
      parent[i] = i;
    }
  }

  int find(int x) {
    if (parent[x] == x)
      return x;
    return parent[x] = find(parent[x]);
  }

  bool same(int x, int y) { return find(x) == find(y); }

  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    parent[x] = y;
    return true;
  }
};

int n;
UnionFind uf(0);
vector<int> in, out;
vector<bool> f;

string solve() {
  int p = -1;
  for (int i = 0; p == -1; i++) {
    if (f[i])
      p = uf.find(i);
  }

  for (int i = 0; i < 26; i++) {
    if (f[i] && p != uf.find(i))
      return "NG";
  }

  for (int i = 0; i < 26; i++) {
    if (in[i] != out[i])
      return "NG";
  }

  return "OK";
}

int main() {
  while (scanf("%d", &n), n) {
    uf = UnionFind(26);
    in = vector<int>(26, 0);
    out = vector<int>(26, 0);
    f = vector<bool>(26, false);

    for (int i = 0; i < n; i++) {
      string str;
      cin >> str;
      int a = str.front() - 'a';
      int b = str.back() - 'a';
      uf.unite(a, b);
      out[a]++;
      in[b]++;
      f[a] = f[b] = true;
    }

    cout << solve() << endl;
  }
}

Error: -6
double free or corruption (out)


Fixed submission:

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

struct UnionFind {
  vector<int> parent;

  UnionFind(int n) {
    parent.resize(n);
    for (int i = 0; i < n; i++) {
      parent[i] = i;
    }
  }

  int find(int x) {
    if (parent[x] == x)
      return x;
    return parent[x] = find(parent[x]);
  }

  bool same(int x, int y) { return find(x) == find(y); }

  bool unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return false;
    parent[x] = y;
    return true;
  }
};

int n;
UnionFind uf(0);
vector<int> in, out;
vector<bool> f;

string solve() {
  int p = -1;
  for (int i = 0; compl p; i++) {
    if (f[i])
      p = uf.find(i);
  }

  for (int i = 0; i < 26; i++) {
    if (f[i] && !uf.same(p, uf.find(i)))
      return "NG";
  }

  for (int i = 0; i < 26; i++) {
    if (in[i] != out[i])
      return "NG";
  }

  return "OK";
}

int main() {
  while (scanf("%d", &n), n) {
    uf = UnionFind(26);
    in = vector<int>(26, 0);
    out = vector<int>(26, 0);
    f = vector<bool>(26, false);

    for (int i = 0; i < n; i++) {
      string str;
      cin >> str;
      int a = str.front() - 'a';
      int b = str.back() - 'a';
      uf.unite(a, b);
      out[a]++;
      in[b]++;
      f[a] = f[b] = true;
    }

    cout << solve() << endl;
  }
}