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

#include <algorithm>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
int n, m, q;
struct B {
  int to, b;
};
struct P {
  int pos, cost;
};
struct C {
  int from, to;
  bool a;
};
vector<B> e[100001]; //???
C c[200001];         //???
int a[100001];       //?????????
int b[100001];       //??\\??????
const int inf = 9999999;
bool operator<(P a, P b) { return a.cost < b.cost; }
bool operator>(P a, P b) { return a.cost > b.cost; }
int main() {
  cin >> n >> m >> q;
  for (int i = 1; i <= m; i++) {
    int ai, bi;
    cin >> ai >> bi;
    e[ai].push_back(B{bi, i});
    e[bi].push_back(B{ai, i});
    c[i] = C{ai, bi, true};
  }
  fill(a + 1, a + 1 + n, inf);
  priority_queue<P, vector<P>, greater<P>> p;
  a[1] = 0;
  p.push(P{1, 0});
  while (!p.empty()) {
    P t = p.top();
    p.pop();
    if (a[t.pos] != t.cost)
      continue;
    for (int i = 0; i < e[t.pos].size(); i++) {
      if (a[e[t.pos][i].to] > a[t.pos] + 1) {
        a[e[t.pos][i].to] = t.cost + 1;
        p.push(P{e[t.pos][i].to, t.cost + 1});
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    if (a[c[i].from] == a[c[i].to] + 1)
      b[c[i].from]++;
    if (a[c[i].to] == a[c[i].from] + 1)
      b[c[i].to]++;
  }
  int sum = 0;
  for (int z = 0; z < q; z++) {
    int u;
    cin >> u;
    int from, to;
    queue<int> p;
    c[u].a = false;
    if (a[c[u].from] == a[c[u].to] + 1)
      from = c[u].to, to = c[u].from;
    else if (a[c[u].to] == a[c[u].from] + 1)
      from = c[u].from, to = c[u].to;
    else {
      cout << sum << endl;
      goto stop;
    }
    if (b[from] == 0 && from != 1) {
      cout << sum << endl;
      goto stop;
    }
    b[to]--;
    if (b[to] == 0) {
      sum++;
      p.push(to);
    }
    while (!p.empty()) {
      int t = p.front();
      p.pop();
      for (int i = 0; i < e[t].size(); i++) {
        if (a[e[t][i].to] == a[t] + 1 && c[e[t][i].b].a && b[e[t][i].to] != 0) {
          b[e[t][i].to]--;
          if (b[e[t][i].to] == 0) {
            sum++;
            p.push(e[t][i].to);
          }
        }
      }
    }
    cout << sum << endl;
  stop:;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
typedef long long ll;
int n, m, q;
int a[100001]; //?????????
ll b[100001];  //????????°
struct B {
  int to, b;
};
vector<B> e[100001]; //???
struct A {
  int from, to;
  bool a;
};
A c[200001]; //????????´???????????????
struct P {
  int pos, cost;
};
const int inf = 9999999;
struct Y {
  ll i;  // from's cost;
  int j; // to;
};
bool operator<(P a, P b) { return a.cost < b.cost; }
bool operator>(P a, P b) { return a.cost > b.cost; }
struct R {
  int cost, b;
};
bool operator<(R a, R b) { return a.cost < b.cost; }
R d[100001];
int x[100001];
int main() {
  cin >> n >> m >> q;
  for (int i = 1; i <= n; i++) {
    a[i] = inf;
    d[i].cost = 0;
    d[i].b = i;
  }
  for (int i = 1; i <= m; i++) {
    int u1, u2;
    cin >> u1 >> u2;
    e[u1].push_back(B{u2, i});
    e[u2].push_back(B{u1, i});
    c[i] = A{u1, u2, false};
  }
  priority_queue<P, vector<P>, greater<P>> p;
  a[1] = 0;
  p.push(P{1, 0});
  while (!p.empty()) {
    P t = p.top();
    p.pop();
    if (a[t.pos] != t.cost)
      continue;
    for (int i = 0; i < e[t.pos].size(); i++) {
      if (a[e[t.pos][i].to] > a[t.pos] + 1) {
        a[e[t.pos][i].to] = a[t.pos] + 1;
        p.push(P{e[t.pos][i].to, a[t.pos] + 1});
      }
    }
  }
  for (int i = 1; i <= m; i++) {
    if (a[c[i].from] == a[c[i].to] + 1)
      x[c[i].from]++;
    if (a[c[i].from] + 1 == a[c[i].to])
      x[c[i].to]++;
  }
  vector<int> l;
  queue<int> r;
  r.push(1);
  while (!r.empty()) {
    int t = r.front();
    r.pop();
    l.push_back(t);
    for (int i = 0; i < e[t].size(); i++) {
      x[e[t][i].to]--;
      if (x[e[t][i].to] == 0)
        r.push(e[t][i].to);
    }
  }
  b[1] = 1;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < e[l[i]].size(); j++) {
      if (a[e[l[i]][j].to] == a[l[i]] + 1) {
        b[e[l[i]][j].to] += b[l[i]];
      }
    }
  }
  int sum = 0;
  for (int z = 0; z < q; z++) {
    int u;
    cin >> u;
    int from, to;
    if (a[c[u].from] == a[c[u].to] + 1)
      from = c[u].to, to = c[u].from;
    else if (a[c[u].from] + 1 == a[c[u].to])
      from = c[u].from, to = c[u].to;
    else {
      cout << sum << endl;
      continue;
    }
    if (b[from] == 0) {
      cout << sum << endl;
      continue;
    }
    queue<Y> p;
    p.push(Y{b[from], to});
    c[u].a = true;
    while (!p.empty()) {
      Y t = p.front();
      p.pop();
      for (int i = 0; i < e[t.j].size(); i++) {
        if (!c[e[t.j][i].b].a && b[e[t.j][i].to] != 0 &&
            a[e[t.j][i].to] == a[t.j] + 1) {
          p.push(Y{t.i, e[t.j][i].to});
        }
      }
      if (b[t.j] != 0 && b[t.j] - t.i == 0)
        sum++;
      b[t.j] -= t.i;
    }
    cout << sum << endl;
  }
}