This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<iostream>
#include<vector>
#include<algorithm>
#define ep emplace
#define eb emplace_back
#define fi first
#define se second
#define all(x) (x).begin(),(x).end()
using namespace std;
typedef long long ll;
typedef pair<int,int>pi;
typedef pair<ll,ll>pl;
const int inf=1e9+7;
const ll INF=1e18+7;
vector<int>adj[100010];
vector<vector<pi> >bcc;
vector<pi>stk;
vector<int>cur;
int disc[100010];
int dct;
bool arp[100010];
int dfs(int x,int p)
{
cur.eb(x);
int ret=disc[x]=++dct;
int c=0;
for(int&t:adj[x])
{
if(disc[t]!=0)
{
ret=min(ret,disc[t]);
if(disc[x]>disc[t])
stk.eb(t,x);
}
else
{
int rt=dfs(t,x);
if(rt>=disc[x])
{
vector<pi>cb;
pi b(-1,-1);
while(b!=pi(x,t))
{
b=stk.back();
stk.pop_back();
cb.eb(b);
}
bcc.eb(cb);
c++;
}
ret=min(ret,rt);
}
}
if(c-(p==0?1:0)>0)
arp[x]=1;
return ret;
}
vector<int>adj2[200010];
int sz[100010],siz[100010];
int pa[100010];
void dfs2(int x,int p)
{
pa[x]=p;
sz[x]=siz[x];
for(int&t:adj2[x])
if(t!=p)
dfs2(t,x),sz[x]+=sz[t];
return;
}
int num[100010];
inline ll npr(int n,int r)
{
ll ret=1;
for(int i=n;i>n-r;i--)
ret*=i;
return ret;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++)
{
int u,v;
cin>>u>>v;
adj[u].eb(v);
adj[v].eb(u);
}
ll ans=0;
for(int v=0;v++<n;)
{
if(disc[v]==0)
{
cur.clear();
bcc.clear();
bcc.eb(vector<pi>());
dfs(v,0);
int bct=(int)bcc.size()-1,vct=bct;
if(bct==0)
continue;
for(int&t:cur)
if(arp[t])
num[t]=++vct;
for(int i=0;i++<vct;)
adj2[i].clear(),siz[i]=1;
for(int i=0;i++<bct;)
{
vector<int>curv;
for(pi&t:bcc[i])
curv.eb(t.fi),curv.eb(t.se);
sort(all(curv));
curv.erase(unique(all(curv)),curv.end());
siz[i]=(int)curv.size();
for(int&t:curv)
if(arp[t])
adj2[i].eb(num[t]),adj2[num[t]].eb(i),siz[i]--;
}
dfs2(1,0);
ll cans=0;
for(int i=0;i++<bct;)
{
int nsz=siz[i]+(int)adj2[i].size()-1;
for(int&t:adj2[i])
if(t!=pa[i])
cans+=nsz*npr(sz[t],2);
if(pa[i]!=0)
cans+=nsz*npr(sz[1]-sz[i],2);
}
ans+=npr(sz[1],3)-cans;
}
}
cout<<ans<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |