답안 #790183

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
790183 2023-07-22T11:51:28 Z jophyyjh 꿈 (IOI13_dreaming) C++14
18 / 100
1000 ms 11712 KB
/**
 * 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 * log(n))       (Optimal: O(n); O(n * log(n)) is just simpler)
 * 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;
int diameter = 0;       // diameter of the resulting graph

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]);
    diameter = std::max(diameter, cost);
    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 costs;
    for (int k = 0; k < n; k++) {
        if (down[k] >= 0)
            continue;
        dfs1(k, -1);
        costs.push_back(dfs2(k, -1));
    }
    std::sort(costs.rbegin(), costs.rend());
    while (int(costs.size()) > 3)
        costs.pop_back();
    for (int i = 1; i < int(costs.size()); i++)
        costs[i] += L;
    std::sort(costs.rbegin(), costs.rend());
    diameter = std::max(diameter, costs[0] + (int(costs.size()) > 1 ? costs[1] : 0));
    return diameter;
}
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1083 ms 11712 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Execution timed out 1072 ms 212 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1083 ms 11712 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 6616 KB Output is correct
2 Correct 14 ms 6672 KB Output is correct
3 Correct 14 ms 6584 KB Output is correct
4 Correct 14 ms 6688 KB Output is correct
5 Correct 16 ms 6632 KB Output is correct
6 Correct 15 ms 7296 KB Output is correct
7 Correct 15 ms 6880 KB Output is correct
8 Correct 14 ms 6560 KB Output is correct
9 Correct 14 ms 6484 KB Output is correct
10 Correct 14 ms 6868 KB Output is correct
11 Correct 0 ms 212 KB Output is correct
12 Correct 4 ms 4816 KB Output is correct
13 Correct 4 ms 4816 KB Output is correct
14 Correct 4 ms 4816 KB Output is correct
15 Correct 4 ms 4816 KB Output is correct
16 Correct 4 ms 4816 KB Output is correct
17 Correct 6 ms 4816 KB Output is correct
18 Correct 4 ms 4944 KB Output is correct
19 Correct 4 ms 4816 KB Output is correct
20 Correct 0 ms 212 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 4 ms 4876 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 57 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Execution timed out 1072 ms 212 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 1083 ms 11712 KB Time limit exceeded
2 Halted 0 ms 0 KB -