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

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
// #define INF (1LL<<59)

struct edge {
  int to, sound;
};

int f(string s) {
  if (s == "C")
    return 0;
  if (s == "C#")
    return 1;
  if (s == "D")
    return 2;
  if (s == "D#")
    return 3;
  if (s == "E")
    return 4;
  if (s == "F")
    return 5;
  if (s == "F#")
    return 6;
  if (s == "G")
    return 7;
  if (s == "G#")
    return 8;
  if (s == "A")
    return 9;
  if (s == "A#")
    return 10;
  if (s == "B")
    return 11;
  return -1;
}

bool isSame(vector<int> &a, vector<int> &b) {
  rep(i, a.size()) {
    if (a[i] != b[i + 1])
      return false;
  }
  return true;
}

int main() {
  int input;
  cin >> input;
  rep(loop, input) {
    int n, m;
    scanf("%d %d", &n, &m);
    vector<int> step, song;
    step.pb(-1); // 1-index???????????????
    rep(i, n) {
      string s;
      cin >> s;
      step.pb(f(s));
    }
    rep(i, m) {
      string s;
      cin >> s;
      song.pb(f(s));
    }
    song.pb(-1); //????????????????°???????????????????

    vector<edge> G[50010];
    rep(i, n) G[i].pb(edge{(int)i + 1, step[i + 1]});
    rep(i, n) G[i + 1].pb(edge{(int)i, (step[i] - 1 + 12) % 12});
    rep(i, n - 1) G[i].pb(edge{(int)i + 2, (step[i + 2] + 1) % 12});
    G[n].pb(edge{n + 1, -1});
    G[n - 1].pb(edge{n + 2, -1});

    bool f = true;

    queue<pii> que;
    que.push(make_pair(0, 0));
    while (!que.empty()) {
      int pos = que.front().first;
      int num = que.front().second;
      que.pop();

      if (n + 1 - pos > 2 * (m + 1 - num))
        continue;

      if (num == m + 1) {
        cout << "Yes" << endl;
        f = false;
        break;
      }

      rep(i, G[pos].size()) {
        edge e = G[pos][i];
        if (e.sound == song[num]) {
          que.push(make_pair(e.to, num + 1));
        }
      }
    }
    if (f)
      cout << "No" << endl;
  }
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define vi vector<int>
#define pb push_back
#define INF 999999999
// #define INF (1LL<<59)

struct edge {
  int to, sound;
};

int f(string s) {
  if (s == "C")
    return 0;
  if (s == "C#")
    return 1;
  if (s == "D")
    return 2;
  if (s == "D#")
    return 3;
  if (s == "E")
    return 4;
  if (s == "F")
    return 5;
  if (s == "F#")
    return 6;
  if (s == "G")
    return 7;
  if (s == "G#")
    return 8;
  if (s == "A")
    return 9;
  if (s == "A#")
    return 10;
  if (s == "B")
    return 11;
  return -1;
}

bool isSame(vector<int> &a, vector<int> &b) {
  rep(i, a.size()) {
    if (a[i] != b[i + 1])
      return false;
  }
  return true;
}

int main() {
  int input;
  cin >> input;
  rep(loop, input) {
    int n, m;
    scanf("%d %d", &n, &m);
    vector<int> step, song;
    step.pb(-1); // 1-index???????????????
    rep(i, n) {
      string s;
      cin >> s;
      step.pb(f(s));
    }
    rep(i, m) {
      string s;
      cin >> s;
      song.pb(f(s));
    }
    song.pb(-1); //????????????????°???????????????????

    vector<edge> G[50010];
    rep(i, n) G[i].pb(edge{(int)i + 1, step[i + 1]});
    rep(i, n) G[i + 1].pb(edge{(int)i, (step[i] - 1 + 12) % 12});
    rep(i, n - 1) G[i].pb(edge{(int)i + 2, (step[i + 2] + 1) % 12});
    G[n].pb(edge{n + 1, -1});
    G[n - 1].pb(edge{n + 2, -1});

    bool f = true;

    queue<pii> que;
    que.push(make_pair(0, 0));
    while (!que.empty()) {

      if (que.front().second == m + 1) {
        cout << "Yes" << endl;
        f = false;
        break;
      }

      rep(i, G[que.front().first].size()) {
        edge e = G[que.front().first][i];
        if (e.sound == song[que.front().second]) {
          que.push(make_pair(e.to, que.front().second + 1));
        }
      }
      que.pop();
    }
    if (f)
      cout << "No" << endl;
  }
}