Submission #1101815

# Submission time Handle Problem Language Result Execution time Memory
1101815 2024-10-16T21:22:02 Z totoro Dreaming (IOI13_dreaming) C++17
47 / 100
1000 ms 13380 KB
// UNSOLVED SUBTASK 1 (14 pts)
// UNSOLVED SUBTASK 2 (10 pts)
// UNSOLVED SUBTASK 3 (23 pts)
// UNSOLVED SUBTASK 4 (18 pts)
// UNSOLVED SUBTASK 5 (12 pts)
// UNSOLVED SUBTASK 6 (23 pts)
// [+-+]----------------------
// TOTAL      0/100 pts

#include "dreaming.h"

#include <algorithm>
#include <queue>
#include <vector>

struct Edge {
    int from;
    int to;
    int length;
    Edge(int from, int to, int length) : from(from), to(to), length(length) {}
    bool operator<(const Edge& other) {
        return from < other.from;
    }
};
bool operator<(const Edge& a, const Edge& b) {
    return a.from < b.from;
}

int farthest_node(const std::vector<std::vector<Edge>>& tree, int a) {
    std::vector<bool> visited(tree.size());
    std::priority_queue<std::pair<int, int>, std::vector<std::pair<int, int>>, std::greater<std::pair<int, int>>> q;
    q.emplace(0, a);
    int last;
    while (!q.empty()) {
        auto el = q.top();
        q.pop();
        if (visited[el.second]) continue;
        visited[el.second] = true;
        for (auto edge : tree[el.second]) {
            q.emplace(el.first + edge.length, edge.to);
        }
        last = el.second;
    }
    return last;
}

std::vector<Edge> dijkstra_path(const std::vector<std::vector<Edge>>& tree, int a, int b) {
    std::vector<bool> visited(tree.size());
    std::priority_queue<std::pair<int, std::pair<Edge, int>>> q;
    Edge dummy = Edge(-1, a, -1);
    q.emplace(0, std::pair<Edge, int>(dummy, a));
    std::vector<Edge> from(tree.size(), dummy);
    while (!q.empty() && q.top().second.second != b) {
        auto el = q.top();
        q.pop();
        if (visited[el.second.second]) continue;
        visited[el.second.second] = true;
        from[el.second.second] = el.second.first;
        for (auto edge : tree[el.second.second]) {
            q.emplace(el.first + edge.length, std::pair<Edge, int>(edge, edge.to));
        }
    }
    if (q.empty()) return {};
    from[b] = q.top().second.first;
    std::vector<Edge> path;
    int current = b;
    while (current != a) {
        path.push_back(from[current]);
        current = from[current].from;
    }
    return path;
}

int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
    std::vector<std::vector<Edge>> tree(N);
    for (int i = 0; i < M; ++i) {
        tree[A[i]].emplace_back(A[i], B[i], T[i]);
        tree[B[i]].emplace_back(B[i], A[i], T[i]);
    }
    // floodfill
    std::vector<int> color(N);
    std::vector<int> parents;
    int current = 1;
    for (int i = 0; i < N; ++i) {
        if (color[i]) continue;
        parents.push_back(i);
        std::vector<int> stack;
        stack.push_back(i);
        while (!stack.empty()) {
            int back = stack.back();
            stack.pop_back();
            if (color[back]) continue;
            color[back] = current;
            for (auto edge : tree[back]) {
                stack.push_back(edge.to);
            }
        }
        ++current;
    }

    // radius and centers
    std::vector<std::pair<int, int>> radius_centers(parents.size());
    int sp = 0;
    int largest_diameter = 0;
    for (auto parent : parents) {
        int b = farthest_node(tree, parent);
        int a = farthest_node(tree, b);
        if (a == b) {
            radius_centers[sp++] = std::make_pair(0, parent);
            continue;
        }
        auto diameter = dijkstra_path(tree, b, a);
        int sum = 0;
        for (auto edge : diameter) {
            sum += edge.length;
        }
        largest_diameter = std::max(largest_diameter, sum);
        int small_sum = 0;
        int i = 0;
        while (i < diameter.size() && small_sum <= sum / 2) {
            small_sum += diameter[i++].length;
        }
        if (sum - small_sum + diameter[i - 1].length < small_sum) {
            --i;
            small_sum = sum - small_sum + diameter[i].length;
        }
        radius_centers[sp++] = std::make_pair(small_sum, i > 0 ? diameter[i - 1].from : diameter[i].to);
    }

    if (radius_centers.size() < 2) {
        return largest_diameter;
    }
    std::sort(radius_centers.rbegin(), radius_centers.rend());
    return std::max(largest_diameter, radius_centers[0].first + L + radius_centers[1].first);
}

Compilation message

dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:120:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<Edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  120 |         while (i < diameter.size() && small_sum <= sum / 2) {
      |                ~~^~~~~~~~~~~~~~~~~
dreaming.cpp: In function 'int farthest_node(const std::vector<std::vector<Edge> >&, int)':
dreaming.cpp:44:12: warning: 'last' may be used uninitialized in this function [-Wmaybe-uninitialized]
   44 |     return last;
      |            ^~~~
# Verdict Execution time Memory Grader output
1 Correct 56 ms 12404 KB Output is correct
2 Correct 56 ms 12344 KB Output is correct
3 Correct 43 ms 7976 KB Output is correct
4 Correct 10 ms 2100 KB Output is correct
5 Correct 6 ms 1620 KB Output is correct
6 Correct 14 ms 3020 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 27 ms 5268 KB Output is correct
9 Correct 36 ms 6652 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 50 ms 8920 KB Output is correct
12 Correct 60 ms 11120 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 436 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 504 KB Output is correct
20 Correct 1 ms 436 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 56 ms 12404 KB Output is correct
2 Correct 56 ms 12344 KB Output is correct
3 Correct 43 ms 7976 KB Output is correct
4 Correct 10 ms 2100 KB Output is correct
5 Correct 6 ms 1620 KB Output is correct
6 Correct 14 ms 3020 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 27 ms 5268 KB Output is correct
9 Correct 36 ms 6652 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 50 ms 8920 KB Output is correct
12 Correct 60 ms 11120 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 436 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 504 KB Output is correct
33 Correct 1 ms 436 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 57 ms 12208 KB Output is correct
40 Correct 62 ms 12364 KB Output is correct
41 Correct 39 ms 7916 KB Output is correct
42 Correct 9 ms 2068 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 432 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 1 ms 340 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 81 ms 11196 KB Output is correct
58 Correct 82 ms 11340 KB Output is correct
59 Correct 82 ms 11096 KB Output is correct
60 Correct 78 ms 11152 KB Output is correct
61 Correct 80 ms 11340 KB Output is correct
62 Correct 77 ms 11308 KB Output is correct
63 Correct 83 ms 11060 KB Output is correct
64 Correct 90 ms 11000 KB Output is correct
65 Correct 91 ms 11304 KB Output is correct
66 Correct 81 ms 11280 KB Output is correct
67 Correct 80 ms 11084 KB Output is correct
68 Correct 77 ms 11092 KB Output is correct
69 Correct 77 ms 11460 KB Output is correct
70 Correct 72 ms 11432 KB Output is correct
71 Correct 1 ms 340 KB Output is correct
72 Correct 2 ms 596 KB Output is correct
73 Correct 3 ms 596 KB Output is correct
74 Correct 3 ms 596 KB Output is correct
75 Correct 2 ms 596 KB Output is correct
76 Correct 3 ms 596 KB Output is correct
77 Correct 4 ms 760 KB Output is correct
78 Correct 2 ms 756 KB Output is correct
79 Correct 2 ms 756 KB Output is correct
80 Correct 69 ms 11080 KB Output is correct
81 Correct 73 ms 11072 KB Output is correct
82 Correct 76 ms 11320 KB Output is correct
83 Correct 82 ms 11284 KB Output is correct
84 Correct 2 ms 596 KB Output is correct
85 Correct 2 ms 596 KB Output is correct
86 Correct 3 ms 596 KB Output is correct
87 Correct 2 ms 608 KB Output is correct
88 Correct 2 ms 608 KB Output is correct
89 Correct 2 ms 608 KB Output is correct
90 Correct 2 ms 596 KB Output is correct
91 Correct 2 ms 620 KB Output is correct
92 Correct 2 ms 596 KB Output is correct
93 Correct 2 ms 596 KB Output is correct
94 Correct 1 ms 340 KB Output is correct
95 Correct 1 ms 340 KB Output is correct
96 Correct 1 ms 504 KB Output is correct
97 Correct 1 ms 432 KB Output is correct
98 Correct 1 ms 504 KB Output is correct
99 Correct 1 ms 340 KB Output is correct
100 Correct 0 ms 340 KB Output is correct
101 Correct 1 ms 340 KB Output is correct
102 Correct 1 ms 340 KB Output is correct
103 Correct 1 ms 340 KB Output is correct
104 Correct 6 ms 1620 KB Output is correct
105 Correct 14 ms 3152 KB Output is correct
106 Correct 1 ms 504 KB Output is correct
107 Correct 27 ms 5488 KB Output is correct
108 Correct 36 ms 6644 KB Output is correct
109 Correct 1 ms 340 KB Output is correct
110 Correct 51 ms 8888 KB Output is correct
111 Correct 58 ms 10864 KB Output is correct
112 Correct 1 ms 340 KB Output is correct
113 Correct 70 ms 13380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1063 ms 7640 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 340 KB Output is correct
6 Correct 1 ms 340 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 1 ms 340 KB Output is correct
9 Correct 1 ms 340 KB Output is correct
10 Correct 1 ms 436 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 504 KB Output is correct
20 Correct 1 ms 436 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 340 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 2 ms 596 KB Output is correct
28 Correct 4 ms 844 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 2 ms 596 KB Output is correct
31 Correct 3 ms 596 KB Output is correct
32 Incorrect 1 ms 508 KB Output isn't correct
33 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 56 ms 12404 KB Output is correct
2 Correct 56 ms 12344 KB Output is correct
3 Correct 43 ms 7976 KB Output is correct
4 Correct 10 ms 2100 KB Output is correct
5 Correct 6 ms 1620 KB Output is correct
6 Correct 14 ms 3020 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 27 ms 5268 KB Output is correct
9 Correct 36 ms 6652 KB Output is correct
10 Correct 1 ms 340 KB Output is correct
11 Correct 50 ms 8920 KB Output is correct
12 Correct 60 ms 11120 KB Output is correct
13 Correct 1 ms 340 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 340 KB Output is correct
18 Correct 1 ms 340 KB Output is correct
19 Correct 1 ms 340 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 340 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 436 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 1 ms 340 KB Output is correct
29 Correct 1 ms 340 KB Output is correct
30 Correct 1 ms 340 KB Output is correct
31 Correct 1 ms 340 KB Output is correct
32 Correct 1 ms 504 KB Output is correct
33 Correct 1 ms 436 KB Output is correct
34 Correct 1 ms 340 KB Output is correct
35 Correct 1 ms 340 KB Output is correct
36 Correct 1 ms 340 KB Output is correct
37 Correct 1 ms 340 KB Output is correct
38 Correct 1 ms 340 KB Output is correct
39 Correct 57 ms 12208 KB Output is correct
40 Correct 62 ms 12364 KB Output is correct
41 Correct 39 ms 7916 KB Output is correct
42 Correct 9 ms 2068 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 340 KB Output is correct
45 Correct 1 ms 340 KB Output is correct
46 Correct 1 ms 340 KB Output is correct
47 Correct 1 ms 432 KB Output is correct
48 Correct 1 ms 340 KB Output is correct
49 Correct 1 ms 340 KB Output is correct
50 Correct 1 ms 340 KB Output is correct
51 Correct 1 ms 340 KB Output is correct
52 Correct 1 ms 340 KB Output is correct
53 Correct 1 ms 340 KB Output is correct
54 Correct 1 ms 340 KB Output is correct
55 Correct 1 ms 340 KB Output is correct
56 Correct 1 ms 340 KB Output is correct
57 Correct 81 ms 11196 KB Output is correct
58 Correct 82 ms 11340 KB Output is correct
59 Correct 82 ms 11096 KB Output is correct
60 Correct 78 ms 11152 KB Output is correct
61 Correct 80 ms 11340 KB Output is correct
62 Correct 77 ms 11308 KB Output is correct
63 Correct 83 ms 11060 KB Output is correct
64 Correct 90 ms 11000 KB Output is correct
65 Correct 91 ms 11304 KB Output is correct
66 Correct 81 ms 11280 KB Output is correct
67 Correct 80 ms 11084 KB Output is correct
68 Correct 77 ms 11092 KB Output is correct
69 Correct 77 ms 11460 KB Output is correct
70 Correct 72 ms 11432 KB Output is correct
71 Correct 1 ms 340 KB Output is correct
72 Correct 2 ms 596 KB Output is correct
73 Correct 3 ms 596 KB Output is correct
74 Correct 3 ms 596 KB Output is correct
75 Correct 2 ms 596 KB Output is correct
76 Correct 3 ms 596 KB Output is correct
77 Correct 4 ms 760 KB Output is correct
78 Correct 2 ms 756 KB Output is correct
79 Correct 2 ms 756 KB Output is correct
80 Correct 69 ms 11080 KB Output is correct
81 Correct 73 ms 11072 KB Output is correct
82 Correct 76 ms 11320 KB Output is correct
83 Correct 82 ms 11284 KB Output is correct
84 Correct 2 ms 596 KB Output is correct
85 Correct 2 ms 596 KB Output is correct
86 Correct 3 ms 596 KB Output is correct
87 Correct 2 ms 608 KB Output is correct
88 Correct 2 ms 608 KB Output is correct
89 Correct 2 ms 608 KB Output is correct
90 Correct 2 ms 596 KB Output is correct
91 Correct 2 ms 620 KB Output is correct
92 Correct 2 ms 596 KB Output is correct
93 Correct 2 ms 596 KB Output is correct
94 Correct 1 ms 340 KB Output is correct
95 Correct 1 ms 340 KB Output is correct
96 Correct 1 ms 504 KB Output is correct
97 Correct 1 ms 432 KB Output is correct
98 Correct 1 ms 504 KB Output is correct
99 Correct 1 ms 340 KB Output is correct
100 Correct 0 ms 340 KB Output is correct
101 Correct 1 ms 340 KB Output is correct
102 Correct 1 ms 340 KB Output is correct
103 Correct 1 ms 340 KB Output is correct
104 Correct 6 ms 1620 KB Output is correct
105 Correct 14 ms 3152 KB Output is correct
106 Correct 1 ms 504 KB Output is correct
107 Correct 27 ms 5488 KB Output is correct
108 Correct 36 ms 6644 KB Output is correct
109 Correct 1 ms 340 KB Output is correct
110 Correct 51 ms 8888 KB Output is correct
111 Correct 58 ms 10864 KB Output is correct
112 Correct 1 ms 340 KB Output is correct
113 Correct 70 ms 13380 KB Output is correct
114 Execution timed out 1063 ms 7640 KB Time limit exceeded
115 Halted 0 ms 0 KB -