Submission #1115810

#TimeUsernameProblemLanguageResultExecution timeMemory
1115810gustavo_dDuathlon (APIO18_duathlon)C++17
23 / 100
1094 ms1048576 KiB
// https://oj.uz/problem/view/APIO18_duathlon > p520 #include <bits/stdc++.h> using namespace std; typedef long long ll; const int MAXN = 1e5; vector<int> adj[MAXN]; ll sz[MAXN]; ll sum_dist[MAXN]; bool vis[MAXN]; ll ans = 0; void dfs(int v, int pai) { vis[v] = true; sz[v] = 1; sum_dist[v] = 0; for (int viz : adj[v]) { if (viz == pai) continue; dfs(viz, v); sz[v] += sz[viz]; sum_dist[v] += sum_dist[viz] + sz[viz]; } ll add = 0; for (int viz : adj[v]) { if (viz == pai) continue; ans += 2LL*sum_dist[viz]; // lca sendo ponta ans += 2LL*sum_dist[viz] * (sz[v] - sz[viz] - 1LL); add += 2LL*sz[viz] * (sz[v] - sz[viz] - 1LL); } ans += add / 2; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int n, m; cin >> n >> m; for (int i=0; i<m; i++) { int u, v; cin >> u >> v; u--; v--; adj[u].push_back(v); adj[v].push_back(u); } for (int i=0; i<n; i++) { if (vis[i]) continue; dfs(i, -1); } cout << ans << '\n'; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...