結果

問題 No.1436 Rgaph
ユーザー 👑 kmjpkmjp
提出日時 2021-03-19 23:43:35
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,639 bytes
コンパイル時間 2,546 ms
コンパイル使用メモリ 213,508 KB
実行使用メモリ 11,648 KB
最終ジャッジ日時 2024-04-29 19:42:09
合計ジャッジ時間 5,861 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 AC 4 ms
7,424 KB
testcase_02 AC 3 ms
7,424 KB
testcase_03 AC 4 ms
7,424 KB
testcase_04 AC 3 ms
7,552 KB
testcase_05 WA -
testcase_06 AC 19 ms
7,680 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 18 ms
7,680 KB
testcase_12 AC 16 ms
7,680 KB
testcase_13 AC 4 ms
7,552 KB
testcase_14 AC 18 ms
7,680 KB
testcase_15 AC 11 ms
7,552 KB
testcase_16 AC 17 ms
7,552 KB
testcase_17 AC 4 ms
7,552 KB
testcase_18 AC 3 ms
7,552 KB
testcase_19 AC 5 ms
7,552 KB
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define FORR2(x,y,arr) for(auto& [x,y]:arr)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
template<class T> bool chmax(T &a, const T &b) { if(a<b){a=b;return 1;}return 0;}
template<class T> bool chmin(T &a, const T &b) { if(a>b){a=b;return 1;}return 0;}
//-------------------------------------------------------


int N,M;
vector<int> E[2][1010];
int ok[2020][2];
string S;

int id[1010][1010];
int cnt[1010][1010];

vector<int> R[2];
int vis[1010];
vector<int> path;

void dfs(int cur,int start,int depth) {
	if(cur==start&&depth%2==1) {
		if(R[1].empty()) {
			int i;
			vector<int> T=path;
			int tar=0;
			FOR(i,T.size()) if(T[i]<T[tar]) tar=i;
			rotate(T.begin(),T.begin()+tar,T.end());
			if(T!=R[0]&&T!=R[1]) {
				R[1]=T;
				if(R[0].empty()) swap(R[0],R[1]);
			}
			
		}
		return;
	}
	if(vis[cur]!=-1) return;
	vis[cur]=depth;
	path.push_back(cur);
	FORR(e,E[0][cur]) dfs(e,start,depth+1);
	path.pop_back();
	vis[cur]=-2;
}

void solve() {
	int i,j,k,l,r,x,y; string s;
	
	cin>>N>>M;
	MINUS(id);
	FOR(i,M) {
		cin>>x>>y;
		x--,y--;
		E[0][x].push_back(y);
		E[1][y].push_back(x);
		id[x][y]=i;
	}
	S=string(M,'R');
	ok[0][0]=ok[0][1]=1;
	queue<int> Q;
	Q.push(0);
	Q.push(1);
	
	while(Q.size()) {
		int cur=Q.front()/2;
		int id=Q.front()%2;
		Q.pop();
		FORR(e,E[id][cur]) if(ok[e][id]==0) {
			ok[e][id]=1;
			Q.push(e*2+id);
		}
	}
	FOR(i,N) if(ok[i][0]==0||ok[i][1]==0) return _P("-1\n");
	FOR(i,N) {
		MINUS(vis);
		dfs(i,i,0);
	}
	if(R[1].empty()) return _P("-1\n");
	if(R[0].size()<R[1].size()) swap(R[0],R[1]);
	R[0].push_back(R[0][0]);
	R[1].push_back(R[1][0]);
	FOR(i,R[0].size()-1) cnt[R[0][i]][R[0][i+1]]++;
	FOR(i,R[1].size()-1) cnt[R[1][i]][R[1][i+1]]++;
	
	int c=0;
	FOR(x,N) FOR(y,N) if(cnt[x][y]==2) {
		if(c==0) S[id[x][y]]='R';
		else S[id[x][y]]='B';
		c^=1;
	}
	x=c;
	FOR(i,R[0].size()-1) if(cnt[R[0][i]][R[0][i+1]]==1) {
		if(x>-1) S[id[R[0][i]][R[0][i+1]]]='B', x--;
		else S[id[R[0][i]][R[0][i+1]]]='R', x++;
	}
	assert(x==-1);
	x=c;
	FOR(i,R[1].size()-1) if(cnt[R[1][i]][R[1][i+1]]==1) {
		if(x>0) S[id[R[1][i]][R[1][i+1]]]='B', x--;
		else S[id[R[1][i]][R[1][i+1]]]='R', x++;
	}
	assert(x==1);
	cout<<S<<endl;
	
}


int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}
0