# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
858688 | damwuan | Dreaming (IOI13_dreaming) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "dreaming.h"
#include<bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=(j);i<=(k);i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define bit(n,i) ((n>>i)&1)
#define all(x) x.begin(),x.end()
//#pragma GCC optimize("O2,unroll-loops")
//#pragma GCC target("avx,avx2,bmi,bmi2,sse,sse2,sse3,ssse3,sse4,popcnt")
//#define int long long
typedef long long ll;
typedef pair<int,int> pii;
typedef double ld;
typedef pair<ld,ld> pdd;
typedef pair<ll,ll> pll;
typedef vector<pii> vii;
typedef vector<int> vi;
const ll maxn=2e5+5;
const ll offset=1e18;
const ll block_sz=317;
const ll inf=1e18;
const ll mod=1e9+7;
vector<int> adj[maxn];
bool vis[maxn];
int a[maxn],b[maxn],t[maxn],dep[maxn],g;
int dfs(int u,int pre)
{
int x=u,y;
vis[u]=true;
for(int i: adj[u])
{
int v=u^a[i]^b[i];
int w=t[i];
if (v==pre) continue;
dep[v]=dep[u]+w;
y=dfs(v,u);
if (dep[y]>dep[x]) x=y;
}
return x;
}
vector<int> L;
bool ok=0;
void dfs1(int u,int pre,int aim)
{
L.pb(u);
if (u==aim) ok=1;
if (ok) return;
for(int i: adj[u])
{
int v=u^a[i]^b[i];
int w=t[i];
if (v==pre) continue;
dfs1(v,u,aim);
if (ok) return;
}
L.pop_back();
}
int Find_diameter(int u)
{
dep[u]=0;
u=dfs(u,u);
dep[u]=0;
int v=dfs(u,u);
g=max(g,dep[v]);
// cerr<< "wtf "<<v<<'\n';
ok=0;
L.clear();
dfs1(u,u,v);
// for(int kz:L) cerr<< "ad "<<kz<<' '<<dep[kz]<<' '<<dep[v]-dep[kz]<<'\n';
if (L.size()==1) return 0;
for1(i,0,L.size()-2)
{
// cerr<< "clmm "<<L[i]<<'\n';
int x1=max(dep[L[i]],dep[v]-dep[L[i]]);
int x2=max(dep[L[i+1]],dep[v]-dep[L[i+1]]);
if (x2>=x1) return x1;
}
}
vector<int> Lis;
int travelTime(int n, int m, int l, int A[], int B[], int T[])
{
for1(i,1,m)
{
a[i]=A[i-1]+1;
b[i]=B[i-1]+1;
t[i]=T[i-1];
}
for1(i,1,m)
{
adj[a[i]].pb(i);
adj[b[i]].pb(i);
// cerr<< a[i]<<' '<<b[i]<<' '<<t[i]<<'\n';
}
// return 2;
for1(i,1,n)
{
if (!vis[i])
{
int x=Find_diameter(i);
// cerr<< i<<' '<<x<<'\n';
Lis.pb(x);
}
}
// for(int v:Lis) cerr<< v<<'\n';
sort(all(Lis),greater<int>());
if (Lis.size()==1)
{
return max(g,Lis[0]);
}
else
{
return max(g,Lis[0]+Lis[1]+l);
}
//return 42;
}
#define fail(s, x...) do { \
fprintf(stderr, s "\n", ## x); \
exit(1); \
} while(0)
#define MAX_N 100000
static int A[MAX_N];
static int B[MAX_N];
static int T[MAX_N];
int main() {
int N, M, L, i;
int res;
FILE *f = fopen("dreaming.in", "r");
if (!f)
fail("Failed to open input file.");
res = fscanf(f, "%d%d%d", &N, &M, &L);
for (i = 0; i < M; i++)
res = fscanf(f, "%d%d%d", &A[i], &B[i], &T[i]);
fclose(f);
int answer = travelTime(N, M, L, A, B, T);
printf("%d\n", answer);
return 0;
}