#include <bits/stdc++.h>
using namespace std;
#define int long long
mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
const int INF = 1e18, N = 1e5 + 5;
vector<int> g[N];
int siz;
bool vis[N];
int subtree[N];
int ans = 0;
int n, m;
void dfsini(int node)
{
vis[node] = true;
subtree[node] = 1;
siz++;
for(int to: g[node])
{
if(vis[to]) continue;
dfsini(to);
subtree[node] += subtree[to];
}
}
void dfs_calc(int node, int par = -1)
{
ans += (siz - subtree[node]) * (n - (siz - subtree[node]) - 1);
for(int to: g[node])
{
if(to == par) continue;
ans += (subtree[to]) * (n - subtree[to] - 1);
}
for(int to: g[node])
{
if(to == par) continue;
dfs_calc(to, node);
}
}
void Solve()
{
cin >> n >> m;
for(int i = 1; i <= m; i++)
{
int u, v;
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
for(int i = 1; i <= n; i++)
{
if(!vis[i])
{
siz = 0;
dfsini(i);
dfs_calc(i);
}
}
cout << ans << "\n";
}
int32_t main()
{
auto begin = std::chrono::high_resolution_clock::now();
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
for(int i = 1; i <= t; i++)
{
//cout << "Case #" << i << ": ";
Solve();
}
auto end = std::chrono::high_resolution_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
//cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n";
return 0;
}
/*
Sample 1:
4 3
1 2
2 3
3 4
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
533 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
533 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1075 ms |
510020 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2684 KB |
Output is correct |
7 |
Correct |
2 ms |
2692 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
3 ms |
2644 KB |
Output is correct |
10 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
7232 KB |
Output is correct |
2 |
Correct |
47 ms |
8448 KB |
Output is correct |
3 |
Correct |
50 ms |
8456 KB |
Output is correct |
4 |
Correct |
50 ms |
8440 KB |
Output is correct |
5 |
Correct |
49 ms |
8524 KB |
Output is correct |
6 |
Correct |
60 ms |
11348 KB |
Output is correct |
7 |
Correct |
56 ms |
10668 KB |
Output is correct |
8 |
Correct |
52 ms |
10084 KB |
Output is correct |
9 |
Correct |
59 ms |
9464 KB |
Output is correct |
10 |
Incorrect |
43 ms |
8532 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Runtime error |
655 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
7180 KB |
Output is correct |
2 |
Correct |
47 ms |
8404 KB |
Output is correct |
3 |
Runtime error |
768 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
533 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
533 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |