Problem p00550 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define MAX_N 114514
#define MAX_Q 214514
#define fi first
#define sc second
#define pb push_back
typedef pair<int, int> P;
typedef pair<P, int> Pi;
int N, Q, M;
vector<P> G[MAX_N];
int R[MAX_Q];
int S[MAX_N];
int S2[MAX_N];
int cou[MAX_Q];
int main() {
  clock_t start = clock();
  clock_t end;
  scanf("%d%d%d", &N, &M, &Q);
  for (int i = 0; i < M; i++) {
    int A, B;
    scanf("%d%d", &A, &B);
    A--;
    B--;
    G[A].pb(P(B, i + 1));
    G[B].pb(P(A, i + 1));
  }
  for (int i = 0; i < Q; i++) {
    int X;
    scanf("%d", &X);
    R[X] = i + 1;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < (int)G[i].size(); j++) {
      G[i][j].sc = M + 1 - R[G[i][j].sc];
      if (G[i][j].sc == M + 1) {
        G[i][j].sc = 0;
      }
    }
  }
  // end=clock();
  /*
  if((end-start)/(CLOCKS_PER_SEC)>(double)1.0){
          return 0;
  }
  */
  for (int i = 0; i < N; i++) {
    S[i] = N + 1;
    S2[i] = M + 2;
  }
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, 0));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    if (S[p.sc] <= p.fi) {
      continue;
    }
    S[p.sc] = p.fi;
    for (int i = 0; i < (int)G[p.sc].size(); i++) {
      if (S[G[p.sc][i].fi] > p.fi + 1) {
        que.push(P(p.fi + 1, G[p.sc][i].fi));
      }
    }
  }
  priority_queue<Pi, vector<Pi>, greater<Pi>> que2;
  que2.push(Pi(P(0, 0), 0));
  while (!que2.empty()) {
    Pi p = que2.top();
    que2.pop();
    // printf("%d %d %d\\n",p.fi.fi,p.fi.sc,p.sc);
    if (S[p.sc] < p.fi.fi) {
      continue;
    }
    if (S2[p.sc] <= p.fi.sc) {
      continue;
    }
    S2[p.sc] = p.fi.sc;
    for (int i = 0; i < (int)G[p.sc].size(); i++) {
      if (S[G[p.sc][i].fi] == p.fi.fi + 1) {
        que2.push(
            Pi(P(p.fi.fi + 1, max(p.fi.sc, G[p.sc][i].sc)), G[p.sc][i].fi));
      }
    }
  }
  /*
  for(int i=0;i<N;i++){
          printf("%d %d\\n",S[i],S2[i]);
  }*/

  for (int i = 0; i < N; i++) {
    // if(S2[i]==-1){continue;}
    cou[M + 1 - S2[i]]++;
  }
  int ans = 0;
  for (int i = 1; i <= Q; i++) {
    ans += cou[i];
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MAX_N 114514
#define MAX_Q 214514
#define fi first
#define sc second
#define pb push_back
typedef pair<int, int> P;
typedef pair<P, int> Pi;
int N, Q, M;
vector<P> G[MAX_N];
int R[MAX_Q];
int S[MAX_N];
int S2[MAX_N];
int cou[MAX_Q];
int main() {
  clock_t start = clock();
  clock_t end;
  scanf("%d%d%d", &N, &M, &Q);
  for (int i = 0; i < M; i++) {
    int A, B;
    scanf("%d%d", &A, &B);
    A--;
    B--;
    G[A].pb(P(B, i + 1));
    G[B].pb(P(A, i + 1));
  }
  for (int i = 0; i < Q; i++) {
    int X;
    scanf("%d", &X);
    R[X] = i + 1;
  }
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < (int)G[i].size(); j++) {
      G[i][j].sc = R[G[i][j].sc];
      if (G[i][j].sc == 0) {
        G[i][j].sc = M + 1;
      }
    }
  }
  // end=clock();
  /*
  if((end-start)/(CLOCKS_PER_SEC)>(double)1.0){
          return 0;
  }
  */
  for (int i = 0; i < N; i++) {
    S[i] = N + 1;
    S2[i] = -1;
  }
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, 0));
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    if (S[p.sc] <= p.fi) {
      continue;
    }
    S[p.sc] = p.fi;
    for (int i = 0; i < (int)G[p.sc].size(); i++) {
      if (S[G[p.sc][i].fi] > p.fi + 1) {
        que.push(P(p.fi + 1, G[p.sc][i].fi));
      }
    }
  }
  priority_queue<Pi, vector<Pi>, greater<Pi>> que2;
  que2.push(Pi(P(0, M + 1), 0));
  while (!que2.empty()) {
    Pi p = que2.top();
    que2.pop();
    // printf("%d %d %d\\n",p.fi.fi,p.fi.sc,p.sc);
    if (S[p.sc] < p.fi.fi) {
      continue;
    }
    if (S2[p.sc] >= p.fi.sc) {
      continue;
    }
    S2[p.sc] = p.fi.sc;
    for (int i = 0; i < (int)G[p.sc].size(); i++) {
      if (S[G[p.sc][i].fi] == p.fi.fi + 1) {
        que2.push(
            Pi(P(p.fi.fi + 1, min(p.fi.sc, G[p.sc][i].sc)), G[p.sc][i].fi));
      }
    }
  }
  /*
  for(int i=0;i<N;i++){
          printf("%d %d\\n",S[i],S2[i]);
  }
  */
  for (int i = 0; i < N; i++) {
    if (S2[i] == -1) {
      continue;
    }
    cou[S2[i]]++;
  }
  int ans = 0;
  for (int i = 1; i <= Q; i++) {
    ans += cou[i];
    printf("%d\\n", ans);
  }
  return 0;
}