/**
* Not too difficult~ Let P_1, P_2, ..., P_k be the connected components of the graph.
* Clearly, every P_i is a tree.
* [Lemma] There exists a way of connecting the edges that minimizes the resulting tree's
* diameter in such a way that,
* for a fixed i, all edges connecting P_i and another P_j (i!=j) are
* incident to the same node in P_i.
* This is a greedy strategy. Greedily, we can again prove that the node in P_i has the
* smallest f(node) := max{ dist(node, n2) : n2 is a node in P_i different from node }.
* Next, we can view each P_i as a "super node" and consider how we shall connect them.
* Since we have to minimize the diameter, we shall connect the super nodes to form a star
* graph, whose diameter is at most 2. Again, we shall be greedy: the center super node in
* our star graph should be the component with the largest f(chosen_node_in_the_comp).
*
* Time Complexity: O(n)
* Implementation 1 (Full solution, greedy, tree dp)
*/
#include <bits/stdc++.h>
#include "dreaming.h"
typedef std::vector<int> vec;
struct edge_t {
int node, len;
};
typedef std::vector<edge_t> adj_list_t;
std::vector<adj_list_t> trees;
vec down, down_node, down2, up;
void dfs1(int k, int parent) {
down[k] = down2[k] = 0;
for (const edge_t& e : trees[k]) {
int child = e.node;
if (parent == child)
continue;
dfs1(child, k);
int d = down[child] + e.len;
if (d > down[k])
down2[k] = down[k], down[k] = d, down_node[k] = child;
else
down2[k] = std::max(down2[k], d);
}
}
int dfs2(int k, int parent) {
int cost = std::max(up[k], down[k]);
for (const edge_t& e : trees[k]) {
int child = e.node;
if (parent == child)
continue;
up[child] = (down_node[k] != child ? down[k] : down2[k]) + e.len;
up[child] = std::max(up[child], up[k] + e.len);
cost = std::min(cost, dfs2(child, k));
dfs2(child, k);
}
return cost;
}
int travelTime(int n, int m, int L, int A[], int B[], int T[]) {
trees.assign(n, adj_list_t());
for (int k = 0; k < m; k++) {
trees[A[k]].push_back(edge_t{B[k], T[k]});
trees[B[k]].push_back(edge_t{A[k], T[k]});
}
down.assign(n, -1); // -1: not calculated
down_node.resize(n);
down2.assign(n, -1);
up.assign(n, 0);
vec max3_costs;
for (int k = 0; k < n; k++) {
if (down[k] >= 0)
continue;
dfs1(k, -1);
max3_costs.push_back(dfs2(k, -1));
std::sort(max3_costs.rbegin(), max3_costs.rend());
if (int(max3_costs.size()) > 3)
max3_costs.pop_back();
}
for (int i = 1; i < int(max3_costs.size()); i++)
max3_costs[i] += L;
std::sort(max3_costs.rbegin(), max3_costs.rend());
int diameter = max3_costs[0] + (int(max3_costs.size()) > 1 ? max3_costs[1] : 0);
return diameter;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1068 ms |
12984 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
61 ms |
304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1068 ms |
12984 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
6676 KB |
Output is correct |
2 |
Correct |
14 ms |
6636 KB |
Output is correct |
3 |
Correct |
14 ms |
6608 KB |
Output is correct |
4 |
Correct |
14 ms |
6760 KB |
Output is correct |
5 |
Correct |
17 ms |
6732 KB |
Output is correct |
6 |
Correct |
15 ms |
7124 KB |
Output is correct |
7 |
Correct |
15 ms |
7040 KB |
Output is correct |
8 |
Correct |
15 ms |
6592 KB |
Output is correct |
9 |
Correct |
14 ms |
6468 KB |
Output is correct |
10 |
Correct |
15 ms |
6968 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
4 ms |
4180 KB |
Output is correct |
13 |
Correct |
4 ms |
4180 KB |
Output is correct |
14 |
Correct |
4 ms |
4180 KB |
Output is correct |
15 |
Correct |
4 ms |
4180 KB |
Output is correct |
16 |
Correct |
4 ms |
4144 KB |
Output is correct |
17 |
Correct |
5 ms |
4256 KB |
Output is correct |
18 |
Correct |
4 ms |
4180 KB |
Output is correct |
19 |
Correct |
4 ms |
4180 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
308 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
4 ms |
4180 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
61 ms |
304 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1068 ms |
12984 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |