Submission #233625

#TimeUsernameProblemLanguageResultExecution timeMemory
233625dolphingarlicIslands (IOI08_islands)C++14
Compilation error
0 ms0 KiB
/* IOI 2008 Islands - to, split the graph up into its connected components. We want the sum of the longest paths in each component - Notice how each component is made of a single cycle with trees appended to each node on the cycle - Case 1: The longest path lies in one of the trees - Just use DP to find this length - Case 2: The longest path goes from one tree to the other and crosses the cycle - Let dp[i] = The length of the longest path going through node i - If we flatten the cycle by removing an edge, then notice how we can use prefix sums to get the answer in this case - Just handle these 2 cases for each component - Complexity: O(N) */ #include <bits/stdc++.h> #define FOR(i, x, y) for (int i = x; i < y; i++) typedef long long ll; using namespace std; struct Edge { int idx, to, len; bool operator!=(Edge B) { return idx != B.idx; } }; vector<Edge> graph[1000001], cycle; ll ans = 0; int cmp_ans, p[5][1000001]; int cycle_start = 0, discarded; bool processed[1000001], visited[1000001], on_cycle[1000001], adding = false; void find_cycle(int node, Edge parent = {0, 0, 0}) { if (visited[node]) { cycle_start = node; cycle.push_back({0, node, 0}); cycle.push_back(parent); adding = true; return; } visited[node] = true; for (Edge i : graph[node]) if (i != parent) { find_cycle(i.to, {i.idx, node, i.len}); if (cycle_start) { if (node == cycle_start) adding = false; if (adding) cycle.push_back(parent); return; } } } void calc_tree(int node, int parent = 0) { processed[node] = true; ll mx = 0, sc = 0; for (Edge i : graph[node]) if (i.to != parent && !on_cycle[i.to]) { calc_tree(i.to, node); if (p[4][i.to] + i.len > mx) sc = mx, mx = p[4][i.to] + i.len; else if (p[4][i.to] + i.len > sc) sc = p[4][i.to] + i.len; } p[4][node] = mx; cmp_ans = max(cmp_ans, mx + sc); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int n; cin >> n; FOR(i, 1, n + 1) { int v, l; cin >> v >> l; graph[i].push_back({i, v, l}); graph[v].push_back({i, i, l}); } FOR(i, 1, n + 1) if (!processed[i]) { cycle.clear(); cycle_start = 0; find_cycle(i); discarded = cycle.back().len; cycle.pop_back(); for (Edge j : cycle) on_cycle[j.to] = true; cmp_ans = 0; FOR(j, 1, cycle.size()) p[0][cycle[j].to] = p[0][cycle[j - 1].to] + cycle[j].len; for (int j = cycle.size() - 2; j >= 0; j--) p[1][cycle[j].to] = p[1][cycle[j + 1].to] + cycle[j + 1].len; ll mx = 0; FOR(j, 0, cycle.size()) { calc_tree(cycle[j].to); p[2][cycle[j].to] = p[4][cycle[j].to] - p[0][cycle[j].to]; p[3][cycle[j].to] = p[4][cycle[j].to] + p[0][cycle[j].to]; p[4][cycle[j].to] = p[4][cycle[j].to] + p[1][cycle[j].to]; cmp_ans = max(cmp_ans, p[3][cycle[j].to] + mx); mx = max(mx, p[2][cycle[j].to]); } mx = p[4][cycle.back().to]; for (int j = cycle.size() - 2; j >= 0; j--) { cmp_ans = max(cmp_ans, p[3][cycle[j].to] + mx + discarded); mx = max(mx, p[4][cycle[j].to]); } ans += cmp_ans; } cout << ans; return 0; }

Compilation message (stderr)

islands.cpp: In function 'void calc_tree(int, int)':
islands.cpp:60:35: error: no matching function for call to 'max(int&, ll)'
     cmp_ans = max(cmp_ans, mx + sc);
                                   ^
In file included from /usr/include/c++/7/bits/char_traits.h:39:0,
                 from /usr/include/c++/7/ios:40,
                 from /usr/include/c++/7/istream:38,
                 from /usr/include/c++/7/sstream:38,
                 from /usr/include/c++/7/complex:45,
                 from /usr/include/c++/7/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:52,
                 from islands.cpp:16:
/usr/include/c++/7/bits/stl_algobase.h:219:5: note: candidate: template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)
     max(const _Tp& __a, const _Tp& __b)
     ^~~
/usr/include/c++/7/bits/stl_algobase.h:219:5: note:   template argument deduction/substitution failed:
islands.cpp:60:35: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'll {aka long long int}')
     cmp_ans = max(cmp_ans, mx + sc);
                                   ^
In file included from /usr/include/c++/7/bits/char_traits.h:39:0,
                 from /usr/include/c++/7/ios:40,
                 from /usr/include/c++/7/istream:38,
                 from /usr/include/c++/7/sstream:38,
                 from /usr/include/c++/7/complex:45,
                 from /usr/include/c++/7/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:52,
                 from islands.cpp:16:
/usr/include/c++/7/bits/stl_algobase.h:265:5: note: candidate: template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)
     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
     ^~~
/usr/include/c++/7/bits/stl_algobase.h:265:5: note:   template argument deduction/substitution failed:
islands.cpp:60:35: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'll {aka long long int}')
     cmp_ans = max(cmp_ans, mx + sc);
                                   ^
In file included from /usr/include/c++/7/algorithm:62:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:65,
                 from islands.cpp:16:
/usr/include/c++/7/bits/stl_algo.h:3462:5: note: candidate: template<class _Tp> constexpr _Tp std::max(std::initializer_list<_Tp>)
     max(initializer_list<_Tp> __l)
     ^~~
/usr/include/c++/7/bits/stl_algo.h:3462:5: note:   template argument deduction/substitution failed:
islands.cpp:60:35: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
     cmp_ans = max(cmp_ans, mx + sc);
                                   ^
In file included from /usr/include/c++/7/algorithm:62:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:65,
                 from islands.cpp:16:
/usr/include/c++/7/bits/stl_algo.h:3468:5: note: candidate: template<class _Tp, class _Compare> constexpr _Tp std::max(std::initializer_list<_Tp>, _Compare)
     max(initializer_list<_Tp> __l, _Compare __comp)
     ^~~
/usr/include/c++/7/bits/stl_algo.h:3468:5: note:   template argument deduction/substitution failed:
islands.cpp:60:35: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
     cmp_ans = max(cmp_ans, mx + sc);
                                   ^
islands.cpp: In function 'int main()':
islands.cpp:17:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define FOR(i, x, y) for (int i = x; i < y; i++)
islands.cpp:86:13:
         FOR(j, 1, cycle.size()) p[0][cycle[j].to] = p[0][cycle[j - 1].to] + cycle[j].len;
             ~~~~~~~~~~~~~~~~~~          
islands.cpp:86:9: note: in expansion of macro 'FOR'
         FOR(j, 1, cycle.size()) p[0][cycle[j].to] = p[0][cycle[j - 1].to] + cycle[j].len;
         ^~~
islands.cpp:17:40: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
 #define FOR(i, x, y) for (int i = x; i < y; i++)
islands.cpp:91:13:
         FOR(j, 0, cycle.size()) {
             ~~~~~~~~~~~~~~~~~~          
islands.cpp:91:9: note: in expansion of macro 'FOR'
         FOR(j, 0, cycle.size()) {
         ^~~
islands.cpp:97:58: error: no matching function for call to 'max(int&, ll)'
             cmp_ans = max(cmp_ans, p[3][cycle[j].to] + mx);
                                                          ^
In file included from /usr/include/c++/7/bits/char_traits.h:39:0,
                 from /usr/include/c++/7/ios:40,
                 from /usr/include/c++/7/istream:38,
                 from /usr/include/c++/7/sstream:38,
                 from /usr/include/c++/7/complex:45,
                 from /usr/include/c++/7/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:52,
                 from islands.cpp:16:
/usr/include/c++/7/bits/stl_algobase.h:219:5: note: candidate: template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)
     max(const _Tp& __a, const _Tp& __b)
     ^~~
/usr/include/c++/7/bits/stl_algobase.h:219:5: note:   template argument deduction/substitution failed:
islands.cpp:97:58: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'll {aka long long int}')
             cmp_ans = max(cmp_ans, p[3][cycle[j].to] + mx);
                                                          ^
In file included from /usr/include/c++/7/bits/char_traits.h:39:0,
                 from /usr/include/c++/7/ios:40,
                 from /usr/include/c++/7/istream:38,
                 from /usr/include/c++/7/sstream:38,
                 from /usr/include/c++/7/complex:45,
                 from /usr/include/c++/7/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:52,
                 from islands.cpp:16:
/usr/include/c++/7/bits/stl_algobase.h:265:5: note: candidate: template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)
     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
     ^~~
/usr/include/c++/7/bits/stl_algobase.h:265:5: note:   template argument deduction/substitution failed:
islands.cpp:97:58: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'll {aka long long int}')
             cmp_ans = max(cmp_ans, p[3][cycle[j].to] + mx);
                                                          ^
In file included from /usr/include/c++/7/algorithm:62:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:65,
                 from islands.cpp:16:
/usr/include/c++/7/bits/stl_algo.h:3462:5: note: candidate: template<class _Tp> constexpr _Tp std::max(std::initializer_list<_Tp>)
     max(initializer_list<_Tp> __l)
     ^~~
/usr/include/c++/7/bits/stl_algo.h:3462:5: note:   template argument deduction/substitution failed:
islands.cpp:97:58: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
             cmp_ans = max(cmp_ans, p[3][cycle[j].to] + mx);
                                                          ^
In file included from /usr/include/c++/7/algorithm:62:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:65,
                 from islands.cpp:16:
/usr/include/c++/7/bits/stl_algo.h:3468:5: note: candidate: template<class _Tp, class _Compare> constexpr _Tp std::max(std::initializer_list<_Tp>, _Compare)
     max(initializer_list<_Tp> __l, _Compare __comp)
     ^~~
/usr/include/c++/7/bits/stl_algo.h:3468:5: note:   template argument deduction/substitution failed:
islands.cpp:97:58: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
             cmp_ans = max(cmp_ans, p[3][cycle[j].to] + mx);
                                                          ^
islands.cpp:98:43: error: no matching function for call to 'max(ll&, int&)'
             mx = max(mx, p[2][cycle[j].to]);
                                           ^
In file included from /usr/include/c++/7/bits/char_traits.h:39:0,
                 from /usr/include/c++/7/ios:40,
                 from /usr/include/c++/7/istream:38,
                 from /usr/include/c++/7/sstream:38,
                 from /usr/include/c++/7/complex:45,
                 from /usr/include/c++/7/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:52,
                 from islands.cpp:16:
/usr/include/c++/7/bits/stl_algobase.h:219:5: note: candidate: template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)
     max(const _Tp& __a, const _Tp& __b)
     ^~~
/usr/include/c++/7/bits/stl_algobase.h:219:5: note:   template argument deduction/substitution failed:
islands.cpp:98:43: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
             mx = max(mx, p[2][cycle[j].to]);
                                           ^
In file included from /usr/include/c++/7/bits/char_traits.h:39:0,
                 from /usr/include/c++/7/ios:40,
                 from /usr/include/c++/7/istream:38,
                 from /usr/include/c++/7/sstream:38,
                 from /usr/include/c++/7/complex:45,
                 from /usr/include/c++/7/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:52,
                 from islands.cpp:16:
/usr/include/c++/7/bits/stl_algobase.h:265:5: note: candidate: template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)
     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
     ^~~
/usr/include/c++/7/bits/stl_algobase.h:265:5: note:   template argument deduction/substitution failed:
islands.cpp:98:43: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
             mx = max(mx, p[2][cycle[j].to]);
                                           ^
In file included from /usr/include/c++/7/algorithm:62:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:65,
                 from islands.cpp:16:
/usr/include/c++/7/bits/stl_algo.h:3462:5: note: candidate: template<class _Tp> constexpr _Tp std::max(std::initializer_list<_Tp>)
     max(initializer_list<_Tp> __l)
     ^~~
/usr/include/c++/7/bits/stl_algo.h:3462:5: note:   template argument deduction/substitution failed:
islands.cpp:98:43: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
             mx = max(mx, p[2][cycle[j].to]);
                                           ^
In file included from /usr/include/c++/7/algorithm:62:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:65,
                 from islands.cpp:16:
/usr/include/c++/7/bits/stl_algo.h:3468:5: note: candidate: template<class _Tp, class _Compare> constexpr _Tp std::max(std::initializer_list<_Tp>, _Compare)
     max(initializer_list<_Tp> __l, _Compare __comp)
     ^~~
/usr/include/c++/7/bits/stl_algo.h:3468:5: note:   template argument deduction/substitution failed:
islands.cpp:98:43: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
             mx = max(mx, p[2][cycle[j].to]);
                                           ^
islands.cpp:103:70: error: no matching function for call to 'max(int&, ll)'
             cmp_ans = max(cmp_ans, p[3][cycle[j].to] + mx + discarded);
                                                                      ^
In file included from /usr/include/c++/7/bits/char_traits.h:39:0,
                 from /usr/include/c++/7/ios:40,
                 from /usr/include/c++/7/istream:38,
                 from /usr/include/c++/7/sstream:38,
                 from /usr/include/c++/7/complex:45,
                 from /usr/include/c++/7/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:52,
                 from islands.cpp:16:
/usr/include/c++/7/bits/stl_algobase.h:219:5: note: candidate: template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)
     max(const _Tp& __a, const _Tp& __b)
     ^~~
/usr/include/c++/7/bits/stl_algobase.h:219:5: note:   template argument deduction/substitution failed:
islands.cpp:103:70: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'll {aka long long int}')
             cmp_ans = max(cmp_ans, p[3][cycle[j].to] + mx + discarded);
                                                                      ^
In file included from /usr/include/c++/7/bits/char_traits.h:39:0,
                 from /usr/include/c++/7/ios:40,
                 from /usr/include/c++/7/istream:38,
                 from /usr/include/c++/7/sstream:38,
                 from /usr/include/c++/7/complex:45,
                 from /usr/include/c++/7/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:52,
                 from islands.cpp:16:
/usr/include/c++/7/bits/stl_algobase.h:265:5: note: candidate: template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)
     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
     ^~~
/usr/include/c++/7/bits/stl_algobase.h:265:5: note:   template argument deduction/substitution failed:
islands.cpp:103:70: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'll {aka long long int}')
             cmp_ans = max(cmp_ans, p[3][cycle[j].to] + mx + discarded);
                                                                      ^
In file included from /usr/include/c++/7/algorithm:62:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:65,
                 from islands.cpp:16:
/usr/include/c++/7/bits/stl_algo.h:3462:5: note: candidate: template<class _Tp> constexpr _Tp std::max(std::initializer_list<_Tp>)
     max(initializer_list<_Tp> __l)
     ^~~
/usr/include/c++/7/bits/stl_algo.h:3462:5: note:   template argument deduction/substitution failed:
islands.cpp:103:70: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
             cmp_ans = max(cmp_ans, p[3][cycle[j].to] + mx + discarded);
                                                                      ^
In file included from /usr/include/c++/7/algorithm:62:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:65,
                 from islands.cpp:16:
/usr/include/c++/7/bits/stl_algo.h:3468:5: note: candidate: template<class _Tp, class _Compare> constexpr _Tp std::max(std::initializer_list<_Tp>, _Compare)
     max(initializer_list<_Tp> __l, _Compare __comp)
     ^~~
/usr/include/c++/7/bits/stl_algo.h:3468:5: note:   template argument deduction/substitution failed:
islands.cpp:103:70: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
             cmp_ans = max(cmp_ans, p[3][cycle[j].to] + mx + discarded);
                                                                      ^
islands.cpp:104:43: error: no matching function for call to 'max(ll&, int&)'
             mx = max(mx, p[4][cycle[j].to]);
                                           ^
In file included from /usr/include/c++/7/bits/char_traits.h:39:0,
                 from /usr/include/c++/7/ios:40,
                 from /usr/include/c++/7/istream:38,
                 from /usr/include/c++/7/sstream:38,
                 from /usr/include/c++/7/complex:45,
                 from /usr/include/c++/7/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:52,
                 from islands.cpp:16:
/usr/include/c++/7/bits/stl_algobase.h:219:5: note: candidate: template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)
     max(const _Tp& __a, const _Tp& __b)
     ^~~
/usr/include/c++/7/bits/stl_algobase.h:219:5: note:   template argument deduction/substitution failed:
islands.cpp:104:43: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
             mx = max(mx, p[4][cycle[j].to]);
                                           ^
In file included from /usr/include/c++/7/bits/char_traits.h:39:0,
                 from /usr/include/c++/7/ios:40,
                 from /usr/include/c++/7/istream:38,
                 from /usr/include/c++/7/sstream:38,
                 from /usr/include/c++/7/complex:45,
                 from /usr/include/c++/7/ccomplex:39,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:52,
                 from islands.cpp:16:
/usr/include/c++/7/bits/stl_algobase.h:265:5: note: candidate: template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)
     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
     ^~~
/usr/include/c++/7/bits/stl_algobase.h:265:5: note:   template argument deduction/substitution failed:
islands.cpp:104:43: note:   deduced conflicting types for parameter 'const _Tp' ('long long int' and 'int')
             mx = max(mx, p[4][cycle[j].to]);
                                           ^
In file included from /usr/include/c++/7/algorithm:62:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:65,
                 from islands.cpp:16:
/usr/include/c++/7/bits/stl_algo.h:3462:5: note: candidate: template<class _Tp> constexpr _Tp std::max(std::initializer_list<_Tp>)
     max(initializer_list<_Tp> __l)
     ^~~
/usr/include/c++/7/bits/stl_algo.h:3462:5: note:   template argument deduction/substitution failed:
islands.cpp:104:43: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
             mx = max(mx, p[4][cycle[j].to]);
                                           ^
In file included from /usr/include/c++/7/algorithm:62:0,
                 from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:65,
                 from islands.cpp:16:
/usr/include/c++/7/bits/stl_algo.h:3468:5: note: candidate: template<class _Tp, class _Compare> constexpr _Tp std::max(std::initializer_list<_Tp>, _Compare)
     max(initializer_list<_Tp> __l, _Compare __comp)
     ^~~
/usr/include/c++/7/bits/stl_algo.h:3468:5: note:   template argument deduction/substitution failed:
islands.cpp:104:43: note:   mismatched types 'std::initializer_list<_Tp>' and 'long long int'
             mx = max(mx, p[4][cycle[j].to]);
                                           ^