結果

問題 No.1436 Rgaph
ユーザー chocoruskchocorusk
提出日時 2021-03-19 23:35:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 4,168 bytes
コンパイル時間 4,023 ms
コンパイル使用メモリ 200,692 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-29 19:30:31
合計ジャッジ時間 9,861 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

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;
    assert(m>20);
    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;
    int mn=INF;
    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]<mn){
            mn=d[1][i];
            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;
}
0