Submission #317215

#TimeUsernameProblemLanguageResultExecution timeMemory
317215SeanliuBalanced Tree (info1cup18_balancedtree)C++14
Compilation error
0 ms0 KiB
#include <iostream> #include <vector> using namespace std; const int maxN = 3e5 + 326; int T, N, col[maxN], u, v, dep[maxN], md[maxN][2], ans; vector<int> adj[maxN]; void dfs(int p = 1, int u = 1){ dep[u] = dep[p] + 1; for(int v : adj[u]) if(p != v){ dfs(u, v); if(!md[u][0]){ md[u][0] = md[v][0]; } else { ans = max(ans, md[u][0] + md[v][0] - 2 * dep[u]); md[u][0] = max(md[u][0], md[v][0]); } if(!md[u][1]){ md[u][1] = md[v][1]; } else { ans = max(ans, md[u][1] + md[v][1] - 2 * dep[u]); md[u][1] = max(md[u][1], md[v][1]); } ans = max(md[v][col[u]] - dep[u]); } } void solve(){ cin >> N; ans = 0; for(int i = 0; i < N - 1; i++){ cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } int hasz = 0, haso = 0; for(int i = 1; i <= N; i++){ cin >> col[i]; (col[i] ? hasz : haso) += 1; } if(hasz < 2 || haso < 2){ cout << -1 << endl; } else { dfs(); cout << ans << endl; for(int i = 1; i <= N; i++) cout << col[i] << " \n"[i == N]; } for(int i = 1; i <= N; i++){ vector<int>().swap(adj[i]); md[i][0] = md[i][1] = 0; dep[i] = 0; } } int main(){ cin >> T; while(T--){ solve(); } }

Compilation message (stderr)

balancedtree.cpp: In function 'void dfs(int, int)':
balancedtree.cpp:25:35: error: no matching function for call to 'max(int)'
   25 |   ans = max(md[v][col[u]] - dep[u]);
      |                                   ^
In file included from /usr/include/c++/9/bits/char_traits.h:39,
                 from /usr/include/c++/9/ios:40,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from balancedtree.cpp:1:
/usr/include/c++/9/bits/stl_algobase.h:222:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  222 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/9/bits/stl_algobase.h:222:5: note:   template argument deduction/substitution failed:
balancedtree.cpp:25:35: note:   candidate expects 2 arguments, 1 provided
   25 |   ans = max(md[v][col[u]] - dep[u]);
      |                                   ^
In file included from /usr/include/c++/9/bits/char_traits.h:39,
                 from /usr/include/c++/9/ios:40,
                 from /usr/include/c++/9/ostream:38,
                 from /usr/include/c++/9/iostream:39,
                 from balancedtree.cpp:1:
/usr/include/c++/9/bits/stl_algobase.h:268:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  268 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/9/bits/stl_algobase.h:268:5: note:   template argument deduction/substitution failed:
balancedtree.cpp:25:35: note:   candidate expects 3 arguments, 1 provided
   25 |   ans = max(md[v][col[u]] - dep[u]);
      |                                   ^