結果

問題 No.2148 ひとりUNO
ユーザー tnakao0123tnakao0123
提出日時 2022-12-08 19:24:54
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,857 bytes
コンパイル時間 455 ms
コンパイル使用メモリ 55,520 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-22 17:02:46
合計ジャッジ時間 2,508 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2148.cc:  No.2148 ひとりUNO - yukicoder
 */

#include<cstdio>
#include<vector>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 200000 + 3;

enum { B, G, R };

/* typedef */

struct UFT {
  vector<int> links, ranks, sizes;
  UFT() {}

  void init(int n) {
    links.resize(n);
    for (int i = 0; i < n; i++) links[i] = i;
    ranks.assign(n, 1);
    sizes.assign(n, 1);
  }

  int root(int i) {
    int i0 = i;
    while (links[i0] != i0) i0 = links[i0];
    return (links[i] = i0);
  }

  int rank(int i) { return ranks[root(i)]; }
  int size(int i) { return sizes[root(i)]; }
  bool same(int i, int j) { return root(i) == root(j); }

  int merge(int i0, int i1) {
    int r0 = root(i0), r1 = root(i1), mr;
    if (r0 == r1) return r0;
    if (ranks[r0] == ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      ranks[r0]++;
      mr = r0;
    }
    else if (ranks[r0] > ranks[r1]) {
      links[r1] = r0;
      sizes[r0] += sizes[r1];
      mr = r0;
    }
    else {
      links[r0] = r1;
      sizes[r1] += sizes[r0];
      mr = r1;
    }
    return mr;
  }
};

/* global variables */

int gids[MAX_N];
UFT uft;

/* subroutines */

inline int c2i(char c) {
  return (c == 'B') ? B : (c == 'G') ? G : (c == 'R') ? R : -1;
}

inline int allocid(int x, int &gn) {
  return (gids[x] >= 0) ? gids[x] : (gids[x] = gn++);
}

/* main */

int main() {
  int tn;
  scanf("%d", &tn);

  while (tn--) {
    int n;
    scanf("%d", &n);

    int gn = 0;
    fill(gids, gids + n + 3, -1);
    uft.init(n + 3);

    for (int i = 0; i < n; i++) {
      char s[4];
      int d;
      scanf("%s%d", s, &d);

      int u = allocid(c2i(s[0]) + n, gn);
      int v = allocid(d - 1, gn);
      uft.merge(u, v);
    }

    if (uft.size(0) == gn) puts("YES");
    else puts("NO");
  }
  return 0;
}
0