alexjercan/bugnet huggingface dataset:
Problem p01126 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define LOOP(i, x, n) for (int i = x; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define MP make_pair
#define FR first
#define SC second
#define int long long
using namespace std;
const int MOD = 1000000007;
const int INF = 1000000009;

signed main() {
  int n, m, a;
  while (cin >> n >> m >> a, n) {
    a--;
    vector<pair<int, pair<int, int>>> k;
    REP(i, m) {
      int h, p, q;
      cin >> h >> p >> q;
      p--;
      q--;
      k.PB(MP(h, MP(p, q)));
    }
    sort(ALL(k));
    vector<int> am(n + 1);
    LOOP(i, 1, n + 1) am[i] = i;
    for (int i = 0; i < k.size(); i++) {
      int x = k[i].SC.FR, y = k[i].SC.SC;
      swap(am[x], am[y]);
    }
    cout << am[a] + 1 << endl;
  }
  return 0;
}


Error: -6
free(): invalid pointer


Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define LOOP(i, x, n) for (int i = x; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define PB push_back
#define MP make_pair
#define FR first
#define SC second
#define int long long
using namespace std;
const int MOD = 1000000007;
const int INF = 1000000009;

signed main() {
  int n, m, a;
  while (cin >> n >> m >> a, n) {
    a--;
    vector<pair<int, pair<int, int>>> k;
    REP(i, m) {
      int h, p, q;
      cin >> h >> p >> q;
      p--;
      q--;
      k.PB(MP(h, MP(p, q)));
    }
    sort(ALL(k));
    vector<int> am(n);
    REP(i, n) am[i] = i + 1;
    for (int i = k.size() - 1; i >= 0; i--) {
      int x = k[i].SC.FR, y = k[i].SC.SC;
      x--;
      y--;
      swap(am[x], am[y]);
    }
    cout << am[a] + 1 << endl;
  }
  return 0;
}
Problem p01126 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int h, p, q;
  int n, m, a;
  while (cin >> n >> m >> a, n) {
    int num[1002][102];
    for (int i = 0; i < 1002; i++) {
      for (int j = 0; j < 102; j++) {
        num[i][j] = -1;
      }
    }
    int maxh = 0;
    for (int i = 0; i < m; i++) {
      cin >> h >> p >> q;
      num[h][p - 1] = q - 1;
      num[h][q - 1] = p - 1;
      maxh = max(h, maxh);
    }
    int ans = a - 1;
    for (int i = maxh; i >= 0; i--) {
      for (int j = 0; j < n; j++) {
        if ((num[i][j] == ans) || num[i][ans] == j) {
          ans = j;
          // cout<<ans<<endl;
          break;
        }
      }
    }
    cout << ans + 1 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int h, p, q;
  int n, m, a;
  while (cin >> n >> m >> a, n) {
    int num[102][102] = {0};
    for (int i = 0; i < m; i++) {
      cin >> h >> p >> q;
      num[p - 1][q - 1] = h;
      num[q - 1][p - 1] = h;
    }
    int ans = 0;
    for (int i = 1000; i >= 1; i--) {
      for (int j = 0; j < m; j++) {
        if (num[j][ans] == i || num[ans][j] == i) {
          ans = j;
          break;
        }
      }
    }
    cout << ans + 1 << endl;
  }
  return 0;
}
Problem p01126 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
int main() {

  int n, m, a;
  // a,bÌcüÉ©©é¡üÌ³Ìê
  set<int> rightLine[101];
  set<int> leftLine[101];
  while (cin >> n >> m >> a && !(n == 0 && m == 0 && a == 0)) {
    for (int i = 0; i < 101; i++) {
      rightLine[i].clear();
      leftLine[i].clear();
    }
    for (int i = 0; i < m; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      rightLine[p].insert(-h);
      leftLine[q].insert(-h);
    }
    int ch = -1002;
    int ct = a;
    // S[·éÜÅV~[g
    while (1) {
      int t = 1;
      int s = 1;
      // ©gÌEÆ¶ð`FbN
      if (ct != 1) {
        set<int>::iterator it = leftLine[ct].upper_bound(ch);
        if (it != leftLine[ct].end())
          t = *it;
      }
      if (ct != n) {
        set<int>::iterator it = rightLine[ct].upper_bound(ch);
        if (it != rightLine[ct].end())
          s = *it;
      }
      if (t == 1 && s == 1)
        break;
      else if (t == 1) {
        ch = s;
        ct++;
      } else if (s == 1) {
        ch = t;
        ct--;
      } else {
        if (s > t) {
          ch = t;
          ct--;
        } else {
          ch = s;
          ct++;
        }
      }
    }
    cout << ct << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
int main() {

  int n, m, a;
  // a,bÌcüÉ©©é¡üÌ³Ìê
  set<int> rightLine[101];
  set<int> leftLine[101];
  while (cin >> n >> m >> a && !(n == 0 && m == 0 && a == 0)) {
    for (int i = 0; i < 101; i++) {
      rightLine[i].clear();
      leftLine[i].clear();
    }
    for (int i = 0; i < m; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      rightLine[p].insert(-h);
      leftLine[q].insert(-h);
    }
    int ch = -1002;
    int ct = a;
    // S[·éÜÅV~[g
    while (1) {
      int t = 1;
      int s = 1;
      // ©gÌEÆ¶ð`FbN
      if (ct != 1) {
        set<int>::iterator it = leftLine[ct].upper_bound(ch);
        if (it != leftLine[ct].end())
          t = *it;
      }
      if (ct != n) {
        set<int>::iterator it = rightLine[ct].upper_bound(ch);
        if (it != rightLine[ct].end())
          s = *it;
      }
      if (t == 1 && s == 1)
        break;
      else if (t == 1) {
        ch = s;
        ct++;
      } else if (s == 1) {
        ch = t;
        ct--;
      }
    }
    cout << ct << endl;
  }

  return 0;
}
Problem p01126 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m, a;
  int rope[1000][3];

  while (1) {
    scanf("%d", &n);
    scanf("%d", &m);
    scanf("%d", &a);
    if (n == 0)
      break;

    int height = 0;
    for (int i = 0; i < m; i++) {
      scanf("%d", &rope[i][0]);
      scanf("%d", &rope[i][1]);
      scanf("%d", &rope[i][2]);

      if (rope[i][0] > height)
        height = rope[i][0];
    }

    //        cout<<"Height :"<<height<<endl;
    //        cout<<"Pos :"<<a<<endl;
    //        cout<<endl;
    while (height > 0) {
      //            cout<<"Height = "<<height<<endl;
      for (int j = 0; j < m; j++) {

        if (height == rope[j][0]) {
          int step = rope[j][1] + rope[j][2] - (a * 2);

          if (step == 1) {
            a++;
            //                        cout<<"Pos :"<<a<<endl;
            break;
          } else if (step == (-1)) {
            a--;
            //                      cout<<"Pos :"<<a<<endl;
            break;
          }
        }
      }
      --height;
    }
    cout << a << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  while (1) {
    int n, m, a;
    cin >> n >> m >> a;
    if (n == 0 && m == 0 && a == 0)
      break;

    int rope[m][3];
    int height = 0;
    for (int i = 0; i < m; i++) {
      cin >> rope[i][0] >> rope[i][1] >> rope[i][2];
      if (rope[i][0] > height)
        height = rope[i][0];
    }

    while (height > 0) {
      for (int i = 0; i < m; i++) {
        if (height == rope[i][0]) {
          if ((rope[i][1] + rope[i][2]) - (a * 2) == 1) {
            a++;
            height--;
          } else if ((rope[i][1] + rope[i][2]) - (a * 2) == -1) {
            a--;
            height--;
          }
        }
      }
    }

    cout << a << endl;
  }

  return 0;
}
Problem p01126 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

struct st {
  int a, b, c;
};
int main() {
  int n, m, a;
  while (cin >> n >> m >> a, n) {
    vector<st> v;
    rep(i, m) {
      int h, p, q;
      cin >> h >> p >> q;
      v.push_back({h, p, q});
    }
    sort(v.begin(), v.end(), [](st a, st b) { return a.a > b.a; });
    int pos = a;
    for (st i : v) {
      if (i.b == pos)
        pos = i.c;
      else if (i.c == pos)
        pos = i.b;
    }
    cout << pos << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

struct st {
  int a, b, c;
};
int main() {
  int n, m, a;
  while (cin >> n >> m >> a, n) {
    vector<st> v;
    rep(i, n) {
      int h, p, q;
      cin >> h >> p >> q;
      v.push_back({h, p, q});
    }
    sort(v.begin(), v.end(), [](st a, st b) { return a.a > b.a; });
    int pos = a;
    for (st i : v) {
      if (i.b == pos)
        pos = i.c;
      else if (i.c == pos)
        pos = i.b;
    }
    cout << pos << endl;
  }
}
Problem p01126 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  while (true) {
    int n, m, a;
    cin >> n >> m >> a;
    if (n == 0 && m == 0 && a == 0)
      return 0;

    int amida[1001][n + 1];
    for (int i = 0; i < 1001; i++)
      for (int j = 0; j < n + 1; j++)
        amida[i][j] = 0;

    for (int i = 0; i < m; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      for (int j = p; j < q; j++)
        amida[h][j] = 1;
    }

    for (int i = 1000; i > 0; i--) {
      if (amida[i][a])
        a++;
      else if (amida[i][a - 1])
        a--;
    }
    cout << a << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  while (true) {
    int n, m, a;
    cin >> n >> m >> a;
    if (n == 0 && m == 0 && a == 0)
      return 0;

    int amida[m + 1][n + 1];
    for (int i = 0; i < m + 1; i++)
      for (int j = 0; j < n + 1; j++)
        amida[i][j] = 0;

    for (int i = 0; i < m; i++) {
      int h, p, q;
      cin >> h >> p >> q;
      for (int j = p; j < q; j++)
        amida[h][j] = 1;
    }

    for (int i = m; i > 0; i--) {
      if (amida[i][a] && a < n)
        a++;
      else if (a > 1 && amida[i][a - 1])
        a--;
    }
    cout << a << endl;
  }
}
Problem p01126 (C++) — buggy submission, verdict: Runtime Error


// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2001

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

struct Line {
  int height;
  int from;
  int to;

  bool operator<(const Line &right) const { return height < right.height; }
};

int main() {
  int N, M, A;

  while (cin >> N >> M >> A) {

    if (N == 0 && M == 0 && A == 0) {
      break;
    }

    vector<Line> lines(M);

    vector<int> amida(N + 1, 0);
    rep(i, amida.size()) { amida[i] = i; }

    if (M != 0) {
      int H, P, Q;
      rep(i, M) {
        cin >> H >> P >> Q;
        lines[i] = {H, P, Q};
      }

      sort(ALL(lines));

      rep(i, M) {
        int p = lines[i].from;
        int q = lines[i].to;
        swap(amida[p], amida[q]);
      }
    }
    cout << amida[A] << endl;
  }

  return 0;
}

Error: 0

Fixed submission:


// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2001

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <vector>

#define ALL(v) (v).begin(), (v).end()
#define REP(i, p, n) for (int i = p; i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define dump(a) (cerr << #a << "=" << (a) << endl)
#define DUMP(list)                                                             \\
  cout << "{ ";                                                                \\
  for (auto nth : list) {                                                      \\
    cout << nth << " ";                                                        \\
  }                                                                            \\
  cout << "}" << endl;

using namespace std;

struct Line {
  int height;
  int from;
  int to;

  bool operator<(const Line &right) const { return height < right.height; }
};

int main() {
  int N, M, A;

  while (cin >> N >> M >> A) {

    if (N == 0 && M == 0 && A == 0) {
      break;
    }

    vector<Line> lines(M);

    vector<int> amida(N + 1, 0);
    REP(i, 1, N + 2) { amida[i] = i; }

    if (M != 0) {
      int H, P, Q;
      rep(i, M) {
        cin >> H >> P >> Q;
        lines[i] = {H, P, Q};
      }

      sort(ALL(lines));

      rep(i, M) {
        int p = lines[i].from;
        int q = lines[i].to;
        swap(amida[p], amida[q]);
      }
    }
    cout << amida[A] << endl;
  }

  return 0;
}
Problem p01126 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

class position {
public:
  int x, y;
  int height;
};

int main() {
  int n, m, a;
  while (1) {
    cin >> n >> m >> a;
    if (!n && !m && !a)
      break;
    int dt[n + 1];
    rep(i, n + 1) dt[i] = i;
    position p[m];
    rep(i, m) cin >> p[i].height >> p[i].x >> p[i].y;
    for (int i = 1; i < m - 1; i++) {
      for (int j = m - 1; j >= i; j--) {
        if (p[j].height < p[j - 1].height) {
          position alt = p[j];
          p[j] = p[j - 1];
          p[j - 1] = alt;
        }
      }
    }
    rep(i, m) {
      int alt = dt[p[i].x];
      dt[p[i].x] = dt[p[i].y];
      dt[p[i].y] = alt;
    }
    cout << dt[a] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
using namespace std;

class position {
public:
  int x, y;
  int height;
};

int main() {
  int n, m, a;
  while (1) {
    cin >> n >> m >> a;
    if (!n && !m && !a)
      break;
    int dt[n + 1];
    rep(i, n + 1) dt[i] = i;
    position p[m];
    rep(i, m) cin >> p[i].height >> p[i].x >> p[i].y;
    for (int i = 1; i < n - 1; i++) {
      for (int j = n - 1; j >= i; j--) {
        if (p[j].height < p[j - 1].height) {
          position alt = p[j];
          p[j] = p[j - 1];
          p[j - 1] = alt;
        }
      }
    }
    rep(i, m) {
      int alt = dt[p[i].x];
      dt[p[i].x] = dt[p[i].y];
      dt[p[i].y] = alt;
    }
    cout << dt[a] << endl;
  }
  return 0;
}
Problem p01127 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int h, w;
vector<int> dp;

bool dfs(vector<char> &ch, vector<string> &c, int s) {
  if (dp[s] != -1)
    return dp[s];
  if (s == (1 << ch.size()) - 1) {
    return true;
  }
  bool isok = false;
  for (int col = 0; col < ch.size(); col++) {
    bool piyo = true;
    if (s & (1 << col))
      continue;
    int l = 100;
    int r = 0;
    int u = 100;
    int b = 0;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (c[i][j] == ch[col]) {
          l = min(l, j);
          r = max(r, j);
          u = min(u, i);
          b = max(b, i);
        }
      }
    }
    b++, r++;
    stack<pair<int, int>> st;
    for (int i = u; i < b; i++) {
      for (int j = l; j < r; j++) {
        if (!isalpha(c[i][j])) {
          piyo = false;
        } else {
          if (c[i][j] == ch[col]) {
            c[i][j] = '.';
            st.push(make_pair(i, j));
          }
        }
      }
    }
    piyo &= dfs(ch, c, s + (1 << col));
    while (!st.empty()) {
      pair<int, int> val = st.top();
      st.pop();
      c[val.first][val.second] = ch[col];
    }
    isok |= piyo;
  }
  return dp[s] = isok;
}

int main() {
  int n;
  cin >> n;
  for (int loop = 0; loop < n; loop++) {
    dp.clear();

    cin >> h >> w;
    vector<string> c(h);
    for (auto &v : c)
      cin >> v;
    set<char> s;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (c[i][j] != '.')
          s.insert(c[i][j]);
      }
    }
    vector<char> ch(s.begin(), s.end());
    dp.resize((1 << ch.size()) + 1, -1);
    bool res = dfs(ch, c, 0);
    cout << (res ? "SAFE" : "SUSPICIOUS") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  for (int loop = 0; loop < n; loop++) {
    int h, w;
    cin >> h >> w;
    vector<string> c(h);
    for (auto &v : c)
      cin >> v;
    set<char> s;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        if (c[i][j] != '.')
          s.insert(c[i][j]);
      }
    }

    vector<char> ch(s.begin(), s.end());
    sort(ch.begin(), ch.end());
    bool res = (ch.size() == 0);
    do {
      auto cp = c;
      bool isok = true;
      for (int col = 0; col < ch.size(); col++) {
        int l = 100;
        int r = 0;
        int u = 100;
        int b = 0;
        for (int i = 0; i < h; i++) {
          for (int j = 0; j < w; j++) {
            if (cp[i][j] == ch[col]) {
              l = min(l, j);
              r = max(r, j);
              u = min(u, i);
              b = max(b, i);
            }
          }
        }
        b++, r++;
        for (int i = u; i < b; i++) {
          for (int j = l; j < r; j++) {
            if (cp[i][j] == '.') {
              isok = false;
            } else {
              if (cp[i][j] == ch[col]) {
                cp[i][j] = '.';
              }
            }
          }
        }
      }
      res |= isok;
    } while (next_permutation(ch.begin(), ch.end()));

    cout << (res ? "SAFE" : "SUSPICIOUS") << endl;
  }
  return 0;
}
Problem p01127 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define loop(n, i) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define HERE cout << "HERE: " << __LINE__ << endl;
#define INSP(v) cout << v << " at " << __LINE__ << endl;

using namespace std;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  while (n--) {
    int h, w;
    cin >> h >> w;
    vector<string> B(h);
    loop(h, i) cin >> B[i];

    unordered_set<char> alphs;
    loop(h, i) loop(w, j) {
      char c = B[i][j];
      if (c != '.')
        alphs.insert(c);
    }
    vector<char> perm(all(alphs));
    sort(all(perm));
    int safe = 0;
    do {
      map<char, int> layer = {{'.', 0}};
      loop(perm.size(), i) layer[perm[i]] = i + 1;

      for (char c : perm) {
        int l = 100, r = 0, t = 100, b = 0;
        loop(h, i) loop(w, j) if (B[i][j] == c) {
          l = min(j, l);
          r = max(j, r);
          t = min(i, t);
          b = max(i, b);
        }
        for (int i = t; i <= b; i++) {
          for (int j = l; j <= r; j++) {
            if (layer[B[i][j]] < layer[c])
              goto NG;
          }
        }
      }
      safe = 1;
      break;
    NG:
      continue;
    } while (next_permutation(all(perm)));
    if (safe)
      cout << "SAFE" << endl;
    else
      cout << "SUSPICIOUS" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define loop(n, i) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
#define HERE cout << "HERE: " << __LINE__ << endl;
#define INSP(v) cout << v << " at " << __LINE__ << endl;

using namespace std;
typedef long long ll;

int main() {
  int n;
  cin >> n;
  while (n--) {
    int w, h;
    cin >> w >> h;
    vector<string> B(h);
    loop(h, i) cin >> B[i];

    unordered_set<char> alphs;
    loop(h, i) loop(w, j) {
      char c = B[i][j];
      if (c != '.')
        alphs.insert(c);
    }
    vector<char> perm(all(alphs));
    sort(all(perm));
    int safe = 0;
    do {
      map<char, int> layer = {{'.', 0}};
      loop(perm.size(), i) layer[perm[i]] = i + 1;

      for (char c : perm) {
        int l = 100, r = 0, t = 100, b = 0;
        loop(h, i) loop(w, j) if (B[i][j] == c) {
          l = min(j, l);
          r = max(j, r);
          t = min(i, t);
          b = max(i, b);
        }
        for (int i = t; i <= b; i++) {
          for (int j = l; j <= r; j++) {
            if (layer[B[i][j]] < layer[c])
              goto NG;
          }
        }
      }
      safe = 1;
      break;
    NG:
      continue;
    } while (next_permutation(all(perm)));
    if (safe)
      cout << "SAFE" << endl;
    else
      cout << "SUSPICIOUS" << endl;
  }
  return 0;
}
Problem p01127 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#define REP(i, a, n) for (int i = ((int)a); i < ((int)n); i++)
using namespace std;

struct rect {
  int ly, ry, lx, rx;
};

int T, H, W;
char S[55][55];

int main(void) {
  cin >> T;
  while (T--) {
    cin >> H >> W;
    REP(i, 0, H) cin >> S[i];

    set<char> st;
    REP(i, 0, H) REP(j, 0, W) if (S[i][j] != '.') st.insert(S[i][j]);
    map<char, int> ctoi;
    int t = 0;
    for (char i : st)
      ctoi[i] = t++;

    int a[10];
    REP(i, 0, 10) a[i] = i;

    map<char, rect> p;
    REP(i, 0, H)
    REP(j, 0, W) if (S[i][j] != '.') p[S[i][j]] = (rect){i, i, j, j};
    REP(i, 0, H) REP(j, 0, W) {
      if (S[i][j] != '.') {
        p[S[i][j]].ly = min(p[S[i][j]].ly, i);
        p[S[i][j]].ry = max(p[S[i][j]].ry, i);
        p[S[i][j]].lx = min(p[S[i][j]].lx, j);
        p[S[i][j]].rx = max(p[S[i][j]].rx, j);
      }
    }

    bool ans = false;
    do {
      bool f = true;
      for (auto r : p) {
        char c = r.first;
        rect q = r.second;
        REP(i, q.ly, q.ry + 1) REP(j, q.lx, q.rx + 1) {
          if (S[i][j] == '.' || a[ctoi[c]] < a[ctoi[S[i][j]]]) {
            f = false;
            goto END;
          }
        }
      }
    END:
      ans = ans || f;
      if (ans)
        break;
    } while (next_permutation(a, a + (int)st.size()));
    cout << (ans ? "SAFE" : "SUSPICIOUS") << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#define REP(i, a, n) for (int i = ((int)a); i < ((int)n); i++)
using namespace std;

struct rect {
  int ly, ry, lx, rx;
};

int T, H, W;
char S[55][55];

int main(void) {
  cin >> T;
  while (T--) {
    cin >> H >> W;
    REP(i, 0, H) cin >> S[i];

    set<char> st;
    REP(i, 0, H) REP(j, 0, W) if (S[i][j] != '.') st.insert(S[i][j]);
    map<char, int> ctoi;
    for (char i : st)
      ctoi[i] = ctoi.size();

    int a[7];
    REP(i, 0, 7) a[i] = i;

    int ans = false;
    do {
      map<char, rect> p;
      REP(i, 0, H)
      REP(j, 0, W) if (S[i][j] != '.') p[S[i][j]] = (rect){i, i, j, j};

      REP(i, 0, H) REP(j, 0, W) {
        if (S[i][j] != '.') {
          p[S[i][j]].ly = min(p[S[i][j]].ly, i);
          p[S[i][j]].ry = max(p[S[i][j]].ry, i);
          p[S[i][j]].lx = min(p[S[i][j]].lx, j);
          p[S[i][j]].rx = max(p[S[i][j]].rx, j);
        }
      }

      bool f = true;
      for (pair<char, rect> r : p) {
        char c = r.first;
        rect q = r.second;
        REP(i, q.ly, q.ry + 1) REP(j, q.lx, q.rx + 1) {
          if (S[i][j] == '.' || a[ctoi[c]] < a[ctoi[S[i][j]]]) {
            f = false;
            goto END;
          }
        }
      END:;
      }
      ans = ans || f;
      if (ans)
        break;
    } while (next_permutation(a, a + st.size()));

    cout << (ans ? "SAFE" : "SUSPICIOUS") << endl;
  }
  return 0;
}