Problem p03768 (C++)

Buggy submission (status: Time Limit Exceeded):
// Author : pakhandi
//
using namespace std;

#include <bits/stdc++.h>

#define wl(n) while (n--)
#define fl(i, a, b) for (i = a; i < b; i++)
#define rev(i, a, b) for (i = a; i >= b; i--)

#define si(n) scanf("%d", &n)
#define sll(l) scanf("%lld", &l)

#define debug(x) cout << "\n#(" << x << ")#\n"
#define nline printf("\n")

#define mem(a, i) memset(a, i, sizeof(a))

#define MOD 1000000007
#define ll long long int
#define u64 unsigned long long int

#define PB push_back
#define SZ size
#define MP make_pair

#define IGNORE cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

const ll INF = (1LL << 58);
const bool DEBUG = 0;

const int MAX_LIMIT = 100005;

int nodes, edges;
std::vector<int> adj[MAX_LIMIT];
int dist[MAX_LIMIT], vis[MAX_LIMIT];
queue<int> q;

set<int> done, allDone;

int C[MAX_LIMIT], memo[MAX_LIMIT][15];

std::vector<pair<int, pair<int, int>>> Q;

void bfs(int node, int distLimit, int thisColor) {
  q.push(node);
  vis[node] = 1;
  dist[node] = distLimit;
  while (!q.empty()) {
    node = q.front();
    q.pop();

    if (allDone.find(node) == allDone.end())
      C[node] = thisColor;
    done.insert(node);
    allDone.insert(node);
    // cerr << node << " " << thisColor << "\n";
    if (dist[node] == 0) {
      // cerr << node + 1 << "\n";
      continue;
    }

    int i, limit = adj[node].size();
    fl(i, 0, limit) {
      int v = adj[node][i];
      if (vis[v])
        continue;

      dist[v] = dist[node] - 1;
      // bool flag = 0;
      // for(int j = dist[v]; j <= 10; j++)
      // {
      // 	if(memo[v][j])
      // 		flag = 1;
      // }
      // if(flag)
      // {
      // 	cerr << node + 1 << " " << v + 1 << "\n";
      // 	continue;
      // }

      vis[v] = 1;
      memo[v][dist[v]] = 1;
      // cout << v + 1 << " " << dist[v] << "\n";
      q.push(v);
    }
  }
  return;
}

int main() {
  int i, j;

  scanf("%d %d", &nodes, &edges);

  for (int i = 0; i < edges; i++) {
    int x, y;
    scanf("%d %d", &x, &y);

    x--;
    y--;
    adj[x].PB(y);
    adj[y].PB(x);
  }

  int qws;
  scanf("%d", &qws);

  for (int i = 0; i < qws; i++) {
    int x, y, c;
    scanf("%d %d %d", &x, &y, &c);
    x--;

    Q.PB(MP(x, MP(y, c)));
  }

  for (int i = qws - 1; i >= 0; i--) {
    if (allDone.size() == nodes)
      break;
    int x = Q[i].first;
    int y = Q[i].second.first;
    int c = Q[i].second.second;

    bool f = 0;

    for (int j = y; j <= 10; j++) {
      if (memo[x][j])
        f = 1;
    }

    if (f)
      continue;

    // cout << x << " " << y << "\n";

    memo[x][y] = 1;

    bfs(x, y, c);

    while (!done.empty()) {
      int v = *done.begin();
      done.erase(v);
      vis[v] = 0;
    }
  }

  for (int i = 0; i < nodes; i++)
    printf("%d\n", C[i]);

  return 0;
}
/*
        Powered by Buggy Plugin
*/


Fixed submission:
// Author : pakhandi
//
using namespace std;

#include <bits/stdc++.h>

#define wl(n) while (n--)
#define fl(i, a, b) for (i = a; i < b; i++)
#define rev(i, a, b) for (i = a; i >= b; i--)

#define si(n) scanf("%d", &n)
#define sll(l) scanf("%lld", &l)

#define debug(x) cout << "\n#(" << x << ")#\n"
#define nline printf("\n")

#define mem(a, i) memset(a, i, sizeof(a))

#define MOD 1000000007
#define ll long long int
#define u64 unsigned long long int

#define PB push_back
#define SZ size
#define MP make_pair

#define IGNORE cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');

const ll INF = (1LL << 58);
const bool DEBUG = 0;

const int MAX_LIMIT = 100005;

int nodes, edges;
std::vector<int> adj[MAX_LIMIT];
int dist[MAX_LIMIT], vis[MAX_LIMIT];
queue<int> q;

set<int> done, allDone;

int C[MAX_LIMIT], memo[MAX_LIMIT][15];

std::vector<pair<int, pair<int, int>>> Q;

void bfs(int node, int distLimit, int thisColor) {
  q.push(node);
  vis[node] = 1;
  dist[node] = distLimit;
  while (!q.empty()) {
    node = q.front();
    q.pop();

    if (allDone.find(node) == allDone.end())
      C[node] = thisColor;
    done.insert(node);
    allDone.insert(node);
    // cerr << node << " " << thisColor << "\n";
    if (dist[node] == 0) {
      // cerr << node + 1 << "\n";
      continue;
    }

    int i, limit = adj[node].size();
    fl(i, 0, limit) {
      int v = adj[node][i];
      if (vis[v])
        continue;

      dist[v] = dist[node] - 1;
      bool flag = 0;
      for (int j = dist[v]; j <= 10; j++) {
        if (memo[v][j])
          flag = 1;
      }
      if (flag) {
        // cerr << node + 1 << " " << v + 1 << "\n";
        continue;
      }

      vis[v] = 1;
      memo[v][dist[v]] = 1;
      // cout << v + 1 << " " << dist[v] << "\n";
      q.push(v);
    }
  }
  return;
}

int main() {
  int i, j;

  scanf("%d %d", &nodes, &edges);

  for (int i = 0; i < edges; i++) {
    int x, y;
    scanf("%d %d", &x, &y);

    x--;
    y--;
    adj[x].PB(y);
    adj[y].PB(x);
  }

  int qws;
  scanf("%d", &qws);

  for (int i = 0; i < qws; i++) {
    int x, y, c;
    scanf("%d %d %d", &x, &y, &c);
    x--;

    Q.PB(MP(x, MP(y, c)));
  }

  for (int i = qws - 1; i >= 0; i--) {
    if (allDone.size() == nodes)
      break;
    int x = Q[i].first;
    int y = Q[i].second.first;
    int c = Q[i].second.second;

    bool f = 0;

    for (int j = y; j <= 10; j++) {
      if (memo[x][j])
        f = 1;
    }

    if (f)
      continue;

    // cout << x << " " << y << "\n";

    memo[x][y] = 1;

    bfs(x, y, c);

    while (!done.empty()) {
      int v = *done.begin();
      done.erase(v);
      vis[v] = 0;
    }
  }

  for (int i = 0; i < nodes; i++)
    printf("%d\n", C[i]);

  return 0;
}
/*
        Powered by Buggy Plugin
*/