結果

問題 No.2148 ひとりUNO
ユーザー ei1333333ei1333333
提出日時 2022-12-03 13:56:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 35 ms / 2,000 ms
コード長 1,987 bytes
コンパイル時間 2,219 ms
コンパイル使用メモリ 204,956 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 04:59:15
合計ジャッジ時間 3,806 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 6 ms
5,376 KB
testcase_02 AC 5 ms
5,376 KB
testcase_03 AC 6 ms
5,376 KB
testcase_04 AC 6 ms
5,376 KB
testcase_05 AC 5 ms
5,376 KB
testcase_06 AC 5 ms
5,376 KB
testcase_07 AC 5 ms
5,376 KB
testcase_08 AC 5 ms
5,376 KB
testcase_09 AC 5 ms
5,376 KB
testcase_10 AC 5 ms
5,376 KB
testcase_11 AC 6 ms
5,376 KB
testcase_12 AC 5 ms
5,376 KB
testcase_13 AC 6 ms
5,376 KB
testcase_14 AC 6 ms
5,376 KB
testcase_15 AC 6 ms
5,376 KB
testcase_16 AC 34 ms
5,376 KB
testcase_17 AC 35 ms
5,376 KB
testcase_18 AC 29 ms
5,376 KB
testcase_19 AC 31 ms
5,376 KB
testcase_20 AC 30 ms
5,376 KB
testcase_21 AC 32 ms
5,376 KB
testcase_22 AC 30 ms
5,376 KB
testcase_23 AC 28 ms
5,376 KB
testcase_24 AC 30 ms
5,376 KB
testcase_25 AC 30 ms
5,376 KB
testcase_26 AC 14 ms
5,376 KB
testcase_27 AC 15 ms
5,376 KB
testcase_28 AC 15 ms
5,376 KB
testcase_29 AC 16 ms
5,376 KB
testcase_30 AC 14 ms
5,376 KB
testcase_31 AC 14 ms
5,376 KB
testcase_32 AC 14 ms
5,376 KB
testcase_33 AC 14 ms
5,376 KB
testcase_34 AC 14 ms
5,376 KB
testcase_35 AC 15 ms
5,376 KB
testcase_36 AC 16 ms
5,376 KB
testcase_37 AC 17 ms
5,376 KB
testcase_38 AC 17 ms
5,376 KB
testcase_39 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

const string colors = "RGBY";

void test(int t) {
  int N;
  cin >> N;
  vector< int > C(N), D(N);
  for (int i = 0; i < N; i++) {
    char c;
    cin >> c >> D[i];
    C[i] = colors.find(c);
  }
  vector< int > cnt(1 << 4);
  {
    map< int, int > mp;
    for (int i = 0; i < N; i++) {
      mp[D[i]] |= 1 << C[i];
    }
    for (auto &p: mp) {
      cnt[p.second]++;
    }
  }
  for(int i = 0; i < 4; i++) {
    cnt[1 << i] = min(cnt[1 << i], 1);
  }
  auto get_only = [&](int bit) {
    if(bit == 1 or bit == 2 or bit == 4 or bit == 8) return bit;
    return 0;
  };
  auto end = [&](int only)-> bool {
    for(int i = 0; i < 4; i++) {
      if((~only >> i) & 1) {
        for(int j = 0; j < (1 << 4); j++) {
          if(cnt[j] > 0 and ((j >> i) & 1)) {
            return false;
          }
        }
      }
    }
    return true;
  };
  auto dfs = [&](auto &dfs, int depth, int now, int last, int only) -> bool {
    if(last) cnt[last]++;
    if(end(only)) return true;
    if(last) cnt[last]--;
    if(depth >= 11) return false;
    for(int i = 0; i < 4; i++) {
      if((last >> i) & 1) {
        if(dfs(dfs, depth + 1, i, last ^ (1 << i), only)) {
          return true;
        }
      }
    }
    if(last) cnt[last]++;
    for(int i = 1; i < (1 << 4); i++) {
      if(cnt[i] > 0 and ((i >> now) & 1)) {
        cnt[i]--;
        if(dfs(dfs, depth + 1, now, i ^ (1 << now), only | get_only(i))) {
          return true;
        }
        cnt[i]++;
      }
    }
    if(last) cnt[last]--;
    return false;
  };
  for(int i = 1; i < (1 << 4); i++) {
    if(cnt[i] > 0) {
      cnt[i]--;
      for(int j = 0; j < 4; j++) {
        if((i >> j) & 1) {
          if (dfs(dfs, 1, j, i ^ (1 << j), get_only(i))) {
            cout << "YES" << endl;
            return;
          }
        }
      }
      cnt[i]++;
    }
  }
  cout << "NO" << endl;
}

int main() {
  int T;
  cin >> T;
  for(int t = 1; t <= T; t++) {
    test(t);
  }
}
0