結果

問題 No.3784 01 Walk
コンテスト
ユーザー 👑 potato167
提出日時 2026-10-30 23:38:30
言語 C++17
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -fplugin=/usr/lib/yukicoder/noavx512-gcc15.so -lm -std=c++17 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 523 ms / 2,000 ms
+ 599µs
コード長 8,141 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,917 ms
コンパイル使用メモリ 240,964 KB
実行使用メモリ 73,412 KB
最終ジャッジ日時 2026-10-30 23:38:58
合計ジャッジ時間 27,317 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

// https://github.com/potato167/po167_library
#include <bits/stdc++.h>
#include <cassert>
using namespace std;
using ll=long long;
const ll ILL=2167167167167167167;
const int INF=2100000000;
#define rep(i,a,b) for (int i=(int)(a);i<(int)(b);i++)
#define all(p) p.begin(),p.end()
template<class T> using pq_ = priority_queue<T, vector<T>, greater<T>>;
template<class T> int LB(vector<T> &v,T a){return lower_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> int UB(vector<T> &v,T a){return upper_bound(v.begin(),v.end(),a)-v.begin();}
template<class T> bool chmin(T &a,T b){if(b<a){a=b;return 1;}else return 0;}
template<class T> bool chmax(T &a,T b){if(a<b){a=b;return 1;}else return 0;}
template<class T> void So(vector<T> &v) {sort(v.begin(),v.end());}
template<class T> void Sore(vector<T> &v) {sort(v.begin(),v.end(),[](T x,T y){return x>y;});}
bool yneos(bool a,bool upp=false){if(a){cout<<(upp?"YES\n":"Yes\n");}else{cout<<(upp?"NO\n":"No\n");}return a;}
template<class T> void vec_out(vector<T> &p,int ty=0){
    if(ty==2){cout<<'{';for(int i=0;i<(int)p.size();i++){if(i){cout<<",";}cout<<'"'<<p[i]<<'"';}cout<<"}\n";}
    else{if(ty==1){cout<<p.size()<<"\n";}for(int i=0;i<(int)(p.size());i++){if(i) cout<<" ";cout<<p[i];}cout<<"\n";}}
template<class T> T vec_min(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmin(ans,x);return ans;}
template<class T> T vec_max(vector<T> &a){assert(!a.empty());T ans=a[0];for(auto &x:a) chmax(ans,x);return ans;}
template<class T> T vec_sum(vector<T> &a){T ans=T(0);for(auto &x:a) ans+=x;return ans;}
int pop_count(long long a){int res=0;while(a){res+=(int)(a&1),a>>=1;}return res;}
template<class T> T square(T a){return a * a;}

namespace po167{
struct SCC{
    std::vector<std::vector<int>> G;
    std::vector<std::vector<int>> G_rev;
    int size;
    SCC(int n):G(n),G_rev(n),size(n){}
    void clear(){
        for(auto &x:G) x.clear();
        for(auto &x:G_rev) x.clear();
    }
    void reserve(int ind,size_t n_){
        assert(ind<size);
        G[ind].reserve(n_);
        G_rev[ind].reserve(n_);
    }
    int add_ver(){
        G.push_back({});
        G_rev.push_back({});
        size++;
        return size-1;
    }
    void add_edge(int from,int to){
        assert(0<=to&&to<size);
        assert(0<=from&&from<size);
        G[from].push_back(to);
        G_rev[to].push_back(from);
    }

    void dfs1(std::vector<int> &order,std::vector<int> &seen,int ind,int &now){
        if(seen[ind]!=0) return;
        seen[ind]=-1;
        for(auto x:G[ind]){
            dfs1(order,seen,x,now);
        }
        order[now]=ind;
        now++;
    }
    void dfs2(std::vector<int> &seen,int ind,int rank){
        if(seen[ind]!=-1) return;
        seen[ind]=rank;
        for(auto x:G_rev[ind]){
            dfs2(seen,x,rank);
        }
    }
    std::vector<std::vector<int>> tp_sort(){
        std::vector<int> seen(size,0),order(size);
        int now=0;
        for(int i=0;i<size;i++) dfs1(order,seen,i,now);
        now=0;
        for(int i=size-1;i>=0;i--){
            if(seen[order[i]]==-1){
                dfs2(seen,order[i],now);
                now++;
            }
        }
        std::vector<std::vector<int>> ans(now);
        for(int i=0;i<size;i++){
            ans[seen[i]].push_back(i);
        }
        return ans;
    }
    std::vector<int> for_two_sat(){
        std::vector<int> seen(size,0),order(size);
        int now=0;
        for(int i=0;i<size;i++) dfs1(order,seen,i,now);
        now=0;
        for(int i=size-1;i>=0;i--){
            if(seen[order[i]]==-1){
                dfs2(seen,order[i],now);
                now++;
            }
        }
        return seen;
    }
};

struct two_SAT{
    po167::SCC graph;
    std::vector<bool> ans;
    bool exists=false;
    int size;
    two_SAT(int n):graph(2*n),ans(n),size(n){}
    void add_clause(int i,bool f,int j,bool g){
        graph.add_edge(i*2+(!f),j*2+(g));
        graph.add_edge(j*2+(!g),i*2+(f));
    }
    void clear(){
        graph.clear();
        exists=false;
    }
    void reserve(int ind,size_t n_){
        assert(ind<size);
        graph.reserve(ind*2,n_);
        graph.reserve(ind*2+1,n_);
    }
    void one_true_in_group(std::vector<std::pair<int,int>> &p){
        int ind;
        for(int i=0;i<(int)(p.size())-1;i++){
            ind=graph.add_ver();
            graph.add_ver();
            ind/=2;
            add_clause(ind,1,p[i].first,p[i].second^1);
            add_clause(ind,0,p[i+1].first,p[i+1].second^1);
            if(i){
                add_clause(ind-1,0,ind,1);
            }
            ans.push_back(false);
            size++;
        }
    }
    bool build(){
        auto rank=graph.for_two_sat();
        exists=true;
        for(int i=0;i<size;i++){
            if(rank[i*2]==rank[i*2+1]){
                exists=false;
                break;
            }
            ans[i]=(rank[i*2]<rank[i*2+1]);
        }
        return exists;
    }
};
}
using po167::SCC;
using po167::two_SAT;


void solve();
// DEAR MYSTERIES / TOMOO
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int t = 1;
    // cin >> t;
    rep(i, 0, t) solve();
}

void solve(){
    int N, M;
    cin >> N >> M;
    map<pair<int, int>, int> m;
    rep(i, 0, M) {
        int a, b, c;
        cin >> a >> b >> c;
        a--, b--;
        m[{a, b + N}] += (1 << c);
        m[{a + N, b}] += (1 << (1 - c));
    }
    po167::SCC S(N * 2);
    for (auto e : m) {
        S.add_edge(e.first.first, e.first.second);
    }
    auto tp = S.tp_sort();
    vector<int> dpL(N * 2, INF), dpR(N * 2, -INF);
    dpL[0] = 0, dpR[0] = 0, dpL[N] = 0, dpR[N] = 0;
    vector<int> pot(N * 2);
    vector<int> seen(N * 2);
    vector<int> rank(N * 2);
    rep(i, 0, tp.size()) for (auto x : tp[i]) rank[x] = i;
    for (auto g : tp) {
        // vec_out(g);
        int ok = 1;
        vector<int> order = {g[0]};
        seen[g[0]] = 1;
        rep(rp, 0, order.size()) {
            int a = order[rp];
            for (auto x : S.G[a]) if (rank[x] == rank[a]){
                int w = m[{a, x}];
                // cout << "# " << a << " " << x << " " << w << endl;
                if (w == 3) ok = 0;
                if (seen[x] == 0) {
                    seen[x] = 1;
                    pot[x] = pot[a] + (w == 1 ? -1 : 1);
                    order.push_back(x);
                }
                if (pot[x] != pot[a] + (w == 1 ? -1 : 1)) {
                    ok = 0;
                }
            }
        }
        if (ok == 0) {
            for (auto x : g) dpL[x] = -INF, dpR[x] = INF;
        }
        else {
            int L = INF, R = -INF;
            for (auto x : g) {
                chmin(L, dpL[x] - pot[x]);
                chmax(R, dpR[x] - pot[x]);
            }
            for (auto x : g) {
                dpL[x] = L + pot[x];
                dpR[x] = R + pot[x];
            }
        }
        for (auto x : g) for (auto y : S.G[x]) if (rank[x] != rank[y]) {
            if (m[{x, y}] & 1) {
                chmin(dpL[y], dpL[x] - 1);
                chmax(dpR[y], dpR[x] - 1);
            }
            if (m[{x, y}] & 2) {
                chmin(dpL[y], dpL[x] + 1);
                chmax(dpR[y], dpR[x] + 1);
            }
        }
    }
    rep(i, 0, N) {
        int ans = max(dpR[i], dpR[i + N]);
        chmax(ans, -min(dpL[i], dpL[i + N]));
        if (ans > N * 2) {
            cout << "-1\n";
        }
        else {
            cout << ans << "\n";
        }
    }
}

/* t 回目の操作だとして、
 * (c + t) & 1 ならば 1
 * そうでないなら -1 として、
 * 総和の絶対値の最大化になる
 * 偶数閉路であって、これが 0 にならないものが存在するならば最大値がドーン
 * まずはそれの検知が必要だけど簡単なのか?
 * 頂点を倍化する
 * そのあとポテンシャルをつけることで、全てのアレをあれしたいが
 * それができないから総和が非負な偶数閉路が存在するわけで
 * DFS をすればとりあえず検知はできる
 * そうしたらとりあえず
 * DAG でええか
 *
 */
0