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

#include <iostream>
using namespace std;

int main() {
  while (1) {
    int n, m, sum = 0;
    cin >> n >> m;

    if (n == 0 && m == 0) {
      break;
    } else {
      int a[10000];
      int b[10000];
      int fri1[500];
      int fri2[500];

      for (int i = 0; i < m; i++) {
        int ai, bi;
        cin >> ai >> bi;
        a[i] = ai - 2;
        b[i] = bi - 2;
      }

      for (int i = 0; i < n - 1; i++) {
        fri1[i] = 0;
        fri2[i] = 0;
      }

      for (int i = 0; i < m; i++) {
        if (a[i] == -1) {
          fri1[b[i]] = 1;
        }
        if (b[i] == -1) {
          fri1[a[i]] = 1;
        }
      }

      for (int i = 0; i < n - 1; i++) {
        if (fri1[i]) {
          for (int j = 0; j < m; j++) {
            if (a[j] == i) {
              fri2[b[j]] = 1;
            }
            if (b[j] == i) {
              fri2[a[j]] = 1;
            }
          }
        }
      }

      for (int i = 0; i < n - 1; i++) {
        if (fri1[i] == 1) {
          fri2[i] = 1;
        }
      }

      for (int i = 0; i < n - 1; i++) {
        sum += fri2[i];
      }

      cout << sum << endl;
    }
  }

  return 0;
}

Error: -6
double free or corruption (out)


Fixed submission:

#include <iostream>
using namespace std;

int main() {
  while (1) {
    int n, m, sum = 0;
    cin >> n >> m;

    while (n != 0 && m != 0) {
      int *a, *b, *fri1, *fri2;
      a = new int[m];
      b = new int[m];
      fri1 = new int[n - 1];
      fri2 = new int[n - 1];

      for (int i = 0; i < m; i++) {
        int ai, bi;
        cin >> ai >> bi;
        a[i] = ai - 2;
        b[i] = bi - 2;
      }

      for (int i = 0; i < n - 1; i++) {
        fri1[i] = 0;
        fri2[i] = 0;
      }

      for (int i = 0; i < m; i++) {
        if (a[i] == -1) {
          fri1[b[i]] = 1;
        }
        if (b[i] == -1) {
          fri1[a[i]] = 1;
        }
      }

      for (int i = 0; i < n - 1; i++) {
        if (fri1[i]) {
          for (int j = 0; j < m; j++) {
            if (a[j] == i) {
              fri2[b[j]] = 1;
            }
            if (b[j] == i) {
              fri2[a[j]] = 1;
            }
          }
        }
      }

      for (int i = 0; i < n - 1; i++) {
        if (fri1[i] == 1) {
          fri2[i] = 1;
        }
      }

      for (int i = 0; i < n - 1; i++) {
        sum += fri2[i];
      }

      cout << sum << endl;

      delete[] a;
      delete[] b;
      delete[] fri1;
      delete[] fri2;
    }
  }

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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10
#define MAX_N 20000
#define MAX_M 16

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int n, m, a, b;
int dp[1000][1000];

int main() {
  while (cin >> n >> m && n + m) {
    REP(i, 1000) REP(j, 1000) dp[i][j] = INF;
    REP(i, m) {
      cin >> a >> b;
      dp[a][b] = 1;
      dp[b][a] = 1;
    }
    FOR(k, 1, n + 1) {
      FOR(i, 1, n + 1) {
        FOR(j, 1, n + 1) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]); }
      }
    }
    int ans = 0;
    FOR(i, 2, n + 1) {
      if (dp[1][i] <= 2)
        ans++;
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (long long i = (a); i < (b); i++)
#define REP(i, N) for (long long i = 0; i < (N); i++)
#define ALL(s) (s).begin(), (s).end()
#define fi first
#define se second

#define PI acos(-1.0)
#define INF 1000000007
#define EPS 1e-10
#define MAX_N 20000
#define MAX_M 16

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<double, double> PD;
typedef pair<string, ll> PS;
typedef vector<ll> V;
typedef pair<P, char> PC;

int n, m, a, b;
int dp[100][100];

int main() {
  while (cin >> n >> m && n + m) {
    REP(i, 100) REP(j, 100) dp[i][j] = INF;
    REP(i, m) {
      cin >> a >> b;
      dp[a][b] = 1;
      dp[b][a] = 1;
    }
    REP(k, n) {
      REP(i, n) {
        REP(j, n) { dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j]); }
      }
    }
    int ans = 0;
    FOR(i, 2, n + 1) {
      if (dp[1][i] <= 2)
        ans++;
    }
    cout << ans << endl;
  }
}
Problem p00468 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <string.h>
using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m, n + m) {
    cin.clear();
    int a, b;
    int cne[501][501] = {{0}};
    for (int i = 0; i < m; i++) {
      cin >> a >> b;
      if (a > b)
        swap(a, b);
      cne[a][b] = 1;
      cne[b][a] = 1;
    }
    int f[501] = {0};
    int cnt = 0;
    for (int i = 1; i <= n; i++) {
      if (cne[1][i]) {
        f[i] = 1;
        for (int j = 2; j <= n; j++) {
          if (cne[i][j]) {
            f[j] = 1;
          }
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      if (f[i])
        cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <string.h>
using namespace std;
int cne[501][501];
int main() {
  int n, m;
  while (1) {
    scanf("%d%d", &n, &m);

    if (n == 0 && m == 0)
      break;
    int a, b;
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < m; j++) {
        cne[i][j] = 0;
      }
    }
    for (int i = 0; i < m; i++) {
      cin >> a >> b;
      if (a > b)
        swap(a, b);
      cne[a][b] = 1;
    }
    int cnt = 0;
    for (int i = 1; i < m - 1; i++) {
      for (int j = i + 1; j < m; j++) {
        if (cne[i][j] == 1 && cne[1][j] == 1)
          cnt++;
      }
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00468 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int n, m, a[11234], b[11234];
int my_friends[11234], their_friends[11234];
int ans = 0;
bool cancel = false;

int main(void) {
  while (true) {
    ans = 0;
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0) {
      break;
    }
    for (int i = 0; i < m; ++i) {
      scanf("%d%d", &a[i], &b[i]);
    }

    memset(my_friends, -1, sizeof(my_friends));
    memset(their_friends, -1, sizeof(their_friends));
    my_friends[0] = 1, their_friends[0] = 1;
    for (int i = 0; i < m; ++i) {
      if (a[i] == 1) {
        my_friends[b[i] - 1] = b[i];
        ans++;
      }
    }
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < m; ++j) {
        if (a[i] == my_friends[j]) {
          for (int k = 0; k < n; ++k) {
            if (b[i] == my_friends[k] || b[i] == their_friends[k]) {
              cancel = true;
              break;
            }
          }
          if (cancel == false) {
            their_friends[b[i] - 1] = b[i];
            ans++;
          }
          cancel = false;
        } else if (b[i] == my_friends[j]) {
          for (int k = 0; k < n; ++k) {
            if (a[i] == my_friends[k] || a[i] == their_friends[k]) {
              cancel = true;
              break;
            }
          }
          if (cancel == false) {
            their_friends[a[i] - 1] = a[i];
            ans++;
          }
          cancel = false;
        }
      }
    }

    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int n, m, a[11234], b[11234];
int ans = 0;

int main(void) {
  while (true) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0) {
      break;
    }
    for (int i = 0; i < m; ++i) {
      scanf("%d%d", &a[i], &b[i]);
    }

    bool friends_flag = false;
    for (int i = 0; i < m; ++m) {
      if (a[i] == 1) {
        friends_flag = true;
        break;
      }
    }
    if (friends_flag == false) {
      printf("0\\n");
    } else {
      int friends[11234];
      for (int i = 1; i <= n; ++i) {
        for (int j = 0; j < m; ++j) {
          if (i == a[j]) {
            friends[b[i] - 1] = 1;
          }
          if (friends[i] == a[j]) {
            friends[b[i] - 1] = 1;
          }
        }
      }
      for (int i = 0; i < n; ++i) {
        if (friends[i] == 1) {
          ans++;
        }
      }
      printf("%d\\n", ans);
      ans = 0;
    }
  }
  return 0;
}
Problem p00468 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;

int main(void) {
  for (;;) {
    bool a[600][600] = {{0}};
    bool flag[1000] = {0};
    bool fla[1000] = {0};
    int n;
    int m;
    int cnt = 0;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;

    for (int i = 0; i < m; i++) {
      int p, q;
      cin >> p >> q;
      if (q == 1)
        swap(p, q);
      if (p == 1)
        flag[q] = 1, fla[q] = 1;
      a[p][q] = 1;
      a[q][p] = 1;
    }

    for (int i = 2; i <= n; i++) {
      for (int j = 2; j <= n; j++) {
        if (fla[j] == 1 || fla[i] == 1)
          if (a[i][j] == 1)
            flag[j] = 1, flag[i] = 1; //,cout<<"-"<<i<<j<<endl;
        // if(fla[j]==1&&a[j][i]==1)fla[i]=1;//,cout<<"+"<<i<<j<<endl;
      }
    }
    for (int i = 2; i <= n; i++) {
      if (flag[i] == 1)
        cnt++; //,cout<<i<<"-"<<endl;
    }
    cout << cnt << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
using namespace std;
bool a[1000][1000] = {{0}};
int main(void) {
  for (;;) {
    for (int i = 0; i < 1000; i++) {
      for (int j = 0; j < 1000; j++) {
        a[i][j] = 0;
      }
    }
    int n;
    int m;
    int cnt = 0;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;

    for (int i = 0; i < m; i++) {
      int p, q;
      cin >> p >> q;
      a[p][q] = 1;
    }
    bool flag[1000] = {0};
    for (int i = 2; i < m; i++) {
      if (a[1][i] == 1)
        flag[i] = 1;
      if (a[i][1] == 1)
        flag[i] = 1;
      // if(flag[i]==1)cnt++;
    }
    for (int i = 2; i < m; i++) {
      for (int j = 2; j < m; j++) {
        int f = 0;
        if (flag[i] == 1 && a[i][j] == 1)
          flag[j] = 1;
        if (flag[j] == 1 && a[j][i] == 1)
          flag[i] = 1;
      }
    }
    for (int i = 2; i < n; i++) {
      if (flag[i] == 1)
        cnt++;
    }
    cout << cnt << endl;
  }
  return 0;
}
Problem p00468 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m) {
    int i, a[10001] = {0}, b[10001] = {0};
    bool list[2][1001] = {false};
    if (n == 0 && m == 0)
      goto END;
    for (i = 0; i < m; i++) {
      cin >> a[i] >> b[i];
      if (a[i] == 1) {
        list[0][b[i]] = true;
        list[1][b[i]] = true;
      }
      if (b[i] == 1) {
        list[0][a[i]] = true;
        list[1][a[i]] = true;
      }
    }
    for (i = 0; i < m; i++) {
      if (list[0][a[i]] == true) {
        list[1][b[i]] = true;
      }
      if (list[0][b[i]] == true) {
        list[1][a[i]] = true;
      }
    }
    int ans = 0;
    for (i = 2; i <= n; i++) {
      if (list[1][i])
        ans++;
    }
    cout << ans << endl;
  }
END:;
  return 0;
}

Error: WA

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, m, number[1000] = {0}, i, a[1000] = {0}, b[1000] = {0}, j,
            numberb[1000] = {0};
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      goto END;
    for (i = 0; i < m; i++) {
      cin >> a[i] >> b[i];
      if (a[i] == 1)
        number[b[i]] = 1;
    }
    for (i = 2; i <= n; i++) {
      if (number[i] == 1) {
        for (j = 0; j < m; j++) {
          if (a[j] == i)
            numberb[b[j]] = 1;
        }
      }
    }
    int ans = 0;
    for (i = 2; i <= n; i++) {
      if (number[i] == 1)
        ans++;
      else if (numberb[i] == 1)
        ans++;
    }
    cout << ans << endl;
  }
END:;
  return 0;
}
Problem p00468 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int friends[500];
  int n, m;
  pair<int, int> student[10000];
  int result[100];
  int I = 0;
  for (int i = 0;; i++) {
    cin >> n;
    cin >> m;
    if (m == 0 && n == 0) {
      break;
    }

    for (int a = 0; a < 500; a++) {
      friends[a] = 0;
      student[a].first = 0;
      student[a].second = 0;
    }

    for (int a = 0; a < m; a++) {
      cin >> student[a].first >> student[a].second;
      student[a].first--;
      student[a].second--;
    }

    sort(student, student + m);

    friends[0] = 1;

    for (int a = 0; a < m; a++) {
      if (student[a].first == 0) {
        for (int b = 0; b < m; b++) {
          if (student[b].first == student[a].second) {
            friends[student[b].second] = 1;
          }
          if (student[b].second == student[a].second) {
            friends[student[b].first] = 1;
          }
        }
        friends[student[a].second] = 1;
      }
    }

    result[i] = -1;
    for (int a = 0; a < n; a++) {
      if (friends[a] == 1) {
        result[i]++;
      }
    }
    I++;
  }

  for (int a = 0; a < I; a++) {
    cout << result[a] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

int main() {
  int friends[500];
  int n, m;
  pair<int, int> student[500];
  int result[100];
  int I = 0;
  for (int i = 0;; i++) {
    cin >> n;
    cin >> m;
    if (m == 0 && n == 0) {
      break;
    }

    for (int a = 0; a < 500; a++) {
      friends[a] = 0;
      student[a].first = 0;
      student[a].second = 0;
    }

    for (int a = 0; a < m; a++) {
      cin >> student[a].first >> student[a].second;
      student[a].first--;
      student[a].second--;
    }

    sort(student, student + m);

    friends[0] = 1;

    for (int a = 0; a < m; a++) {
      if (student[a].first == 0) {
        for (int b = a; b < m; b++) {
          if (student[b].first == student[a].second) {
            friends[student[b].second] = 1;
          }
        }
        friends[student[a].second] = 1;
      }
    }

    result[i] = -1;
    for (int a = 0; a < n; a++) {
      if (friends[a] == 1) {
        result[i]++;
      }
    }
    I++;
  }

  for (int a = 0; a < I; a++) {
    cout << result[a] << endl;
  }

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

#include <cstdio>
#include <cstring>

using namespace std;

int n, m;
int fr[501][501];
int s[501];

void f(int from, int depth) {
  s[from] = 1;
  if (depth == 3)
    return;
  for (int i = 1; i <= n; ++i) {
    if (fr[from][i])
      f(i, depth + 1);
  }
}

int main() {
  while (scanf("%d %d", &n, &m), n || m) {
    for (int i = 1; i <= n; ++i)
      memset(fr[i], 0, sizeof(fr[i]));
    memset(s, 0, sizeof(s));
    for (int i = 0; i < m; ++i) {
      int a, b;
      scanf("%d %d", &a, &b);
      fr[a][b] = fr[b][a] = 1;
    }
    f(1, 1);
    int cnt = 0;
    for (int i = 1; i <= n; ++i)
      if (s[i])
        cnt++;
    printf("%d\\n", cnt - 1);
  }
}

Error: 0

Fixed submission:

#include <cstdio>
#include <cstring>

using namespace std;

int n, m;
int fr[501][501];
int s[501];

void f(int from, int depth) {
  s[from] = 1;
  if (depth == 3)
    return;
  for (int i = 1; i <= n; ++i) {
    if (fr[from][i])
      f(i, depth + 1);
  }
}

int main() {
  while (scanf("%d %d", &n, &m), n || m) {
    for (int i = 1; i <= n; ++i)
      memset(fr[i], 0, sizeof(fr[i]));
    memset(s, 0, sizeof(s));
    for (int i = 0; i < 5; ++i) {
      int a, b;
      scanf("%d %d", &a, &b);
      fr[a][b] = 1;
    }
    f(1, 1);
    int cnt = 0;
    for (int i = 1; i <= n; ++i)
      if (s[i])
        cnt++;
    printf("%d\\n", cnt - 1);
  }
}
Problem p00468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define pb(a) push_back(a)
#define all(a) (a).begin(), (a).end()
#define debug(x) cout << "debug " << x << endl;
using namespace std;

#define N 505
#define INF 999999

int M[N][N];

int bfs(int n) {
  int dis[N];   //????????¨????????¢
  queue<int> q; //?¨??????????????????\\??????
  rep(i, N) dis[i] = INF;

  dis[1] = 0;
  q.push(1);

  int u;
  while (!q.empty()) {
    u = q.front();
    q.pop();
    rep(v, n + 1) {
      if (M[u][v] && dis[v] == INF) {
        dis[v] = dis[u] + 1; //??°???????????±???
        q.push(v);
      }
    }
  }
  int ans = 0;
  rep(i, n + 1) if (dis[i] == 1 || dis[i] == 2) ans++;
  return ans;
}

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    rep(i, N) rep(j, N) M[i][j] = 0;
    rep(i, m) {
      int a, b;
      cin >> a >> b;
      M[a][b] = M[b][a] = 1;
    }

    cout << bfs(n) << endl;
  }
}

Error: TLE

Fixed submission:

#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <vector>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) range(i, 0, b)
#define debug(x) cout << "debug " << x << endl;
using namespace std;

int ans;
int n, d;

void solve(int tree[][505], int x, int deep) {
  if (deep == 2)
    return;
  range(i, 1, n + 1) {
    if (tree[x][i] == 1) {
      ans++;
      deep++;
      solve(tree, i, deep);
    }
  }
}

int main() {
  while (cin >> n >> d, n || d) {
    int tree[505][505] = {0};
    ans = 0;
    rep(i, d) {
      int a, b;
      cin >> a >> b;
      tree[a][b] = 1;
    }
    solve(tree, 1, 0);
    cout << ans << endl;
  }
}
Problem p00468 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int ans = 0, n, m, x, y, fri[510], sfri[510][510];
int frin[510];
int main() {

  while (1) {
    fill(fri, fri + 510, 0);
    fill(sfri[0], sfri[510] + 510, 0);
    fill(frin, frin + 510, 0);
    ans = 0;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < m; i++) {
      cin >> x >> y;
      sfri[x][y] = 1;
      if (x == 1) {
        if (fri[y] == 0) {
          ans += 1;
          fri[y] = 1;
          frin[y] = 2;
          // cout << 'p' << endl;
        }
      }
    }
    for (int u = 2; u <= 500; u++) {
      for (int t = 2; t <= 500; t++) {
        if (sfri[u][t] == 1 && frin[u] == 2 && fri[t] == 0) {
          ans += 1;
          fri[t] = 1;
          // cout << t << endl;
        }
        if (sfri[u][t] == 1 && frin[t] == 2 && fri[u] == 0) {
          ans += 1;
          fri[u] = 1;
          // cout << u << endl;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>

using namespace std;

int main() {

  int n, a, fren = 0;
  while (1) {
    cin >> n >> a;
    if (n == 0 && a == 0)
      break;
    n++;
    int fre[n][n], f[n];
    fill(fre[0], fre[n], 0);
    fill(f, f + n, 0);
    int fre1, fre2;
    for (int y = 1; y < a; y++) {
      cin >> fre1 >> fre2;
      if (fre1 < fre2) {
        fre[fre1][fre2] = 1;
      } else {
        fre[fre2][fre1] = 1;
      }
    }
    for (int u = 1; u < n; u++) {
      if (fre[1][u] == 1) {
        if (f[u] == 0) {
          fren += 1;
          f[u] = 1;
        }

        for (int r = u; r < n; r++) {
          if (fre[u][r] == 1) {
            if (f[r] == 0) {
              fren += 1;
              f[r] = 1;
            }
          }
        }
      }
    }
    cout << fren << endl;
    fren = 0;
  }

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

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, M;
  while (cin >> N >> M, N || M) {

    vector<int> v[500];
    for (int i = 0; i < M; i++) {
      int a, b;
      cin >> a >> b;
      v[a - 1].push_back(b - 1);
      v[b - 1].push_back(a - 1);
    }

    bool f[500] = {0};
    for (int i = 0; i < v[0].size(); i++) {
      f[v[0][i]] = 1;
      for (int j = 0; j < v[v[0][i]].size(); j++) {
        f[v[v[0][i]][j]] = 1;
      }
    }

    int ans = 0;
    for (int i = 1; i < N; i++) {
      if (f[i])
        ans++;
    }

    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, M;
  while (cin >> N >> M, N || M) {

    vector<int> v[500];
    for (int i = 0; i < M; i++) {
      int a, b;
      cin >> a >> b;
      v[a - 1].push_back(b - 1);
      v[b - 1].push_back(a - 1);
    }

    bool f[500] = {0};
    for (int i = 0; i < v[0].size(); i++) {
      f[v[0][i]] = 1;
      for (int j = 0; j < v[i].size(); j++) {
        f[v[v[0][i]][j]] = 1;
      }
    }

    int ans = 0;
    for (int i = 1; i < N; i++) {
      if (f[i])
        ans++;
    }

    cout << ans << endl;
  }
}
Problem p00468 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
#define MAX 502
#define MAX_P 10002

int main() {
  int n, m;

  while (cin >> n >> m, n + m) {
    int a, b, tmp[MAX] = {0}, cnt = 0, friA[MAX_P] = {0}, friB[MAX_P] = {0},
              t = 0;
    for (int i = 0; i < m; i++) {
      cin >> a >> b;
      if (a == 1) {
        tmp[b] = 1;
      } else {
        friA[t] = a;
        friB[t] = b;
        t++;
      }
    }

    for (int i = 0; i < t; i++) {
      if (tmp[friA[i]] == 1 && !tmp[friB[i]])
        tmp[friB[i]] = 2;
      else if (!tmp[friA[i]] && tmp[friB[i]] == 1)
        tmp[friA[i]] = 2;
    }

    for (int i = 0; i < MAX; i++) {
      if (tmp[i] > 0) {
        cnt++;
      }
    }
    cout << cnt << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 502

int main() {
  int n, m;

  while (cin >> n >> m, n + m) {
    int a, b, tmp[MAX] = {0}, cnt = 0, friA[MAX] = {0}, friB[MAX] = {0}, t = 0;
    for (int i = 0; i < m; i++) {
      cin >> a >> b;
      if (a == 1) {
        tmp[b] = 1;
      } else {
        friA[t] = a;
        friB[t++] = b;
      }
    }

    for (int i = 0; i < t; i++) {
      if (tmp[friA[i]] == 1 && !tmp[friB[i]])
        tmp[friB[i]] = 2;
      else if (!tmp[friA[i]] && tmp[friB[i]] == 1)
        tmp[friA[i]] = 2;
    }

    for (int i = 0; i < MAX; i++) {
      if (tmp[i] > 0) {
        cnt++;
      }
    }
    cout << cnt << endl;
  }

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

#include <bits/stdc++.h>

using namespace std;

#define MAX 500
#define MAX_M 10001

int main() {
  int N, M;
  while (cin >> N >> M, (N | M)) {
    bool F[MAX] = {0}, C[MAX] = {0};
    int A[MAX_M] = {0}, B[MAX_M] = {0}, idx = 0;
    for (int i = 0; i < M; i++) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      if (!a) {
        F[b] = C[b] = true;
      } else {
        A[idx] = a;
        B[idx++] = b;
      }
    }
    for (int i = 0; i < idx; i++) {
      if (!F[A[i]] && F[B[i]]) {
        C[A[i]] = true;
      } else if (F[A[i]] && !F[B[i]]) {
        C[B[i]] = true;
      }
    }
    cout << count(C, C + N, 1) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MAX 500

int main() {
  int N, M;
  while (cin >> N >> M, (N | M)) {
    bool F[MAX] = {0}, C[MAX] = {0};
    int A[MAX] = {0}, B[MAX] = {0}, idx = 0;
    for (int i = 0; i < M; i++) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      if (!a) {
        F[b] = C[b] = true;
      } else {
        A[idx] = a;
        B[idx++] = b;
      }
    }
    for (int i = 0; i < idx; i++) {
      if (!F[A[i]] && F[B[i]]) {
        C[A[i]] = true;
      } else if (F[A[i]] && !F[B[i]]) {
        C[B[i]] = true;
      }
    }
    cout << count(C, C + N, 1) << endl;
  }
  return 0;
}