結果

問題 No.3056 Disconnected Coloring
ユーザー Kude
提出日時 2025-03-14 21:51:42
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 46 ms / 2,000 ms
コード長 2,003 bytes
コンパイル時間 4,191 ms
コンパイル使用メモリ 301,228 KB
実行使用メモリ 7,324 KB
最終ジャッジ日時 2025-03-14 21:52:13
合計ジャッジ時間 8,528 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
namespace {
#pragma GCC diagnostic ignored "-Wunused-function"
#include<atcoder/all>
#pragma GCC diagnostic warning "-Wunused-function"
using namespace std;
using namespace atcoder;
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--)
#define all(x) begin(x), end(x)
#define rall(x) rbegin(x), rend(x)
template<class T> bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; }
template<class T> bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; }
using ll = long long;
using P = pair<int,int>;
using VI = vector<int>;
using VVI = vector<VI>;
using VL = vector<ll>;
using VVL = vector<VL>;

} int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, m;
  cin >> n >> m;
  struct E {
    int u, v, i;
  };
  vector<E> es(m);
  rep(i, m) cin >> es[i].u >> es[i].v, es[i].u--, es[i].v--, es[i].i = i;
  string ans(m, '.');
  VI deg(n);
  bool ok = m % 2 == 0;
  for (auto& [u, v, i] : es) {
    if (u > v) swap(u, v);
    ok &= u != 0 || v != n - 1;
    deg[u]++, deg[v]++;
  }
  if (!ok) {
    cout << -1 << '\n';
    return 0;
  }
  int s = 0, t = n - 1;
  if (deg[s] > deg[t]) swap(s, t);
  int cr = m / 2, cb = m / 2;
  if (deg[t] <= m / 2) {
    for (auto [u, v, i] : es) {
      if (u == 0) {
        ans[i] = 'R';
        cr--;
      } else if (v == n - 1) {
        ans[i] = 'B';
        cb--;
      }
    }
    rep(i, m) if (ans[i] == '.') {
      if (cr) ans[i] = 'R', cr--;
      else ans[i] = 'B', cb--;
    }
  } else {
    vector<char> visited(n);
    for (auto [u, v, i] : es) {
      if (u == s || v == s) {
        ans[i] = 'R';
        cr--;
        visited[u] = visited[v] = true;
      }
    }
    for (auto [u, v, i] : es) {
      if ((u == t || v == t) && !visited[u ^ v ^ t] && cr) {
        ans[i] = 'R';
        cr--;
      }
    }
    assert(cr == 0);
    rep(i, m) if (ans[i] == '.') ans[i] = 'B';
  }
  cout << ans << '\n';
}
0