제출 #464727

#제출 시각아이디문제언어결과실행 시간메모리
464727AdamGS꿈 (IOI13_dreaming)C++14
100 / 100
108 ms19148 KiB
#include "dreaming.h"
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=1e5+7, INF=1e9+7;
vector<pair<int,int>>V[LIM];
vector<int>spojna;
int dp_down[LIM][2], jaki[LIM][2], odw[LIM], dp_up[LIM];
void cnt_down(int x, int o) {
	odw[x]=1;
	spojna.pb(x);
	for(auto i : V[x]) if(i.st!=o) {
		cnt_down(i.st, x);
		if(dp_down[i.st][0]+i.nd>=dp_down[x][0]) {
			dp_down[x][1]=dp_down[x][0];
			jaki[x][1]=jaki[x][0];
			dp_down[x][0]=dp_down[i.st][0]+i.nd;
			jaki[x][0]=i.st;
		} else if(dp_down[i.st][0]+i.nd>=dp_down[x][1]) {
			dp_down[x][1]=dp_down[i.st][0]+i.nd;
			jaki[x][1]=i.st;
		}
	}
}
void cnt_up(int x, int o, int v) {
	if(x!=o) {
		dp_up[x]=dp_up[o];
		if(jaki[o][0]==x) {
			if(jaki[o][1]!=-1) {
				dp_up[x]=max(dp_up[x], dp_down[o][1]);
			}
		} else {
			dp_up[x]=max(dp_up[x], dp_down[o][0]);
		}
		dp_up[x]+=v;
	}
	for(auto i : V[x]) if(i.st!=o) cnt_up(i.st, x, i.nd);
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
	rep(i, N) jaki[i][0]=jaki[i][1]=-1;
	rep(i, M) {
		V[A[i]].pb({B[i], T[i]});
		V[B[i]].pb({A[i], T[i]});
	}
	int ans=0;
	vector<int>spojne;
	rep(i, N) if(!odw[i]) {
		spojna.clear();
		cnt_down(i, i);
		cnt_up(i, i, 0);
		int mi=INF, srednica=0;
		for(auto j : spojna) {
			mi=min(mi, max(dp_down[j][0], dp_up[j]));
			srednica=max(srednica, dp_down[j][0]+max(dp_up[j], dp_down[j][1]));
		}
		ans=max(ans, srednica);
		spojne.pb(mi);
	}
	sort(all(spojne));
	reverse(all(spojne));
	if(spojne.size()>1) {
		ans=max(ans, spojne[0]+spojne[1]+L);
	}
	if(spojne.size()>2) {
		ans=max(ans, spojne[1]+spojne[2]+2*L);
	}
	return ans;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...