#include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
vector <int> adj[200001];
int sze[200001];
void calc (int pos, int par) {
for (auto j : adj[pos]) {
if (j == par) continue;
calc(j, pos);
sze[pos] += sze[j];
}
sze[pos]++;
}
int dfs (int pos, int par, int cnt = 0) {
int sum = (sze[pos] - 1) * cnt;
int sum2 = sze[pos] - 1;
for (auto j : adj[pos]) {
if (j == par) continue;
sum += dfs(j, pos, cnt + sze[pos] - sze[j]);
sum2 -= sze[j];
sum += sze[j] * (sum2);
}
return sum;
}
signed main () {
cin >> n;
for (int i = 1; i < n; i++) {
int a, b;
cin >> a >> b;
adj[a].push_back(b);
adj[b].push_back(a);
}
calc(1, -1);
cout << dfs(1, -1) << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
6496 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
6496 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1109 ms |
899212 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
732 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
863 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
643 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
52 ms |
11312 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
6496 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
6496 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |