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

#include <cstdio>

constexpr int MAX_N = 500;
constexpr int MAX_M = 10000;

int N, M;
int e[MAX_N][MAX_N];
bool flag[MAX_N] = {false};

int dfs(int n = 0, int depth = 0) {
  if (depth == 2) {
    if (flag[n]) {
      return 0;
    } else {
      flag[n] = true;
      return 1;
    }
  }

  int res = 0;
  if (!flag[n]) {
    ++res;
    flag[n] = true;
  }

  for (int i = 0; i < N; ++i) {
    if (e[n][i]) {
      res += dfs(i, depth + 1);
    }
  }

  return res;
}

int main() {
  while (true) {
    scanf("%d%d", &N, &M);
    if (N == 0 && M == 0) {
      break;
    }

    for (int i = 0; i < N; ++i) {
      flag[i] = false;
      for (int j = 0; j < N; ++j) {
        e[i][j] = false;
      }
    }
    flag[0] = true;

    for (int i = 0; i < M; ++i) {
      int a, b;
      scanf("%d%d", &a, &b);
      --a, --b;
      e[a][b] = e[b][a] = true;
    }

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

Error: 0

Fixed submission:

#include <cstdio>

constexpr int MAX_N = 500;
constexpr int MAX_M = 10000;

int N, M;
int e[MAX_N][MAX_N];
bool flag[MAX_N] = {false};

int dfs(int n = 0, int depth = 0) {
  if (depth == 2) {
    if (flag[n]) {
      return 0;
    } else {
      flag[n] = true;
      return 1;
    }
  }

  int res = 0;
  if (!flag[n]) {
    ++res;
    flag[n] = true;
  }

  for (int i = 0; i < N; ++i) {
    if (e[n][i]) {
      res += dfs(i, depth + 1);
    }
  }

  return res;
}

int main() {
  while (true) {
    scanf("%d%d", &N, &M);
    if (N == 0 && M == 0) {
      break;
    }

    for (int i = 0; i < N; ++i) {
      flag[i] = false;
      for (int j = 0; j < M; ++j) {
        e[i][j] = false;
      }
    }
    flag[0] = true;

    for (int i = 0; i < M; ++i) {
      int a, b;
      scanf("%d%d", &a, &b);
      --a, --b;
      e[a][b] = e[b][a] = true;
    }

    printf("%d\\n", dfs());
  }
  return 0;
}
Problem p00468 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer"
int main() {
  while (1) {
    int N, M;
    cin >> N >> M;
    if (!N)
      break;
    vector<vector<int>> edges(N);
    for (int i = 0; i < M; ++i) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      edges[a].push_back(b);
      edges[b].push_back(a);
    }
    vector<int> comes(N);
    for (auto e : edges[0]) {
      comes[e] = true;
    }
    vector<int> friends(N);
    for (int i = 1; i < N; ++i) {
      if (comes[i]) {
        friends[i] = true;
        for (auto e : edges[i])
          friends[e] = true;
      }
    }
    int ans = count(friends.begin() + 1, friends.end(), 1);
    cout << ans << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer"
int main() {
  while (1) {
    int N, M;
    cin >> N >> M;
    if (!N)
      break;
    vector<vector<int>> edges(N);
    for (int i = 0; i < N; ++i) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      edges[a].push_back(b);
      edges[b].push_back(a);
    }
    vector<int> comes(N);
    for (auto e : edges[0]) {
      comes[e] = true;
    }
    vector<int> friends(N);
    for (int i = 1; i < N; ++i) {
      if (comes[i]) {
        friends[i] = true;
        for (auto e : edges[i])
          friends[e] = true;
      }
    }
    int ans = count(friends.begin() + 1, friends.end(), 1);
    cout << ans << endl;
  }

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

#include <bits/stdc++.h>
using namespace std;
void masatoy(int, int, int);
int n, m, m1[501][501], m2[501];
int main() {
  while (1) {
    memset(m1, 0, sizeof(m1));
    memset(m2, 0, sizeof(m2));
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;
    for (int i = 0; i < m; i++) {
      int x, y;
      cin >> x >> y;
      m1[x][y] = 1;
      m1[y][x] = 1;
    }
    for (int i = 2; i <= n; i++) {
      if (m1[1][i] == 1)
        m2[i] = 1;
    }
    for (int i = 2; i <= n; i++) {
      if (m1[1][i] == 1) {
        for (int j = 1; j <= n; j++) {
          if (m1[i][j] == 1)
            m2[j] = 1;
        }
      }
    }
    int sum = 0;
    for (int i = 2; i <= n; i++) {
      if (m2[i] == 1)
        sum++;
    }
    cout << sum << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int m1[10001], m2[10001][501], m3[10001][501], n,
    m; //??????????????°????¨??????????????????????
void masatoy(int retu, int shindo) {
  for (int j = 1; j <= n; j++) {
    if (m2[retu][j] == 1 && shindo < 3) {
      m1[j] = 1;
      masatoy(j, shindo + 1);
    }
  }
}
void masatoy2(int retu, int shindo) {
  for (int j = 1; j <= n; j++) {
    if (m3[j][retu] == 1 && shindo < 3) {
      m1[j] = 1;
      masatoy2(j, shindo + 1);
    }
  }
}
int main() {
  while (1) {
    int sum = 0;
    memset(m1, 0, sizeof(m1));
    memset(m2, 0, sizeof(m2));
    memset(m3, 0, sizeof(m3));
    m2[1][1] = 1;
    cin >> n >> m;
    if (n == 0 && m == 0)
      return 0;
    for (int i = 1; i <= n; i++) {
      int x, y;
      cin >> x >> y;
      m2[x][y] = 1;
      m3[y][x] = 1;
    }
    masatoy(1, 1);
    for (int i = 1; i <= n; i++)
      if (m1[i] == 1)
        sum++;
    cout << sum << endl;
  }
}
Problem p00468 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

namespace _ {
struct _ {
  _() { // initialize
    cin.tie(0);
    ios::sync_with_stdio(false);
  }
} _;
} // namespace _
template <typename T, size_t size> inline size_t len(const T (&)[size]) {
  return size;
}
#define println(s) cout << s << '\\n';
#define str to_string
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define all(c) (c).begin(), (c).end()

#define MAX_N 600
#define MAX_M 10100
int a[MAX_M];
int b[MAX_M];

int main() {
  while (true) {
    bool isMyfriend[MAX_N + 1] = {false};
    bool isKnown[MAX_N + 1] = {false};

    int n;
    cin >> n;
    int m;
    cin >> m;
    if (n == 0 && m == 0)
      break;

    // find friends
    rep(i, m) {
      cin >> a[i] >> b[i];
      // a < b
      if (a[i] == 1) {
        isMyfriend[b[i] - 1] = true;
      }
    }
    // find friend's friends
    rep(i, m) {
      // a <-> b
      // count: 1 <-> x
      // count: _ <-> x
      rep(x, n) {
        if (isMyfriend[x] && ((a[i] - 1) == x || (b[i] - 1) == x)) {
          isKnown[a[i] - 1] = true;
          isKnown[b[i] - 1] = true;
        }
      }
    }

    int count = 0;
    for (int i = 1; i < n; i++) {
      if (isMyfriend[i] || isKnown[i]) {
        // cout << "!" << i << endl; // DEBUG
        count++;
      }
    }
    cout << count << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <functional>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

namespace _ {
struct _ {
  _() { // initialize
    cin.tie(0);
    ios::sync_with_stdio(false);
  }
} _;
} // namespace _
template <typename T, size_t size> inline size_t len(const T (&)[size]) {
  return size;
}
#define println(s) cout << s << '\\n';
#define str to_string
#define rep(i, n) for (int i = 0; i < int(n); i++)
#define all(c) (c).begin(), (c).end()

#define MAX_N 501

int a[MAX_N];
int b[MAX_N];

int main() {
  while (true) {
    bool isMyfriend[MAX_N + 1] = {false};
    bool isKnown[MAX_N + 1] = {false};

    int n;
    cin >> n;
    int m;
    cin >> m;
    if (n == 0 && m == 0)
      break;

    rep(i, m) {
      cin >> a[i] >> b[i];
      // a < b
      if (a[i] == 1) {
        isMyfriend[b[i] - 1] = true;
      }
    }
    rep(i, m) {
      // a <-> b
      // count: 1 <-> x
      // count: _ <-> x
      rep(x, MAX_N) {
        if (isMyfriend[x] && ((a[i] - 1) == x || (b[i] - 1) == x)) {
          isKnown[a[i] - 1] = true;
          isKnown[b[i] - 1] = true;
        }
      }
    }

    int count = 0;
    for (int i = 1; i < m; i++) {
      if (isMyfriend[i] || isKnown[i]) {
        // cout << "!" << i << endl; // DEBUG
        count++;
      }
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00468 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {

  int n;
  int list;
  while (cin >> n >> list, n + list) {
    // cout << n << list << endl;
    typedef pair<int, int> F;
    vector<F> v(100001);
    vector<int> x(n + 1, 0);
    bool array[100001];
    bool dcheck[100001];
    int answer = 0;
    rep(i, n + 1) array[i] = false;
    rep(i, n + 1) dcheck[i] = true;
    rep(i, list) cin >> v[i].first >> v[i].second;
    rep(i, list) {
      if (v[i].first == 1) {
        array[v[i].second] = true;
        // cout << v[i].second << endl;
        answer++;
      }
    }
    // cout << "------v[i].second-------- " << endl;
    rep(i, list) {
      if ((array[v[i].first] == true && v[i].first != 1 &&
           array[v[i].second] == false && dcheck[v[i].second] == true) ||
          (array[v[i].second] == true && array[v[i].first] == false &&
           v[i].first != 1 && dcheck[v[i].first] == true)) {
        answer++;
        // cout <<v[i].first << " and "<< v[i].second << endl;
        if (array[v[i].first])
          dcheck[v[i].second] = false;
        else
          dcheck[v[i].first] = false;
      }
    }
    cout << answer << endl;
    // cout <<"x is"; rep(i,n) cout << " " << x[i];
    // cout <<"v is"; rep(i,n) cout << " " <<
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {

  int n;
  int list;
  while (cin >> n >> list, n + list) {
    typedef pair<int, int> F;
    vector<F> v(100);
    vector<int> x(n + 1, 0);
    bool array[n];
    rep(i, n + 1) array[i] = false;
    rep(i, list) cin >> v[i].first >> v[i].second;
    rep(i, list) {
      if (v[i].first == 1) {
        x[v[i].second]++;
        array[v[i].second] = true;
      }
    }
    rep(i, list) {
      if (array[v[i].first])
        x[v[i].second]++;
    }
    int answer = 0;
    rep(i, n) {
      if (x[i] != 0)
        answer++;
    }
    cout << answer << endl;
  }
  return 0;
}
Problem p00468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>
using namespace std;
int a[10000], b[10000];
int main() {
  while (1) {
    int i, n, m, l = 0, s[501] = {0}, j;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    s[1] = 3;
    for (i = 0; i < m; i++) {
      cin >> a[i] >> b[i];
    }
    for (i = 0; i < m; i++) {
      for (j = 0; j < m; j++) {
        if (s[a[j]] > 0 && s[b[j]] < s[a[j]] - 1)
          s[b[j]] = s[a[j]] - 1;
        if (s[b[j]] > 0 && s[a[j]] < s[b[j]] - 1)
          s[a[j]] = s[b[j]] - 1;
      }
    }
    for (i = 2; i <= n; i++) {
      if (s[i] != 0)
        l++;
    }
    cout << l << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
int s[1001] = {0};
int main() {
  while (1) {
    int i, j, n, m, l = 0, a, b;
    cin >> n >> m;
    memset(s, 0, sizeof(s));
    s[1] = 3;
    for (i = 0; i < m; i++) {
      cin >> a >> b;
      if (s[a] > 0 && s[b] == 0)
        s[b] = s[a] - 1;
      if (s[b] > 0 && s[a] == 0)
        s[a] = s[b] - 1;
    }
    for (i = 1; i <= n; i++) {
      if (s[i] > 0)
        l++;
    }
    cout << l << endl;
  }
  return 0;
}
Problem p00468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <vector>

int main() {

  bool a[524] = {false}, c[524] = {false};
  int b[10024][3] = {0};
  int i, j, x, y;
  int n, m, count = 0;

  scanf("%d\\n%d\\n", &n, &m);
  while (n != 0 || m != 0) {
    if (n == 0)
      break;
    count = 0;
    for (i = 0; i < 524; i++) {
      a[i] = false;
      c[i] = false;
    }
    memset(b, 0, sizeof(b));
    a[1] = true;
    c[1] = true;
    j = 0;
    for (i = 1; i <= m; i++) {
      scanf("%d %d\\n", &x, &y);
      // printf("%d %d\\n",x,y);
      if (x == 1) {
        count++;
        a[y] = true;
        c[y] = true;
      }

      if (y == 1) {
        count++;
        a[x] = true;
        c[x] = true;
      }
      b[i][1] = x;
      b[i][2] = y;
    }
    // printf("%d\\n",count);
    for (j = 1; j <= m; j++) {
      if (a[b[j][1]] == true) {
        if (c[b[j][2]] == false) {
          count++;
          c[b[j][2]] = true;
        }
      }
      if (a[b[j][2]] == true) {
        if (c[b[j][1]] == false) {
          count++;
          c[b[j][1]] = true;
        }
      }
    }
    // printf("%d\\n",count);
    printf("%d\\n", count);
    scanf("%d\\n%d\\n", &n, &m);
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <vector>

int main() {

  bool a[524] = {false}, c[524] = {false};
  int b[524][2] = {0};
  int i, j, x, y;
  int n, m, count = 0;

  scanf("%d\\n%d\\n", &n, &m);
  while (n != 0 || m != 0) {
    if (n == 0)
      break;
    count = 0;
    for (i = 0; i < 524; i++) {
      a[i] = false;
      c[i] = false;
    }
    memset(b, 0, sizeof(b));
    a[1] = true;
    c[1] = true;
    j = 0;
    for (i = 1; i <= m; i++) {
      scanf("%d %d\\n", &x, &y);
      // printf("%d %d\\n",x,y);
      if (x == 1) {
        count++;
        a[y] = true;
        c[y] = true;
      }
      b[i][1] = x;
      b[i][2] = y;
    }
    // printf("%d\\n",count);
    for (j = 1; j <= m; j++) {
      if (a[b[j][1]] == true) { //&&c[b[j][2]]==false){
                                // printf("i=%d b[j][1] = %d\\n",j,b[j][1]);
        if (c[b[j][2]] == false) {
          count++;
          c[b[j][2]] = true;
        }
      }
    }
    // printf("%d\\n",count);
    printf("%d\\n", count);
    scanf("%d\\n%d\\n", &n, &m);
  }
  return 0;
}
Problem p00468 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>
int d[500];
int x[10000];
int y[10000];
int min(int a, int b) { return a < b ? a : b; }
int main() {
  int n, m, i;
  while (1) {
    scanf("%d", &n);
    scanf("%d", &m);
    if (n == 0)
      return 0;
    for (i = 0; i < n; i++)
      d[i] = 3;
    for (i = 0; i < m; i++) {
      int a, b;
      scanf("%d %d", &a, &b);
      if (a == 1)
        d[b - 1] = 1;
      x[i] = a - 1;
      y[i] = b - 1;
    }
    for (i = 0; i < m; i++) {
      if (d[x[i]] == 1)
        d[y[i]] = min(d[y[i]], 2);
      if (d[y[i]] == 1)
        d[x[i]] = min(d[x[i]], 2);
    }
    int ans = 0;
    for (i = 1; i < n; i++)
      if (d[i] <= 2)
        ans++;
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  while (true) {
    int n, m, ans = 0, w = 0, a[10000][2] = {}, fri[10000] = {};
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < m; i++) {
      for (int r = 0; r < 2; r++) {
        cin >> a[i][r];
      }
    }
    for (int i = 0; i < m; i++) {
      for (int r = 0; r < 2; r++) {
        if (a[i][r] == 1) {
          ans++;
          fri[w] = a[i][r + 1];
          a[i][r] = 0;
          w++;
        }
      }
    }
    for (int j = 0; j < w; j++) {
      for (int i = 0; i < m; i++) {
      ROOP:
        for (int r = 0; r < 2; r++) {
          if (fri[j] == a[i][r]) {
            for (int b; b < w; w++) {
              if (fri[b] == a[i][r + 1]) {
                a[i][r + 1] = 0;
                r++;
                goto ROOP;
              }
            }
            if (0 != a[i][r + 1]) {
              a[i][r + 1] = 0;
              ans++;
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p00468 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
int main(void) {
  int n, m, s[10001], x[10001], flg[10001], i, cnt;
  while (1) {
    scanf("%d%d", &n, &m);
    if (n == 0 && m == 0)
      break;
    for (i = 1; i <= m; i++) {
      scanf("%d%d", &s[i], &x[i]);
    }
    for (i = 1; i <= n; i++) {
      flg[i] = 0;
    }
    for (i = 1; i <= m; i++) {
      if (s[i] == 1) {
        flg[x[i]] = 1;
      }
      if (x[i] == 1) {
        flg[s[i]] = 1;
      }
    }
    for (i = 1; i <= m; i++) {
      if (flg[s[i]] == 1 && flg[x[i]] == 0) {
        flg[x[i]] = 2;
      }
      if (flg[x[i]] == 1 && flg[s[i]] == 0) {
        flg[s[i]] = 2;
      }
    }
    cnt = 0;
    for (i = 2; i <= n; i++) {
      if (flg[i] == 1 || flg[i] == 2) {
        cnt++;
        //			printf("%d\\n",i);
      }
    }
    printf("%d\\n", cnt);
  }
  return 0;
}

Error: WA

Fixed submission:

#include <stdio.h>
int main(void) {
  int a, b, s[1001], x[1001], flg[1001], i, j, cnt;
  while (1) {
    scanf("%d%d", &a, &b);
    if (a == 0 && b == 0)
      break;
    cnt = 0;
    for (i = 1; i <= b; i++) {
      scanf("%d%d", &s[i], &x[i]);
      flg[i] = 0;
    }
    for (i = 1; i <= b; i++) {
      if (s[i] == 1) {
        flg[x[i]] = 1;
      }
      if (x[i] == 1) {
        flg[s[i]] = 1;
      }
    }
    for (i = 1; i <= b; i++) {
      if (flg[i] != 0) {
        for (j = 1; j <= b; j++) {
          if (s[j] == i) {
            flg[x[j]] = 1;
          }
          if (x[j] == i) {
            flg[s[j]] = 1;
          }
        }
      }
    }
    for (i = 0; i < b; i++) {
      if (flg[i] != 0) {
        cnt++;
      }
    }
    printf("%d\\n", cnt);
  }
  return 0;
}
Problem p00468 (C++) — buggy submission, verdict: Runtime Error

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

bool invite[500];
bool are_friend[500][500];

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    memset(invite, false, sizeof(invite));
    memset(are_friend, false, sizeof(are_friend));
    for (int i = 0; i < m; i++) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      are_friend[a][b] = are_friend[b][a] = true;
    }
    for (int i = 1; i < n; i++) {
      if (are_friend[0][i]) {
        invite[i] = true;
        for (int j = 1; j < n; j++) {
          if (are_friend[i][j]) {
            invite[j] = true;
          }
        }
      }
    }
    cout << count(invite, invite + n, true) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

bool invite[500];
bool are_friend[500][500];

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    memset(invite, false, sizeof(invite));
    memset(are_friend, false, sizeof(are_friend));
    for (int i = 0; i < m; i++) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      are_friend[a][b] = are_friend[b][a] = true;
    }
    for (int i = 1; i < m; i++) {
      if (are_friend[0][i]) {
        invite[i] = true;
        for (int j = 1; j < m; j++) {
          if (are_friend[i][j]) {
            invite[j] = true;
          }
        }
      }
    }
    cout << count(invite, invite + n, true) << endl;
  }
  return 0;
}
Problem p00468 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<vector<bool>> relation(n, vector<bool>(n));
    for (int i = 0; i < m; ++i) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      relation[a][b] = relation[b][a] = true;
    }
    vector<bool> friends(n);
    for (int i = 0; i < n; ++i) {
      if (relation[0][i]) {
        friends[i] = true;
        for (int j = 0; j < n; ++j) {
          if (relation[i][j]) {
            friends[j] = true;
          }
        }
      }
    }
    friends[0] = false;
    cout << count(friends.begin(), friends.end(), true) << endl;
  }
}

Error: -11

Fixed submission:

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

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<vector<bool>> relation(n, vector<bool>(n));
    for (int i = 0; i < n; ++i) {
      int a, b;
      cin >> a >> b;
      a--;
      b--;
      relation[a][b] = relation[b][a] = true;
    }
    vector<bool> friends(n);
    for (int i = 0; i < n; ++i) {
      if (relation[0][i]) {
        friends[i] = true;
        for (int j = 0; j < n; ++j) {
          if (relation[i][j]) {
            friends[j] = true;
          }
        }
      }
    }
    friends[0] = false;
    cout << count(friends.begin(), friends.end(), true) << endl;
  }
}
Problem p00468 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;

int main() {
  int n, m;
  P p;

  while (cin >> n >> m && n && m) {
    int ans = 0;
    bool matrix[n + 2][n + 2];
    for (int i = 0; i < n + 2; i++)
      for (int j = 0; j < n + 2; j++)
        matrix[i][j] = false;
    for (int i = 0; i < m; i++) {
      cin >> p.F >> p.S;
      matrix[p.F][p.S] = matrix[p.S][p.F] = true;
    }

    int here;
    map<int, bool> cnt;
    for (int i = 1; i <= n; i++) {
      if (matrix[1][i]) {
        here = i;
        cnt[here] = true;
        for (int j = 1; j <= n; j++) {
          if (i == j)
            continue;
          if (matrix[here][j]) {
            cnt[j] = true;
          }
        }
      }
    }
    map<int, bool>::iterator it = cnt.begin();
    while (it != cnt.end()) {
      if ((*it).F != 1)
        ans++;
      it++;
    }

    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
#define F first
#define S second
using namespace std;
typedef pair<int, int> P;

int main() {
  int n, m;
  P p;

  while (cin >> n >> m && n && m) {
    int ans = 0;
    bool matrix[m + 2][m + 2];
    for (int i = 0; i < m + 2; i++)
      for (int j = 0; j < m + 2; j++)
        matrix[i][j] = false;
    for (int i = 0; i < m; i++) {
      cin >> p.F >> p.S;
      matrix[p.F][p.S] = matrix[p.S][p.F] = true;
    }

    int here;
    map<int, bool> cnt;
    for (int i = 0; i < m; i++) {
      if (matrix[1][i]) {
        here = i;
        cnt[here] = true;
        for (int j = 0; j < m; j++) {
          if (i == j)
            continue;
          if (matrix[here][j]) {
            cnt[j] = true;
          }
        }
      }
    }
    map<int, bool>::iterator it = cnt.begin();
    while (it != cnt.end()) {
      if ((*it).F != 1)
        ans++;
      it++;
    }

    cout << ans << endl;
  }
  return 0;
}