제출 #374632

#제출 시각아이디문제언어결과실행 시간메모리
374632morato철인 이종 경기 (APIO18_duathlon)C++17
0 / 100
1150 ms1048576 KiB
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 5; long long ans; vector<int> adj[N]; int subtree[N], n; void dfs(int v, int p = 0) { subtree[v] = 1; for (int u : adj[v]) if (u != p) { dfs(u, v); subtree[v] += subtree[u]; } vector<long long> pref; pref.push_back(n - subtree[v]); long long local_ans = 0; for (int u : adj[v]) if (u != p) { assert(!pref.empty()); local_ans += pref.back() * 1ll * subtree[u]; pref.push_back(pref.back() + 1ll * subtree[u]); } ans += 2ll * local_ans; } int main() { ios::sync_with_stdio(false); cin.tie(0); int m; cin >> n >> m; for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; adj[a].push_back(b); adj[b].push_back(a); } for (int i = 1; i <= n; i++) { if (!subtree[i]) { dfs(i); } } 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...