Submission #802176

# Submission time Handle Problem Language Result Execution time Memory
802176 2023-08-02T10:40:13 Z borisAngelov Dreaming (IOI13_dreaming) C++17
47 / 100
54 ms 17044 KB
#include "dreaming.h"
#include <bits/stdc++.h>

using namespace std;

const int maxn = 100005;

int n, m;

vector<pair<int, int>> g[maxn];

long long dp[maxn];
bool visited[maxn];

void dfs_down(int node, int par)
{
    visited[node] = true;
    dp[node] = 0;

    for (int i = 0; i < g[node].size(); ++i)
    {
        if (g[node][i].first != par)
        {
            dfs_down(g[node][i].first, node);
            dp[node] = max(dp[node], g[node][i].second + dp[g[node][i].first]);
        }
    }
}

long long longest = 0;

long long dfs_up(int node, int par, long long up)
{
    long long result = max(up, dp[node]);
    longest = max(longest, dp[node] + up);

    long long max1 = 0;
    long long max2 = 0;

    for (int i = 0; i < g[node].size(); ++i)
    {
        int child = g[node][i].first;
        int edge = g[node][i].second;

        if (child == par)
        {
            continue;
        }

        if (dp[child] + edge > max1)
        {
            max2 = max1;
            max1 = dp[child] + edge;
        }
        else if (dp[child] + edge > max2)
        {
            max2 = dp[child] + edge;
        }
    }

    for (int i = 0; i < g[node].size(); ++i)
    {
        if (g[node][i].first == par)
        {
            continue;
        }

        int child = g[node][i].first;
        int edge = g[node][i].second;

        long long new_up = 0;

        if (dp[child] + edge == max1)
        {
            new_up = max(up, max2) + edge;
        }
        else
        {
            new_up = max(up, max1) + edge;
        }

        result = min(result, dfs_up(child, node, new_up));
    }

    return result;
}

int travelTime(int N, int M, int L, int A[], int B[], int T[])
{
    n = N;
    m = M;

    for (int i = 0; i < m; ++i)
    {
        int x = A[i] + 1;
        int y = B[i] + 1;
        int w = T[i];

        g[x].push_back(make_pair(y, w));
        g[y].push_back(make_pair(x, w));
    }

    vector<long long> dists;

    for (int i = 1; i <= n; ++i)
    {
        if (visited[i] == false)
        {
            dfs_down(i, -1);
            dists.push_back(dfs_up(i, -1, 0));
        }
    }

    sort(dists.begin(), dists.end());

    if (dists.size() == 1)
    {
        return max(longest, dists[dists.size() - 1]);
    }

    long long ans = max(longest, dists[dists.size() - 1] + dists[dists.size() - 2] + L);

    if (dists.size() >= 3)
    {
        ans = max(ans, dists[dists.size() - 2] + dists[2] + 2 * L);
    }

    return ans;
}

/*int main()
{
    int A[8] = {0, 8, 2, 5, 5, 1, 1, 10};
    int B[8] = {8, 2, 7, 11, 1, 3, 9, 6};
    int T[8] = {4, 2, 4, 3, 7, 1, 5, 3};

    std::cout << travelTime(12, 8, 2, A, B, T) << std::endl;

    return 0;
}*/

Compilation message

dreaming.cpp: In function 'void dfs_down(int, int)':
dreaming.cpp:20:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |     for (int i = 0; i < g[node].size(); ++i)
      |                     ~~^~~~~~~~~~~~~~~~
dreaming.cpp: In function 'long long int dfs_up(int, int, long long int)':
dreaming.cpp:40:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |     for (int i = 0; i < g[node].size(); ++i)
      |                     ~~^~~~~~~~~~~~~~~~
dreaming.cpp:61:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   61 |     for (int i = 0; i < g[node].size(); ++i)
      |                     ~~^~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 29 ms 11212 KB Output is correct
2 Correct 30 ms 12188 KB Output is correct
3 Correct 29 ms 10660 KB Output is correct
4 Correct 6 ms 4064 KB Output is correct
5 Correct 5 ms 3540 KB Output is correct
6 Correct 9 ms 4820 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 16 ms 6772 KB Output is correct
9 Correct 21 ms 8912 KB Output is correct
10 Correct 2 ms 2664 KB Output is correct
11 Correct 30 ms 10016 KB Output is correct
12 Correct 36 ms 11112 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2656 KB Output is correct
4 Correct 2 ms 2656 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2672 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2660 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 2 ms 2652 KB Output is correct
15 Correct 1 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 1 ms 2660 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 2 ms 2652 KB Output is correct
21 Correct 1 ms 2644 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 2 ms 2660 KB Output is correct
24 Correct 2 ms 2656 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 29 ms 11212 KB Output is correct
2 Correct 30 ms 12188 KB Output is correct
3 Correct 29 ms 10660 KB Output is correct
4 Correct 6 ms 4064 KB Output is correct
5 Correct 5 ms 3540 KB Output is correct
6 Correct 9 ms 4820 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 16 ms 6772 KB Output is correct
9 Correct 21 ms 8912 KB Output is correct
10 Correct 2 ms 2664 KB Output is correct
11 Correct 30 ms 10016 KB Output is correct
12 Correct 36 ms 11112 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 1 ms 2644 KB Output is correct
16 Correct 1 ms 2656 KB Output is correct
17 Correct 2 ms 2656 KB Output is correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 2 ms 2672 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 1 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2660 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 2 ms 2652 KB Output is correct
28 Correct 1 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 1 ms 2660 KB Output is correct
32 Correct 1 ms 2644 KB Output is correct
33 Correct 2 ms 2652 KB Output is correct
34 Correct 1 ms 2644 KB Output is correct
35 Correct 1 ms 2644 KB Output is correct
36 Correct 2 ms 2660 KB Output is correct
37 Correct 2 ms 2656 KB Output is correct
38 Correct 2 ms 2644 KB Output is correct
39 Correct 44 ms 12564 KB Output is correct
40 Correct 29 ms 12252 KB Output is correct
41 Correct 22 ms 10712 KB Output is correct
42 Correct 7 ms 4052 KB Output is correct
43 Correct 2 ms 2656 KB Output is correct
44 Correct 2 ms 2772 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 1 ms 2644 KB Output is correct
47 Correct 1 ms 2644 KB Output is correct
48 Correct 2 ms 2644 KB Output is correct
49 Correct 2 ms 2644 KB Output is correct
50 Correct 1 ms 2644 KB Output is correct
51 Correct 2 ms 2644 KB Output is correct
52 Correct 1 ms 2644 KB Output is correct
53 Correct 1 ms 2644 KB Output is correct
54 Correct 2 ms 2644 KB Output is correct
55 Correct 1 ms 2644 KB Output is correct
56 Correct 1 ms 2644 KB Output is correct
57 Correct 36 ms 10012 KB Output is correct
58 Correct 39 ms 10024 KB Output is correct
59 Correct 38 ms 9940 KB Output is correct
60 Correct 50 ms 9932 KB Output is correct
61 Correct 38 ms 9960 KB Output is correct
62 Correct 38 ms 9932 KB Output is correct
63 Correct 37 ms 9652 KB Output is correct
64 Correct 36 ms 9700 KB Output is correct
65 Correct 42 ms 9884 KB Output is correct
66 Correct 54 ms 9992 KB Output is correct
67 Correct 38 ms 9940 KB Output is correct
68 Correct 39 ms 9972 KB Output is correct
69 Correct 38 ms 10140 KB Output is correct
70 Correct 45 ms 10316 KB Output is correct
71 Correct 1 ms 2644 KB Output is correct
72 Correct 2 ms 2772 KB Output is correct
73 Correct 3 ms 2772 KB Output is correct
74 Correct 2 ms 2840 KB Output is correct
75 Correct 2 ms 2772 KB Output is correct
76 Correct 2 ms 2888 KB Output is correct
77 Correct 2 ms 2900 KB Output is correct
78 Correct 3 ms 2848 KB Output is correct
79 Correct 2 ms 2796 KB Output is correct
80 Correct 39 ms 9888 KB Output is correct
81 Correct 38 ms 9948 KB Output is correct
82 Correct 39 ms 9936 KB Output is correct
83 Correct 39 ms 9988 KB Output is correct
84 Correct 2 ms 2772 KB Output is correct
85 Correct 2 ms 2800 KB Output is correct
86 Correct 2 ms 2772 KB Output is correct
87 Correct 3 ms 2772 KB Output is correct
88 Correct 2 ms 2772 KB Output is correct
89 Correct 3 ms 2796 KB Output is correct
90 Correct 3 ms 2772 KB Output is correct
91 Correct 3 ms 2848 KB Output is correct
92 Correct 3 ms 2796 KB Output is correct
93 Correct 2 ms 2772 KB Output is correct
94 Correct 1 ms 2644 KB Output is correct
95 Correct 2 ms 2668 KB Output is correct
96 Correct 2 ms 2644 KB Output is correct
97 Correct 1 ms 2644 KB Output is correct
98 Correct 2 ms 2644 KB Output is correct
99 Correct 2 ms 2644 KB Output is correct
100 Correct 2 ms 2664 KB Output is correct
101 Correct 2 ms 2644 KB Output is correct
102 Correct 2 ms 2644 KB Output is correct
103 Correct 1 ms 2644 KB Output is correct
104 Correct 5 ms 3424 KB Output is correct
105 Correct 9 ms 4880 KB Output is correct
106 Correct 2 ms 2644 KB Output is correct
107 Correct 16 ms 6740 KB Output is correct
108 Correct 20 ms 8992 KB Output is correct
109 Correct 1 ms 2644 KB Output is correct
110 Correct 28 ms 10016 KB Output is correct
111 Correct 32 ms 11136 KB Output is correct
112 Correct 2 ms 2664 KB Output is correct
113 Correct 48 ms 17044 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 14 ms 6360 KB Output is correct
2 Incorrect 15 ms 6484 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2644 KB Output is correct
2 Correct 1 ms 2644 KB Output is correct
3 Correct 1 ms 2656 KB Output is correct
4 Correct 2 ms 2656 KB Output is correct
5 Correct 1 ms 2644 KB Output is correct
6 Correct 2 ms 2672 KB Output is correct
7 Correct 1 ms 2644 KB Output is correct
8 Correct 2 ms 2644 KB Output is correct
9 Correct 1 ms 2644 KB Output is correct
10 Correct 1 ms 2644 KB Output is correct
11 Correct 2 ms 2644 KB Output is correct
12 Correct 2 ms 2660 KB Output is correct
13 Correct 1 ms 2644 KB Output is correct
14 Correct 2 ms 2652 KB Output is correct
15 Correct 1 ms 2644 KB Output is correct
16 Correct 2 ms 2644 KB Output is correct
17 Correct 2 ms 2644 KB Output is correct
18 Correct 1 ms 2660 KB Output is correct
19 Correct 1 ms 2644 KB Output is correct
20 Correct 2 ms 2652 KB Output is correct
21 Correct 1 ms 2644 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 2 ms 2660 KB Output is correct
24 Correct 2 ms 2656 KB Output is correct
25 Correct 2 ms 2644 KB Output is correct
26 Correct 2 ms 2644 KB Output is correct
27 Correct 2 ms 2772 KB Output is correct
28 Correct 2 ms 2788 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 2 ms 2772 KB Output is correct
31 Correct 2 ms 2772 KB Output is correct
32 Incorrect 2 ms 2644 KB Output isn't correct
33 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 29 ms 11212 KB Output is correct
2 Correct 30 ms 12188 KB Output is correct
3 Correct 29 ms 10660 KB Output is correct
4 Correct 6 ms 4064 KB Output is correct
5 Correct 5 ms 3540 KB Output is correct
6 Correct 9 ms 4820 KB Output is correct
7 Correct 2 ms 2644 KB Output is correct
8 Correct 16 ms 6772 KB Output is correct
9 Correct 21 ms 8912 KB Output is correct
10 Correct 2 ms 2664 KB Output is correct
11 Correct 30 ms 10016 KB Output is correct
12 Correct 36 ms 11112 KB Output is correct
13 Correct 2 ms 2644 KB Output is correct
14 Correct 1 ms 2644 KB Output is correct
15 Correct 1 ms 2644 KB Output is correct
16 Correct 1 ms 2656 KB Output is correct
17 Correct 2 ms 2656 KB Output is correct
18 Correct 1 ms 2644 KB Output is correct
19 Correct 2 ms 2672 KB Output is correct
20 Correct 1 ms 2644 KB Output is correct
21 Correct 2 ms 2644 KB Output is correct
22 Correct 1 ms 2644 KB Output is correct
23 Correct 1 ms 2644 KB Output is correct
24 Correct 2 ms 2644 KB Output is correct
25 Correct 2 ms 2660 KB Output is correct
26 Correct 1 ms 2644 KB Output is correct
27 Correct 2 ms 2652 KB Output is correct
28 Correct 1 ms 2644 KB Output is correct
29 Correct 2 ms 2644 KB Output is correct
30 Correct 2 ms 2644 KB Output is correct
31 Correct 1 ms 2660 KB Output is correct
32 Correct 1 ms 2644 KB Output is correct
33 Correct 2 ms 2652 KB Output is correct
34 Correct 1 ms 2644 KB Output is correct
35 Correct 1 ms 2644 KB Output is correct
36 Correct 2 ms 2660 KB Output is correct
37 Correct 2 ms 2656 KB Output is correct
38 Correct 2 ms 2644 KB Output is correct
39 Correct 44 ms 12564 KB Output is correct
40 Correct 29 ms 12252 KB Output is correct
41 Correct 22 ms 10712 KB Output is correct
42 Correct 7 ms 4052 KB Output is correct
43 Correct 2 ms 2656 KB Output is correct
44 Correct 2 ms 2772 KB Output is correct
45 Correct 2 ms 2644 KB Output is correct
46 Correct 1 ms 2644 KB Output is correct
47 Correct 1 ms 2644 KB Output is correct
48 Correct 2 ms 2644 KB Output is correct
49 Correct 2 ms 2644 KB Output is correct
50 Correct 1 ms 2644 KB Output is correct
51 Correct 2 ms 2644 KB Output is correct
52 Correct 1 ms 2644 KB Output is correct
53 Correct 1 ms 2644 KB Output is correct
54 Correct 2 ms 2644 KB Output is correct
55 Correct 1 ms 2644 KB Output is correct
56 Correct 1 ms 2644 KB Output is correct
57 Correct 36 ms 10012 KB Output is correct
58 Correct 39 ms 10024 KB Output is correct
59 Correct 38 ms 9940 KB Output is correct
60 Correct 50 ms 9932 KB Output is correct
61 Correct 38 ms 9960 KB Output is correct
62 Correct 38 ms 9932 KB Output is correct
63 Correct 37 ms 9652 KB Output is correct
64 Correct 36 ms 9700 KB Output is correct
65 Correct 42 ms 9884 KB Output is correct
66 Correct 54 ms 9992 KB Output is correct
67 Correct 38 ms 9940 KB Output is correct
68 Correct 39 ms 9972 KB Output is correct
69 Correct 38 ms 10140 KB Output is correct
70 Correct 45 ms 10316 KB Output is correct
71 Correct 1 ms 2644 KB Output is correct
72 Correct 2 ms 2772 KB Output is correct
73 Correct 3 ms 2772 KB Output is correct
74 Correct 2 ms 2840 KB Output is correct
75 Correct 2 ms 2772 KB Output is correct
76 Correct 2 ms 2888 KB Output is correct
77 Correct 2 ms 2900 KB Output is correct
78 Correct 3 ms 2848 KB Output is correct
79 Correct 2 ms 2796 KB Output is correct
80 Correct 39 ms 9888 KB Output is correct
81 Correct 38 ms 9948 KB Output is correct
82 Correct 39 ms 9936 KB Output is correct
83 Correct 39 ms 9988 KB Output is correct
84 Correct 2 ms 2772 KB Output is correct
85 Correct 2 ms 2800 KB Output is correct
86 Correct 2 ms 2772 KB Output is correct
87 Correct 3 ms 2772 KB Output is correct
88 Correct 2 ms 2772 KB Output is correct
89 Correct 3 ms 2796 KB Output is correct
90 Correct 3 ms 2772 KB Output is correct
91 Correct 3 ms 2848 KB Output is correct
92 Correct 3 ms 2796 KB Output is correct
93 Correct 2 ms 2772 KB Output is correct
94 Correct 1 ms 2644 KB Output is correct
95 Correct 2 ms 2668 KB Output is correct
96 Correct 2 ms 2644 KB Output is correct
97 Correct 1 ms 2644 KB Output is correct
98 Correct 2 ms 2644 KB Output is correct
99 Correct 2 ms 2644 KB Output is correct
100 Correct 2 ms 2664 KB Output is correct
101 Correct 2 ms 2644 KB Output is correct
102 Correct 2 ms 2644 KB Output is correct
103 Correct 1 ms 2644 KB Output is correct
104 Correct 5 ms 3424 KB Output is correct
105 Correct 9 ms 4880 KB Output is correct
106 Correct 2 ms 2644 KB Output is correct
107 Correct 16 ms 6740 KB Output is correct
108 Correct 20 ms 8992 KB Output is correct
109 Correct 1 ms 2644 KB Output is correct
110 Correct 28 ms 10016 KB Output is correct
111 Correct 32 ms 11136 KB Output is correct
112 Correct 2 ms 2664 KB Output is correct
113 Correct 48 ms 17044 KB Output is correct
114 Correct 14 ms 6360 KB Output is correct
115 Incorrect 15 ms 6484 KB Output isn't correct
116 Halted 0 ms 0 KB -