#include "bits/stdc++.h"
#define ll long long
using namespace std;
const ll mod = 1000000007;
vector<int> v[100001];
int anc[100001][20], d[100001];
int logg = 20;
void dfs(int x, int par) {
for(int i : v[x]) {
if(i != par) {
anc[i][0] = x;
d[i] = d[x] + 1;
dfs(i, x);
}
}
}
int get_lca(int a, int b) {
if(d[a] < d[b])
swap(a, b);
int dif = d[a] - d[b];
for(int i = 0; i < logg; i++) {
if(dif & (1 << i))
a = anc[a][i];
}
if(a == b)
return a;
for(int i = logg - 1; i >= 0; i--) {
if(anc[a][i] != anc[b][i]) {
a = anc[a][i];
b = anc[b][i];
}
}
if(anc[a][0] == anc[b][0])
return anc[a][0];
return -1;
}
signed main () {
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
// freopen("cowland.in","r",stdin);
// freopen("cowland.out","w",stdout);
int n, m;
cin >> n >> m;
for(int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
v[a].push_back(b);
v[b].push_back(a);
}
for(int i = 1; i <= n; i++) {
if(!anc[i][0]) {
anc[i][0] = i;
dfs(i, 0);
}
}
for(int j = 1; j < logg; j++) {
for(int i = 1; i <= n; i++)
anc[i][j] = anc[anc[i][j - 1]][j - 1];
}
ll ans = 0;
for(int i = 1; i <= n; i++) {
for(int j = i + 1; j <= n; j++) {
int lca = get_lca(i, j);
if(lca == -1)
continue;
ll dis = (d[i] + d[j]) - (d[lca] * 2);
ans += ((dis - 1) * 2LL);
}
}
cout << ans;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
462 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
462 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1088 ms |
404968 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
2772 KB |
Output is correct |
2 |
Correct |
39 ms |
2772 KB |
Output is correct |
3 |
Correct |
41 ms |
2788 KB |
Output is correct |
4 |
Correct |
41 ms |
2836 KB |
Output is correct |
5 |
Correct |
48 ms |
2816 KB |
Output is correct |
6 |
Correct |
45 ms |
2812 KB |
Output is correct |
7 |
Correct |
46 ms |
2812 KB |
Output is correct |
8 |
Correct |
47 ms |
2772 KB |
Output is correct |
9 |
Correct |
47 ms |
2804 KB |
Output is correct |
10 |
Correct |
41 ms |
2688 KB |
Output is correct |
11 |
Correct |
88 ms |
2772 KB |
Output is correct |
12 |
Correct |
46 ms |
2772 KB |
Output is correct |
13 |
Correct |
47 ms |
2792 KB |
Output is correct |
14 |
Correct |
49 ms |
2772 KB |
Output is correct |
15 |
Correct |
47 ms |
2784 KB |
Output is correct |
16 |
Correct |
47 ms |
2772 KB |
Output is correct |
17 |
Correct |
35 ms |
2796 KB |
Output is correct |
18 |
Correct |
34 ms |
2772 KB |
Output is correct |
19 |
Correct |
36 ms |
2772 KB |
Output is correct |
20 |
Correct |
35 ms |
2772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1062 ms |
15052 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
2772 KB |
Output is correct |
2 |
Correct |
40 ms |
2792 KB |
Output is correct |
3 |
Runtime error |
935 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1081 ms |
15260 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
462 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
462 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |