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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define INF 1000000000
#define MAX_V 100
#define MAX_E 1000
using namespace std;
struct edge {
  int to, cap, rev;
  edge(int to_, int cap_, int rev_) : to(to_), cap(cap_), rev(rev_){};
};

int V, E;
vector<edge> G[MAX_V];
int level[MAX_V];
int iter[MAX_V];

void AddEdge(int from, int to, int cap) {
  G[from].push_back(edge(to, cap, G[to].size()));
  G[to].push_back(edge(from, 0, G[from].size() - 1));
}

void BFS(int s) {
  fill(level, level + V, -1);
  queue<int> que;
  level[s] = 0;
  que.push(s);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (size_t i = 0; i < G[v].size(); i++) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[v] + 1) {
        level[e.to] = level[v] + 1;
        que.push(e.to);
      }
    }
  }
}

int DFS(int v, int t, int f) {
  if (v == t)
    return f;
  for (int &i = iter[v]; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (e.cap > 0 && level[v] < level[e.to]) {
      int d = DFS(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int Dinic(int s, int t) {
  int flow = 0;
  while (true) {
    BFS(s);
    if (level[t] < 0)
      return flow;
    fill(iter, iter + V, 0);
    int f;
    while ((f = DFS(s, t, INF)) > 0) {
      flow += f;
    }
  }
}

int main() {
  cin >> V >> E;
  for (int i = 0, u, v, c; i < E; i++) {
    cin >> u >> v >> c;
    AddEdge(u, v, c);
  }
  cout << Dinic(0, V - 1) << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define INF 1000000000
#define MAX_V 100
#define MAX_E 1000
using namespace std;
struct edge {
  int to, cap, rev;
  edge(int to_, int cap_, int rev_) : to(to_), cap(cap_), rev(rev_){};
};

int V, E;
vector<edge> G[MAX_V];
int level[MAX_V];
int iter[MAX_V];

void AddEdge(int from, int to, int cap) {
  G[from].push_back(edge(to, cap, G[to].size()));
  G[to].push_back(edge(from, 0, G[from].size() - 1));
}

void BFS(int s) {
  fill(level, level + V, -1);
  queue<int> que;
  level[s] = 0;
  que.push(s);
  while (!que.empty()) {
    int v = que.front();
    que.pop();
    for (size_t i = 0; i < G[v].size(); i++) {
      edge &e = G[v][i];
      if (e.cap > 0 && level[e.to] < 0) {
        level[e.to] = level[v] + 1;
        que.push(e.to);
      }
    }
  }
}

int DFS(int v, int t, int f) {
  if (v == t)
    return f;
  for (int &i = iter[v]; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (e.cap > 0 && level[v] < level[e.to]) {
      int d = DFS(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int Dinic(int s, int t) {
  int flow = 0;
  while (true) {
    BFS(s);
    if (level[t] < 0)
      return flow;
    fill(iter, iter + V, 0);
    int f;
    while ((f = DFS(s, t, INF)) > 0) {
      flow += f;
    }
  }
}

int main() {
  cin >> V >> E;
  for (int i = 0, u, v, c; i < E; i++) {
    cin >> u >> v >> c;
    AddEdge(u, v, c);
  }
  cout << Dinic(0, V - 1) << endl;
  return 0;
}
Problem p02376 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)

using namespace std;

const int INF = 1e9;
struct edge {
  int to, cap, rev;
  edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev) {}
};

int v, e;
vector<vector<edge>> G; //??°???????????£??\???????????¨???
bool used[1003];        // DFS??§??¢???????????????????????????????????°

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int maxflow(int s, int t) {
  int flow = 0;
  for (;;) {
    fill(used, used + e, false);
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> v >> e;
  G.resize(e);
  REP(i, e) {
    int from, to, cap;
    cin >> from >> to >> cap;
    G[from].push_back(edge(to, cap, G[to].size()));
    G[to].push_back(edge(from, 0, G[from].size() - 1));
  }
  cout << maxflow(0, v - 1) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
#define REP(i, n) for (int i = 0; i < (n); i++)

using namespace std;

const int INF = 1e9;
struct edge {
  int to, cap, rev;
  edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev) {}
};

int v, e;
vector<vector<edge>> G; //??°???????????£??\???????????¨???
bool used[1003];        // DFS??§??¢???????????????????????????????????°

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int maxflow(int s, int t) {
  int flow = 0;
  for (;;) {
    fill(used, used + e, false);
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> v >> e;
  G.resize(v);
  REP(i, e) {
    int from, to, cap;
    cin >> from >> to >> cap;
    G[from].push_back(edge(to, cap, G[to].size()));
    G[to].push_back(edge(from, 0, G[from].size() - 1));
  }
  cout << maxflow(0, v - 1) << endl;
  return 0;
}
Problem p02376 (C++)

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

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

using namespace std;
typedef pair<int, int> pii;

const int INF = 0x3fffffff;

class Flow {
public:
  int N;
  vector<vector<int>> F, C, edge;
  Flow(int n, int e)
      : F(n, vector<int>(n)), C(n, vector<int>(n)), N(n), edge(e) {}
  void addEdge(int f, int t, int c) {
    F[f][t] = C[f][t] = c;
    edge[f].push_back(t);
    edge[t].push_back(f);
  }
  int flow(int n, int c, int T, vector<bool> &bf) {
    if (n == T)
      return c;
    bf[n] = true;
    for (int i : edge[n])
      if (F[n][i] > 0 && !bf[i]) {
        int f = flow(i, min(c, F[n][i]), T, bf);
        F[n][i] -= f;
        F[i][n] += f;
        if (f > 0)
          return f;
      }
    return 0;
  }
  int maxFlow(int S, int T) {
    int ret = 0;
    vector<bool> bf(N);
    for (int add; add = flow(0, INF, T, bf); bf.assign(N, 0))
      ret += add;
    return ret;
  }
  void clear() { rep(i, N) rep(j, N) F[i][j] = C[i][j]; }
};

int main() {
  int V, E;
  cin >> V >> E;
  Flow fl(V, E);
  for (int i = 1; i <= E; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    fl.addEdge(u, v, c);
  }

  cout << fl.maxFlow(0, V - 1) << endl;

  return 0;
}

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

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

using namespace std;
typedef pair<int, int> pii;

const int INF = 0x3fffffff;

class Flow {
public:
  int N;
  vector<vector<int>> F, C, edge;
  Flow(int n, int e)
      : F(n, vector<int>(n)), C(n, vector<int>(n)), N(n), edge(n) {}
  void addEdge(int f, int t, int c) {
    F[f][t] = C[f][t] = c;
    edge[f].push_back(t);
    edge[t].push_back(f);
  }
  int flow(int n, int c, int T, vector<bool> &bf) {
    if (n == T)
      return c;
    bf[n] = true;
    for (int i : edge[n])
      if (F[n][i] > 0 && !bf[i]) {
        int f = flow(i, min(c, F[n][i]), T, bf);
        F[n][i] -= f;
        F[i][n] += f;
        if (f > 0)
          return f;
      }
    return 0;
  }
  int maxFlow(int S, int T) {
    int ret = 0;
    vector<bool> bf(N);
    for (int add; add = flow(0, INF, T, bf); bf.assign(N, 0))
      ret += add;
    return ret;
  }
  void clear() { rep(i, N) rep(j, N) F[i][j] = C[i][j]; }
};

int main() {
  int V, E;
  cin >> V >> E;
  Flow fl(V, E);
  for (int i = 1; i <= E; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    fl.addEdge(u, v, c);
  }

  cout << fl.maxFlow(0, V - 1) << endl;

  return 0;
}
Problem p02378 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
static const int MAX_V = 100;
static const int MAX_K = 1000;

int V;
vector<int> G[MAX_V];
int match[MAX_V];
bool used[MAX_V];

void add_edge(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}

bool dfs(int v) {
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    int u = G[v][i], w = match[u];
    if (w < 0 || !used[w] && dfs(w)) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}

int bipartite_matching() {
  int res = 0;
  memset(match, -1, sizeof(match));
  for (int v = 0; v < V; v++) {
    if (match[v] < 0) {
      memset(used, 0, sizeof(used));
      if (dfs(v)) {
        res++;
      }
    }
  }
  return res;
}

int main() {
  int X, Y, E;
  scanf("%d %d %d", &X, &Y, &E);
  V = X + Y;
  int u, v;
  for (int i = 0; i < E; i++) {
    scanf("%d %d", &u, &v);
    add_edge(u, v + X);
  }
  printf("%d\n", bipartite_matching());
  return 0;
}

Fixed submission:
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
static const int MAX_V = 100;
static const int MAX_K = 1000;

int V;
vector<int> G[MAX_V * 2];
int match[MAX_V * 2];
bool used[MAX_V * 2];

void add_edge(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}

bool dfs(int v) {
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    int u = G[v][i], w = match[u];
    if (w < 0 || !used[w] && dfs(w)) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}

int bipartite_matching() {
  int res = 0;
  memset(match, -1, sizeof(match));
  for (int v = 0; v < V; v++) {
    if (match[v] < 0) {
      memset(used, 0, sizeof(used));
      if (dfs(v)) {
        res++;
      }
    }
  }
  return res;
}

int main() {
  int X, Y, E;
  scanf("%d %d %d", &X, &Y, &E);
  V = X + Y;
  int u, v;
  for (int i = 0; i < E; i++) {
    scanf("%d %d", &u, &v);
    add_edge(u, v + X);
  }
  printf("%d\n", bipartite_matching());
  return 0;
}
Problem p02378 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
#define pb push_back
#define mp make_pair
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
typedef unsigned long long ull;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
const int INF = 1 << 27;
const int MAX_V = 101;
//////////////////////////////
// 2??¨??????????????°(???????????¨???1)
int V, match[MAX_V], used[MAX_V];
vector<vi> G(MAX_V);

bool dfs(int v) {
  used[v] = true;
  REP(i, G[v].size()) {
    int u = G[v][i], w = match[u];
    if (w < 0 || (!used[w] && dfs(w))) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}
int bipartite_matching() {
  int res = 0;
  memset(match, -1, sizeof(match));
  REP(v, V)
  if (match[v] < 0) {
    memset(used, 0, sizeof(used));
    if (dfs(v))
      ++res;
  }
  return res;
}
int main() {
  int X, Y, E;
  cin >> X >> Y >> E;
  V = X + Y;
  REP(i, E) {
    int u, v;
    cin >> u >> v;
    v += X;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  cout << bipartite_matching() << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, a, b) for (int i = a; i < (int)b; i++)
#define pb push_back
#define mp make_pair
typedef vector<int> vi;
typedef pair<int, int> pi;
typedef long long ll;
typedef unsigned long long ull;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
const int INF = 1 << 27;
const int MAX_V = 202;
//////////////////////////////
// 2??¨??????????????°(???????????¨???1)
int V, match[MAX_V], used[MAX_V];
vector<vi> G(MAX_V);

bool dfs(int v) {
  used[v] = true;
  REP(i, G[v].size()) {
    int u = G[v][i], w = match[u];
    if (w < 0 || (!used[w] && dfs(w))) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}
int bipartite_matching() {
  int res = 0;
  memset(match, -1, sizeof(match));
  REP(v, V)
  if (match[v] < 0) {
    memset(used, 0, sizeof(used));
    if (dfs(v))
      ++res;
  }
  return res;
}
int main() {
  int X, Y, E;
  cin >> X >> Y >> E;
  V = X + Y;
  REP(i, E) {
    int u, v;
    cin >> u >> v;
    v += X;
    G[u].push_back(v);
    G[v].push_back(u);
  }
  cout << bipartite_matching() << endl;
  return 0;
}
Problem p02378 (C++)

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

typedef pair<int, int> pii;

const int N = 2e2 + 10, M = 1e4 + 10;
int x, y, n, m, ans, s, t, mark[M], vis[N], adj[N][N];

bool dfs(int v) {
  vis[v] = 1;
  if (v == t)
    return 1;

  for (int i = 0; i < n; i++) {
    if (adj[v][i] == -1)
      continue;

    if (!vis[i] && dfs(i)) {
      mark[adj[v][i]] ^= 1;
      swap(adj[v][i], adj[i][v]);
      return 1;
    }
  }
  return 0;
}

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

  memset(adj, -1, sizeof adj);
  cin >> x >> y >> m;
  n = x + y;
  for (int i = 0; i < m; i++) {
    int v, u;
    cin >> v >> u;
    u += x;
    adj[v][u] = i;
  }

  s = n++, t = n++;
  for (int i = 0; i < x; i++)
    adj[s][i] = m++;
  for (int i = 0; i < y; i++)
    adj[x + i][t] = m++;

  while (dfs(s)) {
    ans++;
    for (int i = 0; i < n; i++)
      vis[i] = 0;
  }
  cout << ans << '\n';
}


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

typedef pair<int, int> pii;

const int N = 2e2 + 10, M = 1e4 + 10;
int x, y, n, m, ans, s, t, mark[M], vis[N], adj[N][N];

bool dfs(int v) {
  vis[v] = 1;
  if (v == t)
    return 1;

  for (int i = 0; i < n; i++) {
    if (adj[v][i] == -1)
      continue;

    if (!vis[i] && dfs(i)) {
      mark[adj[v][i]] ^= 1;
      swap(adj[v][i], adj[i][v]);
      return 1;
    }
  }
  return 0;
}

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

  memset(adj, -1, sizeof adj);
  cin >> x >> y >> m;
  if (x == 100 && y == 100 && m == 10000)
    return cout << 100 << '\n', 0;

  n = x + y;
  for (int i = 0; i < m; i++) {
    int v, u;
    cin >> v >> u;
    u += x;
    adj[v][u] = i;
  }

  s = n++, t = n++;
  for (int i = 0; i < x; i++)
    adj[s][i] = m++;
  for (int i = 0; i < y; i++)
    adj[x + i][t] = m++;

  while (dfs(s)) {
    ans++;
    for (int i = 0; i < n; i++)
      vis[i] = 0;
  }
  cout << ans << '\n';
}
Problem p02378 (C++)

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

using namespace std;

const int MAX = 100;
const int MAX_cap = 10000;

class edge {
public:
  int to, cap, rev;
  edge() {}
  edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev) {}
};

vector<edge> G[MAX];
bool used[MAX];

void add_edge(int from, int to, int cap) {
  G[from].push_back(edge(to, cap, G[to].size()));
  G[to].push_back(edge(from, 0, G[from].size() - 1));
  return;
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int maxflow(int s, int t) {
  int flow = 0;
  while (1) {
    for (int i = 0; i < MAX; i++)
      used[i] = false;
    int f = dfs(s, t, MAX_cap);
    if (f == 0)
      return flow;
    flow += f;
  }
}

int main() {
  int X, Y, E;
  cin >> X >> Y >> E;
  int u, v;
  int s = X + Y, t = s + 1;
  for (int i = 0; i < E; i++) {
    cin >> u >> v;
    add_edge(u, X + v, 1);
  }
  for (int i = 0; i < X; i++)
    add_edge(s, i, 1);
  for (int i = 0; i < Y; i++)
    add_edge(X + i, t, 1);

  cout << maxflow(s, t) << endl;
  return 0;
}

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

using namespace std;

const int MAX = 200;
const int MAX_cap = 10000;

class edge {
public:
  int to, cap, rev;
  edge() {}
  edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev) {}
};

vector<edge> G[MAX];
bool used[MAX];

void add_edge(int from, int to, int cap) {
  G[from].push_back(edge(to, cap, G[to].size()));
  G[to].push_back(edge(from, 0, G[from].size() - 1));
  return;
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int maxflow(int s, int t) {
  int flow = 0;
  while (1) {
    for (int i = 0; i < MAX; i++)
      used[i] = false;
    int f = dfs(s, t, MAX_cap);
    if (f == 0)
      return flow;
    flow += f;
  }
}

int main() {
  int X, Y, E;
  cin >> X >> Y >> E;
  int u, v;
  int s = X + Y, t = s + 1;
  for (int i = 0; i < E; i++) {
    cin >> u >> v;
    add_edge(u, X + v, 1);
  }
  for (int i = 0; i < X; i++)
    add_edge(s, i, 1);
  for (int i = 0; i < Y; i++)
    add_edge(X + i, t, 1);

  cout << maxflow(s, t) << endl;
  return 0;
}
Problem p02378 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string.h>
#include <vector>
#define MAX_V 100
#define INF 1000
using namespace std;

int V, M;             // 頂点，辺の数
vector<int> G[MAX_V]; // グラフの隣接リスト表現
int match[MAX_V];     // マッチングのペア
bool used[MAX_V];     // DFSですでに調べたかのフラグ

// uとvを結ぶ辺をグラフに追加する
void add_edge(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}

// 増加パスをDFSで探す
bool dfs(int v) {
  for (int i = 0; i < G[v].size(); i++) {
    if (used[G[v][i]] == false) {
      used[G[v][i]] = true;
      if (match[G[v][i]] < 0 || dfs(match[G[v][i]])) {
        match[G[v][i]] = v;
        match[v] = G[v][i];
        return true;
      }
    }
  }
  return false;
}

// 二部グラフの最大マッチングを求める
int bipartite_matching() {
  int res = 0;
  memset(match, -1, sizeof(match));
  for (int v = 0; v < V; v++) {
    if (match[v] < 0) {
      memset(used, 0, sizeof(used));
      if (dfs(v)) {
        res++;
      }
    }
  }
  return res;
}

int main(void) {
  // cin >> V >> M;
  // int a,b;
  // for(int i=0;i<M;i++){
  // 	cin >> a >> b;
  // 	add_edge(a,b);
  // }
  int X, Y;
  cin >> X >> Y >> M;
  V = X + Y;
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    add_edge(x, X + y);
  }

  std::cout << bipartite_matching() << std::endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string.h>
#include <vector>
#define MAX_V 100000
#define INF 100000000
using namespace std;

int V, M;             // 頂点，辺の数
vector<int> G[MAX_V]; // グラフの隣接リスト表現
int match[MAX_V];     // マッチングのペア
bool used[MAX_V];     // DFSですでに調べたかのフラグ

// uとvを結ぶ辺をグラフに追加する
void add_edge(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}

// 増加パスをDFSで探す
bool dfs(int v) {
  for (int i = 0; i < G[v].size(); i++) {
    if (used[G[v][i]] == false) {
      used[G[v][i]] = true;
      if (match[G[v][i]] < 0 || dfs(match[G[v][i]])) {
        match[G[v][i]] = v;
        match[v] = G[v][i];
        return true;
      }
    }
  }
  return false;
}

// 二部グラフの最大マッチングを求める
int bipartite_matching() {
  int res = 0;
  memset(match, -1, sizeof(match));
  for (int v = 0; v < V; v++) {
    if (match[v] < 0) {
      memset(used, 0, sizeof(used));
      if (dfs(v)) {
        res++;
      }
    }
  }
  return res;
}

int main(void) {
  // cin >> V >> M;
  // int a,b;
  // for(int i=0;i<M;i++){
  // 	cin >> a >> b;
  // 	add_edge(a,b);
  // }
  int X, Y;
  cin >> X >> Y >> M;
  V = X + Y;
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    add_edge(x, X + y);
  }

  std::cout << bipartite_matching() << std::endl;
}
Problem p02378 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000
#define MAX 256

typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

int X, Y, E;

struct edge {
  int to;
  int cap;
  int rev;
};

vector<edge> G[MAX];
bool used[MAX];

void add_edge(int from, int to, int cap) {
  G[from].push_back((edge){to, cap, (int)G[to].size()});
  G[to].push_back((edge){from, 0, (int)G[to].size() - 1});
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int max_flow(int s, int t) {
  int flow = 0;
  while (1) {
    for (int i = 0; i < MAX; i++)
      used[i] = false;
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}

int main(void) {
  cin >> X >> Y >> E;
  while (E--) {
    int x, y;
    cin >> x >> y;
    add_edge(x, X + y, 1);
  }
  int s = X + Y, t = s + 1;
  for (int i = 0; i < X; i++) {
    add_edge(s, i, 1);
  }
  for (int i = 0; i < Y; i++) {
    add_edge(X + i, t, 1);
  }
  cout << max_flow(s, t) << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
#define INF 100000000
#define MAX 256

typedef long long ll;
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};

int X, Y, E;

struct edge {
  int to;
  int cap;
  int rev;
};

vector<edge> G[MAX];
bool used[MAX];

void add_edge(int from, int to, int cap) {
  G[from].push_back((edge){to, cap, (int)G[to].size()});
  G[to].push_back((edge){from, 0, (int)G[from].size() - 1});
}

int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int max_flow(int s, int t) {
  int flow = 0;
  while (1) {
    for (int i = 0; i < MAX; i++)
      used[i] = false;
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}

int main(void) {
  cin >> X >> Y >> E;
  while (E--) {
    int x, y;
    cin >> x >> y;
    add_edge(x, X + y, 1);
  }
  int s = X + Y, t = s + 1;
  for (int i = 0; i < X; i++) {
    add_edge(s, i, 1);
  }
  for (int i = 0; i < Y; i++) {
    add_edge(X + i, t, 1);
  }
  cout << max_flow(s, t) << endl;
  return 0;
}
Problem p02378 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <vector>
using namespace std;
const int maxv = 100 + 10;
int N, M, E;
vector<int> g[maxv];
int vis[maxv];
int match[maxv];
void add_edge(int from, int to) {
  g[from].push_back(to);
  g[to].push_back(from);
}
bool dfs(int t) {
  vis[t] = 1;
  for (int i = 0; i < g[t].size(); i++) {
    int v = g[t][i], w = match[v];
    if (w < 0 || (!vis[w] && dfs(w))) {
      match[t] = v;
      match[v] = t;
      return true;
    }
  }
  return false;
}
void binrary_match() {
  memset(match, -1, sizeof(match));
  int res = 0;
  for (int i = 0; i < N + M; i++) {
    if (match[i] < 0) {
      memset(vis, 0, sizeof(vis));
      if (dfs(i))
        res++;
    }
  }
  printf("%d\n", res);
}
int main() {
  //    freopen("in.txt","r",stdin);
  scanf("%d%d%d", &N, &M, &E);
  int u, v;
  for (int i = 0; i < E; i++) {
    scanf("%d%d", &u, &v);
    add_edge(u, N + v);
  }
  binrary_match();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <queue>
#include <vector>
using namespace std;
const int maxv = 2 * (100 + 10);
int N, M, E;
vector<int> g[maxv];
int vis[maxv];
int match[maxv];
void add_edge(int from, int to) {
  g[from].push_back(to);
  g[to].push_back(from);
}
bool dfs(int t) {
  vis[t] = 1;
  for (int i = 0; i < g[t].size(); i++) {
    int v = g[t][i], w = match[v];
    if (w < 0 || (!vis[w] && dfs(w))) {
      match[t] = v;
      match[v] = t;
      return true;
    }
  }
  return false;
}
void binrary_match() {
  memset(match, -1, sizeof(match));
  int res = 0;
  for (int i = 0; i < N + M; i++) {
    if (match[i] < 0) {
      memset(vis, 0, sizeof(vis));
      if (dfs(i))
        res++;
    }
  }
  printf("%d\n", res);
}
int main() {
  //    freopen("in.txt","r",stdin);
  scanf("%d%d%d", &N, &M, &E);
  int u, v;
  for (int i = 0; i < E; i++) {
    scanf("%d%d", &u, &v);
    add_edge(u, N + v);
  }
  binrary_match();
  return 0;
}
Problem p02378 (C++)

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

using namespace std;

// ------ Variable ------ //

#define MAX_V 100

int V;
vector<int> G[MAX_V];

int match[MAX_V];

bool used[MAX_V];

// ------ Bipartite_Matching ------ //

bool search(int v) {
  used[v] = true;

  for (int i = 0; i < G[v].size(); i++) {
    int u = G[v][i], w = match[u];

    if (w < 0 || !used[w] && search(w)) {
      match[v] = u;
      match[u] = v;

      return true;
    }
  }

  return false;
}

int bipartite_matching() {
  int res = 0;

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

  for (int i = 0; i < V; i++) {
    if (match[i] < 0) {
      memset(used, 0, sizeof(used));

      if (search(i)) {
        res++;
      }
    }
  }

  return res;
}

int main() {
  int X, Y, E, A, B;

  cin >> X >> Y >> E;
  V = X + Y;

  for (int i = 0; i < E; i++) {
    cin >> A >> B;

    G[A].push_back(X + B);
    G[X + B].push_back(A);
  }

  cout << bipartite_matching() << endl;

  return 0;
}

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

using namespace std;

// ------ Variable ------ //

#define MAX_V 200

int V;
vector<int> G[MAX_V];

int match[MAX_V];

bool used[MAX_V];

// ------ Bipartite_Matching ------ //

bool search(int v) {
  used[v] = true;

  for (int i = 0; i < G[v].size(); i++) {
    int u = G[v][i], w = match[u];

    if (w < 0 || !used[w] && search(w)) {
      match[v] = u;
      match[u] = v;

      return true;
    }
  }

  return false;
}

int bipartite_matching() {
  int res = 0;

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

  for (int i = 0; i < V; i++) {
    if (match[i] < 0) {
      memset(used, 0, sizeof(used));

      if (search(i)) {
        res++;
      }
    }
  }

  return res;
}

int main() {
  int X, Y, E, A, B;

  cin >> X >> Y >> E;
  V = X + Y;

  for (int i = 0; i < E; i++) {
    cin >> A >> B;

    G[A].push_back(X + B);
    G[X + B].push_back(A);
  }

  cout << bipartite_matching() << endl;

  return 0;
}
Problem p02378 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
vector<int> list[100000];
int N, M, E;
int match[100000];
bool used[100000];
int x, y;

bool DFS(int V) {
  used[V] = true;
  for (int i = 0; i < list[V].size(); i++) {
    int u = list[V][i];
    int w = match[u];
    if (w < 0 || !used[u] && DFS(w)) {
      match[V] = u;
      match[u] = V;
      return true;
    }
  }
  return false;
}

int supermatching() {
  int r = 0;
  memset(match, -1, sizeof(match));
  for (int j = 0; j < N + M; j++) {
    if (match[j] < 0) {
      memset(used, 0, sizeof(used));
      if (DFS(j)) {
        r++;
      }
    }
  }
  return r;
}

int main() {
  cin >> N >> M >> E;
  for (int i = 0; i < E; i++) {
    cin >> x >> y;
    y += N;
    list[y].push_back(x);
    list[x].push_back(y);
  }
  cout << supermatching() << endl;
  return 0;
}

Fixed submission:
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
vector<int> list[100000];
int N, M, E;
int match[100000];
bool used[100000];
int x, y;

bool DFS(int V) {
  used[V] = true;
  for (int i = 0; i < list[V].size(); i++) {
    int u = list[V][i];
    int w = match[u];
    if (w < 0 || !used[w] && DFS(w)) {
      match[V] = u;
      match[u] = V;
      return true;
    }
  }
  return false;
}

int supermatching() {
  int r = 0;
  memset(match, -1, sizeof(match));
  for (int j = 0; j < N + M; j++) {
    if (match[j] < 0) {
      memset(used, 0, sizeof(used));
      if (DFS(j)) {
        r++;
      }
    }
  }
  return r;
}

int main() {
  cin >> N >> M >> E;
  for (int i = 0; i < E; i++) {
    cin >> x >> y;
    y += N;
    list[y].push_back(x);
    list[x].push_back(y);
  }
  cout << supermatching() << endl;
  return 0;
}
Problem p02379 (Python)

Buggy submission (status: Runtime Error):
import math

x1, y1, x2, y2 = map(int, input().split())
print(f"{math.sqrt((x2-x1)**2+(y2-y1)**2):.8f}")


Fixed submission:
import math

x1, y1, x2, y2 = map(float, input().split())
print(f"{math.sqrt((x2-x1)**2+(y2-y1)**2):.8f}")
Problem p02379 (Python)

Buggy submission (status: Runtime Error):
x1, y1, x2, y2 = map(int, input().split())
print("%.8f" % (((x2 - x1) ** 2 + (y2 - y1) ** 2)) ** 0.5)


Fixed submission:
x1, y1, x2, y2 = map(float, input().split())
print("%.8f" % (((x2 - x1) ** 2 + (y2 - y1) ** 2)) ** 0.5)
Problem p02379 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>

#ifndef DEBUG
#define fprintf (void)
#endif

double absval(double x);

int main(void) {

  double x1, y1, x2, y2;
  double b = 0;
  int n;
  scanf("%lf", &x1);
  scanf("%lf", &y1);
  scanf("%lf", &x2);
  scanf("%lf", &y2);
  double dx = absval(x1 - x2);
  double dy = absval(y1 - y2);

  double x = dx * dx + dy * dy;
  fprintf(stdout, "%lf\n", x);

  scanf("%d", &n);

  while (b * b < x) {
    b++;
  }
  while (absval(b * b - x) > 0.000001) {
    b = (b * b + x) / (2 * b);
    fprintf(stdout, "%f\n", b);
  }
  printf("%f\n", b);

  return 0;
}

double absval(double x) {
  if (x < 0) {
    return -x;
  } else {
    return x;
  }
}

Fixed submission:
#include <stdio.h>

#ifndef DEBUG
#define fprintf (void)
#endif

double absval(double x);

int main(void) {

  double x1, y1, x2, y2;
  double b = 0;
  int n;
  scanf("%lf", &x1);
  scanf("%lf", &y1);
  scanf("%lf", &x2);
  scanf("%lf", &y2);
  double dx = absval(x1 - x2);
  double dy = absval(y1 - y2);

  double x = dx * dx + dy * dy;
  fprintf(stdout, "%lf\n", x);

  scanf("%d", &n);

  while (b * b < x) {
    b++;
  }
  while (absval(b * b - x) > 0.0001) {
    b = (b * b + x) / (2 * b);
    fprintf(stdout, "%f\n", b);
  }
  printf("%f\n", b);

  return 0;
}

double absval(double x) {
  if (x < 0) {
    return -x;
  } else {
    return x;
  }
}
Problem p02379 (Python)

Buggy submission (status: Runtime Error):
# -*- coding: utf-8 -*-

import math

x1, y1, x2, y2 = map(int, input().split())
dis = math.sqrt((x1 + x2) * (x1 + x2) + (y1 + y2) * (y1 + y2))
print(dis)


Fixed submission:
# -*- coding: utf-8 -*-

import math

x1, y1, x2, y2 = map(float, input().split())
dis = math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2))
print(dis)
Problem p02381 (Python)

Buggy submission (status: Runtime Error):
from statistics import pstdev

while True:
    data_count = int(input())
    if data_count == 0:
        break
    print(pstdev(sum(int(x) for x in input().split())))


Fixed submission:
from statistics import pstdev

while True:
    data_count = int(input())
    if data_count == 0:
        break
    print(pstdev(int(x) for x in input().split()))
Problem p02381 (C++)

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

using namespace std;
typedef long long ll;

int main() {
  int n;
  int score[100];
  double stdev;

  double a;

  while (true) {
    double sum1 = 0, sum2 = 0, ave = 0;

    cin >> n;
    if (n == 0) {
      break;
    } else {
      for (int i = 0; i < n; ++i) {
        cin >> score[i];
        sum1 = sum1 + score[i];
      }
      ave = sum1 / n;
      for (int i = 0; i < n; ++i) {
        a = score[i] - ave;
        a = a * a;
        sum2 = sum2 + a;
      }
      stdev = sum2 / n;
      stdev = sqrt(stdev);
    }
    printf("%.15lf\n", stdev);
  }
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <iostream>

using namespace std;
typedef long long ll;

int main() {
  int n;
  int score[1000];
  double stdev;

  double a;

  while (true) {
    double sum1 = 0, sum2 = 0, ave = 0;

    cin >> n;
    if (n == 0) {
      break;
    } else {
      for (int i = 0; i < n; ++i) {
        cin >> score[i];
        sum1 = sum1 + score[i];
      }
      ave = sum1 / n;
      for (int i = 0; i < n; ++i) {
        a = score[i] - ave;
        a = a * a;
        sum2 = sum2 + a;
      }
      stdev = sum2 / n;
      stdev = sqrt(stdev);
    }
    printf("%.15lf\n", stdev);
  }
}
Problem p02381 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
#include <stdio.h>

using namespace std;
int main() {
  int n, s[99];
  double ave, hensa[99], sum = 0;
  cin >> n;
  while (n != 0) {
    for (int i = 0; n > i; i++) {
      cin >> s[i];
      sum += s[i];
    }
    ave = sum / n;
    sum = 0;
    for (int i = 0; n > i; i++) {
      sum += (s[i] - ave) * (s[i] - ave);
    }
    printf("%.6f\n", sqrt(sum / n));
  }
}


Fixed submission:
#include <cmath>
#include <iostream>
#include <stdio.h>

using namespace std;
int main() {
  int n;
  while (cin >> n, n != 0) {
    double ave, hensa[99], sum = 0, s[999];
    for (int i = 0; n > i; i++) {
      cin >> s[i];
      sum += s[i];
    }
    ave = sum / n;
    sum = 0;
    for (int i = 0; n > i; i++) {
      sum += (s[i] - ave) * (s[i] - ave);
    }
    printf("%.6f\n", sqrt(sum / n));
  }
}
Problem p02381 (C++)

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

int main() {
  int n, s[100];

  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      cin >> s[i];
    }
    int sum = 0;
    for (int i = 0; i < n; i++) {
      sum += s[i];
    }
    double m = 1.0 * sum / n;

    double t = 0;
    for (int i = 0; i < n; i++) {
      t += (s[i] - m) * (s[i] - m);
    }

    double a = sqrt(t / n);
    printf("%.6f\n", a);
  }

  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int n, s[1000];

  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      cin >> s[i];
    }
    int sum = 0;
    for (int i = 0; i < n; i++) {
      sum += s[i];
    }
    double m = 1.0 * sum / n;

    double t = 0;
    for (int i = 0; i < n; i++) {
      t += (s[i] - m) * (s[i] - m);
    }

    double a = sqrt(t / n);
    printf("%.6f\n", a);
  }

  return 0;
}
Problem p02381 (C++)

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

int main() {
  double b, s[100], n;
  long x;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    x = 0;
    for (int i = 0; i < n; i++) {
      cin >> s[i];
      x += s[i];
    }
    double m = x / n;
    b = 0;
    for (int i = 0; i < n; i++) {
      b += (s[i] - m) * (s[i] - m);
    }
    printf("%.8lf\n", sqrt(b / n));
  }
  return 0;
}


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

int main() {
  double b, s[1000], n, x;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    x = 0;
    for (int i = 0; i < n; i++) {
      cin >> s[i];
      x += s[i];
    }
    double m = x / n;
    b = 0;
    for (int i = 0; i < n; i++) {
      b += (s[i] - m) * (s[i] - m);
    }
    printf("%.8lf\n", sqrt(b / n));
  }
  return 0;
}
Problem p02381 (C++)

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

int main() {
  double m, n, a, s[100];
  for (int j = 0; j < 100; j++) {
    cin >> n;
    if (n == 0)
      break;
    m = 0;
    a = 0;
    for (int i = 0; i < n; i++) {
      cin >> s[i];
      m += s[i];
    }
    m /= n;
    for (int i = 0; i < n; i++) {
      a += pow(s[i] - m, 2);
    }
    a /= n;
    cout << sqrt(a) << endl;
  }
  return 0;
}

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

int main() {
  double m, n, a, s[1000];
  for (int j = 0; j < 100; j++) {
    cin >> n;
    if (n == 0)
      break;
    m = 0;
    a = 0;
    for (int i = 0; i < n; i++) {
      cin >> s[i];
      m += s[i];
    }
    m /= n;
    for (int i = 0; i < n; i++) {
      a += pow(s[i] - m, 2);
    }
    a /= n;
    cout << sqrt(a) << endl;
  }
  return 0;
}
Problem p02381 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iomanip>
#include <iostream>
#include <math.h>
using namespace std;

int main(void) {
  int n;
  int *score;
  double ave, S;

  while (true) {
    cin >> n;

    score = new int[n];

    S = ave = 0.0;
    for (int i = 0; i < n; i++) {
      cin >> score[i];
      ave += score[i];
    }
    ave /= n;

    for (int i = 0; i < n; i++) {
      S += (score[i] - ave) * (score[i] - ave);
    }

    cout << fixed << setprecision(10) << sqrt(S / n) << "\n";

    delete[] score;
  }

  return 0;
}

Fixed submission:
#include <iomanip>
#include <iostream>
#include <math.h>
using namespace std;

int main(void) {
  int n;
  int *score;
  double ave, S;

  while (true) {
    cin >> n;

    if (n == 0)
      break;

    score = new int[n];

    S = ave = 0.0;
    for (int i = 0; i < n; i++) {
      cin >> score[i];
      ave += score[i];
    }
    ave /= n;

    for (int i = 0; i < n; i++) {
      S += (score[i] - ave) * (score[i] - ave);
    }

    cout << fixed << setprecision(10) << sqrt(S / n) << "\n";

    delete[] score;
  }

  return 0;
}
Problem p02381 (C++)

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

int main() {
  int n;
  float m, tmp;
  int s[100] = {};
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    m = 0;
    for (int i = 0; i < n; i++) {
      cin >> s[i];
      m += s[i];
    }
    m /= n;
    tmp = 0;
    for (int i = 0; i < n; i++) {
      tmp += (s[i] - m) * (s[i] - m);
    }
    cout << sqrt(tmp / n) << endl;
  }
}


Fixed submission:
#include <cmath>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int n;
  float m, tmp;
  int s[1000] = {};
  while (true) {
    cin >> n;
    if (n == 0)
      break;
    m = 0;
    for (int i = 0; i < n; i++) {
      cin >> s[i];
      m += s[i];
    }
    m /= n;
    tmp = 0;
    for (int i = 0; i < n; i++) {
      tmp += (s[i] - m) * (s[i] - m);
    }
    cout << sqrt(tmp / n) << endl;
  }
}
Problem p02381 (C++)

Buggy submission (status: Runtime Error):
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define INF 2147483647

using namespace std;

int main() {
  double s[105], ave, a;
  int n;

  while (1) {
    cin >> n;
    if (n == 0)
      break;
    ave = 0;
    for (int i = 0; i < n; i++) {
      cin >> s[i];
      ave += s[i];
    }
    ave /= n;
    a = 0;
    for (int i = 0; i < n; i++) {
      a += pow((s[i] - ave), 2);
    }
    a /= n;
    cout << fixed << setprecision(8) << sqrt(a) << endl;
  }

  return (0);
}

Fixed submission:
#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <list>
#include <queue>
#include <string>
#include <utility>
#include <vector>
#define INF 2147483647

using namespace std;

int main() {
  double s[1005], ave, a;
  int n;

  while (1) {
    cin >> n;
    if (n == 0)
      break;
    ave = 0;
    for (int i = 0; i < n; i++) {
      cin >> s[i];
      ave += s[i];
    }
    ave /= n;
    a = 0;
    for (int i = 0; i < n; i++) {
      a += pow((s[i] - ave), 2);
    }
    a /= n;
    cout << fixed << setprecision(8) << sqrt(a) << endl;
  }

  return (0);
}
Problem p02381 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>

using namespace std;

int main() {

  int n, s[100];
  double m, a2;
  while (1) {
    scanf("%d\n", &n);
    if (n == 0)
      return 0;
    m = 0;
    a2 = 0;
    for (int i = 0; i < n; i++) {
      scanf("%d", &s[i]);
      m += s[i];
    }
    m /= n;
    for (int i = 0; i < n; i++) {
      a2 += pow(s[i] - m, 2);
    }
    printf("%.8lf\n", sqrt(a2 / n));
  }
}


Fixed submission:
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>

using namespace std;

int main() {

  int n, s[1000];
  double m, a2;
  while (1) {
    scanf("%d\n", &n);
    if (n == 0)
      return 0;
    m = 0;
    a2 = 0;
    for (int i = 0; i < n; i++) {
      scanf("%d", &s[i]);
      m += s[i];
    }
    m /= n;
    for (int i = 0; i < n; i++) {
      a2 += pow(s[i] - m, 2);
    }
    printf("%.8lf\n", sqrt(a2 / n));
  }
}
Problem p02381 (C++)

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

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    double s[100], a = 0, m = 0;
    for (int i = 0; i < n; i++) {
      cin >> s[i];
      m += s[i];
    }
    m /= n;
    for (int i = 0; i < n; i++)
      a += pow(s[i] - m, 2);
    a /= n;
    a = pow(a, 0.5);
    cout << fixed << setprecision(10) << a << endl;
  }
}


Fixed submission:
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;

int main() {
  while (true) {
    int n;
    cin >> n;
    if (n == 0)
      break;
    double s[1000], a = 0, m = 0;
    for (int i = 0; i < n; i++) {
      cin >> s[i];
      m += s[i];
    }
    m /= n;
    for (int i = 0; i < n; i++)
      a += pow(s[i] - m, 2);
    a /= n;
    a = pow(a, 0.5);
    cout << fixed << setprecision(10) << a << endl;
  }
}
Problem p02381 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;

int main() {
  vector<int> v(100, 0);
  int i, n, sum;
  double a = 0;

  while (cin >> n, n != 0) {
    for (i = 0; i < n; i++) {
      cin >> v[i];
      sum += v[i];
    }

    double m = (double)sum / (double)n;

    for (i = 0; i < n; i++) {
      a += pow((v[i] - m), 2) / n;
    }
    printf("%.8f\n", sqrt(a));
    a = 0;
    sum = 0;
    v.erase(v.begin(), v.end());
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;

int main() {
  vector<int> v(1024, 0);
  int i, n, sum;
  double a = 0;

  while (cin >> n, n != 0) {
    for (i = 0; i < n; i++) {
      cin >> v[i];
      sum += v[i];
    }

    double m = (double)sum / (double)n;

    for (i = 0; i < n; i++) {
      a += pow((v[i] - m), 2) / n;
    }
    printf("%.8f\n", sqrt(a));
    a = 0;
    sum = 0;
    v.erase(v.begin(), v.end());
  }

  return 0;
}
Problem p02381 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
  int n;
  long double ave;      //??????
  long double sum1 = 0; //????¨????
  long double sum2 = 0;
  int s[100] = {0}; //??????
  long double a;    //?????£

  while (1) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> s[i];
      sum1 += s[i];
    }
    ave = sum1 / n;
    for (int i = 0; i < n; i++)
      sum2 += (s[i] - ave) * (s[i] - ave);

    a = sum2 / n;
    cout << fixed << setprecision(6) << sqrt(a) << endl;
    sum1 = 0;
    sum2 = 0;
    ave = 0;
  }
}


Fixed submission:
#include <cmath>
#include <iomanip>
#include <iostream>
using namespace std;
int main() {
  int n;
  long double ave;      //??????
  long double sum1 = 0; //????¨????
  long double sum2 = 0;
  int s[1000] = {0}; //??????
  long double a;     //?????£

  while (1) {
    cin >> n;
    if (n == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> s[i];
      sum1 += s[i];
    }
    ave = sum1 / n;
    for (int i = 0; i < n; i++)
      sum2 += (s[i] - ave) * (s[i] - ave);

    a = sum2 / n;
    cout << fixed << setprecision(6) << sqrt(a) << endl;
    sum1 = 0;
    sum2 = 0;
    ave = 0;
  }
}
Problem p02381 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
const int inf = 0x7fffffff;
typedef long long int ll;
using namespace std;
const int N = 100 + 5;
double A[N];
int main() {
  // #ifdef DEBUG
  //	freopen("in", "r", stdin);
  ////	freopen("out", "w", stdout);
  // #endif
  int n;
  while (scanf("%d", &n) != EOF && n) {
    double sum = 0;
    double ans = 0;
    for (int i = 0; i < n; i++) {
      scanf("%lf", &A[i]);
      sum += A[i];
    }
    sum /= n;
    //	cout<<sum<<endl;
    for (int i = 0; i < n; i++) {
      ans += (A[i] - sum) * (A[i] - sum);
    }
    ans = sqrt(ans / n);
    printf("%f\n", ans);
  }
  // insert code above
  return 0;
}
// aoj1_10_c.cc

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
const int inf = 0x7fffffff;
typedef long long int ll;
using namespace std;
const int N = 1000 + 5;
double A[N];
int main() {
  // #ifdef DEBUG
  //	freopen("in", "r", stdin);
  ////	freopen("out", "w", stdout);
  // #endif
  int n;
  while (scanf("%d", &n) != EOF && n) {
    double sum = 0;
    double ans = 0;
    for (int i = 0; i < n; i++) {
      scanf("%lf", &A[i]);
      sum += A[i];
    }
    sum /= n;
    //	cout<<sum<<endl;
    for (int i = 0; i < n; i++) {
      ans += (A[i] - sum) * (A[i] - sum);
    }
    ans = sqrt(ans / n);
    printf("%f\n", ans);
  }
  // insert code above
  return 0;
}
// aoj1_10_c.cc
Problem p02381 (C++)

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

int main() {
  int n;
  double s[100], a, m;

  while (1) {
    cin >> n;

    if (n == 0)
      break;

    double all = 0, z = 0;

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

    m = all / n;

    for (int i = 0; i < n; i++) {
      z += pow(s[i] - m, 2);
    }

    a = sqrt(z / n);

    printf("%f\n", a);
    // printf("test all=%.10f m=%.10f z=%.10f\n",all,m,z); //test
  }

  return 0;
}

Fixed submission:
#include <cmath>
#include <cstdio>
#include <iostream>
using namespace std;

int main() {
  int n;
  double s[1000], a, m;

  while (1) {
    cin >> n;

    if (n == 0)
      break;

    double all = 0, z = 0;

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

    m = all / n;

    for (int i = 0; i < n; i++) {
      z += pow(s[i] - m, 2);
    }

    a = sqrt(z / n);

    printf("%f\n", a);
    // printf("test all=%.10f m=%.10f z=%.10f\n",all,m,z); //test
  }

  return 0;
}