//#pragma GCC optimize("Ofast,unroll-loops,O3")
//#pragma GCC optimize("avx,avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,fma,tune=native")
#include<bits/stdc++.h>
//#include<bits/extc++.h>
//#pragma pack(1)
#define fast ios::sync_with_stdio(0); cin.tie(0);
#define int long long
#define pii pair<int,int>
#define x first
#define y second
#define N 100015
using namespace std;
//using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> order_multiset;
//typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> order_set;
int n,m,dfn[N],low[N],boss[N],sz[N],cont[N],nbcc=0,t=0,ans=0;
stack<int>st;
vector<int>g[N],bcc[N];
set<int>G[N];
map<int,int>cnt[N];
vector<pii>edge;
void tarjan(int p,int lp){
dfn[p]=low[p]=++t;
st.push(p);
for (auto i:g[p]){
if (!dfn[i]){
tarjan(i,p);
low[p]=min(low[p],low[i]);
}
else if (i!=lp&&dfn[i]<dfn[p])
low[p]=min(low[p],dfn[i]);
}
if (dfn[p]==low[p]){
nbcc++;
for (int x=0;x!=p;st.pop()){
x=st.top(); boss[x]=nbcc; cont[nbcc]++;
bcc[nbcc].push_back(x);
}
}
}
void build(){
for (auto [a,b]:edge){
if (boss[a]!=boss[b]){
G[boss[a]].insert(boss[b]);
G[boss[b]].insert(boss[a]);
cnt[boss[a]][boss[b]]++;
cnt[boss[b]][boss[a]]++;
}
}
}
void dfs(int p,int lp){
sz[p]=cont[p];
for (auto i:G[p]){
if (i!=lp){
dfs(i,p);
sz[p]+=sz[i];
}
}
}
void dfs1(int p,int lp){
sz[p]+=sz[lp];
int tans=0;
for (auto i:g[p]){
if (cnt[i][p]>1)
tans=(tans+cont[p]*(cont[p]-1)*sz[i]*2);
else tans=(tans+(cont[p]-1)*sz[i]*2+(cont[p]-1)*(cont[p]-2)*sz[i]*2);
tans=(tans+cont[p]*(sz[p]-cont[p]-sz[i])*sz[i]);
}
ans=(ans+tans);
for (auto i:G[p]){
if (i!=lp){
sz[p]-=sz[i];
dfs1(i,p);
sz[p]+=sz[i];
}
}
sz[p]-=sz[lp];
}
signed main(){
fast
cin>>n>>m;
for (int i=1;i<=m;i++){
int a,b; cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
edge.push_back({a,b});
}
tarjan(1,0);
build();
/*
for (int i=1;i<=nbcc;i++){
cout<<i<<" : "<<cont[i]<<" -> ";
for (auto j:bcc[i])
cout<<j<<" ";
cout<<'\n';
}
*/
dfs(1,0); dfs1(1,0);
// cout<<ans<<'\n';
for (int i=1;i<=nbcc;i++){
ans=(ans+cont[i]*(cont[i]-1)*(cont[i]-2));
}
cout<<ans<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14408 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14424 KB |
Output is correct |
7 |
Incorrect |
8 ms |
14420 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14408 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14424 KB |
Output is correct |
7 |
Incorrect |
8 ms |
14420 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
83 ms |
34064 KB |
Output is correct |
2 |
Correct |
63 ms |
33876 KB |
Output is correct |
3 |
Incorrect |
55 ms |
27780 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
14812 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
363 ms |
61756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
8 ms |
14816 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
318 ms |
61752 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14408 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14424 KB |
Output is correct |
7 |
Incorrect |
8 ms |
14420 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
14408 KB |
Output is correct |
2 |
Correct |
8 ms |
14420 KB |
Output is correct |
3 |
Correct |
7 ms |
14420 KB |
Output is correct |
4 |
Correct |
7 ms |
14420 KB |
Output is correct |
5 |
Correct |
7 ms |
14420 KB |
Output is correct |
6 |
Correct |
8 ms |
14424 KB |
Output is correct |
7 |
Incorrect |
8 ms |
14420 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |