結果

問題 No.1436 Rgaph
ユーザー chocoruskchocorusk
提出日時 2021-03-19 22:51:11
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,125 bytes
コンパイル時間 3,890 ms
コンパイル使用メモリ 199,148 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 18:15:03
合計ジャッジ時間 6,743 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 3 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 AC 17 ms
5,376 KB
testcase_12 AC 17 ms
5,376 KB
testcase_13 AC 2 ms
5,376 KB
testcase_14 AC 20 ms
5,376 KB
testcase_15 AC 11 ms
5,376 KB
testcase_16 AC 17 ms
5,376 KB
testcase_17 AC 4 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 4 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 3 ms
5,376 KB
testcase_23 AC 3 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 4 ms
5,376 KB
testcase_26 AC 4 ms
5,376 KB
testcase_27 AC 4 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 4 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;
    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 c[1010]={};
    bool ok=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){
            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;
                    }
                }
            }
            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