結果

問題 No.2914 正閉路検出
ユーザー umimelumimel
提出日時 2024-10-04 22:57:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 87 ms / 2,000 ms
コード長 4,296 bytes
コンパイル時間 1,779 ms
コンパイル使用メモリ 188,316 KB
実行使用メモリ 32,480 KB
最終ジャッジ日時 2024-10-04 22:57:28
合計ジャッジ時間 6,700 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,820 KB
testcase_01 AC 2 ms
6,816 KB
testcase_02 AC 1 ms
6,816 KB
testcase_03 AC 2 ms
6,816 KB
testcase_04 AC 1 ms
6,816 KB
testcase_05 AC 1 ms
6,816 KB
testcase_06 AC 1 ms
6,820 KB
testcase_07 AC 2 ms
6,816 KB
testcase_08 AC 1 ms
6,816 KB
testcase_09 AC 2 ms
6,820 KB
testcase_10 AC 1 ms
6,816 KB
testcase_11 AC 2 ms
6,820 KB
testcase_12 AC 2 ms
6,820 KB
testcase_13 AC 1 ms
6,816 KB
testcase_14 AC 1 ms
6,820 KB
testcase_15 AC 1 ms
6,816 KB
testcase_16 AC 2 ms
6,816 KB
testcase_17 AC 2 ms
6,820 KB
testcase_18 AC 1 ms
6,820 KB
testcase_19 AC 11 ms
7,164 KB
testcase_20 AC 23 ms
12,796 KB
testcase_21 AC 7 ms
6,820 KB
testcase_22 AC 29 ms
16,880 KB
testcase_23 AC 20 ms
11,788 KB
testcase_24 AC 32 ms
18,640 KB
testcase_25 AC 3 ms
6,816 KB
testcase_26 AC 6 ms
6,816 KB
testcase_27 AC 39 ms
18,820 KB
testcase_28 AC 48 ms
20,204 KB
testcase_29 AC 74 ms
25,712 KB
testcase_30 AC 11 ms
8,912 KB
testcase_31 AC 38 ms
19,132 KB
testcase_32 AC 49 ms
21,252 KB
testcase_33 AC 11 ms
8,780 KB
testcase_34 AC 75 ms
24,844 KB
testcase_35 AC 73 ms
25,280 KB
testcase_36 AC 71 ms
26,280 KB
testcase_37 AC 61 ms
32,444 KB
testcase_38 AC 54 ms
30,920 KB
testcase_39 AC 76 ms
27,344 KB
testcase_40 AC 74 ms
32,184 KB
testcase_41 AC 87 ms
28,932 KB
testcase_42 AC 76 ms
32,480 KB
testcase_43 AC 73 ms
31,692 KB
testcase_44 AC 70 ms
27,188 KB
testcase_45 AC 78 ms
32,192 KB
testcase_46 AC 76 ms
30,948 KB
testcase_47 AC 76 ms
30,468 KB
testcase_48 AC 77 ms
27,980 KB
testcase_49 AC 77 ms
31,320 KB
testcase_50 AC 76 ms
31,472 KB
testcase_51 AC 47 ms
21,756 KB
testcase_52 AC 48 ms
22,748 KB
testcase_53 AC 47 ms
21,936 KB
testcase_54 AC 48 ms
22,568 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define fi first
#define se second
mt19937_64 rng(chrono::system_clock::now().time_since_epoch().count());
const ll MOD1000000007 = 1000000007;
const ll MOD998244353 = 998244353;
const ll MOD[3] = {999727999, 1070777777, 1000000007};
const ll LINF = 1LL << 60LL;
const int IINF = (1 << 30) - 2;


template<typename T> 
struct edge{
    int from;
    int to;
    T cost;
    int id;

    edge(){}
    edge(int to, T cost=1) : from(-1), to(to), cost(cost){}
    edge(int to, T cost, int id) : from(-1), to(to), cost(cost), id(id){}
    edge(int from, int to, T cost, int id) : from(from), to(to), cost(cost), id(id){}

    void reverse(){swap(from, to);}
};

template<typename T>
struct edges : std::vector<edge<T>>{
    void sort(){
        std::sort(
            (*this).begin(),
            (*this).end(), 
            [](const edge<T>& a, const edge<T>& b){
                return a.cost < b.cost;
            }
        );
    }
};

template<typename T = bool>
struct graph : std::vector<edges<T>>{
private:
    int n = 0;
    int m = 0;
    edges<T> es;
    bool dir;

public:
    graph(int n, bool dir) : n(n), dir(dir){
        (*this).resize(n);
    }

    void add_edge(int from, int to, T cost=1){
        if(dir){
            es.push_back(edge<T>(from, to, cost, m));
            (*this)[from].push_back(edge<T>(from, to, cost, m++));
        }else{
            if(from > to) swap(from, to);
            es.push_back(edge<T>(from, to, cost, m));
            (*this)[from].push_back(edge<T>(from, to, cost, m));
            (*this)[to].push_back(edge<T>(to, from, cost, m++));
        }
    }

    int get_vnum(){
        return n;
    }

    int get_enum(){
        return m;
    }

    bool get_dir(){
        return dir;
    }

    edge<T> get_edge(int i){
        return es[i];
    }

    edges<T> get_edge_set(){
        return es;
    }
};

template<typename T>
struct redge{
    int from, to;
    T cap, cost;
    int rev;
    
    redge(int to, T cap, T cost=(T)(1)) : from(-1), to(to), cap(cap), cost(cost){}
    redge(int to, T cap, T cost, int rev) : from(-1), to(to), cap(cap), cost(cost), rev(rev){}
};

template<typename T> using Edges = vector<edge<T>>;
template<typename T> using weighted_graph = vector<Edges<T>>;
template<typename T> using tree = vector<Edges<T>>;
using unweighted_graph = vector<vector<int>>;
template<typename T> using residual_graph = vector<vector<redge<T>>>;


void solve(){
    int n, m; cin >> n >> m;
    graph<ll> G(n, true);
    vector<tuple<int, int, ll>> es(2*m);
    for(int i=0; i<m; i++){
        int u, v; cin >> u >> v;
        ll w; cin >> w;
        u--; v--;
        es[2*i] = {u, v, w};
        es[2*i+1] = {v, u, -w};
        G.add_edge(u, v, w);
        G.add_edge(v, u, -w);
    }

    vector<ll> dist(n, -LINF);
    vector<int> dep(n, 0);
    vector<int> epar(n, -1), vpar(n, -1);
    function<void(int, int)> dfs = [&](int v, int p){
        for(auto e : G[v]) if(e.to != p && dist[e.to] == -LINF){
            dist[e.to] = dist[v] + e.cost;
            dep[e.to] = dep[v] + 1;
            epar[e.to] = e.id;
            vpar[e.to] = v;
            dfs(e.to, v);
        }
    };

    for(int s=0; s<n; s++) if(dist[s]==-LINF){
        dist[s] = 0;
        dfs(s, -1);
    }

    for(int v=0; v<n; v++)for(auto e : G[v]){
        if(dist[e.from]-dist[e.to]+e.cost!=0 && dep[e.to] < dep[e.from]){
            vector<int> path;
            int a = e.to, b = e.from;
            path.pb(e.id);
            while(b != a){
                path.pb(epar[b]);
                b = vpar[b];
            }

            reverse(all(path));
            ll sum = 0;
            for(int i : path) sum += get<2>(es[i]);
            if(sum < 0) reverse(all(path));

            cout << (int)path.size() << '\n';
            cout << a+1 << '\n';
            for(int i=0; i<(int)path.size(); i++){
                cout << path[i]/2+1;
                if(i<(int)path.size()-1) cout << ' ';
            }
            cout << '\n';
            return;
        }
    }

    cout << -1 << '\n';
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    
    int T=1;
    //cin >> T;
    while(T--) solve();
}
0