#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<vector<int>> graph;
vector<vector<int>> cgraph;
vector<int> comp;
vector<int> art;
vector<int> low;
vector<int> num;
vector<int> vis;
stack<int> s;
int c = 0;
int d = 0;
int root = 0;
int sub = 0;
vector<ll> compsize;
ll sol = 0;
ll n, m;
void dfs(int i, int p)
{
num[i] = low[i] = ++c;
s.push(i);
for (int ele:graph[i])
{
if (i==root) sub++;
if (ele == p) continue;
if (!num[ele])
{
dfs(ele, i);
if (low[ele]>=num[i]) art[i] = 1;
if (low[ele]==num[i])
{
while(s.top() != i)
{
comp[s.top()] = d;
compsize[d]++;
s.pop();
}
comp[i] = d;
compsize[d]++;
s.pop();
d++;
}
}
low[i] = min(low[i], low[ele]);
}
if (num[i]==low[i] && comp[i]==-1)
{
art[i] = 1;
while (s.top() != i)
{
comp[s.top()] = d;
compsize[d]++;
s.pop();
}
comp[i] = d;
compsize[d]++;
s.pop();
d++;
}
}
int dfs2(int i)
{
vis[i] = 1;
ll ss = 0;
vector<int> cs;
for (int ele:cgraph[i])
{
if (!vis[ele])
{
int h = dfs2(ele);
sol += compsize[i]*(h-compsize[ele])*(h-compsize[ele]);
sol += compsize[i]*compsize[ele]*(h-compsize[ele])*2;
sol -= compsize[i]*(h-compsize[ele]);
cs.push_back(ele);
ss+=h;
}
}
for (int ele:cs)
{
sol += compsize[ele]*(n-ss-compsize[i])*(n-ss-compsize[i]);
sol += compsize[i]*compsize[ele]*(n-ss-compsize[i])*2;
sol -= compsize[ele]*(n-ss-compsize[i]);
}
return ss+compsize[i];
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cin >> n >> m;
graph.assign(n, vector<int>());
cgraph.assign(2*n+m, vector<int>());
compsize.assign(2*n+m, 0);
low.assign(n, 0);
num.assign(n, 0);
comp.assign(n, -1);
art.assign(n, 0);
vis.assign(2*n+m, 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);
}
for (int i = 0; i < n; i++)
{
if (!num[i])
{
sub = 0;
root = i;
c = 0;
dfs(i, -1);
if (sub==1) art[i] = 0;
}
}
for (int i = 0; i <n; i++)
{
if (art[i])
{
compsize[comp[i]]--;
comp[i] = d;
compsize[d] = 1;
d++;
}
}
for (int i = 0; i <n; i++)
{
if (art[i])
{
for (int ele:graph[i])
{
if (art[ele] && ele > i)
{
cgraph[comp[i]].push_back(d);
cgraph[d].push_back(comp[i]);
cgraph[d].push_back(comp[ele]);
cgraph[comp[ele]].push_back(d);
d++;
continue;
}
cgraph[comp[i]].push_back(comp[ele]);
cgraph[comp[ele]].push_back(comp[i]);
}
}
}
for (int i = 0; i < n; i++)
{
if (!vis[comp[i]])
{
dfs2(comp[i]);
}
}
sol = (n)*(n-1)*(n-2) - sol;
cout << sol << "\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
26088 KB |
Output is correct |
2 |
Correct |
84 ms |
26196 KB |
Output is correct |
3 |
Incorrect |
100 ms |
30092 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
1 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
1 ms |
604 KB |
Output is correct |
9 |
Correct |
1 ms |
604 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 |
111 ms |
25052 KB |
Output is correct |
2 |
Correct |
128 ms |
25060 KB |
Output is correct |
3 |
Correct |
158 ms |
25248 KB |
Output is correct |
4 |
Correct |
143 ms |
25052 KB |
Output is correct |
5 |
Correct |
136 ms |
24860 KB |
Output is correct |
6 |
Correct |
146 ms |
36148 KB |
Output is correct |
7 |
Correct |
158 ms |
33064 KB |
Output is correct |
8 |
Correct |
146 ms |
30852 KB |
Output is correct |
9 |
Correct |
171 ms |
28744 KB |
Output is correct |
10 |
Incorrect |
161 ms |
24916 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
600 KB |
Output is correct |
3 |
Incorrect |
2 ms |
604 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
151 ms |
24996 KB |
Output is correct |
2 |
Correct |
143 ms |
24888 KB |
Output is correct |
3 |
Incorrect |
186 ms |
24524 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |