Submission #944088

# Submission time Handle Problem Language Result Execution time Memory
944088 2024-03-12T08:17:53 Z wii Dreaming (IOI13_dreaming) C++17
59 / 100
1000 ms 31928 KB
#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;

#define sz(x) (int)(x).size()
const int MaxN = 1e5 + 5;

template<typename T> bool maximize(T &res, const T &val) { if (res < val) { res = val; return true; } return false; }
template<typename T> bool minimize(T &res, const T &val) { if (val < res) { res = val; return true; } return false; }

const int Inf = 0x3f3f3f3f;

int n, m, l;
vector<int> pref[MaxN], suff[MaxN];
vector<pair<int, int>> adj[MaxN];

int vis[MaxN];
int dep[MaxN], dp[MaxN];
void dfs(int u, int p) {
    vis[u] = true;

    for (auto [v, w]: adj[u]) if (v != p) {
        dep[v] = dep[u] + w;
        dfs(v, u);

        maximize(dp[u], dp[v] + w);
    }
}

int ans, diameter;
void reroot(int u, int p) {
    minimize(ans, dp[u]);
    maximize(diameter, dp[u]);

    for (int i = 0; i < sz(adj[u]); ++i) {
        auto [v, w] = adj[u][i];
        pref[u].emplace_back(dp[v] + w);
        suff[u].emplace_back(dp[v] + w);
    }

    for (int i = 0; i < sz(adj[u]); ++i) {
        auto [v, w] = adj[u][i];
        if (i) maximize(pref[u][i], pref[u][i - 1]);
    }

    for (int i = sz(adj[u]) - 1; i >= 0; --i) {
        auto [v, w] = adj[u][i];
        if (i + 1 < sz(adj[u])) maximize(suff[u][i], suff[u][i + 1]);
    }

    for (int i = 0; i < sz(adj[u]); ++i) {
        auto [v, w] = adj[u][i];
        if (v == p) continue;

        int old = dp[v];
        int nw = max((i ? pref[u][i - 1] : 0), (i + 1 < sz(adj[u]) ? suff[u][i + 1] : 0));

        dp[u] = nw;
        dp[v] = max(dp[u] + w, old);

        reroot(v, u);

        dp[u] = pref[u].back();
        dp[v] = old;
    }
}

int used[MaxN];
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) {
        int u = A[i];
        int v = B[i];
        int w = T[i];

        adj[u].emplace_back(v, w);
        adj[v].emplace_back(u, w);
    }

    vector<int> best;

    if (1)
    for (int i = 0; i < n; ++i) if (!vis[i]) {
        dfs(i, i);

        ans = Inf;
        reroot(i, i);

        best.push_back(ans);

        for (int i = 0; i < n; ++i) if (vis[i] && !used[i]) {
            //for (auto [v, w]: adj[i]) if (vis[v] && !used[v])
            //    cout << i << " " << v << " " << w << endl;
        } //cout << endl;

        for (int i = 0; i < n; ++i) if (vis[i]) used[i] = true;
    }

    sort(best.begin(), best.end(), greater<int>());
    int maxD = 0;

    maximize(maxD, best[0]);
    if (best.size() >= 2) maximize(maxD, best[0] + best[1] + l);
    if (best.size() >= 3) maximize(maxD, best[1] + best[2] + 2*l);

    return max(maxD, diameter);
}

Compilation message

dreaming.cpp: In function 'void reroot(int, int)':
dreaming.cpp:42:14: warning: structured binding declaration set but not used [-Wunused-but-set-variable]
   42 |         auto [v, w] = adj[u][i];
      |              ^~~~~~
dreaming.cpp:47:14: warning: structured binding declaration set but not used [-Wunused-but-set-variable]
   47 |         auto [v, w] = adj[u][i];
      |              ^~~~~~
# Verdict Execution time Memory Grader output
1 Correct 52 ms 24912 KB Output is correct
2 Correct 56 ms 25316 KB Output is correct
3 Correct 37 ms 22356 KB Output is correct
4 Correct 8 ms 12124 KB Output is correct
5 Correct 7 ms 11356 KB Output is correct
6 Correct 13 ms 13404 KB Output is correct
7 Correct 3 ms 10076 KB Output is correct
8 Correct 23 ms 16220 KB Output is correct
9 Correct 31 ms 19692 KB Output is correct
10 Correct 2 ms 10072 KB Output is correct
11 Correct 43 ms 20844 KB Output is correct
12 Correct 50 ms 23016 KB Output is correct
13 Correct 3 ms 10076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9820 KB Output is correct
2 Correct 2 ms 9816 KB Output is correct
3 Correct 2 ms 9820 KB Output is correct
4 Correct 2 ms 9820 KB Output is correct
5 Correct 2 ms 9820 KB Output is correct
6 Correct 2 ms 9816 KB Output is correct
7 Correct 2 ms 9820 KB Output is correct
8 Correct 2 ms 9820 KB Output is correct
9 Correct 2 ms 9816 KB Output is correct
10 Correct 2 ms 9972 KB Output is correct
11 Correct 2 ms 9820 KB Output is correct
12 Correct 2 ms 9816 KB Output is correct
13 Correct 3 ms 9816 KB Output is correct
14 Correct 2 ms 9820 KB Output is correct
15 Correct 2 ms 10044 KB Output is correct
16 Correct 2 ms 10072 KB Output is correct
17 Correct 2 ms 9820 KB Output is correct
18 Correct 2 ms 9820 KB Output is correct
19 Correct 2 ms 10060 KB Output is correct
20 Correct 2 ms 9820 KB Output is correct
21 Correct 2 ms 9820 KB Output is correct
22 Correct 2 ms 9820 KB Output is correct
23 Correct 2 ms 9820 KB Output is correct
24 Correct 2 ms 9820 KB Output is correct
25 Correct 2 ms 9820 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 24912 KB Output is correct
2 Correct 56 ms 25316 KB Output is correct
3 Correct 37 ms 22356 KB Output is correct
4 Correct 8 ms 12124 KB Output is correct
5 Correct 7 ms 11356 KB Output is correct
6 Correct 13 ms 13404 KB Output is correct
7 Correct 3 ms 10076 KB Output is correct
8 Correct 23 ms 16220 KB Output is correct
9 Correct 31 ms 19692 KB Output is correct
10 Correct 2 ms 10072 KB Output is correct
11 Correct 43 ms 20844 KB Output is correct
12 Correct 50 ms 23016 KB Output is correct
13 Correct 3 ms 10076 KB Output is correct
14 Correct 2 ms 9820 KB Output is correct
15 Correct 2 ms 9816 KB Output is correct
16 Correct 2 ms 9820 KB Output is correct
17 Correct 2 ms 9820 KB Output is correct
18 Correct 2 ms 9820 KB Output is correct
19 Correct 2 ms 9816 KB Output is correct
20 Correct 2 ms 9820 KB Output is correct
21 Correct 2 ms 9820 KB Output is correct
22 Correct 2 ms 9816 KB Output is correct
23 Correct 2 ms 9972 KB Output is correct
24 Correct 2 ms 9820 KB Output is correct
25 Correct 2 ms 9816 KB Output is correct
26 Correct 3 ms 9816 KB Output is correct
27 Correct 2 ms 9820 KB Output is correct
28 Correct 2 ms 10044 KB Output is correct
29 Correct 2 ms 10072 KB Output is correct
30 Correct 2 ms 9820 KB Output is correct
31 Correct 2 ms 9820 KB Output is correct
32 Correct 2 ms 10060 KB Output is correct
33 Correct 2 ms 9820 KB Output is correct
34 Correct 2 ms 9820 KB Output is correct
35 Correct 2 ms 9820 KB Output is correct
36 Correct 2 ms 9820 KB Output is correct
37 Correct 2 ms 9820 KB Output is correct
38 Correct 2 ms 9820 KB Output is correct
39 Correct 59 ms 25152 KB Output is correct
40 Correct 52 ms 24956 KB Output is correct
41 Correct 39 ms 22696 KB Output is correct
42 Correct 9 ms 12124 KB Output is correct
43 Correct 2 ms 9820 KB Output is correct
44 Correct 2 ms 9820 KB Output is correct
45 Correct 2 ms 9820 KB Output is correct
46 Correct 2 ms 9820 KB Output is correct
47 Correct 2 ms 9820 KB Output is correct
48 Correct 2 ms 9816 KB Output is correct
49 Correct 2 ms 9820 KB Output is correct
50 Correct 2 ms 9820 KB Output is correct
51 Correct 2 ms 9820 KB Output is correct
52 Correct 2 ms 9816 KB Output is correct
53 Correct 2 ms 9816 KB Output is correct
54 Correct 2 ms 9820 KB Output is correct
55 Correct 2 ms 9816 KB Output is correct
56 Correct 3 ms 9820 KB Output is correct
57 Correct 57 ms 20228 KB Output is correct
58 Correct 65 ms 20172 KB Output is correct
59 Correct 72 ms 20556 KB Output is correct
60 Correct 63 ms 20460 KB Output is correct
61 Correct 66 ms 20544 KB Output is correct
62 Correct 60 ms 20564 KB Output is correct
63 Correct 58 ms 20652 KB Output is correct
64 Correct 83 ms 20524 KB Output is correct
65 Correct 69 ms 20600 KB Output is correct
66 Correct 64 ms 20820 KB Output is correct
67 Correct 65 ms 20432 KB Output is correct
68 Correct 66 ms 20376 KB Output is correct
69 Correct 80 ms 20564 KB Output is correct
70 Correct 86 ms 20508 KB Output is correct
71 Correct 2 ms 9820 KB Output is correct
72 Correct 4 ms 10332 KB Output is correct
73 Correct 3 ms 10328 KB Output is correct
74 Correct 3 ms 10328 KB Output is correct
75 Correct 3 ms 10332 KB Output is correct
76 Correct 3 ms 10332 KB Output is correct
77 Correct 3 ms 10332 KB Output is correct
78 Correct 4 ms 10584 KB Output is correct
79 Correct 4 ms 10332 KB Output is correct
80 Correct 62 ms 20628 KB Output is correct
81 Correct 60 ms 20516 KB Output is correct
82 Correct 60 ms 20680 KB Output is correct
83 Correct 61 ms 20304 KB Output is correct
84 Correct 3 ms 10076 KB Output is correct
85 Correct 3 ms 10304 KB Output is correct
86 Correct 4 ms 10332 KB Output is correct
87 Correct 4 ms 10332 KB Output is correct
88 Correct 3 ms 10332 KB Output is correct
89 Correct 3 ms 10332 KB Output is correct
90 Correct 3 ms 10072 KB Output is correct
91 Correct 3 ms 10332 KB Output is correct
92 Correct 4 ms 10332 KB Output is correct
93 Correct 3 ms 10332 KB Output is correct
94 Correct 2 ms 10028 KB Output is correct
95 Correct 2 ms 9816 KB Output is correct
96 Correct 2 ms 9820 KB Output is correct
97 Correct 2 ms 9820 KB Output is correct
98 Correct 2 ms 9820 KB Output is correct
99 Correct 2 ms 10060 KB Output is correct
100 Correct 2 ms 9820 KB Output is correct
101 Correct 2 ms 9816 KB Output is correct
102 Correct 2 ms 9820 KB Output is correct
103 Correct 2 ms 9820 KB Output is correct
104 Correct 7 ms 11356 KB Output is correct
105 Correct 13 ms 13404 KB Output is correct
106 Correct 3 ms 10076 KB Output is correct
107 Correct 23 ms 16264 KB Output is correct
108 Correct 46 ms 19764 KB Output is correct
109 Correct 3 ms 10072 KB Output is correct
110 Correct 42 ms 20820 KB Output is correct
111 Correct 53 ms 22812 KB Output is correct
112 Correct 3 ms 10076 KB Output is correct
113 Correct 71 ms 31928 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1041 ms 12992 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 2 ms 9820 KB Output is correct
2 Correct 2 ms 9816 KB Output is correct
3 Correct 2 ms 9820 KB Output is correct
4 Correct 2 ms 9820 KB Output is correct
5 Correct 2 ms 9820 KB Output is correct
6 Correct 2 ms 9816 KB Output is correct
7 Correct 2 ms 9820 KB Output is correct
8 Correct 2 ms 9820 KB Output is correct
9 Correct 2 ms 9816 KB Output is correct
10 Correct 2 ms 9972 KB Output is correct
11 Correct 2 ms 9820 KB Output is correct
12 Correct 2 ms 9816 KB Output is correct
13 Correct 3 ms 9816 KB Output is correct
14 Correct 2 ms 9820 KB Output is correct
15 Correct 2 ms 10044 KB Output is correct
16 Correct 2 ms 10072 KB Output is correct
17 Correct 2 ms 9820 KB Output is correct
18 Correct 2 ms 9820 KB Output is correct
19 Correct 2 ms 10060 KB Output is correct
20 Correct 2 ms 9820 KB Output is correct
21 Correct 2 ms 9820 KB Output is correct
22 Correct 2 ms 9820 KB Output is correct
23 Correct 2 ms 9820 KB Output is correct
24 Correct 2 ms 9820 KB Output is correct
25 Correct 2 ms 9820 KB Output is correct
26 Correct 3 ms 10088 KB Output is correct
27 Correct 4 ms 10120 KB Output is correct
28 Correct 6 ms 10332 KB Output is correct
29 Correct 3 ms 10072 KB Output is correct
30 Correct 5 ms 10076 KB Output is correct
31 Correct 5 ms 10332 KB Output is correct
32 Correct 3 ms 10076 KB Output is correct
33 Correct 4 ms 10076 KB Output is correct
34 Correct 5 ms 10332 KB Output is correct
35 Correct 2 ms 9820 KB Output is correct
36 Correct 2 ms 9864 KB Output is correct
37 Correct 2 ms 9816 KB Output is correct
38 Correct 3 ms 9820 KB Output is correct
39 Correct 2 ms 9820 KB Output is correct
40 Correct 3 ms 9820 KB Output is correct
41 Correct 2 ms 9820 KB Output is correct
42 Correct 2 ms 9820 KB Output is correct
43 Correct 3 ms 9820 KB Output is correct
44 Correct 2 ms 9820 KB Output is correct
45 Correct 2 ms 9820 KB Output is correct
46 Correct 3 ms 9820 KB Output is correct
47 Correct 3 ms 9820 KB Output is correct
48 Correct 2 ms 9820 KB Output is correct
49 Correct 2 ms 9820 KB Output is correct
50 Correct 2 ms 9820 KB Output is correct
51 Correct 2 ms 9820 KB Output is correct
52 Correct 2 ms 9816 KB Output is correct
53 Correct 2 ms 9820 KB Output is correct
54 Correct 3 ms 10332 KB Output is correct
55 Correct 4 ms 10584 KB Output is correct
56 Correct 7 ms 10076 KB Output is correct
57 Correct 7 ms 10076 KB Output is correct
58 Correct 4 ms 10332 KB Output is correct
59 Correct 3 ms 10332 KB Output is correct
60 Correct 7 ms 10076 KB Output is correct
61 Correct 8 ms 10076 KB Output is correct
62 Correct 4 ms 10344 KB Output is correct
63 Correct 3 ms 10332 KB Output is correct
64 Correct 7 ms 10088 KB Output is correct
65 Correct 7 ms 10308 KB Output is correct
66 Correct 4 ms 10332 KB Output is correct
67 Correct 4 ms 10332 KB Output is correct
68 Correct 7 ms 10324 KB Output is correct
69 Correct 6 ms 10076 KB Output is correct
70 Correct 8 ms 10076 KB Output is correct
71 Correct 7 ms 10128 KB Output is correct
72 Correct 3 ms 10328 KB Output is correct
73 Correct 3 ms 10304 KB Output is correct
74 Correct 4 ms 10244 KB Output is correct
75 Correct 4 ms 10332 KB Output is correct
76 Correct 3 ms 10316 KB Output is correct
77 Correct 4 ms 10328 KB Output is correct
78 Correct 8 ms 10076 KB Output is correct
79 Correct 8 ms 10220 KB Output is correct
80 Correct 8 ms 10076 KB Output is correct
81 Correct 7 ms 10076 KB Output is correct
82 Correct 5 ms 10332 KB Output is correct
83 Correct 5 ms 10328 KB Output is correct
84 Correct 7 ms 10076 KB Output is correct
85 Correct 8 ms 10076 KB Output is correct
86 Correct 7 ms 10076 KB Output is correct
87 Correct 4 ms 10332 KB Output is correct
88 Correct 6 ms 10076 KB Output is correct
89 Correct 7 ms 10076 KB Output is correct
90 Correct 8 ms 10156 KB Output is correct
91 Correct 7 ms 10076 KB Output is correct
92 Correct 3 ms 10332 KB Output is correct
93 Correct 3 ms 10076 KB Output is correct
94 Correct 3 ms 10332 KB Output is correct
95 Correct 3 ms 10332 KB Output is correct
96 Correct 4 ms 10332 KB Output is correct
97 Correct 3 ms 10332 KB Output is correct
98 Correct 4 ms 10332 KB Output is correct
99 Correct 3 ms 10332 KB Output is correct
100 Correct 4 ms 10332 KB Output is correct
101 Correct 4 ms 10332 KB Output is correct
102 Correct 2 ms 9820 KB Output is correct
103 Correct 3 ms 9820 KB Output is correct
104 Correct 2 ms 9820 KB Output is correct
105 Correct 2 ms 9824 KB Output is correct
106 Correct 3 ms 9820 KB Output is correct
107 Correct 2 ms 9820 KB Output is correct
108 Correct 4 ms 9820 KB Output is correct
109 Correct 2 ms 9820 KB Output is correct
110 Correct 3 ms 10076 KB Output is correct
111 Correct 2 ms 9820 KB Output is correct
112 Correct 3 ms 10076 KB Output is correct
113 Correct 3 ms 10072 KB Output is correct
114 Correct 3 ms 10080 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 52 ms 24912 KB Output is correct
2 Correct 56 ms 25316 KB Output is correct
3 Correct 37 ms 22356 KB Output is correct
4 Correct 8 ms 12124 KB Output is correct
5 Correct 7 ms 11356 KB Output is correct
6 Correct 13 ms 13404 KB Output is correct
7 Correct 3 ms 10076 KB Output is correct
8 Correct 23 ms 16220 KB Output is correct
9 Correct 31 ms 19692 KB Output is correct
10 Correct 2 ms 10072 KB Output is correct
11 Correct 43 ms 20844 KB Output is correct
12 Correct 50 ms 23016 KB Output is correct
13 Correct 3 ms 10076 KB Output is correct
14 Correct 2 ms 9820 KB Output is correct
15 Correct 2 ms 9816 KB Output is correct
16 Correct 2 ms 9820 KB Output is correct
17 Correct 2 ms 9820 KB Output is correct
18 Correct 2 ms 9820 KB Output is correct
19 Correct 2 ms 9816 KB Output is correct
20 Correct 2 ms 9820 KB Output is correct
21 Correct 2 ms 9820 KB Output is correct
22 Correct 2 ms 9816 KB Output is correct
23 Correct 2 ms 9972 KB Output is correct
24 Correct 2 ms 9820 KB Output is correct
25 Correct 2 ms 9816 KB Output is correct
26 Correct 3 ms 9816 KB Output is correct
27 Correct 2 ms 9820 KB Output is correct
28 Correct 2 ms 10044 KB Output is correct
29 Correct 2 ms 10072 KB Output is correct
30 Correct 2 ms 9820 KB Output is correct
31 Correct 2 ms 9820 KB Output is correct
32 Correct 2 ms 10060 KB Output is correct
33 Correct 2 ms 9820 KB Output is correct
34 Correct 2 ms 9820 KB Output is correct
35 Correct 2 ms 9820 KB Output is correct
36 Correct 2 ms 9820 KB Output is correct
37 Correct 2 ms 9820 KB Output is correct
38 Correct 2 ms 9820 KB Output is correct
39 Correct 59 ms 25152 KB Output is correct
40 Correct 52 ms 24956 KB Output is correct
41 Correct 39 ms 22696 KB Output is correct
42 Correct 9 ms 12124 KB Output is correct
43 Correct 2 ms 9820 KB Output is correct
44 Correct 2 ms 9820 KB Output is correct
45 Correct 2 ms 9820 KB Output is correct
46 Correct 2 ms 9820 KB Output is correct
47 Correct 2 ms 9820 KB Output is correct
48 Correct 2 ms 9816 KB Output is correct
49 Correct 2 ms 9820 KB Output is correct
50 Correct 2 ms 9820 KB Output is correct
51 Correct 2 ms 9820 KB Output is correct
52 Correct 2 ms 9816 KB Output is correct
53 Correct 2 ms 9816 KB Output is correct
54 Correct 2 ms 9820 KB Output is correct
55 Correct 2 ms 9816 KB Output is correct
56 Correct 3 ms 9820 KB Output is correct
57 Correct 57 ms 20228 KB Output is correct
58 Correct 65 ms 20172 KB Output is correct
59 Correct 72 ms 20556 KB Output is correct
60 Correct 63 ms 20460 KB Output is correct
61 Correct 66 ms 20544 KB Output is correct
62 Correct 60 ms 20564 KB Output is correct
63 Correct 58 ms 20652 KB Output is correct
64 Correct 83 ms 20524 KB Output is correct
65 Correct 69 ms 20600 KB Output is correct
66 Correct 64 ms 20820 KB Output is correct
67 Correct 65 ms 20432 KB Output is correct
68 Correct 66 ms 20376 KB Output is correct
69 Correct 80 ms 20564 KB Output is correct
70 Correct 86 ms 20508 KB Output is correct
71 Correct 2 ms 9820 KB Output is correct
72 Correct 4 ms 10332 KB Output is correct
73 Correct 3 ms 10328 KB Output is correct
74 Correct 3 ms 10328 KB Output is correct
75 Correct 3 ms 10332 KB Output is correct
76 Correct 3 ms 10332 KB Output is correct
77 Correct 3 ms 10332 KB Output is correct
78 Correct 4 ms 10584 KB Output is correct
79 Correct 4 ms 10332 KB Output is correct
80 Correct 62 ms 20628 KB Output is correct
81 Correct 60 ms 20516 KB Output is correct
82 Correct 60 ms 20680 KB Output is correct
83 Correct 61 ms 20304 KB Output is correct
84 Correct 3 ms 10076 KB Output is correct
85 Correct 3 ms 10304 KB Output is correct
86 Correct 4 ms 10332 KB Output is correct
87 Correct 4 ms 10332 KB Output is correct
88 Correct 3 ms 10332 KB Output is correct
89 Correct 3 ms 10332 KB Output is correct
90 Correct 3 ms 10072 KB Output is correct
91 Correct 3 ms 10332 KB Output is correct
92 Correct 4 ms 10332 KB Output is correct
93 Correct 3 ms 10332 KB Output is correct
94 Correct 2 ms 10028 KB Output is correct
95 Correct 2 ms 9816 KB Output is correct
96 Correct 2 ms 9820 KB Output is correct
97 Correct 2 ms 9820 KB Output is correct
98 Correct 2 ms 9820 KB Output is correct
99 Correct 2 ms 10060 KB Output is correct
100 Correct 2 ms 9820 KB Output is correct
101 Correct 2 ms 9816 KB Output is correct
102 Correct 2 ms 9820 KB Output is correct
103 Correct 2 ms 9820 KB Output is correct
104 Correct 7 ms 11356 KB Output is correct
105 Correct 13 ms 13404 KB Output is correct
106 Correct 3 ms 10076 KB Output is correct
107 Correct 23 ms 16264 KB Output is correct
108 Correct 46 ms 19764 KB Output is correct
109 Correct 3 ms 10072 KB Output is correct
110 Correct 42 ms 20820 KB Output is correct
111 Correct 53 ms 22812 KB Output is correct
112 Correct 3 ms 10076 KB Output is correct
113 Correct 71 ms 31928 KB Output is correct
114 Execution timed out 1041 ms 12992 KB Time limit exceeded
115 Halted 0 ms 0 KB -