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

#include <iostream>
#include <string>
#include <vector>
using namespace std;
char bit[] = {'0', '1'};
string str = "";
int inf = 1 << 28;
int pos_dif(string s, int index, char op) {
  if (index == str.size())
    return -1;
  if (s[index] != str[index] && str[index] == op)
    return index;

  return pos_dif(s, index + 1, op);
}
int swap_bit(int index, string s, int cnt) {
  if (s.size() == 1)
    return 0;
  if (index == str.size()) {
    return cnt;
  }

  if (str[index] == s[index]) {
    return swap_bit(index + 1, s, cnt);
  }
  int pos = pos_dif(s, index + 1, s[index]);
  if (pos == -1)
    return inf;
  char tmp = s[index];
  s[index] = s[pos];
  s[pos] = tmp;
  return swap_bit(index + 1, s, cnt + pos - index);
}
int main() {

  int n, m;
  cin >> n >> m;

  for (int i = 0; i < n; i++) {
    string tmp;
    cin >> tmp;
    str += tmp;
  }
  string s1 = "", s2 = "";
  for (int i = 0; i < m; i++) {
    int tmp;
    cin >> tmp;
    for (int j = i; j < i + tmp; j++) {
      s1 += bit[i % 2];
      s2 += bit[(i + 1) % 2];
    }
  }
  cout << min(swap_bit(0, s1, 0), swap_bit(0, s2, 0)) << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <string>
#include <vector>
using namespace std;
char bit[] = {'0', '1'};
string str = "";
int pos_dif(string s, int index, char op) {
  if (s[index] != str[index] && str[index] == op)
    return index;
  return pos_dif(s, index + 1, op);
}
int swap_bit(int index, string s, int cnt) {
  if (s.size() == 1)
    return 0;
  if (index == str.size()) {
    return cnt;
  }

  if (str[index] == s[index]) {
    return swap_bit(index + 1, s, cnt);
  }
  int pos = pos_dif(s, index + 1, s[index]);

  char tmp = s[index];
  s[index] = s[pos];
  s[pos] = tmp;
  return swap_bit(index + 1, s, cnt + pos - index);
}
int main() {

  int n, m;
  cin >> n >> m;

  for (int i = 0; i < n; i++) {
    string tmp;
    cin >> tmp;
    str += tmp;
  }
  string s1 = "", s2 = "";
  for (int i = 0; i < m; i++) {
    int tmp;
    cin >> tmp;
    for (int j = i; j < i + tmp; j++) {
      s1 += bit[i % 2];
      s2 += bit[(i + 1) % 2];
    }
  }
  cout << min(swap_bit(0, s1, 0), swap_bit(0, s2, 0)) << endl;

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

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;

  string s;
  for (int i = 0; i < n; i++) {
    char tmp;
    cin >> tmp;
    s += tmp;
  }

  string ss1, ss2;
  bool flag = false;
  for (int i = 0; i < m; i++) {
    int cnt = 0;
    cin >> cnt;
    ss1 += string(cnt, '0' + (int)flag);
    ss2 += string(cnt, '0' + (int)!flag);
    flag = !flag;
  }
  // cout<<s<<endl;
  // cout<<ss1<<endl;
  // cout<<ss2<<endl;

  queue<pair<string, int>> qu;
  set<string> hash;
  qu.push(make_pair(s, 0));

  while (!qu.empty()) {
    string ss = qu.front().first;
    int cnt = qu.front().second;
    qu.pop();
    if (ss == ss1 || ss == ss2) {
      cout << cnt << endl;
      break;
    }
    for (int i = 0; i < ss.size() - 1; i++) {
      string tmp = ss;
      swap(tmp[i], tmp[i + 1]);
      if (hash.count(tmp) == 0) {
        hash.insert(tmp);
        qu.push(make_pair(tmp, cnt + 1));
      }
    }
  }
  return 0;
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
using namespace std;
int main() {
  int n, m;
  for (; cin >> n >> m, n;) {
    string s;
    for (int i = 0; i < n; i++) {
      char tmp;
      cin >> tmp;
      s += tmp;
    }

    string ss1, ss2;
    bool flag = false;
    for (int i = 0; i < m; i++) {
      int cnt = 0;
      cin >> cnt;
      ss1 += string(cnt, '0' + flag);
      ss2 += string(cnt, '0' + !flag);
      flag = !flag;
    }
    // cout<<s<<endl;
    // cout<<ss1<<endl;
    // cout<<ss2<<endl;

    queue<pair<string, int>> qu;
    set<string> hash;
    qu.push(make_pair(s, 0));

    while (true) {
      string ss = qu.front().first;
      int cnt = qu.front().second;
      qu.pop();
      if (ss == ss1 || ss == ss2) {
        cout << cnt << endl;
        break;
      }
      for (int i = 0; i < ss.size() - 1; i++) {
        string tmp = ss;
        swap(tmp[i], tmp[i + 1]);
        if (hash.count(tmp) == 0) {
          hash.insert(tmp);
          qu.push(make_pair(tmp, cnt + 1));
        }
      }
    }
  }
  return 0;
}
Problem p00924 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
using i64 = long long int;
using ui64 = unsigned long long int;

////////////////////////////////

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (auto &&x : a)
    cin >> x;
  vector<int> b(m);
  for (auto &&x : b)
    cin >> x;
  vector<int> c, d;
  c.reserve(n);
  d.reserve(n);
  for (int i = 0; i < b.size(); i++) {
    for (int j = 0; j < b[i]; j++) {
      if (i & 1) {
        c.push_back(0);
        d.push_back(1);
      } else {
        c.push_back(1);
        d.push_back(0);
      }
    }
  }
  int lvc = 0, smc = 0, lvd = 0, smd = 0;
  for (int i = 0; i < n; i++) {
    smc += abs(lvc);
    if (a[i] != c[i]) {
      if (c[i])
        lvc++;
      else
        lvc--;
    }
    smd += abs(lvd);
    if (a[i] != d[i]) {
      if (d[i])
        lvd++;
      else
        lvd--;
    }
  }

  if (lvc)
    smc = 1145141919;
  if (lvd)
    smd = 1145141919;
  cout << min(smc, smd) << endl;
}


Error: 0
1 0 0 0 1 1 
0 1 1 1 0 0 
0 -1
0 0
0 1
-1 1
0 1
0 0
1 4


Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using i64 = long long int;
using ui64 = unsigned long long int;

////////////////////////////////

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> a(n);
  for (auto &&x : a)
    cin >> x;
  vector<int> b(m);
  for (auto &&x : b)
    cin >> x;
  vector<int> c, d;
  c.reserve(n);
  d.reserve(n);
  for (int i = 0; i < b.size(); i++) {
    for (int j = 0; j < b[i]; j++) {
      if (i & 1) {
        c.push_back(0);
        d.push_back(1);
      } else {
        c.push_back(1);
        d.push_back(0);
      }
    }
  }
  for (auto &&x : c)
    cerr << x << " ";
  cerr << endl;
  for (auto &&x : d)
    cerr << x << " ";
  cerr << endl;
  int lvc = 0, smc = 0, lvd = 0, smd = 0;
  for (int i = 0; i < n; i++) {
    smc += abs(lvc);
    if (a[i] != c[i]) {
      if (c[i])
        lvc++;
      else
        lvc--;
    }
    smd += abs(lvd);
    if (a[i] != d[i]) {
      if (d[i])
        lvd++;
      else
        lvd--;
    }
    cerr << lvc << " " << lvd << endl;
  }

  if (lvc)
    smc = 1145141919;
  if (lvd)
    smd = 1145141919;
  cerr << smc << " " << smd << endl;
  cout << min(smc, smd) << endl;
}
Problem p00924 (C++) — buggy submission, verdict: Runtime Error

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

int x[65536];

int inversions(int p, int n, int q, int r) {
  int x3[15], y[15], z;
  memset(x, 127, sizeof(x));
  x[p] = 0;
  for (int i = 0; i < n * n; i++) {
    for (int j = 0; j < (1 << n); j++) {
      if (x[j] < 1000) {
        for (int k = 0; k < n; k++) {
          x3[k] = (j / (1 << k)) % 2;
        }
        for (int k = 0; k < n - 1; k++) {
          z = 0;
          if (x3[k] + x3[k + 1] == 1) {
            for (int l = 0; l < n; l++) {
              y[l] = x3[l];
            }
            y[k] = x3[k + 1];
            y[k + 1] = x3[k];
            for (int l = 0; l < n; l++) {
              z += y[l] * (1 << l);
            }
            x[z] = min(x[z], x[j] + 1);
          }
        }
      }
    }
  }
  return min(x[q], x[r]);
}

int n, m, a, s;
int x1[15], x2[15], cnt, r1, r2;

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a;
    s += (1 << i) * a;
  }
  for (int i = 0; i < m; i++) {
    cin >> a;
    for (int j = 0; j < a; j++) {
      x1[j + cnt] = i % 2;
      x2[j + cnt] = (i + 1) % 2;
    }
    cnt += a;
  }
  for (int i = 0; i < n; i++) {
    r1 += x1[i] * (1 << i);
    r2 += x2[i] * (1 << i);
  }
  cout << inversions(s, n, r1, r2) << endl;
  return 0;
}

Error: -11

Fixed submission:

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

int x[65536];

int inversions(int p, int n, int q, int r) {
  int x[15], y[15], z;
  memset(x, 127, sizeof(x));
  x[p] = 0;
  for (int i = 0; i < n * n; i++) {
    for (int j = 0; j < (1 << n); j++) {
      if (x[j] < 1000) {
        for (int k = 0; k < n; k++) {
          x[k] = (j / (1 << k)) % 2;
        }
        for (int k = 0; k < n - 1; k++) {
          if (x[k] + x[k + 1] == 0) {
            for (int l = 0; l < n; l++) {
              y[l] = x[l];
            }
            y[k] = x[k + 1];
            y[k + 1] = x[k];
            for (int l = 0; l < n; l++) {
              z += y[l] * (2 << l);
            }
            x[z] = min(x[z], x[j] + 1);
          }
        }
      }
    }
  }
  return min(x[q], x[r]);
}

int n, m, a, s;
int x1[15], x2[15], cnt, r1, r2;

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> a;
    s += (2 << i);
  }
  for (int i = 0; i < m; i++) {
    cin >> a;
    for (int j = 0; j < a; j++) {
      x1[j + cnt] = i % 2;
      x2[j + cnt] = (i + 1) % 2;
    }
    cnt += a;
  }
  for (int i = 0; i < n; i++) {
    r1 += x1[i] * (2 << i);
    r2 += x2[i] * (2 << i);
  }
  cout << inversions(s, n, r1, r2) << endl;
  return 0;
}
Problem p00925 (C++) — buggy submission, verdict: Runtime Error

/* attention to typo and overflow */
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;
const int INF = 1 << 25;
// void io(){ cin.tie(0);ios::sync_with_stdio(false);} //attention to use of
// endl
/*printf("%.9Lf\\n",cf);*/
char output(bool i, bool j) {
  if (i == 1 && j == 0) {
    return 'M';
  }
  if (i == 0 && j == 1) {
    return 'L';
  }
  if (i == 0 && j == 0) {
    return 'I';
  }
  if (i == 1 && j == 1) {
    return 'U';
  }
  return 'N';
}

int main() {
  // io();
  string s;
  getline(cin, s);
  // cin.ignore();
  int ans;
  cin >> ans;

  vector<int> plus;
  for (int i = 0; i < s.size(); i++) {
    if (i % 2 == 0) {
      if (i != 0 && s[i - 1] == '*') {
        plus[plus.size() - 1] *= s[i] - '0';
      } else {
        plus.push_back(s[i] - '0');
      }
    }
  }

  int one = 0;
  for (int i = 0; i < plus.size(); i++) {
    // cerr<<plus[i]<<endl;
    one += plus[i];
  }

  int two = s[0] - '0';
  for (int i = 1; i < s.size(); i++) {
    if (i % 2 == 0) {
      if (s[i - 1] == '+') {
        two += s[i] - '0';
      } else {
        two *= s[i] - '0';
      }
    }
  }

  // cerr<<ans<<' '<<one<<' '<<two<<endl;

  cout << output(ans == one, ans == two) << endl;

  return 0;
}

Error: 0
11 11 13


Fixed submission:

/* attention to typo and overflow */
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

using namespace std;
const int INF = 1 << 25;
void io() {
  cin.tie(0);
  ios::sync_with_stdio(false);
} // attention to use of endl
/*printf("%.9Lf\\n",cf);*/
char output(bool i, bool j) {
  if (i == 1 && j == 0) {
    return 'M';
  }
  if (i == 0 && j == 1) {
    return 'L';
  }
  if (i == 0 && j == 0) {
    return 'I';
  }
  if (i == 1 && j == 1) {
    return 'U';
  }
  return 'N';
}

int main() {
  io();
  string s;
  getline(cin, s);
  // cin.ignore();
  int ans;
  cin >> ans;

  vector<int> plus;
  for (int i = 0; i < s.size(); i++) {
    if (i % 2 == 0) {
      if (i != 0 && s[i - 1] == '*') {
        plus[plus.size() - 1] *= s[i] - '0';
      } else {
        plus.push_back(s[i] - '0');
      }
    }
  }

  int one = 0;
  for (int i = 0; i < plus.size(); i++) {
    // cerr<<plus[i]<<endl;
    one += plus[i];
  }

  int two = s[0] - '0';
  for (int i = 1; i < s.size(); i++) {
    if (i % 2 == 0) {
      if (s[i - 1] == '+') {
        two += s[i] - '0';
      } else {
        two *= s[i] - '0';
      }
    }
  }

  cerr << ans << ' ' << one << ' ' << two << endl;

  cout << output(ans == one, ans == two) << endl;

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

#include <algorithm>
#include <cctype>
#include <iostream>
#include <string>
using namespace std;

typedef string::const_iterator iter;
int factor(iter &p);
int number(iter &p);

int exprA(iter &p) {
  // cerr << "exprA" << endl;
  int res = number(p);
  while (true) {
    if (*p == '+') {
      ++p;
      res += number(p);
    } else if (*p == '*') {
      ++p;
      res *= number(p);
    } else
      break;
  }
  return res;
}

int exprB(iter &p) {
  // cerr << "exprB" << endl;
  int res = factor(p);
  while (true) {
    if (*p == '+') {
      ++p;
      res += factor(p);
    } else
      break;
  }
  return res;
}

int factor(iter &p) {
  // cerr << "factor" << endl;
  int res = number(p);
  while (true) {
    if (*p == '*') {
      ++p;
      res *= number(p);
    } else
      break;
  }
  return res;
}

int number(iter &p) {
  // cerr << "number" << endl;
  int res = 0;
  while (isdigit(*p)) {
    res *= 10;
    res += *p - '0';
    ++p;
  }
  return res;
}

int main() {
  string S;
  cin >> S;
  int T;
  cin >> T;
  iter p;
  p = S.begin();
  bool l2 = exprA(p) == T;
  p = S.begin();
  bool l1 = exprB(p) == T;
  if (l1 && l2)
    cout << "U" << endl;
  if (l1 && !l2)
    cout << "M" << endl;
  if (l2 && !l1)
    cout << "L" << endl;
  if (!l2 && !l1)
    cout << "I" << endl;
}

Error: TLE

Fixed submission:

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

string s;
int p, T;

int mul(), expr(), expr2(), factor();

int mul() {
  cerr << "mul()" << endl;
  int res = s[p++];
  for (;;) {
    cerr << "s[p] : " << s[p] << endl;
    if (s[p] == '*') {
      p++;
      res *= s[p];
      p++;
    } else {
      break;
    }
  }
  cerr << res << endl;
  return res;
}

int expr() {
  cerr << "expr()" << endl;
  int res = mul();
  for (;;) {
    // cerr << s[p] << endl;
    if (p > s.size())
      break;
    if (s[p] == '+') {
      p++;
      res += mul();
      p++;
    }
  }
  return res;
}

int expr2() {
  cerr << "expr2()" << endl;
  int res = s[p++];
  for (;;) {
    if (p > s.size())
      break;
    if (s[p] == '+') {
      p++;
      res += s[p];
      p++;
    } else if (s[p] == '*') {
      p++;
      res *= s[p];
      p++;
    }
  }
  return res;
}

int main() {
  cin >> s >> T;

  bool l1, l2;
  p = 0;
  l1 = expr() == T;
  p = 0;
  l2 = expr2() == T;

  if (l1 && l2)
    cout << "U" << endl;
  if (l1 && !l2)
    cout << "M" << endl;
  if (l2 && !l1)
    cout << "L" << endl;
  if (!l2 && !l1)
    cout << "I" << endl;
}
Problem p00926 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

#pragma warning(disable : 4996)

int n, m, c, d, imos[1111];

int main() {
  scanf("%d", &n);
  scanf("%d", &m);

  for (int i = 0; i < m; i++) {
    scanf("%d", &c);
    scanf("%d", &d);

    imos[c--]++;
    imos[d--]--;
  }

  int ret = n + 1, sum = 0;

  for (int i = 0; i < n; i++) {
    sum += imos[i];

    if (sum > 0)
      ret += 2;
  }

  printf("%d\\n", ret);

  return 0;
}

Error: WA

Fixed submission:

#include <stdio.h>

int n, m, c, d, imos[1001];

int main() {
  scanf("%d", &n);
  scanf("%d", &m);

  for (int i = 0; i < n; i++) {
    scanf("%d", &c);
    scanf("%d", &d);

    imos[c--]++;
    imos[d--]--;
  }

  int ret = n + 2, sum = 0;

  for (int i = 0; i < n; i++) {
    sum += imos[i];

    if (sum > 0)
      ret += 2;
  }

  printf("%d\\n", ret);

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

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int main() {
  int N, m;
  cin >> N >> m;
  vi shop(N + 10);
  REP(i, m) {
    int c, d;
    cin >> c >> d;
    shop[c - 1]++;
    shop[d - 1]--;
  }
  int res = N + 1;
  int cnt = 0;
  int start = -1;
  REP(i, N) {
    if (shop[i] == 0)
      continue;
    cnt += shop[i];
    if (start == -1)
      start = i;
    if (cnt == 0) {
      res += (i - start) * 2;
      start = -1;
    }
  }
  cout << res << endl;
}

Error: 0
1 1
2 1
4 -1
6 -1
7 1
8 -1


Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int main() {
  int N, m;
  cin >> N >> m;
  vi shop(N);
  REP(i, m) {
    int c, d;
    cin >> c >> d;
    shop[c - 1]++;
    shop[d - 1]--;
  }
  int res = N + 1;
  int cnt = 0;
  int start = -1;
  REP(i, N) {
    if (shop[i] == 0)
      continue;
    cnt += shop[i];
    LOG("%d %d\\n", i, shop[i]);
    if (start == -1)
      start = i;
    if (cnt == 0) {
      res += (i - start) * 2;
      start = -1;
    }
  }
  cout << res << endl;
}
Problem p00926 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

#define int long long
#define pb push_back
#define pf push_front
#define mp make_pair
#define fr first
#define sc second
#define Rep(i, n) for (int i = 0; i < (n); i++)
#define All(v) v.begin(), v.end()

typedef pair<int, int> Pii;
typedef pair<int, Pii> Pip;
const int INF = 1107110711071107;

int N, m;
vector<Pii> d;
unordered_map<int, int> dp[1001][1001];

int dfs(int pre, int now, int n) {
  if (n == m) {
    return (now - pre + (N + 1) - pre);
  }
  if (dp[pre][now][n] > 0)
    return dp[pre][now][n];

  int ret = dfs(min(pre, d[n].sc), d[n].fr, n + 1) + (d[n].fr - now);
  ret = min(ret, dfs(d[n].sc, d[n].fr, n + 1) + (now - pre + d[n].fr - pre));

  return dp[pre][now][n] = ret;
}

signed main() {
  cin >> N >> m;

  Rep(i, m) {
    int c, b;
    cin >> c >> b;
    d.pb(Pii(b, c));
  }

  sort(All(d));

  if (m == 0)
    cout << N + 1 << endl;
  else
    cout << dfs(d[0].sc, d[0].fr, 1) + d[0].fr << endl;
}

Error: MLE

Fixed submission:

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

#define int long long
#define pb push_back
#define pf push_front
#define mp make_pair
#define fr first
#define sc second
#define Rep(i, n) for (int i = 0; i < (n); i++)
#define All(v) v.begin(), v.end()

typedef pair<int, int> Pii;
typedef pair<int, Pii> Pip;
const int INF = 1107110711071107;

int N, m;
vector<Pii> d;
int dp[501][501][1001];

int dfs(int pre, int now, int n) {
  if (n == m) {
    return (now - pre + (N + 1) - pre);
  }
  if (dp[pre][now][n] >= 0)
    return dp[pre][now][n];

  int ret = dfs(pre, d[n].fr, n + 1) + (d[n].fr - now);
  ret = min(ret, dfs(d[n].sc, d[n].fr, n + 1) + (now - pre + d[n].fr - pre));

  return dp[pre][now][n] = ret;
}

signed main() {
  cin >> N >> m;
  fill_n(**dp, 501 * 501 * 1001, -1);
  Rep(i, m) {
    int c, b;
    cin >> c >> b;
    d.pb(Pii(b, c));
  }

  sort(All(d));

  if (m == 0)
    cout << N + 1 << endl;
  else
    cout << dfs(d[0].sc, d[0].fr, 1) + d[0].fr << endl;
}
Problem p00926 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int dfs(int n, vector<vector<int>> &adj) {
  if ((int)adj[n].size() == 0)
    return n;
  int ret = 0;
  for (int i = 0; i < (int)adj[n].size(); i++) {
    ret = max(ret, dfs(adj[n][i], adj));
  }
  adj[n].clear();
  return ret;
}

int main() {
  int n, m;
  cin >> n >> m;

  vector<vector<int>> rest(n + 1);
  for (int i = 0; i < m; i++) {
    int c, d;
    cin >> c >> d;
    rest[c].push_back(d);
  }
  vector<int> c, d;
  for (int i = 1; i <= n; i++) {
    int top = dfs(i, rest);
    if (top != i) {
      c.push_back(i);
      d.push_back(top);
    }
  }
  c.push_back(n + 1);
  d.push_back(n + 1);

  int ans = 0;
  int s = c[0], e = d[0];
  for (int i = 1; i < (int)c.size(); i++) {
    if (c[i] <= e) {
      e = max(e, d[i]);
    } else {
      ans += e - s;
      s = c[i];
      e = d[i];
    }
  }
  ans = n + 1 + ans * 2;
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

int dfs(int n, vector<vector<int>> &adj) {
  if ((int)adj[n].size() == 0)
    return n;
  int ret = 0;
  for (int i = 0; i < (int)adj[n].size(); i++) {
    ret = max(ret, dfs(adj[n][i], adj));
  }
  return ret;
}

int main() {
  int n, m;
  cin >> n >> m;

  vector<vector<int>> rest(n + 1);
  for (int i = 0; i < m; i++) {
    int c, d;
    cin >> c >> d;
    rest[c].push_back(d);
  }
  vector<int> c, d;
  for (int i = 1; i <= n; i++) {
    int top = dfs(i, rest);
    if (top != i) {
      c.push_back(i);
      d.push_back(top);
    }
  }
  c.push_back(n + 1);
  d.push_back(n + 1);

  int ans = 0;
  int s = c[0], e = d[0];
  for (int i = 1; i < (int)c.size(); i++) {
    if (c[i] <= e) {
      e = max(e, d[i]);
    } else {
      ans += e - s;
      s = c[i];
      e = d[i];
    }
  }
  ans = n + 1 + ans * 2;
  cout << ans << endl;
  return 0;
}