제출 #1367140

#제출 시각아이디문제언어결과실행 시간메모리
1367140weedywelonCurrents (EGOI25_currents)C++20
100 / 100
178 ms35932 KiB
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <iomanip>
#include <limits.h>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <deque>
#include <map>
#include <chrono>
#include <random>
#include <bitset>
#include <tuple>
#define SZ(x) int(x.size())
#define FR(i,a,b) for(int i=(a);i<(b);++i)
#define FOR(i,n) FR(i,0,n)
#define FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define A first
#define B second
#define mp(a,b) make_pair(a,b)
typedef long long LL;
typedef long double LD;
typedef unsigned long long ULL;
typedef unsigned __int128 U128;
typedef __int128 I128;
typedef std::pair<int,int> PII;
typedef std::pair<LL,LL> PLL;
using namespace std;

//for all those adjacent to n-1, track their dist from 0. then dijkstra ish to rest

const LL MAXN=2e5+5;
vector<LL> adj[MAXN], radj[MAXN];
LL d0[MAXN], d1[MAXN], dist[MAXN], ans[MAXN];

void bfs(){
	queue<LL> q;
	d0[0]=0;
	q.push(0);
	
	while (!q.empty()){
		LL u=q.front(), d=d0[u];
		q.pop();
		
		for (LL v:adj[u]){
			if (d0[v]!=-1) continue;
			d0[v]=d+1;
			q.push(v);
		}
	}
}

void bfs1(LL n){
	queue<LL> q;
	d1[n-1]=0;
	q.push(n-1);
	
	while (!q.empty()){
		LL u=q.front(), d=d1[u];
		q.pop();
		
		for (LL v:radj[u]){
			if (d1[v]!=-1) continue;
			d1[v]=d+1;
			q.push(v);
		}
	}
}

signed main(){
	FAST;
	LL n,m; cin>>n>>m;
	while (m--){
		LL a,b; cin>>a>>b;
		adj[a].push_back(b);
		radj[b].push_back(a);
	}
	memset(d0,-1,sizeof(d0));
	memset(d1,-1,sizeof(d1));
	bfs();
	bfs1(n);
	
	memset(dist,-1,sizeof(dist));
	memset(ans,-1,sizeof(ans));
	priority_queue<PLL,vector<PLL>, greater<PLL> > pq;
	for (LL u:radj[n-1]){
		dist[u]=max(d0[u], 1LL);
		ans[u]=max(d0[u], d1[u]);
		pq.push(mp(ans[u],u));
	}
	
	while (!pq.empty()){
		LL d=pq.top().A, u=pq.top().B;
		pq.pop();
		if (d!=ans[u]) continue;
		
		//cout<<u<<": "<<d<<"\n";
		for (LL v:radj[u]){
			if (dist[v]==-1 || d+1<dist[v]) dist[v]=d+1;
			LL na=max(d0[v], dist[v]);
			if (ans[v]==-1 || na<ans[v]){
				ans[v]=na;
				pq.push(mp(ans[v],v));
			}
		}
	}
	//FOR(i,n) cout<<d0[i]<<" ";
	//cout<<"\n";
	
	FOR(i,n-1) cout<<ans[i]<<" ";
	return 0;
}
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…
#결과 실행 시간메모리채점기 출력
결과를 불러오는 중입니다…