#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;
vector<pair<int, int>> graph[100001];
int dists[100001];
bool visited[100001];
int maxDist = 0;
int maxNode = 0;
void dfs(int node, int parent, int distance) {
dists[node] = distance;
if (dists[node] > maxDist)
{
maxDist = dists[node];
maxNode = node;
}
for (auto i : graph[node])
{
if (i.first != parent)
{
dfs(i.first, node, distance + i.second);
}
}
visited[node] = true;
}
int travelTime(int n, int m, int l, int a[], int b[], int t[]) {
for (int i = 0; i < m; i++)
{
graph[a[i]].push_back({b[i],t[i]});
graph[b[i]].push_back({a[i], t[i]});
}
dfs(0, -1, 0);
//cout << "Done dfs1 landed on " << maxNode << "\n";
int dfsFrom = maxNode;
maxNode = maxDist = 0;
dfs(dfsFrom, -1, 0);
//cout << "Done dfs2 landed on " << maxNode << "\n";
int currDist = maxDist;
int prevCurrDist = currDist;
int currNode = maxNode;
while (currDist > maxDist/2)
{
for (auto i : graph[currNode])
{
if (dists[i.first] < dists[currNode])
{
currNode = i.first;
prevCurrDist = currDist;
currDist = dists[currNode];
break;
}
}
}
int ans1 = min(max(currDist, maxDist - currDist), max(prevCurrDist, maxDist - prevCurrDist));
int start2 = 0;
for (int i = 0; i < n; i++)
{
if (!visited[i])
{
//cout << "Starting from " << start2 << "\n";
break;
}
start2++;
}
maxNode = maxDist = 0;
dfs(start2, -1, 0);
//cout << "Done dfs3 landed on " << maxNode << "\n";
dfsFrom = maxNode;
maxNode = maxDist = 0;
dfs(dfsFrom, -1, 0);
//cout << "Done dfs4 landed on " << maxNode << "\n";
currDist = maxDist;
prevCurrDist = currDist;
currNode = maxNode;
while (currDist > maxDist/2)
{
for (auto i : graph[currNode])
{
if (dists[i.first] < dists[currNode])
{
currNode = i.first;
prevCurrDist = currDist;
currDist = dists[currNode];
break;
}
}
}
int ans2 = min(max(currDist, maxDist - currDist), max(prevCurrDist, maxDist - prevCurrDist));
return ans1 + ans2 + l;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
63 ms |
12792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
63 ms |
12792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
63 ms |
12792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
27 ms |
4992 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
63 ms |
12792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
63 ms |
12792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |