結果

問題 No.1436 Rgaph
ユーザー chocorusk
提出日時 2021-03-19 23:57:04
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 21 ms / 2,000 ms
コード長 4,111 bytes
コンパイル時間 3,700 ms
コンパイル使用メモリ 192,768 KB
最終ジャッジ日時 2025-01-19 19:49:06
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 28
権限があれば一括ダウンロードができます

ソースコード

diff #
プレゼンテーションモードにする

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#include <atcoder/all>
#define popcount __builtin_popcount
using namespace std;
using namespace atcoder;
typedef long long ll;
typedef pair<int, int> P;
struct SCC{
vector<vector<int>> g, gr;
int n, k;
vector<int> cmp, vs;
vector<bool> used;
void dfs(int x){
used[x]=1;
for(auto y:g[x]){
if(!used[y]) dfs(y);
}
vs.push_back(x);
}
void rdfs(int v, int k){
used[v]=1;
cmp[v]=k;
for(auto y:gr[v]){
if(!used[y]) rdfs(y, k);
}
}
SCC(const vector<vector<int>> &g):g(g), n(g.size()), cmp(n), used(n){
gr.resize(n);
for(int x=0; x<n; x++) for(auto y:g[x]) gr[y].push_back(x);
for(int i=0; i<n; i++){
if(!used[i]) dfs(i);
}
fill(used.begin(), used.end(), 0);
k=0;
for(int i=vs.size()-1; i>=0; i--){
if(!used[vs[i]]) rdfs(vs[i], k++);
}
}
};
int main()
{
int n, m; cin>>n>>m;
vector<P> g[1010], gr[1010];
vector<vector<int>> g1(n);
int a[2020], b[2020];
for(int i=0; i<m; i++){
cin>>a[i]>>b[i];
a[i]--; b[i]--;
g[a[i]].push_back({b[i],i});
gr[b[i]].push_back({a[i], i});
g1[a[i]].push_back(b[i]);
}
SCC scc(g1);
if(scc.k>1){
cout<<-1<<endl;
return 0;
}
if(n==m){
cout<<-1<<endl;
return 0;
}
int ans[2020]={};
const int INF=1e9+7;
for(int i=0; i<n; i++){
int d[2][1010];
fill(d[0], d[0]+n, INF);
fill(d[1], d[1]+n, INF);
queue<P> que;
que.push({i, 0});
d[0][i]=0;
while(!que.empty()){
P p=que.front();que.pop();
int x=p.first, t=p.second;
for(auto q:g[x]){
int y=q.first;
if(d[t^1][y]>d[t][x]+1){
d[t^1][y]=d[t][x]+1;
que.push({y, t^1});
}
}
}
if(d[1][i]<INF){
fill(ans, ans+m, 0);
P p=P(i, 1);
int hoge=1;
while(p!=P(i, 0)){
int x=p.first;
for(auto q:gr[x]){
if(d[p.second^1][q.first]==d[p.second][x]-1){
ans[q.second]=hoge;
hoge^=1;
p=P(q.first, p.second^1);
break;
}
}
}
bool ok=0;
int d1[1010];
fill(d1, d1+n, INF);
d1[0]=0;
for(int j=0; j<n; j++){
for(int x=0; x<n; x++){
for(auto q:g[x]){
int y=q.first;
int e=-1;
if(ans[q.second]) e=1;
if(d1[y]>d1[x]+e){
d1[y]=d1[x]+e;
}
}
}
}
for(int j=0; j<n; j++){
for(int x=0; x<n; x++){
for(auto q:g[x]){
int y=q.first;
int e=-1;
if(ans[q.second]) e=1;
if(d1[y]>d1[x]+e){
ok=1;break;
}
}
}
if(ok)break;
}
if(ok){
for(int i=0; i<m; i++){
if(ans[i]) cout<<'R';
else cout<<'G';
}
cout<<endl;
return 0;
}
}
}
cout<<-1<<endl;
return 0;
}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
0