結果

問題 No.1436 Rgaph
ユーザー se1ka2se1ka2
提出日時 2021-03-19 23:42:22
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,809 bytes
コンパイル時間 1,421 ms
コンパイル使用メモリ 94,208 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-29 19:40:20
合計ジャッジ時間 4,858 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
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 32 ms
5,376 KB
testcase_07 WA -
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 WA -
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 3 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 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 57 ms
5,376 KB
testcase_25 AC 85 ms
5,376 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
権限があれば一括ダウンロードができます

ソースコード

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(n * 2);
    SCC h(n);
    for(int i = 0; i < m; i++){
        int a, b;
        cin >> a >> b;
        a--; b--;
        g0.add_edge(a, b);
        g.add_edge(a, b + n, i);
        g.add_edge(a + n, b, i);
        h.add_edge(a, b);
    }
    int k = h.decomposition();
    if(k > 1){
        cout << -1 << endl;
        return 0;
    }
    vector<int> b = bipartite_graph(g0);
    if(b[n]){
        cout << -1 << endl;
        return 0;
    }
    vector<int> loop;
    int s = n + 1;
    for(int r = 0; r < n; r++){
        int d[2002];
        Edge<int> p[2002];
        for(int u = 0; u < n * 2; u++) d[u] = -1;
        queue<int> que;
        d[r] = 0;
        que.push(r);
        while(que.size()){
            int u = que.front();
            que.pop();
            for(Edge<int> e : g.g[u]){
                int v = e.to;
                if(d[v] == -1){
                    d[v] = d[u] + 1;
                    p[v] = Edge<int>{u, e.cost};
                    que.push(v);
                }
                if(d[(v + n) % (n * 2)] != -1){
                    if(d[v] + d[(v + n) % (n * 2)] < s){
                        loop.clear();
                        s = d[v] + d[(v + n) % (n * 2)];
                        for(int c = 0; c < 2; c++){
                            int w = (v + n * c) % (n * 2);
                            while(w != r){
                                loop.push_back(p[w].cost);
                                w = p[w].to;
                            }
                        }
                    }
                }
            }
        }
    }
    if(s == n){
        cout << -1 << endl;
        return 0;
    }
    int ans[2002]{0};
    for(int i = 0; i <= s / 2; i++) ans[loop[i]] = 1;
    for(int i = 0; i < m; i++){
        if(ans[i]) cout << 'R';
        else cout << 'G';
    }
    cout << endl;
}
0