#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
#define pii pair<int, int>
#define fi first
#define se second
const int INF = 2e9;
const int maxN = 1e5 + 5;
void chmax(int &a, int b) {a = max(a, b);}
void chmin(int &a, int b) {a = min(a, b);}
struct Node {
int child, lg;
Node(int _child, int _lg) :
child(_child),
lg(_lg)
{}
bool operator < (const Node &other) const {
return lg > other.lg;
}
};
int N, M, L;
vector<pii> adj[maxN];
vector<Node> children[maxN];
int idx[maxN], longest[maxN], newLongest[maxN];
vector<int> curComponent, components;
void dfs(int cur, int par, int id) {
idx[cur] = id;
curComponent.push_back(cur);
for (auto a : adj[cur]) {
int nxt, w;
tie(nxt, w) = a;
if (nxt == par) continue;
dfs(nxt, cur, id);
chmax(longest[cur], longest[nxt] + w);
children[cur].push_back(Node(nxt, longest[nxt] + w));
}
}
void reroot(int cur, int par, int downwardsEdge) {
if (par != -1) {
// push "parent" as cur's child
if (children[par][0].child == cur) {
if (children[par].size() > 1) {
children[cur].push_back(Node(par, children[par][1].lg + downwardsEdge));
} else {
children[cur].push_back(Node(par, downwardsEdge));
}
} else {
children[cur].push_back(Node(par, children[par][0].lg + downwardsEdge));
}
}
sort(children[cur].begin(), children[cur].end());
// update newLongest
for (auto c : children[cur]) {
chmax(newLongest[cur], c.lg);
}
// go to children
for (auto a : adj[cur]) {
int nxt, w;
tie(nxt, w) = a;
if (nxt == par) continue;
reroot(nxt, cur, w);
}
}
void calculateTree(int root, int id) {
// gather data about longest and children's longest for each node
curComponent.clear();
dfs(root, -1, id);
// do reroot
reroot(root, -1, -1);
// find out the minimum newLongest of the component
int curMin = INF;
for (auto i : curComponent) {
chmin(curMin, newLongest[i]);
}
components.push_back(curMin);
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
::N = N, ::M = M, ::L = L;
for (int i = 0; i < M; i++) {
adj[A[i]].push_back({B[i], T[i]});
adj[B[i]].push_back({A[i], T[i]});
}
for (int i = 0, cur = 1; i < N; i++) {
if (idx[i] == 0) {
calculateTree(i, cur);
cur++;
}
}
/*
for (int i = 0; i < N; i++) {
printf("%d: %d %d\n", i, idx[i], newLongest[i]);
}
*/
sort(components.begin(), components.end());
int s = components.size();
int ans = components[s-1] + components[s-2] + L;
if (s > 2) {
chmax(ans, components[s-2] + components[s-3] + (L << 1));
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
70 ms |
17864 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
70 ms |
17864 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
10596 KB |
Output is correct |
2 |
Correct |
32 ms |
10592 KB |
Output is correct |
3 |
Correct |
44 ms |
10948 KB |
Output is correct |
4 |
Correct |
34 ms |
11044 KB |
Output is correct |
5 |
Correct |
36 ms |
10980 KB |
Output is correct |
6 |
Correct |
34 ms |
11584 KB |
Output is correct |
7 |
Correct |
35 ms |
11272 KB |
Output is correct |
8 |
Correct |
32 ms |
10936 KB |
Output is correct |
9 |
Correct |
31 ms |
10888 KB |
Output is correct |
10 |
Correct |
33 ms |
11176 KB |
Output is correct |
11 |
Correct |
3 ms |
5008 KB |
Output is correct |
12 |
Correct |
7 ms |
6608 KB |
Output is correct |
13 |
Correct |
7 ms |
6736 KB |
Output is correct |
14 |
Correct |
7 ms |
6676 KB |
Output is correct |
15 |
Correct |
7 ms |
6608 KB |
Output is correct |
16 |
Correct |
8 ms |
6608 KB |
Output is correct |
17 |
Correct |
7 ms |
6224 KB |
Output is correct |
18 |
Correct |
8 ms |
6736 KB |
Output is correct |
19 |
Correct |
7 ms |
6608 KB |
Output is correct |
20 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
70 ms |
17864 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |