結果

問題 No.1436 Rgaph
ユーザー se1ka2se1ka2
提出日時 2021-03-20 00:51:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 158 ms / 2,000 ms
コード長 5,418 bytes
コンパイル時間 1,274 ms
コンパイル使用メモリ 96,988 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-01 05:49:31
合計ジャッジ時間 4,159 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 26 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 158 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 2 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 16 ms
5,376 KB
testcase_21 AC 11 ms
5,376 KB
testcase_22 AC 45 ms
5,376 KB
testcase_23 AC 32 ms
5,376 KB
testcase_24 AC 60 ms
5,376 KB
testcase_25 AC 85 ms
5,376 KB
testcase_26 AC 92 ms
5,376 KB
testcase_27 AC 85 ms
5,376 KB
testcase_28 AC 1 ms
5,376 KB
testcase_29 AC 29 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <queue>
using namespace std;

struct Graph
{
    int n;
    std::vector<std::vector<int>> g;
    
    Graph(){}
    
    Graph(int n) : n(n){
        g.resize(n);
    }
    
    void add_edge(int from, int to){
        g[from].push_back(to);
    }
};

template <typename T>
struct Edge
{
    int to;
    T cost;
};

template <typename T>
struct WeightedGraph
{
    int n;
    std::vector<std::vector<Edge<T>>> g;
    
    WeightedGraph(){}
    
    WeightedGraph(int n) : n(n){
        g.resize(n);
    }
    
    void add_edge(int from, int to, T cost){
        g[from].push_back((Edge<T>){to, cost});
    }
};

std::vector<int> bipartite_graph(Graph &g){
    int n = g.n;
    std::vector<int> b(n + 1, -1);
    b[n] = 0;
    std::queue<int> que;
    for(int i = 0; i < n; i++){
        if(b[i] != -1) continue;
        b[i] = 0;
        que.push(i);
        while(que.size()){
            int u = que.front();
            que.pop();
            for(int v : g.g[u]){
                if(b[u] == b[v]) return b;
                if(b[v] == -1){
                    b[v] = 1 - b[u];
                    que.push(v);
                }
            }
        }
    }
    b[n] = 1;
    return b;
}

struct SCC      //StronglyConnectedComponents
{
    int n;
    int k;
    std::vector<std::vector<int>> g, rg;
    std::vector<bool> used;
    std::vector<int> cmp;
    std::vector<int> vs;
    
    SCC(){}
    
    SCC(int n) : n(n){
        g.resize(n);
        rg.resize(n);
        used.resize(n);
        cmp.resize(n);
    }
    
    void add_edge(int from, int to){
        g[from].push_back(to);
        rg[to].push_back(from);
    }
    
    void dfs(int u){
        used[u] = true;
        for(int v : g[u]){
            if(!used[v]) dfs(v);
        }
        vs.push_back(u);
    }
    
    void rdfs(int u, int k){
        used[u] = true;
        cmp[u] = k;
        for(int v : rg[u]){
            if(!used[v]) rdfs(v, k);
        }
    }
    
    int decomposition(){
        for(int i = 0; i < n; i++) used[i] = false;
        for(int i = 0; i < n; i++){
            if(!used[i]) dfs(i);
        }
        for(int i = 0; i < n; i++) used[i] = false;
        k = 0;
        for(int i = n - 1; i >= 0; i--){
            if(!used[vs[i]]){
                rdfs(vs[i], k);
                k++;
            }
        }
        return k;
    }
    
/*
    int decomposition(Graph &dag){
        k = decomposition();
        dag.n = k;
        dag.g.resize(k);
        std::map<std::pair<int, int>, bool> mp;
        for(int u = 0; u < n; u++){
            for(int v : g[u]){
                if(!mp[std::pair<int, int> (cmp[u], cmp[v])] && cmp[u] != cmp[v]){
                    mp[std::pair<int, int> (cmp[u], cmp[v])] = true;
                    dag.add_edge(cmp[u], cmp[v]);
                }
            }
        }
        return k;
    }
*/
};

int main()
{
    int n, m;
    cin >> n >> m;
    Graph g0(n);
    WeightedGraph<int> g[2];
    g[0].g.resize(n);
    g[1].g.resize(n);
    SCC g1(n);
    int a[2002], b[2002];
    for(int i = 0; i < m; i++){
        cin >> a[i] >> b[i];
        a[i]--; b[i]--;
        g0.add_edge(a[i], b[i]);
        g[0].add_edge(a[i], b[i], i);
        g[1].add_edge(b[i], a[i], i);
        g1.add_edge(a[i], b[i]);
    }
    int k = g1.decomposition();
    if(k > 1){
        cout << -1 << endl;
        return 0;
    }
    vector<int> z = bipartite_graph(g0);
    if(z[n]){
        cout << -1 << endl;
        return 0;
    }
    vector<int> loop;
    int s = n;
    for(int r = 0; r < n; r++){
        int d[2][1002];
        Edge<int> p[2][1002];
        queue<int> que[2];
        for(int c = 0; c < 2; c++){
            for(int u = 0; u < n; u++) d[c][u] = -1;
            d[c][r] = 0;
            que[c].push(r);
        }
        while(que[0].size() && que[1].size()){
            for(int c = 0; c < 2; c++){
                if(que[c].empty()) continue;
                int u = que[c].front();
                que[c].pop();
                for(Edge<int> e : g[c].g[u]){
                    int v = e.to;
                    if(d[c][v] == -1){
                        d[c][v] = d[c][u] + 1;
                        p[c][v] = Edge<int>{u, e.cost};
                        que[c].push(v);
                    }
                    if(d[1 - c][v] != -1){
                        if(d[0][v] + d[1][v] < s && v != r){
                            loop.clear();
                            s = d[0][v] + d[1][v];
                            for(int i = 0; i < 2; i++){
                                int w = v;
                                while(w != r){
                                    loop.push_back(p[i][w].cost);
                                    w = p[i][w].to;
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if(s == n){
        cout << -1 << endl;
        return 0;
    }
    int l;
    for(l = 0; l < s; l++){
        int j = loop[l];
        SCC g2(n);
        for(int i = 0; i < m; i++){
            if(i != j) g2.add_edge(a[i], b[i]);
        }
        int k = g2.decomposition();
        if(k < n) break;
    }
    int ans[2002]{0};
    for(int i = 0; i <= s / 2; i++) ans[loop[(i + l) % s]] = 1;
    for(int i = 0; i < m; i++){
        if(ans[i]) cout << 'R';
        else cout << 'G';
    }
    cout << endl;
}
0