#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10, inf = 0x3f3f3f3f;
vector<int> grafo[maxn];
int sz[maxn];
int mark[maxn], root[maxn];
bool test[maxn];
void dfs(int u, int p=0){
if(mark[u]) return;
sz[u] = 1;
for(auto v : grafo[u]){
if(v == p) continue;
mark[v] = mark[u];
dfs(v, u);
sz[u] += sz[v];
}
}
int ans;
void solve(int u, int p=0, int acc=0){
if(test[u]) return;
test[u] = true;
ans += (2 * acc * (sz[u] - 1));
for(auto v : grafo[u]){
if(v == p) continue;
ans += (sz[u] - sz[v] - 1) * sz[v];
solve(v, u, sz[u] - sz[v] + acc);
}
}
int main(){
int n, m;
cin >> n >> m;
for(int i=1;i<=m;i++){
int a, b;
cin >> a >> b;
grafo[a].push_back(b);
grafo[b].push_back(a);
}
int cnt=0;
for(int i=1;i<=n;i++)
if(!mark[i]) mark[i] = ++cnt, root[cnt] = i, dfs(i);
for(int i=1;i<=cnt;i++)
solve(root[i]);
cout << ans << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
183 ms |
12904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
5 ms |
12904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
228 ms |
12904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
12904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
158 ms |
12904 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
2680 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |