Submission #947636

#TimeUsernameProblemLanguageResultExecution timeMemory
947636atomHard route (IZhO17_road)C++17
52 / 100
565 ms1364 KiB
#include "bits/stdc++.h" // @JASPER'S BOILERPLATE using namespace std; using ll = long long; #ifdef JASPER #include "debug.h" #else #define debug(...) 166 #endif const int N = 5e3 + 5; const int INF = 1e9; int n; vector <int> adj[N]; ll ans, cnt; using T = pair <ll, ll>; T dp[N]; // <maximum distance from current node, number of ways> void dfs(int u, int p, int dep) { dp[u] = make_pair(dep, 1); for (int v : adj[u]) { if (v == p) continue; dfs(v, u, dep + 1); if (dp[u].first < dp[v].first) dp[u] = make_pair(dp[v].first, dp[v].second); else if (dp[u].first == dp[v].first) dp[u].second += dp[v].second; } } signed main() { cin.tie(0) -> sync_with_stdio(0); cin >> n; for (int i = 1; i < n; ++i) { int u, v; cin >> u >> v; adj[u].push_back(v); adj[v].push_back(u); } //sub2: O(n^2) ans = 0, cnt = 1; for (int u = 1; u <= n; ++u) { dfs(u, u, 0); vector <T> cand; for (int v : adj[u]) cand.push_back(dp[v]); sort(cand.rbegin(), cand.rend()); debug(cand); if ((int) cand.size() <= 2) continue; // at least three candidates; ll hardness = cand[0].first * (cand[1].first + cand[2].first); ll cur_cnt = 0; ll d = 0; for (int i = 0; i < (int) (cand.size()); ++i) { if (cand[i].first == cand[2].first) cur_cnt += d * cand[2].second; if (cand[i].first == cand[1].first) d += cand[i].second; } if (ans < hardness) { ans = hardness; cnt = cur_cnt; } else if (ans == hardness) cnt += cur_cnt; } cout << ans << " " << cnt << "\n"; return 0; } /* leaf nodes always product maximum distance? */

Compilation message (stderr)

road.cpp: In function 'int main()':
road.cpp:9:20: warning: statement has no effect [-Wunused-value]
    9 | #define debug(...) 166
      |                    ^~~
road.cpp:51:9: note: in expansion of macro 'debug'
   51 |         debug(cand);
      |         ^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...