#include "bits/stdc++.h"
using namespace std;
const int MAXN = 1e5+5;
vector< vector<int> > adj(MAXN);
bool visited[MAXN];
long long sz[MAXN];
int n, m;
pair<long long, bool> subtask3(int v, int p)
{
pair<long long, bool> ans = {1, false};
visited[v] = true;
for(int e : adj[v])
{
if(e == p)
continue;
if(visited[e])
ans.second = true;
pair<long long, bool> temp = subtask3(e, v);
ans.first += temp.first;
ans.second |= temp.second;
}
return ans;
}
long long subtask5(int v, int p)
{
long long ans = 0;
visited[v] = true;
sz[v] = 1;
long long chi_size = 0;
for(int e : adj[v])
{
if(e == p)
continue;
ans += subtask5(e, v);
sz[v] += sz[e];
chi_size += sz[e];
}
long long par_size = n - chi_size - 1;
ans += par_size * chi_size;
for(int e : adj[v])
{
ans += sz[e] * (n - sz[e] - 1);
}
return ans;
}
int main()
{
cin >> n >> m;
for(int i = 1; i <= m; i++)
{
int f, s;
cin >> f >> s;
adj[f].push_back(s);
adj[s].push_back(f);
}
int max_size = 0;
for(int i = 1; i <= n; i++)
{
max_size = max(max_size, (int)adj[i].size());
}
long long ans = 0;
if(max_size <= 2) // Subtask 3
{
for(int i = 1; i <= n; i++)
{
if(visited[i])
continue;
pair<long long, bool> curr = subtask3(i, -1);
if(curr.second)
{
ans += curr.first * (curr.first - 1) * (curr.first - 2);
} else if(curr.first >= 3)
{
long long temp = 0;
for(long long i = 1; i <= curr.first - 2; i++)
{
temp += i;
}
for(long long i = curr.first - 2; i >= 1; i--)
{
ans += temp * 2;
temp -= i;
}
}
}
} else
{
for(int i = 1; i <= n; i++)
{
if(visited[i])
continue;
ans += subtask5(i, -1);
}
}
cout << ans << "\n";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
640 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
640 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1095 ms |
643844 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
2676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
89 ms |
7972 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2692 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
130 ms |
7916 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
640 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
640 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |