#include<bits/stdc++.h>
using namespace std;
#define int ll
#define ll long long
#define pii pair<int, int>
vector<vector<int>> graph;
vector<vector<int>> cgraph;
vector<vector<ll>> opt;
vector<int> comp;
vector<int> low;
vector<int> num;
vector<int> vis;
vector<int> art;
vector<ll> compsize;
stack<int> s;
int root = -1;
ll sol = 0;
int c = 0;
int d = 0;
ll n, m;
void dfs(int i, int p)
{
num[i] = low[i] = ++c;
s.push(i);
for (int ele:graph[i])
{
if (ele == p) continue;
if (!num[ele])
{
dfs(ele, i);
if (low[ele]==num[i])
{
while(s.top() != ele)
{
comp[s.top()] = d;
compsize[d]++;
s.pop();
}
comp[ele] = d;
compsize[d]++;
s.pop();
d++;
}
}
low[i] = min(low[i], low[ele]);
}
if (num[i]==low[i])
{
art[d] = 1;
while (s.top() != i)
{
comp[s.top()] = d;
compsize[d]++;
s.pop();
}
comp[i] = d;
compsize[d]++;
s.pop();
d++;
}
}
void dfs2(int i, int p)
{
vis[i] = 1;
opt[i][0] = compsize[i];
opt[i][1] = (compsize[i]-1)*(compsize[i]-2) +compsize[i]-1;
if (!art[i]) opt[i][1] += compsize[i]-1;
sol += (compsize[i]-2)*compsize[i]*(compsize[i]-1);
if (!art[i]) sol += compsize[i]*(compsize[i]-1);
vector<array<ll, 2>> ones;
ll sum = 0;
for (int ele:cgraph[i])
{
if (vis[ele]) continue;
dfs2(ele, i);
opt[i][0] += opt[ele][0];
opt[i][1] += opt[ele][1] + opt[ele][0]*compsize[i];
sum += opt[ele][0];
ones.push_back({opt[ele][0], opt[ele][1]});
sol += opt[ele][0]*(compsize[i]-1)*(compsize[i]-2)*2;
sol += opt[ele][0]*(compsize[i]-1)*2;
if (!art[i]) sol += opt[ele][0]*(compsize[i]-1)*2;
sol += opt[ele][1]*compsize[i]*2;
}
for (array<ll, 2> ele:ones)
{
if (!art[i]) sol += ele[0]*(sum-ele[0])*(compsize[i]+1);
else sol += ele[0]*(sum-ele[0])*(compsize[i]);
sol += ele[1]*(sum-ele[0])*2;
}
}
int32_t main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cin >> n >> m;
graph.assign(n, vector<int>());
cgraph.assign(n, vector<int>());
compsize.assign(n, 0);
low.assign(n, 0);
num.assign(n, 0);
comp.assign(n, -1);
vis.assign(n, 0);
art.assign(n, 0);
opt.assign(n, vector<ll>(2, 0));
vector<int> degree(n, 0);
for (int i =0; i < m; i++)
{
int a, b;
cin >> a >> b;
a--; b--;
graph[a].push_back(b);
graph[b].push_back(a);
degree[a]++;
degree[b]++;
}
vector<pii> nodes;
for (int i= 0; i < n; i++) nodes.push_back({degree[i], i});
sort(nodes.begin(), nodes.end());
for (pii ele:nodes)
{
if (!num[ele.second])
{
dfs(ele.second, -1);
}
}
for (int i = 0; i <n; i++)
{
for (int ele:graph[i])
{
if (comp[i] == comp[ele]) continue;
cgraph[comp[i]].push_back(comp[ele]);
}
}
for (pii ele:nodes)
{
if (!vis[comp[ele.first]])
{
dfs2(comp[ele.first], -1);
}
}
cout << sol << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
30516 KB |
Output is correct |
2 |
Correct |
78 ms |
30660 KB |
Output is correct |
3 |
Correct |
85 ms |
29320 KB |
Output is correct |
4 |
Correct |
79 ms |
28612 KB |
Output is correct |
5 |
Incorrect |
79 ms |
26772 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
856 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
700 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
2 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
600 KB |
Output is correct |
10 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
93 ms |
26240 KB |
Output is correct |
2 |
Correct |
106 ms |
26320 KB |
Output is correct |
3 |
Correct |
94 ms |
26192 KB |
Output is correct |
4 |
Correct |
102 ms |
26304 KB |
Output is correct |
5 |
Correct |
96 ms |
26308 KB |
Output is correct |
6 |
Correct |
103 ms |
34496 KB |
Output is correct |
7 |
Correct |
122 ms |
32340 KB |
Output is correct |
8 |
Correct |
105 ms |
30780 KB |
Output is correct |
9 |
Correct |
102 ms |
29376 KB |
Output is correct |
10 |
Correct |
99 ms |
26308 KB |
Output is correct |
11 |
Correct |
96 ms |
26308 KB |
Output is correct |
12 |
Correct |
106 ms |
26396 KB |
Output is correct |
13 |
Incorrect |
105 ms |
26176 KB |
Output isn't correct |
14 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
104 ms |
26248 KB |
Output is correct |
2 |
Correct |
99 ms |
26032 KB |
Output is correct |
3 |
Incorrect |
104 ms |
26052 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
436 KB |
Output is correct |
4 |
Correct |
0 ms |
596 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Incorrect |
0 ms |
344 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |