結果

問題 No.3056 Disconnected Coloring
ユーザー GOTKAKO
提出日時 2025-03-14 21:37:33
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 406 ms / 2,000 ms
コード長 1,793 bytes
コンパイル時間 3,006 ms
コンパイル使用メモリ 209,544 KB
実行使用メモリ 27,960 KB
最終ジャッジ日時 2025-03-14 21:37:45
合計ジャッジ時間 11,228 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 34
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

vector<long long> dijkstra(vector<vector<pair<int,long long>>> &Graph,int start){
int N = Graph.size();
//O((V+E)logV) 一般最短経路魔法.
long long inf = 3e18;
    vector<bool> used(N);
    vector<long long> ret(N,inf);
    priority_queue<pair<long long,int>,vector<pair<long long,int>>,greater<pair<long long,int>>> Q;
    ret.at(start) = 0; Q.push({0,start});
    while(Q.size()){
        auto[nowd,pos] = Q.top(); Q.pop();
        if(start+pos == N-1) continue;
        if(used.at(pos)) continue;
        used.at(pos) = true;
        for(auto [to,w] : Graph.at(pos)){
            if(ret.at(to) > nowd+w){
                ret.at(to) = nowd+w;
                Q.push({ret.at(to),to});
            }
        }
    }
    return ret;
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);

    int N,M; cin >> N >> M;
    vector<vector<pair<int,long long>>> Graph(N);
    for(int i=0; i<M; i++){
        int u,v; cin >> u >> v;
        u--; v--;
        if(u == 0 && v == N-1){cout << "-1\n"; return 0;}
        Graph.at(u).push_back({v,i});
        Graph.at(v).push_back({u,i});
    }
    if(M%2){cout << "-1\n"; return 0;}

    auto dist0 = dijkstra(Graph,0),distN = dijkstra(Graph,N-1);
    vector<char> answer(M,'-');
    int r = M/2,b = r;
    for(auto [to,pos] : Graph.at(0)){
        if(distN.at(to) >= 1e18) continue;
        r--; answer.at(pos) = 'R';
    }
    for(auto [to,pos] : Graph.at(N-1)){
        if(dist0.at(to) >= 1e18) continue;
        b--; answer.at(pos) = 'B';
    }
    if(r < 0 || b < 0){cout << "-1\n"; return 0;}

    for(auto c : answer){
        if(c == '-'){
            if(r) r--,cout << "R";
            else b--,cout << "B";
        }
        else cout << c;
    }
    cout << endl;
}  
0