#include <bits/stdc++.h>
using namespace std;
#define int long long
int n;
vector <int> adj[300001];
int sze[300001];
bool vis[300001];
void calc (int pos, int par) {
vis[pos] = 1;
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) {
vis[pos] = 1;
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; int m; cin >> m;
for (int i = 1; i < n; 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 (!vis[i]) calc(i, -1);
memset(vis, 0, sizeof(vis));
int ans = 0;
for (int i = 1; i <= n; i++) if (!vis[i]) ans += dfs(i, -1);
cout << 2 * ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
617 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
617 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
65 ms |
19356 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10076 KB |
Output is correct |
2 |
Correct |
2 ms |
10076 KB |
Output is correct |
3 |
Correct |
3 ms |
10076 KB |
Output is correct |
4 |
Correct |
4 ms |
10076 KB |
Output is correct |
5 |
Correct |
3 ms |
9984 KB |
Output is correct |
6 |
Correct |
3 ms |
10076 KB |
Output is correct |
7 |
Correct |
4 ms |
10180 KB |
Output is correct |
8 |
Correct |
3 ms |
10076 KB |
Output is correct |
9 |
Correct |
3 ms |
10172 KB |
Output is correct |
10 |
Incorrect |
3 ms |
10328 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
15088 KB |
Output is correct |
2 |
Correct |
69 ms |
14932 KB |
Output is correct |
3 |
Correct |
66 ms |
14992 KB |
Output is correct |
4 |
Correct |
61 ms |
14920 KB |
Output is correct |
5 |
Correct |
57 ms |
15036 KB |
Output is correct |
6 |
Correct |
64 ms |
18740 KB |
Output is correct |
7 |
Correct |
61 ms |
17748 KB |
Output is correct |
8 |
Correct |
61 ms |
16984 KB |
Output is correct |
9 |
Correct |
66 ms |
16396 KB |
Output is correct |
10 |
Incorrect |
60 ms |
14932 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
10072 KB |
Output is correct |
2 |
Correct |
4 ms |
10076 KB |
Output is correct |
3 |
Runtime error |
552 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
13632 KB |
Output is correct |
2 |
Correct |
79 ms |
14968 KB |
Output is correct |
3 |
Runtime error |
871 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
617 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
617 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |