// 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
921 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
921 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1094 ms |
742472 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4176 KB |
Output is correct |
2 |
Correct |
3 ms |
4176 KB |
Output is correct |
3 |
Correct |
3 ms |
4176 KB |
Output is correct |
4 |
Correct |
3 ms |
4348 KB |
Output is correct |
5 |
Correct |
2 ms |
4300 KB |
Output is correct |
6 |
Correct |
3 ms |
4176 KB |
Output is correct |
7 |
Correct |
3 ms |
4484 KB |
Output is correct |
8 |
Correct |
3 ms |
4176 KB |
Output is correct |
9 |
Correct |
2 ms |
4176 KB |
Output is correct |
10 |
Correct |
3 ms |
4360 KB |
Output is correct |
11 |
Correct |
3 ms |
4344 KB |
Output is correct |
12 |
Correct |
3 ms |
4176 KB |
Output is correct |
13 |
Correct |
2 ms |
4176 KB |
Output is correct |
14 |
Correct |
3 ms |
4176 KB |
Output is correct |
15 |
Correct |
3 ms |
4432 KB |
Output is correct |
16 |
Correct |
3 ms |
4176 KB |
Output is correct |
17 |
Correct |
3 ms |
4344 KB |
Output is correct |
18 |
Correct |
2 ms |
4176 KB |
Output is correct |
19 |
Correct |
3 ms |
4176 KB |
Output is correct |
20 |
Correct |
3 ms |
4176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
7696 KB |
Output is correct |
2 |
Correct |
42 ms |
8264 KB |
Output is correct |
3 |
Correct |
32 ms |
8784 KB |
Output is correct |
4 |
Correct |
42 ms |
9032 KB |
Output is correct |
5 |
Correct |
34 ms |
8776 KB |
Output is correct |
6 |
Correct |
46 ms |
13920 KB |
Output is correct |
7 |
Correct |
37 ms |
12104 KB |
Output is correct |
8 |
Correct |
38 ms |
11336 KB |
Output is correct |
9 |
Correct |
44 ms |
10360 KB |
Output is correct |
10 |
Correct |
41 ms |
8468 KB |
Output is correct |
11 |
Correct |
30 ms |
8784 KB |
Output is correct |
12 |
Correct |
30 ms |
8784 KB |
Output is correct |
13 |
Correct |
35 ms |
8784 KB |
Output is correct |
14 |
Correct |
26 ms |
8524 KB |
Output is correct |
15 |
Correct |
27 ms |
8264 KB |
Output is correct |
16 |
Correct |
23 ms |
7248 KB |
Output is correct |
17 |
Correct |
22 ms |
9024 KB |
Output is correct |
18 |
Correct |
23 ms |
9032 KB |
Output is correct |
19 |
Correct |
24 ms |
9076 KB |
Output is correct |
20 |
Correct |
23 ms |
9040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4176 KB |
Output is correct |
2 |
Correct |
3 ms |
4432 KB |
Output is correct |
3 |
Runtime error |
914 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
7496 KB |
Output is correct |
2 |
Correct |
37 ms |
8776 KB |
Output is correct |
3 |
Execution timed out |
1046 ms |
1048576 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
921 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
921 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |