結果

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

ソースコード

diff #
raw source code

#include <cassert>
#include <queue>
#include <set>
#include <ranges>
#include <numeric>
#include <algorithm>
#include <iostream>
#include <utility>
#include <cmath>
#include <vector>
#include <atcoder/scc>
using namespace std;
using ll=long long;
int main(){
	cin.tie(nullptr);
	ios::sync_with_stdio(false);
	int n,m;
	cin>>n>>m;
	assert(1<=n&&n<=200000);
	assert(0<=m&&m<=200000);
	vector<vector<pair<int,int>>> graph(2*n);
	vector<vector<int>> validation_graph(n);
	atcoder::scc_graph g(2*n);
	set<array<int,3>> s;
	for(int i=0;i<m;i++){
		int u,v,c;
		cin>>u>>v>>c;
		assert(!s.count({u,v,c}));
		s.insert({u,v,c});
		assert(1<=u&&u<=n);
		assert(1<=v&&v<=n);
		assert(c==1||c==0);
		u--;v--;
		graph[u].push_back(make_pair(v+n,2*c-1));
		graph[u+n].push_back(make_pair(v,1-2*c));
		g.add_edge(u,v+n);
		g.add_edge(u+n,v);
		validation_graph[u].push_back(v);
	}
	{
		queue<int> bfs;
		bfs.push(0);
		vector<bool> seen(n);
		seen[0]=true;
		while(!bfs.empty()){
			int x=bfs.front();
			bfs.pop();
			for(int y:validation_graph[x]){
				if(!seen[y]){
					seen[y]=true;
					bfs.push(y);
				}
			}
		}
		for(int i=0;i<n;i++)assert(seen[i]);
	}
	auto sccs=g.scc();
	int k=sccs.size();
	int inf=1e9;
	vector<int> pot(2*n,inf);
	vector<int> sccid(2*n,-1);
	vector<bool> bad(k);
	vector<vector<pair<int,int>>> biggraph(k);
	for(int i=0;i<k;i++){
		auto scc=sccs[i];
		for(int x:scc)sccid[x]=i;
		pot[scc[0]]=0;
		auto dfs=[&](auto dfs,int x)->bool{
			for(auto[y,w]:graph[x]){
				if(sccid[y]!=i)continue;
				if(pot[y]==inf){
					pot[y]=pot[x]+w;
					if(dfs(dfs,y))return true;
				}else if(pot[y]!=pot[x]+w)return true;
			}
			return false;
		};
		if(dfs(dfs,scc[0]))bad[i]=true;
	}
	for(int i=0;i<k;i++){
		auto scc=sccs[i];
		for(int x:scc){
			for(auto[y,w]:graph[x]){
				if(sccid[y]!=i){
					biggraph[i].push_back(make_pair(sccid[y],pot[x]+w-pot[y]));
				}
			}
		}
	}
	vector<bool> reached(k);
	vector<int> maxdp(k,-inf),mindp(k,inf);
	reached[sccid[0]]=true;
	maxdp[sccid[0]]=-pot[0];
	mindp[sccid[0]]=-pot[0];
	for(int i=0;i<k;i++){
		if(!reached[i])continue;
		if(bad[i]){
			for(auto[j,w]:biggraph[i]){
				reached[j]=true;
				bad[j]=true;
			}
		}else{
			for(auto[j,w]:biggraph[i]){
				reached[j]=true;
				maxdp[j]=max(maxdp[j],maxdp[i]+w);
				mindp[j]=min(mindp[j],mindp[i]+w);
			}
		}
	}
	for(int i=0;i<n;i++){
		if(bad[sccid[i]]||bad[sccid[i+n]])cout<<-1<<endl;
		else{
			int ans=0;
			if(reached[sccid[i]])ans=max({ans,maxdp[sccid[i]]+pot[i],-(mindp[sccid[i]]+pot[i])});
			if(reached[sccid[i+n]])ans=max({ans,maxdp[sccid[i+n]]+pot[i+n],-(mindp[sccid[i+n]]+pot[i+n])});
			cout<<ans<<endl;
		}
	}
}
			

0