結果

問題 No.3056 Disconnected Coloring
ユーザー anago-pie
提出日時 2025-03-26 13:06:32
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 723 ms / 2,000 ms
コード長 5,860 bytes
コンパイル時間 4,435 ms
コンパイル使用メモリ 300,452 KB
実行使用メモリ 57,488 KB
最終ジャッジ日時 2025-03-26 13:06:57
合計ジャッジ時間 24,924 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i=0; i<n; i++)
#define debug 0
#define YES cout << "Yes" << endl;
#define NO cout << "No" << endl;
using ll = long long;
using ld = long double;
const int mod = 998244353;
const int MOD = 1000000007;
const double pi = atan2(0, -1);
const int inf = 1 << 31 - 1;
const ll INF = 1LL << 63 - 1;
#include <time.h>
#include <chrono>

//vectorの中身を空白区切りで出力
template<typename T>
void printv(vector<T> v) {
	for (int i = 0; i < v.size(); i++) {
		cout << v[i];
		if (i < v.size() - 1) {
			cout << " ";
		}
	}
	cout << endl;
}

//vectorの中身を改行区切りで出力
template<typename T>
void print1(vector<T> v) {
	for (auto x : v) {
		cout << x << endl;
	}
}

//二次元配列を出力
template<typename T>
void printvv(vector<vector<T>> vv) {
	for (vector<T> v : vv) {
		printv(v);
	}
}

//vectorを降順にソート
template<typename T>
void rsort(vector<T>& v) {
	sort(v.begin(), v.end());
	reverse(v.begin(), v.end());
}

//昇順priority_queueを召喚
template<typename T>
struct rpriority_queue {
	priority_queue<T, vector<T>, greater<T>> pq;

	void push(T x) {
		pq.push(x);
	}

	void pop() {
		pq.pop();
	}

	T top() {
		return pq.top();
	}

	size_t size() {
		return pq.size();
	}

	bool empty() {
		return pq.empty();
	}
};

//mod mod下で逆元を算出する
//高速a^n計算(mod ver.)
ll power(ll a, ll n) {
	if (n == 0) {
		return 1;
	}
	else if (n % 2 == 0) {
		ll x = power(a, n / 2);
		x *= x;
		x %= mod;
		return x;
	}
	else {
		ll x = power(a, n - 1);
		x *= a;
		x %= mod;
		return x;
	}
}
//フェルマーの小定理を利用
ll modinv(ll p) {
	return power(p, mod - 2) % mod;
}

//Mexを求める
struct Mex {
	map<int, int> mp;
	set<int> s;
	Mex(int Max) {
		for (int i = 0; i <= Max; i++) {
			s.insert(i);
		}
	}

	int _mex = 0;
	void Input(int x) {
		mp[x]++;
		s.erase(x);
		if (_mex == x) {
			_mex = *begin(s);
		}
	}

	void Remove(int x) {
		if (mp[x] == 0) {
			cout << "Mex ERROR!: NO VALUE WILL BE REMOVED" << endl;
		}
		mp[x]--;
		if (mp[x] == 0) {
			s.insert(x);
			if (*begin(s) == x) {
				_mex = x;
			}
		}
	}

	int mex() {
		return _mex;
	}
};

//条件分岐でYes/Noを出力するタイプのやつ
void YN(bool true_or_false) {
	cout << (true_or_false ? "Yes" : "No") << endl;
}

//最大公約数(ユークリッドの互除法)
ll gcd(ll a, ll b) {
	if (b > a) {
		swap(a, b);
	}
	while (a % b != 0) {
		ll t = a;
		a = b;
		b = t % b;
	}
	return b;
}

//最小公倍数(gcdを定義しておく)
ll lcm(ll a, ll b) {
	ll g = gcd(a, b);
	ll x = (a / g) * b;
	return x;
}

struct UnionFind {
	vector<int> par;
	UnionFind(int N) {
		rep(i, N) {
			par.push_back(i);
		}
	}

	int root(int x){
		if (par[x] == x) {
			return x;
		}
		else {
			return par[x] = root(par[x]);
		}
	}

	bool isSame(int x, int y) {
		return root(x) == root(y);
	}

	void Union(int x, int y) {
		if (!isSame(x, y)) {
			int rx = root(x), ry = root(y);
			if (rx > ry) {
				par[rx] = ry;
			}
			else {
				par[ry] = rx;
			}
		}
	}
};

struct mUnionFind {
	vector<int> par;
	mUnionFind(int N) {
		rep(i, N) {
			par.push_back(i);
		}
	}

	int root(int x) {
		if (par[x] == x) {
			return x;
		}
		else {
			return par[x] = root(par[x]);
		}
	}

	bool isSame(int x, int y) {
		return root(x) == root(y);
	}

	void Union(int x, int y) {
		if (!isSame(x, y)) {
			int rx = root(x), ry = root(y);
			if (rx > ry) {
				par[rx] = ry;
			}
			else {
				par[ry] = rx;
			}
		}
	}
};

struct MUnionFind {
	vector<int> par;
	MUnionFind(int N) {
		rep(i, N) {
			par.push_back(i);
		}
	}

	int root(int x) {
		if (par[x] == x) {
			return x;
		}
		else {
			return par[x] = root(par[x]);
		}
	}

	bool isSame(int x, int y) {
		return root(x) == root(y);
	}

	void Union(int x, int y) {
		if (!isSame(x, y)) {
			int rx = root(x), ry = root(y);
			if (rx < ry) {
				par[rx] = ry;
			}
			else {
				par[ry] = rx;
			}
		}
	}
};


int main() {
	int N, M;
	cin >> N >> M;
	if (M % 2 == 1) {
		cout << -1 << endl;
		return 0;
	}
	mUnionFind muf(N);
	MUnionFind Muf(N);
	vector<vector<int>> edge(M);
	vector<set<int>> path(N);
	rep(i, M) {
		int u, v;
		cin >> u >> v;
		u--;
		v--;
		edge[i] = { u,v };
		path[u].insert(v);
		path[v].insert(u);
	}
	if (path[0].count(N - 1)) {
		cout << -1 << endl;
		return 0;
	}
	set<int> connect1, connectN;

	for (int x : path[0]) {
		connect1.insert(x);
	}
	for (int x : path[N - 1]) {
		connectN.insert(x);
	}

	{
		queue<int> q;
		vector<bool> check(N, false);
		for (int x : connect1) {
			q.push(x);
			while (!q.empty()) {
				int now = q.front();
				q.pop();
				for (int x : path[now]) {
					if (x != 0) {
						Muf.Union(x, now);
						if (x != N - 1 && !check[x]) {
							q.push(x);
							check[x] = true;
						}
					}
				}
			}
		}
	}

	{
		queue<int> q;
		vector<bool> check(N, false);
		for (int x : connectN) {
			q.push(x);
			while (!q.empty()) {
				int now = q.front();
				q.pop();
				for (int x : path[now]) {
					if (x != N-1) {
						muf.Union(x, now);
						if (x != 0 && !check[x]) {
							q.push(x);
							check[x] = true;
						}
					}
				}
			}
		}
	}

	set<int> use_connect1, use_connectN;
	for (int x : connect1) {
		if (Muf.isSame(x, N - 1)) {
			use_connect1.insert(x);
		}
	}
	for (int x : connectN) {
		if (muf.isSame(x, 0)) {
			use_connectN.insert(x);
		}
	}

	int r = M / 2, b = M / 2;
	string ans = "";
	rep(i, M) {
		ans += "*";
	}
	rep(i, M) {
		int u = edge[i][0], v = edge[i][1];
		if (u > v) {
			swap(u, v);
		}

		if (u == 0) {
			if (use_connect1.count(v)) {
				ans[i] = 'R';
				r--;
			}
		}
		if (v == N - 1) {
			if (use_connectN.count(u)) {
				ans[i] = 'B';
				b--;
			}
		}
	}

	rep(i, M) {
		if (ans[i] == '*') {
			if (r > 0) {
				ans[i] = 'R';
				r--;
			}
			else {
				ans[i] = 'B';
				b--;
			}
		}
	}

	cout << ans << endl;
}

0