Problem p03142 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"

using namespace std;

#define Rep(b, e, i) for (ll i = b; i <= e; i++)
#define Repr(e, b, i) for (ll i = e; i >= b; i--)
#define rep(n, i) Rep(0, n - 1, i)
#define repr(n, i) Repr(n - 1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(v) push_back(v)
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define bitcnt(x) __builtin_popcount(x)
#define fst first
#define snd second
#define Pqaz(T) priority_queue<T, vector<T>, greater<T>>
#define Pqza(T) priority_queue<T>
#define put(x) cout << x;
#define putsp(x) cout << x << ' ';
#define putbd cout << "---------------------------------------------" << endl;
#define putln(x) cout << x << endl;
#define debug(x) cerr << #x << "=" << x << endl;
#define ENJYU std::ios::sync_with_stdio(false) std::cin.tie(0);

typedef long long ll;
typedef pair<ll, ll> llP;
typedef pair<int, int> intP;
typedef complex<double> comp;
typedef vector<int> vec;
typedef vector<ll> vecll;
typedef vector<double> vecd;
typedef vector<vec> mat;
typedef vector<vecll> matll;
typedef vector<vecd> matd;

template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}

const int MAX = 1000010;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1 << 29;
const ll INFL = 1e18;
const ll MOD = 1e9 + 7;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
// const int dx[]={-1,-1,-1,0,1,1,1,0},dy[]={-1,0,1,1,1,0,-1,-1};

//********************************template
//END****************************************//

void solve() {
  int N, M;
  cin >> N >> M;
  vector<int> adj[N];
  int root = -1;
  vec dim(N);
  rep(N + M - 1, i) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    adj[a].pb(b);
    dim[b]++;
  }
  rep(N, i) {
    if (!dim[i])
      root = i;
  }
  vec par(N), vst(N);
  queue<int> q;
  q.push(root);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    if (vst[v])
      continue;
    for (int nv : adj[v]) {
      par[nv] = v + 1;
      q.push(nv);
    }
  }
  rep(N, i) { cout << par[i] << endl; }
}

int main() {
  solve();
  // cout << "yui(*-v・)yui" << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;

#define Rep(b, e, i) for (ll i = b; i <= e; i++)
#define Repr(e, b, i) for (ll i = e; i >= b; i--)
#define rep(n, i) Rep(0, n - 1, i)
#define repr(n, i) Repr(n - 1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(v) push_back(v)
#define uniq(v) (v).erase(unique(all(v)), (v).end())
#define bitcnt(x) __builtin_popcount(x)
#define fst first
#define snd second
#define Pqaz(T) priority_queue<T, vector<T>, greater<T>>
#define Pqza(T) priority_queue<T>
#define put(x) cout << x;
#define putsp(x) cout << x << ' ';
#define putbd cout << "---------------------------------------------" << endl;
#define putln(x) cout << x << endl;
#define debug(x) cerr << #x << "=" << x << endl;
#define ENJYU std::ios::sync_with_stdio(false) std::cin.tie(0);

typedef long long ll;
typedef pair<ll, ll> llP;
typedef pair<int, int> intP;
typedef complex<double> comp;
typedef vector<int> vec;
typedef vector<ll> vecll;
typedef vector<double> vecd;
typedef vector<vec> mat;
typedef vector<vecll> matll;
typedef vector<vecd> matd;

template <class T> ostream &operator<<(ostream &o, const vector<T> &v) {
  o << "{";
  for (int i = 0; i < (int)v.size(); i++)
    o << (i > 0 ? ", " : "") << v[i];
  o << "}";
  return o;
}

const int MAX = 1000010;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1 << 29;
const ll INFL = 1e18;
const ll MOD = 1e9 + 7;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
// const int dx[]={-1,-1,-1,0,1,1,1,0},dy[]={-1,0,1,1,1,0,-1,-1};

//********************************template
//END****************************************//

void solve() {
  int N, M;
  cin >> N >> M;
  vector<int> adj[N];
  int root = -1;
  vec dim(N);
  rep(N + M - 1, i) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    adj[a].pb(b);
    dim[b]++;
  }
  rep(N, i) {
    if (!dim[i])
      root = i;
  }
  vec par(N), vst(N);
  queue<int> q;
  q.push(root);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    if (vst[v])
      continue;
    for (int nv : adj[v]) {
      dim[nv]--;
      if (!dim[nv]) {
        par[nv] = v + 1;
        q.push(nv);
      }
    }
  }
  rep(N, i) { cout << par[i] << endl; }
}

int main() {
  solve();
  // cout << "yui(*-v・)yui" << endl;
  return 0;
}