#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
// Problem
// Given a tree with M edge, make new edge with weight = L such that for every pair of nodes, the maxmimum weight path is minimum
// How?
// Maximum path = tree diameter?
// The goal is to minimize the overall tree diameter
const int MAXN = 1e6;
vector<pair<int, int>> adj[MAXN];
bool vis[MAXN];
pair<int, int> mmb[MAXN];
int bmg[MAXN];
int dep[MAXN];
void DFS(int u, int p)
{
vis[u] = 1;
mmb[u] = {0, u};
for (auto &[v, w] : adj[u])
{
if (v == p)
continue;
DFS(v, u);
mmb[u] = max(mmb[u], make_pair(mmb[v].first + w, mmb[v].second));
}
}
int DDFS(int u, int p)
{
bmg[u] = max(bmg[u], dep[u]);
int ans = bmg[u];
for (auto &[v, w] : adj[u])
{
if (v == p)
continue;
dep[v] = dep[u] + w;
ans = min(ans, DDFS(v, u));
}
return ans;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[])
{
for (int i = 0; i < M; i++)
{
adj[A[i]].push_back(make_pair(B[i], T[i]));
adj[B[i]].push_back(make_pair(A[i], T[i]));
}
int ans = 0;
vector<int> dias;
for (int i = 0; i < N; i++)
{
if (!vis[i])
{
DFS(i, -1);
int x = mmb[i].second;
int y = mmb[x].second;
DDFS(x, -1);
ans = max(ans, mmb[x].first);
dep[x] = 0;
DDFS(x, -1);
dep[y] = 0;
dias.push_back(DDFS(y, -1));
}
}
sort(dias.rbegin(), dias.rend());
if ((int)dias.size() >= 2)
{
ans = max(ans, dias[0] + dias[1] + L);
}
if ((int)dias.size() >= 3)
{
ans = max(ans, dias[1] + dias[2] + L * 2);
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
47 ms |
36328 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
23828 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
47 ms |
36328 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
25 ms |
28076 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
10 ms |
23828 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
47 ms |
36328 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |