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

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

const int MAXH = 50;
const int MAXW = 50;
const int di[] = {0, 1, 0, -1};
const int dj[] = {1, 0, -1, 0};

int W, H, li, lj, ri, rj;
string L[MAXH], R[MAXH];
bool vis[MAXH][MAXW][MAXH][MAXW];

struct State {
  int a, b, c, d;
};

bool bfs() {
  memset(vis, 0, sizeof(vis));
  queue<State> que;
  vis[li][lj][ri][rj] = true;
  que.push((State){li, lj, ri, rj});
  while (que.size()) {
    const State s = que.front();
    que.pop();
    for (int k = 0; k < 4; ++k) {
      int na = s.a + di[k];
      int nb = s.b + dj[k];
      int nc = s.c + di[k];
      int nd = s.d - dj[k];
      if (na < 0 || na >= H)
        na = s.a;
      if (nb < 0 || nb >= W)
        nb = s.b;
      if (nc < 0 || nc >= H)
        nc = s.c;
      if (nd < 0 || nd >= W)
        nd = s.d;
      if (L[na][nb] == '#')
        na = s.a, nb = s.b;
      if (R[nc][nd] == '#')
        nc = s.c, nd = s.d;
      if (L[na][nb] == '%' && R[nc][nd] == '%')
        return true;
      if (L[na][nb] == '%' || R[nc][nd] == '%')
        continue;
      if (vis[na][nb][nc][nd])
        continue;
      vis[na][nb][nc][nd] = true;
      que.push((State){na, nb, nc, nd});
    }
  }
  return false;
}

int main() {
  while (cin >> W >> H && (W | H)) {
    for (int i = 0; i < H; ++i) {
      cin >> L[i] >> R[i];
      int l = L[i].find('L');
      int r = R[i].find('R');
      if (l != string::npos) {
        li = i;
        lj = l;
      }
      if (r != string::npos) {
        ri = i;
        rj = r;
      }
    }
    cout << (bfs() ? "Yes" : "No") << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

const int MAXH = 50;
const int MAXW = 50;
const int di[] = {0, 1, 0, -1};
const int dj[] = {1, 0, -1, 0};

int W, H, li, lj, ri, rj;
string L[MAXH], R[MAXH];
bool vis[MAXH][MAXW][MAXH][MAXW];

int dfs(int a, int b, int c, int d) {
  vis[a][b][c][d] = true;
  if (L[a][b] == '%' && R[c][d] == '%')
    return true;
  if (L[a][b] == '%' || R[c][d] == '%')
    return false;
  for (int k = 0; k < 4; ++k) {
    int na = a + di[k];
    int nb = b + dj[k];
    int nc = c + di[k];
    int nd = d - dj[k];
    if (na < 0 || na >= H)
      na = a;
    if (nb < 0 || nb >= W)
      nb = b;
    if (nc < 0 || nc >= H)
      nc = c;
    if (nd < 0 || nd >= W)
      nd = d;
    if (L[na][nb] == '#')
      na = a, nb = b;
    if (R[nc][nd] == '#')
      nc = c, nd = d;
    if (vis[na][nb][nc][nd])
      continue;
    if (dfs(na, nb, nc, nd))
      return true;
  }
  return false;
}

int main() {
  while (cin >> W >> H && (W | H)) {
    for (int i = 0; i < H; ++i) {
      cin >> L[i] >> R[i];
      int l = L[i].find('L');
      int r = L[i].find('R');
      if (l != string::npos) {
        li = i;
        lj = l;
      }
      if (r != string::npos) {
        ri = i;
        rj = r;
      }
    }
    memset(vis, 0, sizeof(0));
    cout << (dfs(li, lj, ri, rj) ? "Yes" : "No") << endl;
  }
  return 0;
}
Problem p01273 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    long long a, b, c, k[20001] = {};
    vector<pair<long long, pair<int, int>>> s;
    k[1] = 1;
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> c;
      s.push_back(make_pair(a, make_pair(b, c)));
    }
    sort(s.begin(), s.end());
    for (int i = 0; i < m; i++) {
      if (k[s[i].second.first] == 1)
        k[s[i].second.second] = 1;
    }
    int ans = 0;
    for (int i = 1; i <= n; i++)
      ans += k[i];
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int n, m;
  while (cin >> n >> m, n + m) {
    long long a, b, c, k[20001] = {}, s[22222][2];
    k[1] = 1;
    for (int i = 0; i < m; i++) {
      cin >> a >> b >> c;
      s[a][0] = b;
      s[a][1] = c;
    }

    for (int i = 1; i <= m; i++) {
      if (k[s[i][0]] == 1)
        k[s[i][1]] = 1;
    }
    int ans = 0;
    for (int i = 1; i <= n; i++)
      ans += k[i];
    cout << ans << endl;
  }
  return 0;
}
Problem p01273 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

#define dump(x) (cout << #x << " = " << x << endl)
#define dump2(x, y)                                                            \\
  (cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl)
#define dump3(x, y, z)                                                         \\
  (cout << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y  \\
        << ", " << z << ")" << endl)

typedef long long ll;
typedef pair<int, int> pii;

int N, M;

#define MAX 20010
int dp[MAX];

class node {
public:
  int t, s, d;
  node(int t, int s, int d) : t(t), s(s), d(d) {}

  bool operator<(const node &r) const { return t < r.t; }
};

int main(void) {
  while (cin >> N >> M, N | M) {
    vector<node> ns;
    rep(i, M) {
      int t, s, d;
      cin >> t >> s >> d;
      ns.push_back(node(t, --s, --d));
    }
    sort(ALL(ns));

    memset(dp, 0, sizeof(dp));
    dp[0] = 1;
    rep(i, ns.size()) dp[ns[i].d] |= dp[ns[i].s];

    int ans = 0;
    rep(i, N) ans += dp[i];
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

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 <string>
#include <vector>
using namespace std;

#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()

#define dump(x) (cout << #x << " = " << x << endl)
#define dump2(x, y)                                                            \\
  (cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl)
#define dump3(x, y, z)                                                         \\
  (cout << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y  \\
        << ", " << z << ")" << endl)

typedef long long ll;
typedef pair<int, int> pii;

int N, M;

#define MAX 20001
vector<int> connect[MAX];
bool visit[MAX];

vector<int> path;

void topological_sort(int i) {
  if (!visit[i]) {
    visit[i] = true;
    rep(j, N) if (find(ALL(connect[i]), j) != connect[i].end())
        topological_sort(j);
    path.push_back(i);
  }
}

int main(void) {
  while (cin >> N >> M, N | M) {
    fill(connect, connect + N, vector<int>());
    fill(visit, visit + N, false);

    rep(i, M) {
      int t, s, d;
      cin >> t >> s >> d;

      s--, d--;
      connect[s].push_back(d);
    }

    path = vector<int>();
    rep(i, N) topological_sort(i);

    cout << path[0] + 1 << endl;
  }
  return 0;
}
Problem p01273 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, m;
  while (cin >> n >> m, n) {
    vector<tuple<int, int, int>> tsd(m);
    vector<unsigned int> infected(n, 0);
    infected[0] = 1;
    for (int i = 0; i < m; i++) {
      int t, s, d;
      cin >> t >> s >> d;
      s--;
      d--;
      tsd[i] = make_tuple(t, s, d);
    }
    sort(tsd.begin(), tsd.end());
    for (int i = 0; i < m; i++) {

      infected[get<2>(tsd[i])] |= infected[get<1>(tsd[i])];
    }
    int res = accumulate(infected.begin(), infected.end(), 0);

    cout << res << endl;
  }
}

Error: WA

Fixed submission:

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

int main() {
  int n, w;
  while (cin >> n >> w, n) {
    vector<int> v(n);
    for (int i = 0; i < n; i++)
      cin >> v[i];

    double res = 0.01;
    vector<int> height(11, 0);
    for (int i = 0; i < n; i++) {
      int h = 0;
      int val = v[i];
      for (; w <= val; h++)
        val -= w;
      height[h]++;
    }
    int rigmost = 0;
    for (int i = 0; i < 11; i++)
      rigmost = max(rigmost, i * (height[i] != 0));
    int highest = *max_element(height.begin(), height.end());

    for (int i = 0; i <= rigmost; i++) {
      res += double(height[i]) / highest * double(rigmost - i) / (rigmost);
    }
    cout << fixed << setprecision(10);
    cout << res << endl;
  }
}
Problem p01273 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define N 20001
using namespace std;
struct set {
  int t;
  int s;
  int d;
};
int main() {
  int n, m, i, j, cnt, source, destination, start, end[N], flag;
  struct set data[N];
  while (1) {
    for (i = 1; i < N; i++)
      end[i] = 0;
    end[1] = 1;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (i = 0; i < m; i++) {
      cin >> data[i].t >> data[i].s >> data[i].d;
    }
    for (i = 0; i < m; i++) {
      for (j = 1; j < m; j++) {
        if (data[j].t < data[j - 1].t)
          swap(data[j], data[j - 1]);
      }
    }
    for (i = 0; i < m; i++) {
      if (end[data[i].s] == 1)
        end[data[i].d] = 1;
    }
    cnt = 0;
    for (i = 1; i <= n; i++)
      if (end[i] == 1)
        cnt++;
    cout << cnt << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
struct set {
  int t;
  int s;
  int d;
};
int main() {
  int n, m, i, j, cnt, source, destination, start, end[20001], flag;
  struct set data[20001];
  while (1) {
    for (i = 1; i < 20001; i++)
      end[i] = 0;
    end[1] = 1;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (i = 0; i < m; i++) {
      cin >> data[i].t >> data[i].s >> data[i].d;
    }
    for (i = 0; i < m; i++) {
      for (j = 1; j < m; j++) {
        if (data[i].t < data[i - 1].t)
          swap(data[i], data[i - 1]);
      }
    }
    for (i = 0; i < m; i++) {
      if (end[data[i].s] == 1)
        end[data[i].d] = 1;
    }
    cnt = 0;
    for (i = 1; i <= n; i++)
      if (end[i] == 1)
        cnt++;
    cout << cnt << endl;

    /*      for(i=0;i<m;i++){
        cout << data[i].t << ' ' << data[i].s << ' ' << data[i].d << endl;
        }
      for(i=0;i<m;i++){
        if(data[i].s==1||data[i].d==1){
          source=data[i].s;
          destination=data[i].d;
          start=i+1;
          flag=1;
          break;
        }
      }
      if(flag==1){
        if(source==1){
          end[data[i].d]=1;
          cnt=2;
        }
        if(destination==1) cnt=1;
        for(i=start;i<m;i++){
          if(end[data[i].s]==1&&end[data[i].d]==0){
            cnt++;
            end[data[i].d]=1;
          }
        }
        cnt=0;
        for(i=1;i<=n;i++){
          if(end[i]==1) cnt++;
        }
        cout << cnt << endl;
      }
      else cout << 1 << endl;
    }
    else cout << 1 << endl;
  }
      */
  }
  return 0;
}
Problem p01273 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string.h>
#include <vector>

using namespace std;
vector<int> ans;
struct edge {
  int to;
  int time;
};

bool virus[20000];
void dfs(vector<edge> e[], int cur, int time) {
  for (int i = 0; i < e[cur - 1].size(); i++) {
    edge te = e[cur - 1][i];
    if (te.time > time) {
      if (virus[cur - 1]) {
        virus[te.to - 1] = true;
        dfs(e, te.to, te.time);
      }
    }
  }
}

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;

    memset(virus, false, sizeof(virus));
    virus[0] = true;

    vector<edge> e[20000];
    for (int i = 0; i < m; i++) {
      int t, s, d;
      cin >> t >> s >> d;
      edge te;
      te.to = d;
      te.time = t;
      e[s - 1].push_back(te);
    }

    dfs(e, 1, 0);

    int count = 0;
    for (int i = 0; i < n; i++) {
      if (virus[i]) {
        count++;
      }
    }
    ans.push_back(count);
  }

  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <string.h>
#include <vector>

using namespace std;
vector<int> ans;
struct edge {
  int src;
  int dis;
};

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;

    bool virus[20000];
    memset(virus, false, sizeof(virus));
    virus[0] = true;

    edge e[20000];
    for (int i = 0; i < m; i++) {
      int t, s, d;
      cin >> t >> s >> d;
      e[t - 1].src = s;
      e[t - 1].dis = d;
    }

    for (int i = 0; i < m; i++) {
      if (virus[e[i].src - 1]) {
        virus[e[i].dis - 1] = true;
      }
    }

    int count = 0;
    for (int i = 0; i < n; i++) {
      if (virus[i]) {
        count++;
      }
    }
    ans.push_back(count);
  }

  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i] << endl;
  }
  return 0;
}
Problem p01273 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
typedef tuple<int, int, int> T;
#define FOR(i, s, x) for (int i = s; i < (int)(x); i++)
#define REP(i, x) FOR(i, 0, x)
#define ALL(c) c.begin(), c.end()
#define DUMP(x) cerr << #x << " = " << (x) << endl

const int dr[4] = {-1, 0, 1, 0};
const int dc[4] = {0, 1, 0, -1};

int main() {
  // use scanf in CodeForces!
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  while (1) {
    int N, M;
    cin >> N >> M;
    if (N == 0 and M == 0)
      break;
    vector<T> packets(M);
    REP(i, M) {
      int t, s, d;
      cin >> t >> s >> d;
      s--, d--;
      packets[i] = T(t, s, d);
    }
    sort(ALL(packets));
    vector<bool> infected(N, false);
    infected[0] = true;
    for (T packet : packets) {
      int t, s, d;
      tie(t, s, d) = packet;
      infected[d] = infected[d] | infected[s];
    }
    cout << accumulate(ALL(infected), 0) << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef pair<int, int> P;
#define FOR(i, s, x) for (int i = s; i < (int)(x); i++)
#define REP(i, x) FOR(i, 0, x)
#define ALL(c) c.begin(), c.end()

int main() {
  while (1) {
    int N, M;
    cin >> N >> M;
    if (N == 0 and M == 0)
      break;
    vector<P> packets(N);
    vector<int> S(N), D(N);
    REP(i, M) {
      int t;
      cin >> t >> S[i] >> D[i];
      S[i]--, D[i]--;
      packets[i] = P(t, i);
    }
    sort(ALL(packets));
    vector<bool> infected(N, false);
    infected[0] = true;
    for (P packet : packets) {
      int i = packet.second, s = S[i], d = D[i];
      infected[d] = infected[d] | infected[s];
    }
    cout << accumulate(ALL(infected), 0) << endl;
  }

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

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

typedef pair<int, int> P;

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<pair<int, P>> data(m);
    vector<bool> v(n + 1, false);
    v[1] = true;

    for (int i = 0; i < m; i++) {
      int t;
      cin >> t;
      P p;
      cin >> p.first >> p.second;
      data[i] = pair<int, P>(t, p);
    }

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

    for (int i = 0; i < m; i++) {
      if (v[data[i].second.first])
        v[data[i].second.second] = true;
    }

    int res = 0;
    for (auto i : v) {
      if (i)
        res++;
    }

    cout << res << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<int> s(m), d(m);
    vector<bool> v(n + 1, false);
    v[1] = true;

    for (int i = 0; i < m; i++) {
      int t;
      cin >> t;
      cin >> s[t - 1] >> d[t - 1];
    }

    for (int i = 0; i < m; i++) {
      if (v[s[i]])
        v[d[i]] = true;
    }

    int res = 0;
    for (auto i : v) {
      if (i)
        res++;
    }

    cout << res << endl;
  }

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

#include <iostream>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    bool infect[n + 1];
    rep(i, n + 1) infect[i] = false;
    infect[1] = true;
    long long ans = 1;
    long long t[m];
    int s[m], d[m];
    rep(i, m) cin >> t[i] >> s[i] >> d[i];
    for (int i = m; i >= 0; i--) {
      for (int j = 0; j < i - 1; j++) {
        if (t[j] > t[j + 1]) {
          long long alt = t[j];
          t[j] = t[j + 1];
          t[j + 1] = alt;
          alt = s[j];
          s[j] = s[j + 1];
          s[j + 1] = alt;
          alt = d[j];
          d[j] = d[j + 1];
          d[j + 1] = alt;
        }
      }
    }
    rep(i, m) {
      if (infect[s[i]]) {
        if (!infect[d[i]])
          ans++;
        infect[d[i]] = true;
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    bool infect[n + 1];
    rep(i, n + 1) infect[i] = false;
    infect[1] = true;
    long long ans = 1;
    long long t[m];
    int s[m], d[m];
    rep(i, m) cin >> t[i] >> s[i] >> d[i];
    for (int i = m; i >= 0; i--) {
      for (int j = 0; j < i - 1; j++) {
        if (t[j] > t[j + 1]) {
          long long alt = t[j];
          t[j] = t[j + 1];
          t[j + 1] = alt;
          alt = s[j];
          s[j] = s[j + 1];
          s[j + 1] = alt;
          alt = d[j];
          d[j] = d[j + 1];
          d[j + 1] = alt;
        }
      }
    }
    rep(i, n) {
      if (infect[s[i]]) {
        if (!infect[d[i]])
          ans++;
        infect[d[i]] = true;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01273 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
  long long n, m;
  while (cin >> n >> m, n) {
    vector<pair<int, pair<int, int>>> v;
    pair<int, pair<int, int>> p;
    long long a[20001] = {0}, t, s = 0;
    a[1] = 1;
    r(i, m) {
      cin >> p.first >> p.second.first >> p.second.second;
      v.push_back(p);
    }
    sort(v.begin(), v.end());
    r(i, m) if (a[v[i].second.first]) a[v[i].second.second] = 1;
    r(i, n + 1) if (a[i]) s++;
    cout << s << endl;
  }
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
int main() {
  long long n, m;
  while (cin >> n >> m, n) {
    long long a[20001] = {0}, b[1000000001][2] = {0}, t, s = 0;
    a[1] = 1;
    r(i, m) {
      cin >> t;
      cin >> b[t][0] >> b[t][1];
    }
    r(i, 1000000001) if (a[b[i][0]]) a[b[i][1]] = 1;
    r(i, 1000000001) if (a[i]) s++;
    cout << s << endl;
  }
}
Problem p01273 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define t first
#define s second.first
#define d second.second

int main() {
  vector<pair<int, pair<int, int>>> a;
  while (1) {
    int n, m;
    int b[20001] = {};
    b[1] = 1;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    a.resize(m);
    for (int i = 0; i < m; i++)
      cin >> a[i].t >> a[i].s >> a[i].d;

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

    int count = 1;
    for (int i = 0; i < m; i++)
      if (b[a[i].s] == 1 && b[a[i].d] == 0) {
        b[a[i].d] = 1;
        count++;
      }
    cout << count << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define t first
#define s second.first
#define d second.second

int main() {
  vector<pair<int, pair<int, int>>> a;

  while (1) {
    int n, m;
    int b[20000] = {};
    b[1] = 1;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    a.resize(m);
    for (int i = 0; i < m; i++)
      cin >> a[i].t >> a[i].s >> a[i].d;

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

    int count = 1;
    for (int i = 0; i < m; i++)
      if (b[a[i].s] == 1) {
        b[a[i].d] = 1;
        count++;
      }
    cout << count << endl;
  }

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

#include <bits/stdc++.h>

using namespace std;

#define MAX 20000

struct edge {
  int from, to, cost;

  edge(int from = -1, int to = -1, int cost = -1)
      : from(from), to(to), cost(cost) {}

  bool operator<(const edge &e) const { return cost < e.cost; }
};

int main() {
  int N, M, t, s, d;

  while (cin >> N >> M, N) {
    edge e[MAX];

    for (int i = 0; i < M; i++) {
      cin >> t >> s >> d;
      e[i] = edge(s - 1, d - 1, t);
    }

    sort(e, e + M);
    bool check[MAX] = {false};
    check[0] = true;

    for (int i = 0; i < M; i++) {
      if (check[e[i].from]) {
        check[e[i].to] = true;
      }
    }

    int ans = 0;
    for (int i = 0; i < N; i++) {
      if (check[i])
        ans++;
    }
    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define MAX 20000

struct edge {
  int to;

  edge(int to = -1) : to(to) {}
};

int ans, N;
edge e[MAX];

void solve(int p) {
  if (e[p].to != -1) {
    ans++;
    solve(e[p].to);
  }
}

int main() {
  int M, t, s, d;

  while (cin >> N >> M, N) {
    for (int i = 0; i < N; i++) {
      e[i] = edge();
    }

    for (int i = 0; i < M; i++) {
      cin >> t >> s >> d;
      e[s - 1] = edge(d - 1);
    }

    ans = 1;
    solve(0);
    cout << ans << endl;
  }

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

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

typedef map<int, pair<int, int>> Packet;

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if ((n | m) == 0) {
      break;
    }
    Packet packets;
    for (int i = 0; i < m; i++) {
      int t, s, d;
      cin >> t >> s >> d;
      packets.insert(make_pair(t, make_pair(s, d)));
    }
    vector<bool> isInfected(n + 1);
    fill(isInfected.begin(), isInfected.end(), false);
    isInfected[1] = true;
    int infectedNum = 1;
    for (Packet::iterator it = packets.begin(); it != packets.end(); it++) {
      if (isInfected[it->second.first] == true &&
          isInfected[it->second.second] == false) {
        isInfected[it->second.second] = true;
        infectedNum++;
      }
    }
    cout << infectedNum << endl;
  }
}

Error: TLE

Fixed submission:

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

struct Packet {
  int t, s, d;
};

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if ((n | m) == 0) {
      break;
    }
    vector<Packet> packets(m);
    for (int i = 0; i < m; i++) {
      cin >> packets[i].t >> packets[i].s >> packets[i].d;
    }
    for (int i = 0; i < m - 1; i++) {
      for (int j = 0; j < m - 1 - i; j++) {
        if (packets[j].t > packets[j + 1].t) {
          Packet temp = packets[j];
          packets[j] = packets[j + 1];
          packets[j + 1] = temp;
        }
      }
    }
    vector<bool> isInfected(n + 1);
    fill(isInfected.begin(), isInfected.end(), false);
    isInfected[1] = true;
    int infectedNum = 1;
    for (int i = 0; i < m; i++) {
      if (isInfected[packets[i].s] == true &&
          isInfected[packets[i].d] == false) {
        isInfected[packets[i].d] = true;
        infectedNum++;
      }
    }
    cout << infectedNum << endl;
  }
}