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

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

vector<int> G[1000000];
vector<pair<int, int>> bridge;
vector<int> articulation;
int ord[1000000], low[1000000];
bool vis[1000000];

void dfs(int v, int p, int &k) {
  vis[v] = true;

  ord[v] = k++;
  low[v] = ord[v];

  bool isArticulation = false;
  int ct = 0;

  for (int i = 0; i < G[v].size(); i++) {
    if (!vis[G[v][i]]) {
      ct++;
      dfs(G[v][i], v, k);
      low[v] = min(low[v], low[G[v][i]]);
      if (~p && ord[v] <= low[G[v][i]])
        isArticulation = true;
      if (ord[v] < low[G[v][i]])
        bridge.push_back(make_pair(min(v, G[v][i]), max(v, G[v][i])));
    } else if (G[v][i] != p) {
      low[v] = min(low[v], ord[G[v][i]]);
    }
  }

  if (p == -1 && ct > 1)
    isArticulation = true;
  if (isArticulation)
    articulation.push_back(v);
}

int main() {
  int n, m;

  scanf("%d %d", &n, &m);

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

  int k = 0;
  for (int i = 0; i < n; i++) {
    if (!vis[i])
      dfs(i, -1, k);
  }

  sort(bridge.begin(), bridge.end());
  sort(articulation.begin(), articulation.end());

  // printf("%d\\n", articulation.size());

  for (int i = 0; i < articulation.size(); i++)
    printf("%d\\n", articulation[i]);

  // printf("%d\\n", bridge.size());

  /*for (int i = 0; i < bridge.size(); i++)
          printf("%d %d\\n", bridge[i].first + 1, bridge[i].second + 1);
  */

  return (0);
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

vector<int> G[1000000];
vector<pair<int, int>> bridge;
vector<int> articulation;
int ord[1000000], low[1000000];
bool vis[1000000];

void dfs(int v, int p, int &k) {
  vis[v] = true;

  ord[v] = k++;
  low[v] = ord[v];

  bool isArticulation = false;
  int ct = 0;

  for (int i = 0; i < G[v].size(); i++) {
    if (!vis[G[v][i]]) {
      ct++;
      dfs(G[v][i], v, k);
      low[v] = min(low[v], low[G[v][i]]);
      if (~p && ord[v] <= low[G[v][i]])
        isArticulation = true;
      if (ord[v] < low[G[v][i]])
        bridge.push_back(make_pair(min(v, G[v][i]), max(v, G[v][i])));
    } else if (G[v][i] != p) {
      low[v] = min(low[v], ord[G[v][i]]);
    }
  }

  if (p == -1 && ct > 1)
    isArticulation = true;
  if (isArticulation)
    articulation.push_back(v);
}

int main() {
  int n, m;

  scanf("%d %d", &n, &m);

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

  int k = 0;
  for (int i = 0; i < n; i++) {
    if (!vis[i])
      dfs(i, -1, k);
  }

  sort(bridge.begin(), bridge.end());
  sort(articulation.begin(), articulation.end());

  // printf("%d\\n", articulation.size());

  for (int i = 0; i < articulation.size(); i++)
    printf("%d\\n", articulation[i] + 1);

  // printf("%d\\n", bridge.size());

  /*for (int i = 0; i < bridge.size(); i++)
          printf("%d %d\\n", bridge[i].first + 1, bridge[i].second + 1);
  */

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int ord[110000], low[110000], n, m, a, b, cnt;
vector<int> x[110000];
bool used[110000];
vector<pair<int, int>> bridge;
void dfs(int v, int k) {
  ord[v] = cnt;
  used[v] = true;
  low[v] = cnt;
  cnt++;
  for (int i = 0; i < x[v].size(); i++) {
    if (used[x[v][i]] == false) {
      dfs(x[v][i], v);
      low[v] = min(low[v], low[x[v][i]]);
      if (ord[v] < low[x[v][i]]) {
        bridge.push_back(make_pair(v, x[v][i]));
      }
    } else if (x[v][i] != k) {
      low[v] = min(low[v], ord[x[v][i]]);
    }
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> a >> b;
    x[a].push_back(b);
    x[b].push_back(a);
  }
  used[1] = true;
  dfs(1, -1);
  for (int i = 0; i < bridge.size(); i++) {
    if (bridge[i].first > bridge[i].second) {
      swap(bridge[i].first, bridge[i].second);
    }
  }
  sort(bridge.begin(), bridge.end());
  for (int i = 0; i < bridge.size(); i++) {
    cout << bridge[i].first << ' ' << bridge[i].second << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
vector<pair<int, int>> X[1 << 17];
int used[1 << 17];
int N, M, A, B, C, sum, cnt2;
queue<int> Q;
int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    cin >> A >> B;
    C = 1; /* A--; B--;*/
    X[A].push_back(make_pair(B, C));
    X[B].push_back(make_pair(A, C));
  }
  for (int i = 0; i < N; i++) {
    sort(X[i].begin(), X[i].end());
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < X[i].size(); j++) {
      int a1 = i, a2 = X[i][j].first;
      for (int k = 0; k < N; k++) {
        used[k] = 0;
      }
      used[0] = 1;
      Q.push(0);
      int cnt = 1;
      while (!Q.empty()) {
        int to = Q.front();
        Q.pop();
        for (int k = 0; k < X[to].size(); k++) {
          int A1 = to, A2 = X[to][k].first;
          if (A1 == a1 && A2 == a2) {
            continue;
          }
          if (A1 == a2 && A2 == a1) {
            continue;
          }
          if (used[X[to][k].first] == 0) {
            used[X[to][k].first] = 1;
            Q.push(X[to][k].first);
            cnt++;
          }
        }
      }
      if (cnt != N && a1 < a2) { /*cnt2 += 1; sum += X[i][j].second;*/
        cout << a1 << ' ' << a2 << endl;
      }
    }
  }
  /*cout<<cnt2<<' '<<sum<<endl;*/
  return 0;
}
Problem p02367 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <stack>
#include <vector>

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

using namespace std;
struct Edge {
  int u;
  int v;
};
bool operator<(const Edge &e1, const Edge &e2) {
  return e1.u < e2.u || (e1.u == e2.u && e1.v < e2.v);
};
typedef vector<vector<int>> Graph;

void DFS(Graph &g, int u, int &clock, vector<int> &low, vector<int> &disc,
         vector<bool> &seen, vector<int> &parent, vector<Edge> &bridges) {
  stack<int> s;
  for (int v : g[u]) {
    if (!seen[v]) {
      clock++;

      disc[v] = clock;
      low[v] = clock;
      seen[v] = true;
      parent[v] = u;

      DFS(g, v, clock, low, disc, seen, parent, bridges);

      low[u] = min(low[u], low[v]);

      if (low[v] > disc[u])
        bridges.push_back(Edge{min(u, v), max(u, v)});
    } else if (v != parent[u]) {
      low[u] = min(low[u], disc[v]);
    }
  }
}

int main() {
  int V, E;
  cin >> V >> E;

  Graph g(V);
  vector<Edge> bridges;

  FOR(i, E) {
    int s, t;
    cin >> s >> t;

    g[s].push_back(t);
    g[t].push_back(s);
  }
  int clock = 2;
  vector<int> low(V, 0);
  vector<int> disc(V, 0);
  vector<int> parent(V, -1);
  vector<bool> seen(V, false);
  disc[0] = 1;
  low[0] = 1;
  seen[0] = true;

  DFS(g, 0, clock, low, disc, seen, parent, bridges);

  sort(bridges.begin(), bridges.end());

  for (Edge e : bridges)
    cout << e.u << " " << e.v << endl;
}


Error: MLE

Fixed submission:

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

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

using namespace std;
struct Edge {
  int u;
  int v;
};
bool operator<(const Edge &e1, const Edge &e2) {
  return e1.u < e2.u || (e1.u == e2.u && e1.v < e2.v);
};
typedef vector<vector<int>> Graph;

void DFS(Graph g, int u, int &clock, vector<int> &low, vector<int> &disc,
         vector<bool> &seen, vector<int> &parent, vector<Edge> &bridges) {
  for (int v : g[u]) {
    if (!seen[v]) {
      clock++;

      disc[v] = clock;
      low[v] = clock;
      seen[v] = true;
      parent[v] = u;

      DFS(g, v, clock, low, disc, seen, parent, bridges);

      low[u] = min(low[u], low[v]);

      if (low[v] > disc[u])
        bridges.push_back(Edge{min(u, v), max(u, v)});
    } else if (v != parent[u]) {
      low[u] = min(low[u], disc[v]);
    }
  }
}

int main() {
  int V, E;
  cin >> V >> E;

  Graph g(V);
  vector<Edge> bridges;

  FOR(i, E) {
    int s, t;
    cin >> s >> t;

    g[s].push_back(t);
    g[t].push_back(s);
  }
  int clock = 2;
  vector<int> low(V, 0);
  vector<int> disc(V, 0);
  vector<int> parent(V, -1);
  vector<bool> seen(V, false);
  disc[0] = 1;
  low[0] = 1;
  seen[0] = true;

  DFS(g, 0, clock, low, disc, seen, parent, bridges);

  sort(bridges.begin(), bridges.end());

  for (Edge e : bridges)
    cout << e.u << " " << e.v << endl;
}
Problem p02367 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = 1ll << 29;
const ll MOD = 1000000007;
const double EPS = 1e-9;
const bool debug = 0;
//---------------------------------//

int V, E;
vector<int> g[112345];
vector<int> depth, low;
set<pii> bridge;

int dfs(int u, int p, int d) {
  depth[u] = d;
  low[u] = d;

  REP(i, g[u].size()) {
    int v = g[u][i];
    if (v == p)
      continue;

    if (depth[v] == -1) {
      chmin(low[u], dfs(v, u, d + 1));
      if (depth[v] == low[v])
        bridge.insert(pii(min(u, v), max(u, v)));
    } else
      chmin(low[u], low[v]);
  }

  return low[u];
}

int main() {
  cin >> V >> E;
  REP(i, E) {
    int s, t;
    scanf("%d %d", &s, &t);
    g[s].push_back(t);
    g[t].push_back(s);
  }

  depth.resize(V, -1);
  low.resize(V, -1);

  dfs(0, -1, 0);

  for (set<pii>::iterator it = bridge.begin(); it != bridge.end(); ++it) {
    printf("%d %d\\n", it->first, it->second);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
template <typename A, typename B> inline bool chmax(A &a, B b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename A, typename B> inline bool chmin(A &a, B b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const ll INF = 1ll << 29;
const ll MOD = 1000000007;
const double EPS = 1e-9;
const bool debug = 0;
//---------------------------------//

int V, E;
vector<int> g[112345];
vector<int> depth, low;
set<pii> bridge;

int dfs(int u, int p, int d) {
  depth[u] = d;
  low[u] = d;

  for (int v : g[u]) {
    if (v == p)
      continue;

    if (depth[v] == -1) {
      chmin(low[u], dfs(v, u, d + 1));
      if (depth[v] == low[v])
        bridge.insert(pii(min(u, v), max(u, v)));
    } else
      chmin(low[u], low[v]);
  }

  return low[u];
}

int main() {
  cin >> V >> E;
  REP(i, E) {
    int s, t;
    scanf("%d %d", &s, &t);
    g[s].push_back(t);
    g[t].push_back(s);
  }

  depth.resize(V, -1);
  low.resize(V, -1);

  dfs(0, -1, 0);

  for (set<pii>::iterator it = bridge.begin(); it != bridge.end(); ++it) {
    printf("%d %d\\n", it->first, it->second);
  }
  return 0;
}
Problem p02367 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <vector>

using namespace std;

struct edge {
  int f, t;
};

const int V_MAX = (int)1e5 + 10;
vector<int> edges[V_MAX];
int ord[V_MAX];
int low[V_MAX];
bool used_v[V_MAX];
vector<edge> ans;

bool comp(edge e1, edge e2) {
  if (e1.f == e2.f) {
    return e1.t < e2.t;
  } else {
    return e1.f < e2.f;
  }
}

void dfs(int u, int prev, int k) {
  low[u] = ord[u] = k;
  k += 1;
  used_v[u] = true;

  for (int i = 0; i < edges[u].size(); i++) {
    int next = edges[u][i];

    if (not used_v[next]) {
      dfs(next, u, k);
      low[u] = min(low[u], low[next]);

      if (low[next] > ord[u]) {
        edge e;
        e.f = min(u, next);
        e.t = max(u, next);
        ans.push_back(e);
      }
    } else if (prev != next) {
      low[u] = min(low[u], ord[next]);
    }
  }
  return;
}

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

  int v, e;
  cin >> v >> e;

  for (int i = 0; i < e; i++) {
    int a, b;
    //        scanf("%d %d", &a, &b);
    cin >> a >> b;
    edges[a].push_back(b);
    edges[b].push_back(a);
  }

  dfs(0, -1, 1);

  sort(ans.begin(), ans.end(), comp);
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i].f << " " << ans[i].t << "\\n";
  }

  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <stdio.h>
#include <vector>

using namespace std;

struct edge {
  int f, t;
};

const int V_MAX = (int)1e5 + 10;
vector<int> edges[V_MAX];
int ord[V_MAX];
int low[V_MAX];
bool used_v[V_MAX];
vector<edge> ans;

bool comp(edge e1, edge e2) {
  if (e1.f == e2.f) {
    return e1.t < e2.t;
  } else {
    return e1.f < e2.f;
  }
}

void dfs(int u, int prev, int k) {
  low[u] = ord[u] = k;
  k += 1;
  used_v[u] = true;

  for (int i = 0; i < edges[u].size(); i++) {
    int next = edges[u][i];

    if (not used_v[next]) {
      dfs(next, u, k);
      low[u] = min(low[u], low[next]);

      if (low[next] > ord[u]) {
        edge e;
        e.f = min(u, next);
        e.t = max(u, next);
        ans.push_back(e);
      }
    } else if (prev != next) {
      low[u] = min(low[u], ord[next]);
    }
  }
  return;
}

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

  int v, e;
  cin >> v >> e;

  for (int i = 0; i < e; i++) {
    int a, b;
    scanf("%d %d", &a, &b);
    edges[a].push_back(b);
    edges[b].push_back(a);
  }

  dfs(0, -1, 1);

  sort(ans.begin(), ans.end(), comp);
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i].f << " " << ans[i].t << "\\n";
  }

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

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

#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

int N, M;
vint G[111111];
bool vis[111111];
int ord[111111], low[111111];
vpint bridge;
void dfs(int v, int p, int &k) {
  ord[v] = k++;
  low[v] = ord[v];
  vis[v] = true;
  for (auto u : G[v]) {
    if (vis[u]) {
      if (u != p)
        chmin(low[v], ord[u]);
    } else {
      dfs(u, v, k);
      if (ord[v] < low[u])
        bridge.pb({min(v, u), max(v, u)});
      chmin(low[v], low[u]);
    }
  }
}

signed main() {
  cin >> N >> M;
  rep(i, M) {
    int a, b;
    cin >> a >> b;
    G[a].pb(b);
    G[b].pb(a);
  }
  int k = 0;
  dfs(0, -1, k);
  sort(all(bridge));
  rep(i, bridge.size()) cout << bridge[i].fi << " " << bridge[i].se << endl;
}

Error: -6
terminate called after throwing an instance of 'std::bad_alloc'
  what():  std::bad_alloc


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
const int SIZE = 100000;
int N, M;
vector<int> G[SIZE];

vector<pair<int, int>> bridge;
int ord[100000], low[100000];
bool vis[100000];

void dfs(int v, int p, int &k) {
  vis[v] = true;
  ord[v] = k++;
  low[v] = ord[v];

  for (auto u : G[v]) {
    if (!vis[u]) {
      dfs(u, v, k);
      low[v] = min(low[v], low[u]);
      if (ord[v] < low[u])
        bridge.push_back(pair<int, int>(min(v, u), max(v, u)));
    } else if (u != p) {
      low[v] = min(low[v], ord[u]);
    }
  }
}

signed main() {
  scanf("%lld%lld", &N, &M);
  for (int i = 0; i < M; i++) {
    int a, b;
    scanf("%lld%lld", &a, &b);
    a--;
    b--;
    G[a].push_back(b);
    G[b].push_back(a);
  }
  int K = 0;
  dfs(0, 0, K);
  sort(bridge.begin(), bridge.end());
  for (int i = 0; i < bridge.size(); i++) {
    printf("%lld %lld\\n", bridge[i].first + 1, bridge[i].second + 1);
  }
  return 0;
}
Problem p02367 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
const int MAX_N = 1e5 + 10;
vector<int> adj[MAX_N];
pair<int, int> ans[MAX_N];
long long int from[MAX_N], to[MAX_N], dp[MAX_N], depth[MAX_N], rt;
bool vis[MAX_N];
void dfs(int v, int de, int p) {
  depth[v] = de;
  vis[v] = 1;
  for (int e : adj[v]) {
    int u = from[e] xor to[e] xor v;
    if (e == p) {
      continue;
    }
    if (vis[u]) {
      dp[v] = min(dp[v], depth[u]);
    } else {
      dfs(u, de + 1, e);
      dp[v] = min(dp[u], dp[v]);
    }
  }
  if (dp[v] >= de && p != -1) {
    ans[rt] = {min(from[p], to[p]), max(from[p], to[p])};
    rt++;
  }
}
int main() {
  int n, m;
  cin >> n >> m;
  for (long long int i = 0; i <= 100000; i++) {
    dp[i] = 999999;
  }
  for (int i = 1; i <= m; i++) {
    cin >> from[i] >> to[i];
    from[i];
    to[i];
    adj[from[i]].push_back(i);
    adj[to[i]].push_back(i);
  }
  dfs(0, 0, -1);
  sort(ans, ans + rt);
  for (int i = 0; i < rt; i++) {
    cout << ans[i].first << " " << ans[i].second << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define pii pair<int, int>
#define S second
#define F first

using namespace std;
const int MAX_N = 1e5 + 10;
const int INF = 1e9 + 10;
int from[MAX_N], to[MAX_N], DP[MAX_N], depth[MAX_N];
vector<int> vec[MAX_N];
vector<pii> ans;
bool mark[MAX_N];
void dfs(int v, int cnt, int l) {
  mark[v] = true;
  depth[v] = cnt;
  for (int en : vec[v]) {
    if (en == l)
      continue;
    int u = to[en] ^ from[en] ^ v;
    if (mark[u])
      DP[v] = min(DP[v], depth[u]);
    else {
      dfs(u, cnt + 1, en);
      DP[v] = min(DP[v], DP[u]);
    }
  }
  if (DP[v] >= depth[v] && l != -1)
    ans.push_back({from[l], to[l]});
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  if (n == 1)
    return 0;
  fill(DP, DP + max(n, m), INF);
  for (int i = 0; i < m; i++) {
    cin >> from[i] >> to[i];
    if (from[i] > to[i])
      swap(from[i], to[i]);
    vec[from[i]].push_back(i);
    vec[to[i]].push_back(i);
  }
  dfs(0, 0, -1);
  sort(ans.begin(), ans.end());
  for (pii u : ans)
    cout << u.F << " " << u.S << '\\n';
}
Problem p02367 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
#define pii pair<int, int>
#define S second
#define F first

using namespace std;
const int MAX_N = 1e5 + 10;
const int INF = 1e9 + 10;
int from[MAX_N], to[MAX_N], DP[MAX_N], depth[MAX_N];
vector<int> vec[MAX_N];
vector<pii> ans;
bool mark[MAX_N];
void dfs(int v, int cnt, int l) {
  mark[v] = true;
  depth[v] = cnt;
  for (int en : vec[v]) {
    if (en == l)
      continue;
    int u = to[en] ^ from[en] ^ v;
    if (mark[u])
      DP[v] = min(DP[v], depth[u]);
    else {
      dfs(u, cnt + 1, en);
      DP[v] = min(DP[v], DP[u]);
    }
  }
  if (DP[v] >= depth[v] && l != -1)
    ans.push_back({from[l], to[l]});
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  cin >> n >> m;
  if (n == 1)
    return 0;
  fill(DP, DP + max(n, m), INF);
  for (int i = 0; i < m; i++) {
    cin >> from[i] >> to[i];
    if (from[i] > to[i])
      swap(from[i], to[i]);
    vec[from[i]].push_back(i);
    vec[to[i]].push_back(i);
  }
  dfs(0, 0, -1);
  sort(ans.begin(), ans.end());
  for (pii u : ans)
    cout << u.F << " " << u.S << '\\n';
}

Error: TLE

Fixed submission:

#include <fstream>
#include <iostream>
#include <vector>
#define pii pair<int, int>
#define S second
#define F first

using namespace std;
const int MAX_N = 1e5 + 10;
const int INF = 1e9 + 10;
int from[MAX_N], to[MAX_N], dp[MAX_N], depth[MAX_N];
vector<int> vec[MAX_N];
bool mark[MAX_N];
ifstream in("in1.txt");
ofstream out("out1.txt");
void dfs(int v, int cnt, int l) {
  mark[v] = true;
  depth[v] = cnt;
  for (int en : vec[v]) {
    if (en == l)
      continue;
    int u = to[en] ^ from[en] ^ v;
    if (mark[u])
      dp[v] = min(dp[v], depth[u]);
    else {
      dfs(u, cnt + 1, en);
      dp[v] = min(dp[v], dp[u]);
    }
  }
  if (dp[v] >= depth[v] && l != -1)
    out << from[l] << " " << to[l] << '\\n';
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m;
  in >> n >> m;
  // fill(dp,dp+max(n,m),INF);
  for (int i = 0; i < m; i++) {
    if (i >= m) {
      dp[i] = INF;
      continue;
    }
    dp[i] = INF;
    in >> from[i] >> to[i];
    vec[from[i]].push_back(i);
    vec[to[i]].push_back(i);
  }
  dfs(0, 0, -1);
}
Problem p02368 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

const int MAX_N = 10000;
const int V = 10000;

vector<int> G[MAX_N];
vector<int> rG[MAX_N];
vector<int> vs;   //??°?????????????????????
bool used[MAX_N]; //?????§????????????????????????
int cmp[MAX_N];   //?±????????????£????????????????????????????????????

void add_edge(int from, int to) {
  G[from].push_back(to);
  rG[to].push_back(from);
}

void dfs(int v) {
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    if (!used[G[v][i]]) {
      dfs(G[v][i]);
    }
  }
  vs.push_back(v);
}

void rdfs(int v, int k) {
  used[v] = true;
  cmp[v] = k;
  for (int i = 0; i < rG[v].size(); i++) {
    if (!used[rG[v][i]]) {
      rdfs(rG[v][i], k);
    }
  }
}

int scc() //?????£??????????????°?????????
{
  memset(used, 0, sizeof(used));
  vs.clear();
  for (int v = 0; v < V; v++) {
    if (!used[v]) {
      dfs(v);
    }
  }
  memset(used, 0, sizeof(used));
  int k = 0;
  for (int i = vs.size() - 1; i >= 0; i--) {
    if (!used[vs[i]]) {
      rdfs(vs[i], k++);
    }
  }
  return k;
}

int main() {
  int n, m, from, to, k, l, u, v;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> from >> to;
    add_edge(from, to);
  }
  k = scc();
  cin >> l;
  for (int i = 0; i < l; i++) {
    cin >> u >> v;
    printf("%d\\n", (cmp[u] == cmp[v]));
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>

using namespace std;

const int MAX_N = 10000;

vector<int> G[MAX_N];
vector<int> rG[MAX_N];
vector<int> vs;   //??°?????????????????????
bool used[MAX_N]; //?????§????????????????????????
int cmp[MAX_N];   //?±????????????£????????????????????????????????????

void add_edge(int from, int to) {
  G[from].push_back(to);
  rG[to].push_back(from);
}

void dfs(int v) {
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    if (!used[G[v][i]]) {
      dfs(G[v][i]);
    }
  }
  vs.push_back(v);
}

void rdfs(int v, int k) {
  used[v] = true;
  cmp[v] = k;
  for (int i = 0; i < rG[v].size(); i++) {
    if (!used[rG[v][i]]) {
      rdfs(rG[v][i], k);
    }
  }
}

int scc(int n, int m) //?????£??????????????°?????????
{
  memset(used, 0, sizeof(used));
  vs.clear();
  for (int v = 0; v < m; v++) {
    if (!used[v]) {
      dfs(v);
    }
  }
  memset(used, 0, sizeof(used));
  int k = 0;
  for (int i = vs.size() - 1; i >= 0; i--) {
    if (!used[vs[i]]) {
      rdfs(vs[i], k++);
    }
  }
  return k;
}

int main() {
  int n, m, from, to, k, l, u, v;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> from >> to;
    add_edge(from, to);
  }
  k = scc(n, m);
  cin >> l;
  for (int i = 0; i < l; i++) {
    cin >> u >> v;
    printf("%d\\n", (cmp[u] == cmp[v]));
  }
}