Submission #561119

# Submission time Handle Problem Language Result Execution time Memory
561119 2022-05-12T09:45:10 Z Zanite Dreaming (IOI13_dreaming) C++17
47 / 100
130 ms 24812 KB
#include "dreaming.h"

#pragma GCC optimize("trapv")

#include <bits/stdc++.h>
using namespace std;

#define pii pair<int, int>
#define fi first
#define se second

const int INF = 2e9;
const int maxN = 1e5 + 5;

void chmax(int &a, int b) {a = max(a, b);}
void chmin(int &a, int b) {a = min(a, b);}

struct Node {
    int child, lg;
    Node(int _child, int _lg) :
        child(_child),
        lg(_lg)
    {}

    bool operator < (const Node &other) const {
        return lg > other.lg;
    }
};

int N, M, L, ans;
vector<pii> adj[maxN];
vector<Node> children[maxN];
int idx[maxN], longest[maxN], newLongest[maxN];

vector<int> curComponent, components;
void dfs(int cur, int par, int id) {
    idx[cur] = id;
	curComponent.push_back(cur);

    for (auto a : adj[cur]) {
        int nxt, w;
        tie(nxt, w) = a;
        if (nxt == par) continue;

        dfs(nxt, cur, id);
        chmax(longest[cur], longest[nxt] + w);
        children[cur].push_back(Node(nxt, longest[nxt] + w));
    }
}

void reroot(int cur, int par, int downwardsEdge) {
    if (par != -1) {
        // push "parent" as cur's child
        if (children[par][0].child == cur) {
            if (children[par].size() > 1) {
                children[cur].push_back(Node(par, children[par][1].lg + downwardsEdge));
            } else {
                children[cur].push_back(Node(par, downwardsEdge));
            }
            
        } else {
            children[cur].push_back(Node(par, children[par][0].lg + downwardsEdge));
        }
    }

	sort(children[cur].begin(), children[cur].end());

	// update newLongest
	for (auto c : children[cur]) {
		chmax(newLongest[cur], c.lg);
	}

	// go to children
	for (auto a : adj[cur]) {
		int nxt, w;
		tie(nxt, w) = a;
		if (nxt == par) continue;

		reroot(nxt, cur, w);
	}
}

void calculateTree(int root, int id) {
    // gather data about longest and children's longest for each node
	curComponent.clear();
    dfs(root, -1, id);

    // do reroot
    reroot(root, -1, -1);

	// find out the minimum newLongest of the component
	int curMin = INF;
	for (auto i : curComponent) {
		chmax(ans, newLongest[i]);
		chmin(curMin, newLongest[i]);
	}
	components.push_back(curMin);
}

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++) {
        adj[A[i]].push_back({B[i], T[i]});
        adj[B[i]].push_back({A[i], T[i]});
    }

    for (int i = 0, cur = 1; i < N; i++) {
        if (idx[i] == 0) {
            calculateTree(i, cur);
            cur++;
        }
    }

	/*
	for (int i = 0; i < N; i++) {
		printf("%d: %d %d\n", i, idx[i], newLongest[i]);
	}
	*/

	sort(components.begin(), components.end());
	int s = components.size();

	if (components.size() == 2) {
		chmax(ans, components[s-1] + components[s-2] + L);
	} else {
		int pos[3];
		pos[0] = max(
			components[s-1] + components[s-2] + L,
			components[s-2] + components[s-3] + 2*L
		);
		pos[1] = max(
			components[s-1] + components[s-2] + L,
			components[s-1] + components[s-3] + 2*L
		);
		pos[2] = max(
			components[s-1] + components[s-3] + L,
			components[s-1] + components[s-2] + 2*L
		);
		chmax(ans, min({pos[0], pos[1], pos[2]}));
	}
	return ans;
}
# Verdict Execution time Memory Grader output
1 Correct 64 ms 18696 KB Output is correct
2 Correct 72 ms 18308 KB Output is correct
3 Correct 47 ms 15944 KB Output is correct
4 Correct 15 ms 6908 KB Output is correct
5 Correct 9 ms 6100 KB Output is correct
6 Correct 18 ms 8052 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 29 ms 10600 KB Output is correct
9 Correct 43 ms 13644 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 64 ms 14812 KB Output is correct
12 Correct 92 ms 16716 KB Output is correct
13 Correct 4 ms 5076 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 5 ms 4948 KB Output is correct
4 Correct 4 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 4 ms 5012 KB Output is correct
7 Correct 4 ms 4948 KB Output is correct
8 Correct 4 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 4 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 4 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 4 ms 4948 KB Output is correct
20 Correct 3 ms 4960 KB Output is correct
21 Correct 4 ms 4948 KB Output is correct
22 Correct 4 ms 4948 KB Output is correct
23 Correct 3 ms 4948 KB Output is correct
24 Correct 3 ms 4948 KB Output is correct
25 Correct 4 ms 4948 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 64 ms 18696 KB Output is correct
2 Correct 72 ms 18308 KB Output is correct
3 Correct 47 ms 15944 KB Output is correct
4 Correct 15 ms 6908 KB Output is correct
5 Correct 9 ms 6100 KB Output is correct
6 Correct 18 ms 8052 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 29 ms 10600 KB Output is correct
9 Correct 43 ms 13644 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 64 ms 14812 KB Output is correct
12 Correct 92 ms 16716 KB Output is correct
13 Correct 4 ms 5076 KB Output is correct
14 Correct 4 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 5 ms 4948 KB Output is correct
17 Correct 4 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 4 ms 5012 KB Output is correct
20 Correct 4 ms 4948 KB Output is correct
21 Correct 4 ms 4948 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 3 ms 4948 KB Output is correct
24 Correct 3 ms 4948 KB Output is correct
25 Correct 3 ms 4948 KB Output is correct
26 Correct 4 ms 4948 KB Output is correct
27 Correct 3 ms 4948 KB Output is correct
28 Correct 3 ms 4948 KB Output is correct
29 Correct 4 ms 4948 KB Output is correct
30 Correct 3 ms 4948 KB Output is correct
31 Correct 3 ms 4948 KB Output is correct
32 Correct 4 ms 4948 KB Output is correct
33 Correct 3 ms 4960 KB Output is correct
34 Correct 4 ms 4948 KB Output is correct
35 Correct 4 ms 4948 KB Output is correct
36 Correct 3 ms 4948 KB Output is correct
37 Correct 3 ms 4948 KB Output is correct
38 Correct 4 ms 4948 KB Output is correct
39 Correct 78 ms 18676 KB Output is correct
40 Correct 93 ms 18324 KB Output is correct
41 Correct 50 ms 16020 KB Output is correct
42 Correct 10 ms 6996 KB Output is correct
43 Correct 3 ms 4948 KB Output is correct
44 Correct 3 ms 5016 KB Output is correct
45 Correct 3 ms 4948 KB Output is correct
46 Correct 3 ms 4948 KB Output is correct
47 Correct 5 ms 4948 KB Output is correct
48 Correct 3 ms 4948 KB Output is correct
49 Correct 4 ms 4948 KB Output is correct
50 Correct 4 ms 4948 KB Output is correct
51 Correct 4 ms 4948 KB Output is correct
52 Correct 3 ms 4948 KB Output is correct
53 Correct 3 ms 4948 KB Output is correct
54 Correct 3 ms 4948 KB Output is correct
55 Correct 3 ms 4948 KB Output is correct
56 Correct 3 ms 4948 KB Output is correct
57 Correct 99 ms 14864 KB Output is correct
58 Correct 115 ms 14736 KB Output is correct
59 Correct 121 ms 14956 KB Output is correct
60 Correct 119 ms 14712 KB Output is correct
61 Correct 124 ms 15040 KB Output is correct
62 Correct 96 ms 14968 KB Output is correct
63 Correct 118 ms 15020 KB Output is correct
64 Correct 90 ms 15044 KB Output is correct
65 Correct 101 ms 15044 KB Output is correct
66 Correct 95 ms 15072 KB Output is correct
67 Correct 124 ms 14968 KB Output is correct
68 Correct 98 ms 15000 KB Output is correct
69 Correct 111 ms 14868 KB Output is correct
70 Correct 105 ms 14752 KB Output is correct
71 Correct 3 ms 4948 KB Output is correct
72 Correct 8 ms 5332 KB Output is correct
73 Correct 6 ms 5332 KB Output is correct
74 Correct 5 ms 5332 KB Output is correct
75 Correct 5 ms 5204 KB Output is correct
76 Correct 5 ms 5288 KB Output is correct
77 Correct 7 ms 5284 KB Output is correct
78 Correct 5 ms 5332 KB Output is correct
79 Correct 5 ms 5332 KB Output is correct
80 Correct 109 ms 14832 KB Output is correct
81 Correct 130 ms 14804 KB Output is correct
82 Correct 106 ms 15028 KB Output is correct
83 Correct 108 ms 15012 KB Output is correct
84 Correct 5 ms 5232 KB Output is correct
85 Correct 4 ms 5164 KB Output is correct
86 Correct 6 ms 5332 KB Output is correct
87 Correct 6 ms 5288 KB Output is correct
88 Correct 5 ms 5204 KB Output is correct
89 Correct 5 ms 5204 KB Output is correct
90 Correct 6 ms 5284 KB Output is correct
91 Correct 5 ms 5332 KB Output is correct
92 Correct 5 ms 5204 KB Output is correct
93 Correct 5 ms 5204 KB Output is correct
94 Correct 3 ms 4948 KB Output is correct
95 Correct 3 ms 4948 KB Output is correct
96 Correct 3 ms 4948 KB Output is correct
97 Correct 3 ms 4948 KB Output is correct
98 Correct 3 ms 4948 KB Output is correct
99 Correct 3 ms 4948 KB Output is correct
100 Correct 5 ms 4948 KB Output is correct
101 Correct 4 ms 4948 KB Output is correct
102 Correct 3 ms 4948 KB Output is correct
103 Correct 4 ms 4948 KB Output is correct
104 Correct 11 ms 6100 KB Output is correct
105 Correct 20 ms 8020 KB Output is correct
106 Correct 3 ms 4948 KB Output is correct
107 Correct 34 ms 10668 KB Output is correct
108 Correct 62 ms 13752 KB Output is correct
109 Correct 5 ms 5164 KB Output is correct
110 Correct 63 ms 14824 KB Output is correct
111 Correct 91 ms 16704 KB Output is correct
112 Correct 4 ms 5076 KB Output is correct
113 Correct 114 ms 24812 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 30 ms 10568 KB Output is correct
2 Correct 32 ms 10636 KB Output is correct
3 Correct 32 ms 10752 KB Output is correct
4 Correct 30 ms 10640 KB Output is correct
5 Correct 35 ms 10604 KB Output is correct
6 Correct 48 ms 11216 KB Output is correct
7 Correct 52 ms 10836 KB Output is correct
8 Correct 33 ms 10536 KB Output is correct
9 Correct 30 ms 10460 KB Output is correct
10 Correct 31 ms 10832 KB Output is correct
11 Correct 4 ms 4948 KB Output is correct
12 Correct 7 ms 6608 KB Output is correct
13 Correct 9 ms 6736 KB Output is correct
14 Correct 9 ms 6692 KB Output is correct
15 Correct 10 ms 6608 KB Output is correct
16 Correct 10 ms 6608 KB Output is correct
17 Correct 7 ms 6224 KB Output is correct
18 Correct 8 ms 6680 KB Output is correct
19 Correct 8 ms 6608 KB Output is correct
20 Incorrect 3 ms 4948 KB Output isn't correct
21 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 4948 KB Output is correct
2 Correct 3 ms 4948 KB Output is correct
3 Correct 5 ms 4948 KB Output is correct
4 Correct 4 ms 4948 KB Output is correct
5 Correct 3 ms 4948 KB Output is correct
6 Correct 4 ms 5012 KB Output is correct
7 Correct 4 ms 4948 KB Output is correct
8 Correct 4 ms 4948 KB Output is correct
9 Correct 3 ms 4948 KB Output is correct
10 Correct 3 ms 4948 KB Output is correct
11 Correct 3 ms 4948 KB Output is correct
12 Correct 3 ms 4948 KB Output is correct
13 Correct 4 ms 4948 KB Output is correct
14 Correct 3 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 4 ms 4948 KB Output is correct
17 Correct 3 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 4 ms 4948 KB Output is correct
20 Correct 3 ms 4960 KB Output is correct
21 Correct 4 ms 4948 KB Output is correct
22 Correct 4 ms 4948 KB Output is correct
23 Correct 3 ms 4948 KB Output is correct
24 Correct 3 ms 4948 KB Output is correct
25 Correct 4 ms 4948 KB Output is correct
26 Correct 3 ms 5076 KB Output is correct
27 Correct 4 ms 5204 KB Output is correct
28 Correct 5 ms 5296 KB Output is correct
29 Correct 4 ms 5076 KB Output is correct
30 Correct 4 ms 5204 KB Output is correct
31 Correct 5 ms 5280 KB Output is correct
32 Correct 5 ms 5076 KB Output is correct
33 Correct 5 ms 5204 KB Output is correct
34 Correct 5 ms 5276 KB Output is correct
35 Correct 4 ms 4948 KB Output is correct
36 Correct 3 ms 4948 KB Output is correct
37 Correct 3 ms 4948 KB Output is correct
38 Correct 3 ms 4948 KB Output is correct
39 Correct 3 ms 4948 KB Output is correct
40 Correct 3 ms 4948 KB Output is correct
41 Correct 3 ms 4948 KB Output is correct
42 Correct 3 ms 4948 KB Output is correct
43 Correct 3 ms 5016 KB Output is correct
44 Correct 3 ms 4948 KB Output is correct
45 Correct 3 ms 4948 KB Output is correct
46 Correct 3 ms 4948 KB Output is correct
47 Correct 3 ms 4948 KB Output is correct
48 Correct 3 ms 4948 KB Output is correct
49 Correct 4 ms 4948 KB Output is correct
50 Correct 4 ms 4948 KB Output is correct
51 Correct 3 ms 4948 KB Output is correct
52 Incorrect 4 ms 4948 KB Output isn't correct
53 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 64 ms 18696 KB Output is correct
2 Correct 72 ms 18308 KB Output is correct
3 Correct 47 ms 15944 KB Output is correct
4 Correct 15 ms 6908 KB Output is correct
5 Correct 9 ms 6100 KB Output is correct
6 Correct 18 ms 8052 KB Output is correct
7 Correct 3 ms 4948 KB Output is correct
8 Correct 29 ms 10600 KB Output is correct
9 Correct 43 ms 13644 KB Output is correct
10 Correct 4 ms 5076 KB Output is correct
11 Correct 64 ms 14812 KB Output is correct
12 Correct 92 ms 16716 KB Output is correct
13 Correct 4 ms 5076 KB Output is correct
14 Correct 4 ms 4948 KB Output is correct
15 Correct 3 ms 4948 KB Output is correct
16 Correct 5 ms 4948 KB Output is correct
17 Correct 4 ms 4948 KB Output is correct
18 Correct 3 ms 4948 KB Output is correct
19 Correct 4 ms 5012 KB Output is correct
20 Correct 4 ms 4948 KB Output is correct
21 Correct 4 ms 4948 KB Output is correct
22 Correct 3 ms 4948 KB Output is correct
23 Correct 3 ms 4948 KB Output is correct
24 Correct 3 ms 4948 KB Output is correct
25 Correct 3 ms 4948 KB Output is correct
26 Correct 4 ms 4948 KB Output is correct
27 Correct 3 ms 4948 KB Output is correct
28 Correct 3 ms 4948 KB Output is correct
29 Correct 4 ms 4948 KB Output is correct
30 Correct 3 ms 4948 KB Output is correct
31 Correct 3 ms 4948 KB Output is correct
32 Correct 4 ms 4948 KB Output is correct
33 Correct 3 ms 4960 KB Output is correct
34 Correct 4 ms 4948 KB Output is correct
35 Correct 4 ms 4948 KB Output is correct
36 Correct 3 ms 4948 KB Output is correct
37 Correct 3 ms 4948 KB Output is correct
38 Correct 4 ms 4948 KB Output is correct
39 Correct 78 ms 18676 KB Output is correct
40 Correct 93 ms 18324 KB Output is correct
41 Correct 50 ms 16020 KB Output is correct
42 Correct 10 ms 6996 KB Output is correct
43 Correct 3 ms 4948 KB Output is correct
44 Correct 3 ms 5016 KB Output is correct
45 Correct 3 ms 4948 KB Output is correct
46 Correct 3 ms 4948 KB Output is correct
47 Correct 5 ms 4948 KB Output is correct
48 Correct 3 ms 4948 KB Output is correct
49 Correct 4 ms 4948 KB Output is correct
50 Correct 4 ms 4948 KB Output is correct
51 Correct 4 ms 4948 KB Output is correct
52 Correct 3 ms 4948 KB Output is correct
53 Correct 3 ms 4948 KB Output is correct
54 Correct 3 ms 4948 KB Output is correct
55 Correct 3 ms 4948 KB Output is correct
56 Correct 3 ms 4948 KB Output is correct
57 Correct 99 ms 14864 KB Output is correct
58 Correct 115 ms 14736 KB Output is correct
59 Correct 121 ms 14956 KB Output is correct
60 Correct 119 ms 14712 KB Output is correct
61 Correct 124 ms 15040 KB Output is correct
62 Correct 96 ms 14968 KB Output is correct
63 Correct 118 ms 15020 KB Output is correct
64 Correct 90 ms 15044 KB Output is correct
65 Correct 101 ms 15044 KB Output is correct
66 Correct 95 ms 15072 KB Output is correct
67 Correct 124 ms 14968 KB Output is correct
68 Correct 98 ms 15000 KB Output is correct
69 Correct 111 ms 14868 KB Output is correct
70 Correct 105 ms 14752 KB Output is correct
71 Correct 3 ms 4948 KB Output is correct
72 Correct 8 ms 5332 KB Output is correct
73 Correct 6 ms 5332 KB Output is correct
74 Correct 5 ms 5332 KB Output is correct
75 Correct 5 ms 5204 KB Output is correct
76 Correct 5 ms 5288 KB Output is correct
77 Correct 7 ms 5284 KB Output is correct
78 Correct 5 ms 5332 KB Output is correct
79 Correct 5 ms 5332 KB Output is correct
80 Correct 109 ms 14832 KB Output is correct
81 Correct 130 ms 14804 KB Output is correct
82 Correct 106 ms 15028 KB Output is correct
83 Correct 108 ms 15012 KB Output is correct
84 Correct 5 ms 5232 KB Output is correct
85 Correct 4 ms 5164 KB Output is correct
86 Correct 6 ms 5332 KB Output is correct
87 Correct 6 ms 5288 KB Output is correct
88 Correct 5 ms 5204 KB Output is correct
89 Correct 5 ms 5204 KB Output is correct
90 Correct 6 ms 5284 KB Output is correct
91 Correct 5 ms 5332 KB Output is correct
92 Correct 5 ms 5204 KB Output is correct
93 Correct 5 ms 5204 KB Output is correct
94 Correct 3 ms 4948 KB Output is correct
95 Correct 3 ms 4948 KB Output is correct
96 Correct 3 ms 4948 KB Output is correct
97 Correct 3 ms 4948 KB Output is correct
98 Correct 3 ms 4948 KB Output is correct
99 Correct 3 ms 4948 KB Output is correct
100 Correct 5 ms 4948 KB Output is correct
101 Correct 4 ms 4948 KB Output is correct
102 Correct 3 ms 4948 KB Output is correct
103 Correct 4 ms 4948 KB Output is correct
104 Correct 11 ms 6100 KB Output is correct
105 Correct 20 ms 8020 KB Output is correct
106 Correct 3 ms 4948 KB Output is correct
107 Correct 34 ms 10668 KB Output is correct
108 Correct 62 ms 13752 KB Output is correct
109 Correct 5 ms 5164 KB Output is correct
110 Correct 63 ms 14824 KB Output is correct
111 Correct 91 ms 16704 KB Output is correct
112 Correct 4 ms 5076 KB Output is correct
113 Correct 114 ms 24812 KB Output is correct
114 Correct 30 ms 10568 KB Output is correct
115 Correct 32 ms 10636 KB Output is correct
116 Correct 32 ms 10752 KB Output is correct
117 Correct 30 ms 10640 KB Output is correct
118 Correct 35 ms 10604 KB Output is correct
119 Correct 48 ms 11216 KB Output is correct
120 Correct 52 ms 10836 KB Output is correct
121 Correct 33 ms 10536 KB Output is correct
122 Correct 30 ms 10460 KB Output is correct
123 Correct 31 ms 10832 KB Output is correct
124 Correct 4 ms 4948 KB Output is correct
125 Correct 7 ms 6608 KB Output is correct
126 Correct 9 ms 6736 KB Output is correct
127 Correct 9 ms 6692 KB Output is correct
128 Correct 10 ms 6608 KB Output is correct
129 Correct 10 ms 6608 KB Output is correct
130 Correct 7 ms 6224 KB Output is correct
131 Correct 8 ms 6680 KB Output is correct
132 Correct 8 ms 6608 KB Output is correct
133 Incorrect 3 ms 4948 KB Output isn't correct
134 Halted 0 ms 0 KB -