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

#include <algorithm>
#include <iostream>
#include <list>
#include <map>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef char CHAR;
typedef unsigned char UCHAR;
typedef short SHORT;
typedef unsigned short USHORT;
typedef int INT;
typedef unsigned int UINT;
typedef long long LLONG;
typedef unsigned long long ULLONG;

#define LengthOf(x) (sizeof(x) / sizeof(x[0]))
#define PI 3.14159265358979323846

#if 1
// No.4
typedef struct {
  INT dist;
  INT prevRoom;
  vector<INT> link;
} NODE;

// 深さ優先
void update(NODE *node, INT curRoom, INT prevRoom, INT dist, INT *roomCnt) {
  // update dist
  if (dist < node[curRoom].dist) {
    if (node[curRoom].dist == 0x7FFFFFFF) {
      *roomCnt += 1;
    } // reached room count.

    node[curRoom].dist = dist;
    node[curRoom].prevRoom = prevRoom;

    // next room
    for (int i = 0; i < node[curRoom].link.size(); ++i) {
      update(node, node[curRoom].link[i], curRoom, dist + 1, roomCnt);
    }
  }
}

// 幅優先
typedef struct {
  INT curRoom;
  INT prevRoom;
  INT dist;
} QUERY;

void update2(NODE *node, INT curRoom, INT prevRoom, INT dist, INT *roomCnt,
             list<QUERY> *query) {
  // update dist
  if (dist < node[curRoom].dist) {
    if (node[curRoom].dist == 0x7FFFFFFF) {
      *roomCnt += 1;
    } // reached room count.

    node[curRoom].dist = dist;
    node[curRoom].prevRoom = prevRoom;

    // next room
    for (int i = 0; i < node[curRoom].link.size(); ++i) {
      // update(node, node[curRoom].link[i], curRoom, dist + 1, roomCnt);
      QUERY newQuery;
      newQuery.curRoom = node[curRoom].link[i];
      newQuery.prevRoom = curRoom;
      newQuery.dist = dist + 1;
      query->push_back(newQuery);
    }
  }

  if (query->empty() == false) {
    QUERY proc = query->front();
    query->pop_front();
    update2(node, proc.curRoom, proc.prevRoom, proc.dist, roomCnt, query);
  }
}

int main() {
  INT N, M;
  cin >> N >> M;

  NODE *node = new NODE[N + 1];
  for (int i = 0; i < N + 1; ++i) {
    node[i].prevRoom = 0;
    node[i].dist = 0x7FFFFFFF;
  }

  for (int i = 0; i < M; ++i) {
    INT a, b;
    cin >> a >> b;
    node[a].link.push_back(b);
    node[b].link.push_back(a);
  }

  INT roomCnt = 0;
  // update(node, 1, 0, 0, &roomCnt);
  list<QUERY> query;
  update2(node, 1, 0, 0, &roomCnt, &query);

  if (roomCnt != N) {
    cout << "No";
  } else {
    cout << "Yes" << endl;
    for (int i = 2; i < N + 1; ++i) {
      cout << node[i].prevRoom << endl;
    }
  }

  return 0;
}
#endif

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>

using namespace std;
typedef char CHAR;
typedef unsigned char UCHAR;
typedef short SHORT;
typedef unsigned short USHORT;
typedef int INT;
typedef unsigned int UINT;
typedef long long LLONG;
typedef unsigned long long ULLONG;

#define LengthOf(x) (sizeof(x) / sizeof(x[0]))
#define PI 3.14159265358979323846

#if 1
// No.4
typedef struct {
  INT dist;
  INT prevRoom;
  vector<INT> link;
} NODE;

void update(NODE *node, INT curRoom, INT prevRoom, INT dist, INT *roomCnt) {
  // update dist
  if (dist < node[curRoom].dist) {
    if (node[curRoom].dist == 0x7FFFFFFF) {
      *roomCnt += 1;
    } // reached room count.

    node[curRoom].dist = dist;
    node[curRoom].prevRoom = prevRoom;

    // next room
    for (int i = 0; i < node[curRoom].link.size(); ++i) {
      update(node, node[curRoom].link[i], curRoom, dist + 1, roomCnt);
    }
  }
}

int main() {
  INT N, M;
  cin >> N >> M;

  NODE *node = new NODE[N + 1];
  for (int i = 0; i < N + 1; ++i) {
    node[i].prevRoom = 0;
    node[i].dist = 0x7FFFFFFF;
  }

  for (int i = 0; i < M; ++i) {
    INT a, b;
    cin >> a >> b;
    node[a].link.push_back(b);
    node[b].link.push_back(a);
  }

  INT roomCnt = 0;
  update(node, 1, 0, 0, &roomCnt);

  if (roomCnt != N) {
    cout << "No";
  } else {
    cout << "Yes" << endl;
    for (int i = 2; i < N + 1; ++i) {
      cout << node[i].prevRoom << endl;
    }
  }

  return 0;
}
#endif