alexjercan/bugnet huggingface dataset:
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int main() {
  vector<int> R[100000];
  queue<int> Q;
  int n, m, s, t, q;
  int visit[100000];

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> s >> t;
    R[s].push_back(t);
    R[t].push_back(s);
  }

  for (int j = 0; j < n; j++)
    visit[j] = 0;
  for (int i = 0; i < n; i++) {
    if (visit[i] == 0) {
      Q.push(i);
      while (Q.empty() == false) {
        int u = Q.front();
        Q.pop();
        visit[u] = 1;
        for (int j = 0; j < R[u].size(); j++) {
          int v = R[u][j];
          if (visit[v] == 0)
            Q.push(v);
        }
        R[u].clear();
        R[u].push_back(i);
      }
    }
  }

  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s >> t;
    if (R[s][0] == R[t][0])
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int main() {
  vector<int> R[100000];
  queue<int> Q;
  int n, m, s, t, q;
  int visit[100000];

  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> s >> t;
    R[s].push_back(t);
    R[t].push_back(s);
  }
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s >> t;
    for (int j = 0; j < n; j++)
      visit[j] = 0;
    Q.push(s);
    int f = 0;
    while (Q.empty() == false) {
      int u = Q.front();
      Q.pop();
      visit[u] = 1;
      if (u == t) {
        f = 1;
      }
      for (int j = 0; j < R[u].size(); j++) {
        int v = R[u][j];
        if (visit[v] == 0)
          Q.push(v);
      }
    }
    if (f == 1)
      cout << "yes" << endl;
    else
      cout << "no" << endl;
  }

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

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

struct UnionFind {
  vector<int> parent;
  UnionFind(int size) : parent(size, -1) {}

  int root(int x) { return parent[x] < 0 ? x : parent[x] = root(parent[x]); }
  void uniteSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y)
      return;
    if (parent[x] > parent[y])
      swap(x, y);
    // parent[x] += parent[y];
    parent[y] = x;
  }
  bool findSet(int x, int y) { return root(x) == root(y); }
};

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

  int nb_v, nb_lines;
  cin >> nb_v >> nb_lines;
  UnionFind G(nb_v);
  for (int i = 0; i < nb_lines; ++i) {
    int v1, v2;
    cin >> v1 >> v2;
    G.uniteSet(v1, v2);
  }

  cin >> nb_lines;
  for (int i = 0; i < nb_lines; ++i) {
    int v1, v2;
    cin >> v1 >> v2;
    cout << (G.findSet(v1, v2) ? "yes\\n" : "no\\n");
  }
}

Error: TLE

Fixed submission:

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

struct status {
  int id;
  vector<int> children{};
  int visited = 0;

  void resetVisited() { this->visited = 0; }
};

deque<int> schedule;
vector<status> graph;

bool bfs(int target_v) {
  if (schedule.empty()) {
    return false;
  }

  int now_v = schedule.front();
  schedule.pop_front();

  for (auto &next_v : graph[now_v].children) {
    if (graph[next_v].visited)
      continue;

    if (next_v == target_v)
      return true;
    graph[next_v].visited = 1;
    schedule.push_back(next_v);
  }
  return bfs(target_v);
}

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

  // setup
  int nb_v;
  cin >> nb_v;
  int nb_e;
  cin >> nb_e;
  graph.resize(nb_v);
  for (int i = 0; i < nb_e; ++i) {
    int v1, v2;
    cin >> v1 >> v2;
    graph[v1].children.push_back(v2);
    graph[v2].children.push_back(v1);
  }
  for (auto &a : graph) {
    sort(a.children.begin(), a.children.end());
  }

  // bfs
  int times;
  cin >> times;
  for (int i = 0; i < times; ++i) {
    int from_v;
    cin >> from_v;
    int to_v;
    cin >> to_v;
    for (auto &a : graph)
      a.resetVisited();
    schedule = {};
    schedule.push_back(from_v);
    cout << (bfs(to_v) ? "yes\\n" : "no\\n");
  }
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define INF 1000000001
#define mod 1000000007
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> d(n);
  vector<vector<int>> g(n);
  int a, b;
  rep(i, m) {
    // cin>>a[i]>>b[i];
    scanf("%d %d", &a, &b);
    g[a].push_back(b);
    g[b].push_back(a);
  }
  int q;
  cin >> q;
  vector<int> s(q), t(q);
  rep(i, q) {
    // cin>>s[i]>>t[i];
    scanf("%d %d", &s[i], &t[i]);
  }
  rep(i, n) { d[i] = 0; }
  int check = 1;
  int i = 1;  //???
  int i2 = 0; //???????????°
  while (check) {
    stack<int> st;
    st.push(i2);
    d[i2] = i;
    while (!st.empty()) {
      int x = st.top();
      st.pop();
      rep(j, g[x].size()) {
        if (d[g[x][j]] == 0) {
          st.push(g[x][j]);
          d[g[x][j]] = i;
        }
      }
    }
    check = 0;
    rep(j, n) {
      if (d[j] == 0) {
        check = 1;
        i++;
        i2 = j;
        break;
      }
    }
  }

  rep(i, q) {
    if (d[s[i]] == d[t[i]]) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <utility>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define ll long long
#define INF 1000000001
#define mod 1000000007
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(m), b(m), d(n);
  rep(i, m) { cin >> a[i] >> b[i]; }
  int q;
  cin >> q;
  vector<int> s(q), t(q);
  rep(i, q) { cin >> s[i] >> t[i]; }
  rep(i, n) { d[i] = 0; }
  int check = 1;
  int i = 1;  //???
  int i2 = 0; //???????????°
  while (check) {
    stack<int> st;
    st.push(i2);
    d[i2] = i;
    while (!st.empty()) {
      int x = st.top();
      st.pop();
      rep(j, m) {
        if (a[j] == x && d[b[j]] == 0) {
          st.push(b[j]);
          d[b[j]] = i;
        }
        if (b[j] == x && d[a[j]] == 0) {
          st.push(a[j]);
          d[a[j]] = i;
        }
      }
    }
    check = 0;
    rep(j, n) {
      if (d[j] == 0) {
        check = 1;
        i++;
        i2 = j;
        break;
      }
    }
  }

  rep(i, q) {
    if (d[s[i]] == d[t[i]]) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }
}
Problem p02240 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stack>
#include <stdio.h>
#include <vector>
#define N 100000
using namespace std;

vector<int> G[N];
int color[N];
int n;

void dfs(int s, int c) {
  stack<int> st;
  color[s] = c;
  st.push(s);
  while (!st.empty()) {
    int u = st.top();
    st.pop();
    for (int i = 0; i < G[u].size(); i++) {
      int v = G[u][i];
      if (color[v] == -1) {
        color[v] = c;
        st.push(v);
      }
    }
  }
  return;
}

void assignColor() {
  int id = 1;
  for (int i = 0; i < n; i++) {
    color[i] = -1;
  }
  for (int u = 0; u < n; ++u) {
    if (color[u] == -1)
      dfs(u, id++);
  }

  return;
}

int main() {
  // freopen("in3.txt","r",stdin);
  int m;
  scanf("%d %d", &n, &m);

  for (int i = 0; i < m; i++) {
    int s, t;
    scanf("%d %d", &s, &t);
    G[s].push_back(t);
    G[t].push_back(s);
  }

  assignColor();

  int q, s, t;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &s, &t);
    if (color[s] == color[t]) {
      printf("yes\\n");
    } else {
      printf("no\\n");
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stack>
#include <stdio.h>
using namespace std;
int G[10000][10000];
int vis[100000];
int n;

bool dfs(int s, int t) {
  if (G[s][t] == 1 || G[t][s] == 1)
    return true;
  vis[s] = 1;
  for (int i = 0; i < n; i++) {
    if (G[s][i] == 1 && vis[i] == 0) {
      bool a = (dfs(i, t));
      if (a) {
        vis[s] = 0;
        return a;
      }
    }
  }
  vis[s] = 0;
  return false;
}

int main() {
  // freopen("in3.txt","r",stdin);
  int m;
  scanf("%d %d", &n, &m);
  for (int i = 0; i < n; i++) {
    vis[i] = 0;
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      G[i][j] = 0;
    }
  }
  for (int i = 0; i < m; i++) {
    int s, t;
    scanf("%d %d", &s, &t);
    G[s][t] = 1;
    G[t][s] = 1;
  }
  int q, s, t;
  scanf("%d", &q);
  for (int i = 0; i < q; i++) {
    scanf("%d %d", &s, &t);
    if (dfs(s, t) || dfs(t, s))
      printf("yes\\n");
    else
      printf("no\\n");
  }
  return 0;
}
Problem p02241 (C++) — buggy submission, verdict: Time Limit Exceeded

// Program Name: Minimum Spanning Tree
// Written by: by_sknight
// Date: 2019/4/27
#include <bits/stdc++.h>
using namespace std;
#define MAX 105
#define INFTY INT_MAX
#define WHITE 0
#define GRAY 1
#define BLACK 2

int n, G[MAX][MAX];

int prim() {
  int d[MAX], p[MAX], color[MAX];
  int u, minv;
  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
    p[i] = -1;
    color[i] = WHITE;
  }

  d[0] = 0;

  while (1) {
    u = -1;
    minv = INFTY;
    for (int i = 0; i < n; i++) {
      if (color[i] != BLACK && d[i] < minv) {
        minv = d[i];
        u = i;
      }
    }
    if (u == -1)
      break;
    color[u] = BLACK;
    for (int v = 0; v < n; v++) {
      if (color[v] != BLACK && G[u][v] < d[v]) {
        d[v] = G[u][v];
        p[v] = u;
        color[v] = GRAY;
      }
    }
  }
  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] != -1) {
      sum += d[i];
    }
  }
  return sum;
}

int main(void) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int k;
      cin >> k;
      G[i][j] = (k == -1 ? INFTY : k);
    }
  }

  int sum = prim();

  cout << sum << endl;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;
#define MAX 105

int n, choice[MAX];

struct MyPower {
  int v, p1, p2;
  void set(int value, int point1, int point2) {
    v = value;
    p1 = point1;
    p2 = point2;
  }
};

bool operator<(MyPower a, MyPower b) { return (a.v > b.v); }

int main(void) {
  int i, j, x;
  priority_queue<MyPower> PQ;
  MyPower MP;
  for (i = 0; i < MAX; i++) {
    choice[i] = 0;
  }
  cin >> n;
  for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++) {
      cin >> x;
      if (x == -1)
        continue;
      MP.set(x, i, j);
      PQ.push(MP);
    }
  }
  /*
  int siz = PQ.size();
  for (i = 0; i<siz; i++) {
          MP = PQ.top();
          PQ.pop();
          cout << MP.v<< " "<<MP.p1<<" "<<MP.p2<<endl;
  }
  */

  int c = 1, total = 0;
  while (c < n) {
    MP = PQ.top();
    PQ.pop();
    if (choice[MP.p1] == 0 || choice[MP.p2] == 0) {
      total += MP.v;
      choice[MP.p1] = 1;
      choice[MP.p2] = 1;
      c++;
    }
  }
  cout << total << endl;
}
Problem p02241 (C++) — buggy submission, verdict: Time Limit Exceeded

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

const int NOTV = 0;
const int VIST = 1;
const int NMAX = 100;
const int VMAX = 2000;
const int NILL = -1;
const int INFTY = 1 << 21;
int n;
int a[NMAX][NMAX]; //??£??\\??????
int v[NMAX];       //????°???¨?????¨?????????
int d[NMAX];       //????°???¨?????¨??????????????????
int vis[NMAX];
// priority_queue <greater<int>> pq;

int prim() {
  for (int i = 0; i < n; i++) {
    vis[i] = NOTV;
    d[i] = INFTY;
    v[i] = -1;
  }
  d[0] = 0;
  int u = 0;
  while (1) {
    int mincost = INFTY;
    u = -1;
    for (int i = 0; i < n; i++) {
      if (vis[i] == NOTV and mincost > d[i]) {
        mincost = d[i];
        u = i;
      }
    }
    if (u == -1)
      break;
    vis[u] = VIST;
    for (int ver = 0; ver < n; ver++) {
      if (vis[ver] == NOTV and a[u][ver] != INFTY) {
        if (a[u][ver] < d[ver]) {
          d[ver] = a[u][ver];
          v[ver] = u;
          vis[ver] = NOTV;
        }
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (v[i] != -1)
      ans += a[i][v[i]];
  }
  return ans;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int e;
      cin >> e;
      a[i][j] = e;
      if (a[i][j] == -1) {
        a[i][j] = INFTY;
      }
    }
  }

  cout << prim() << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

const int NOTV = 0;
const int VIST = 0;
const int NMAX = 100;
const int VMAX = 2000;
const int NILL = -1;
const int INFTY = 1 << 21;
int n;
int a[NMAX][NMAX]; //??£??\\??????
int v[NMAX];       //????°???¨?????¨?????????
int d[NMAX];       //????°???¨?????¨??????????????????
int vis[NMAX];
// priority_queue <greater<int>> pq;

int prim() {
  for (int i = 0; i < n; i++) {
    vis[i] = NOTV;
    d[i] = INFTY;
    v[i] = -1;
  }
  d[0] = 0;
  int u = 0;
  while (1) {
    int mincost = INFTY;
    u = -1;
    for (int i = 0; i < n; i++) {
      if (vis[i] == NOTV and mincost > d[i]) {
        mincost = d[i];
        u = i;
      }
    }
    if (u == -1)
      break;
    vis[u] = VIST;
    for (int ver = 0; ver < n; ver++) {
      if (vis[ver] == NOTV and a[u][ver] != INFTY) {
        if (a[u][ver] < d[ver]) {
          d[ver] = a[u][ver];
          v[ver] = u;
          vis[ver] = NOTV;
        }
      }
    }
    cout << u << endl;
  }
  int ans = 0;
  for (int i = 0; i < n; i++) {
    if (v[i] != -1)
      ans += a[i][v[i]];
  }
  return ans;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int e;
      cin >> e;
      a[i][j] = e;
      if (a[i][j] == -1) {
        a[i][j] = INFTY;
      }
    }
  }

  cout << prim() << endl;
  return 0;
}
Problem p02241 (C++) — buggy submission, verdict: Runtime Error

//
// Created by tattsun on 2017/02/16.
//

#include <iostream>

#define MAX_N 101
#define INFTY 20000
#define NIL -1

enum Color {
  White,
  Glay,
  Black,
};

using namespace std;

int n;
int w[MAX_N][MAX_N];
int d[MAX_N], p[MAX_N];
Color color[MAX_N];

int min(int x, int y) {
  if (x < y)
    return x;
  return y;
}

int solve() {
  d[0] = 0;

  while (true) {
    int u = NIL, c = INFTY;

    for (int i = 0; i < n; i++) {
      if (d[i] < c && color[i] != Black) {
        u = i;
        c = d[i];
      }
    }

    if (u == NIL)
      break;

    color[u] = Black;

    for (int i = 0; i < n; i++) {
      if (color[i] != Black && w[u][i] != INFTY) {
        if (d[i] > w[u][i]) {
          d[i] = w[u][i];
          p[i] = u;
          color[i] = Glay;
        }
      }
    }
  }

  int tc = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] != NIL)
      tc += w[i][p[i]];
  }
  return tc;
}

int main() {
  for (int i = 0; i < MAX_N; i++) {
    d[i] = INFTY;
    p[i] = NIL;
    color[i] = White;
    for (int j = 0; j < MAX_N; j++)
      w[i][j] = INFTY;
  }

  int p;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> p;
      w[i][j] = (p == -1) ? INFTY : p;
    }
  }

  cout << solve() << endl;
}

Error: -11

Fixed submission:

//
// Created by tattsun on 2017/02/16.
//

#include <iostream>

#define MAX_N 101
#define INFTY 20000
#define NIL -1

enum Color {
  White,
  Glay,
  Black,
};

using namespace std;

int n;
int w[MAX_N][MAX_N];
int d[MAX_N], p[MAX_N];
Color color[MAX_N];

int min(int x, int y) {
  if (x < y)
    return x;
  return y;
}

int solve() {
  d[0] = 0;

  while (true) {
    int u = NIL, c = INFTY;

    for (int i = 0; i < n; i++) {
      if (d[i] < c && color[i] != Black) {
        u = i;
        c = d[i];
      }
    }

    if (u == NIL)
      break;

    color[u] = Black;

    for (int i = 0; i < n; i++) {
      if (color[i] != Black && w[u][i] != INFTY) {
        if (d[i] > w[u][i]) {
          d[i] = w[u][i];
          p[i] = u;
          color[i] = Glay;
        }
      }
    }
  }

  int tc = 0;
  for (int i = 0; i < n; i++) {
    if (color[i] == Black) {
      tc += w[i][p[i]];
    }
  }
  return tc;
}

int main() {
  for (int i = 0; i < MAX_N; i++) {
    d[i] = INFTY;
    p[i] = INFTY;
    color[i] = White;
    for (int j = 0; j < MAX_N; j++)
      w[i][j] = INFTY;
  }

  int p;
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> p;
      w[i][j] = (p == -1) ? INFTY : p;
    }
  }

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

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

int n; // number of vertices
int P[20200], A[20200], B[20200], C[20200];
pair<int, int> D[20200]; // Weights and coordinates

int root(int a) { // find the root of a
  if (P[a] == a)
    return a;                 // a is the root
  return (P[a] = root(P[a])); // find root of a, make it a's parent
}

bool is_same_set(int a, int b) { // a and b are in the same group?
  return root(a) == root(b);
}

void unite(int a, int b) { // put a and b in same group
  P[root(a)] = root(b);
}

int main() {
  cin >> n; // the number of vertices
  for (int i = 0; i < n; ++i) {
    P[i] = i; // Initialize vertices to disjoint sets
    for (int j = 0; j < n; ++j) {
      A[(n * i) + j] = i;
      B[(n * i) + j] = j;
      cin >> C[(n * i) + j];
    }
  }
  for (int i = 0; i < n * n; i++) {
    D[i].first = C[i];
    D[i].second = i;
  }
  sort(D, D + n * n);
  int sum = 0;

  for (int i = 0; i < n * n; ++i) {
    if (D[i].first >= 0) { // Assuming the weight is not -1
      if (!is_same_set(A[D[i].second], B[D[i].second])) {
        unite(A[D[i].second], B[D[i].second]);
        sum += D[i].first;
      }
    }
  }
  cout << sum << endl;
}

Error: 0

Fixed submission:

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

int n; // number of vertices
int P[110], A[2020], B[2020], C[2020];
pair<int, int> D[2020]; // Weights and coordinates

void init(int N) { // Initially all the points are individualized
  for (int i = 0; i <= N; ++i)
    P[i] = i;
}

int root(int a) { // find the root of a
  if (P[a] == a)
    return a;                 // a is the root
  return (P[a] = root(P[a])); // find root of a, make it a's parent
}

bool is_same_set(int a, int b) { // a and b are in the same group?
  return root(a) == root(b);
}

void unite(int a, int b) { // put a and b in same group
  P[root(a)] = root(b);
}

int main() {
  cin >> n; // the number of vertices
  for (int i = 0; i < n; ++i) {
    P[i] = i; // Initialize vertices to disjoint sets
    for (int j = 0; j < n; ++j) {
      A[(n * i) + j] = i;
      B[(n * i) + j] = j;
      cin >> C[(n * i) + j];
    }
  }
  for (int i = 0; i < n * n; i++) {
    D[i].first = C[i];
    D[i].second = i;
  }
  sort(D, D + n * n);
  int sum = 0;

  for (int i = 0; i < n * n; ++i) {
    if (D[i].first >= 0) { // Assuming the weight is not -1
      if (!is_same_set(A[D[i].second], B[D[i].second])) {
        unite(A[D[i].second], B[D[i].second]);
        sum += D[i].first;
      }
    }
  }
  cout << sum << endl;
}
Problem p02241 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

int prim() {
  int u, minv;
  int d[MAX], p[MAX], color[MAX];
  // Tに属する頂点とV-Tに属する頂点をつなぐ辺の中で、重みが最小の辺の重みを記録
  // MSTにおける頂点vの親を記録
  // 訪問状態を記録

  for (int i = 0; i < n;
       i++) { // 全ての頂点に関して、colorをwhite、dをinftyに初期化
    d[i] = INFTY;
    p[i] = -1;
    color[i] = WHITE;
  }

  d[0] = 0;

  while (1) {
    minv = INFTY;
    u = -1;
    for (int i = 0; i < n; i++) { // 最小の辺を探索
      if (minv > d[i] && color[i] != BLACK) {
        u = i;
        minv = d[i];
      }
    }
    if (u == -1)
      break;
    color[u] = BLACK;
    for (int v = 0; v < n; v++) {
      if (color[v] != BLACK &&
          M[u][v] != INFTY) { // 未訪問かつuからvに辺があるとき
        if (d[v] > M[u][v]) {
          d[v] = M[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }

  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] != -1)
      sum += M[i][p[i]];
  }

  return sum;
}

int main(int argc, char const *argv[]) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int e;
      cin >> e;
      M[i][j] = (e == -1) ? INFTY : e;
    }
  }

  cout << prim() << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

int prim() {
  int u, minv;
  int d[MAX], p[MAX], color[MAX];
  // Tに属する頂点とV-Tに属する頂点をつなぐ辺の中で、重みが最小の辺の重みを記録
  // MSTにおける頂点vの親を記録
  // 訪問状態を記録

  for (int i = 0; i < n;
       i++) { // 全ての頂点に関して、colorをwhite、dをinftyに初期化
    d[i] = INFTY;
    p[i] = -1;
    color[i] = WHITE;
  }

  d[0] = 0;

  while (1) {
    minv = INFTY;
    u = -1;
    for (int i = 0; i < n; i++) { // 最小の辺を探索
      if (minv > d[i] && color[i] != BLACK) {
        u = i;
        minv = d[i];
      }
    }
    if (u == -1)
      break;
    for (int v = 0; v < n; v++) {
      if (color[v] != BLACK &&
          M[u][v] != INFTY) { // 未訪問かつuからvに辺があるとき
        if (d[v] > M[u][v]) {
          d[v] = M[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }

  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] != -1)
      sum += M[i][p[i]];
  }

  return sum;
}

int main(int argc, char const *argv[]) {
  cin >> n;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int e;
      cin >> e;
      M[i][j] = (e == -1) ? INFTY : e;
    }
  }

  cout << prim() << endl;

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

#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#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;

int main() {
  int n;
  cin >> n;
  vector<vector<int>> edges(n, vector<int>(n));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int a;
      cin >> a;
      edges[i][j] = a;
    }
  }

  vector<bool> done(n, false);
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  q.push(make_pair(0, 0));
  int ans = 0;
  while (!q.empty()) {
    int d = q.top().first;
    int v = q.top().second;
    q.pop();
    if (done[v]) {
      continue;
    }
    done[v] = true;
    ans += d;
    for (int i = 0; i < n; i++) {
      if (edges[v][i] < 0) {
        continue;
      }
      q.push(make_pair(edges[v][i], i));
    }
  }
  cout << ans << endl;
}

Error: WA

Fixed submission:

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

void print(const vector<int> &v) {
  cout << v[0];
  for (int i = 1, size = v.size(); i < size; i++) {
    cout << " " << v[i];
  }
  cout << endl;
}

int main() {
  int n;
  while (cin >> n) {
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    print(a);
    for (int j = 1; j < n; j++) {
      int key = a[j];
      int i = j - 1;
      while (0 <= i && key < a[i]) {
        a[i + 1] = a[i];
        i--;
      }
      a[i + 1] = key;
      print(a);
    }
  }
}