이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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];
}
long long aux = (v == 1 ? 0ll : 1ll * (n - subtree[v]));
vector<long long> pref;
pref.push_back(aux);
long long local_ans = 0;
for (int u : adj[v]) if (u != p) {
local_ans += pref.back() * subtree[u];
pref.push_back(pref.back() + 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);
}
dfs(1);
cout << ans << '\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |