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 <bits/stdc++.h>
#pragma optimize("Ofast")
#pragma target("avx2")
using namespace std;
#define ll long long
#define ld long double
#define pb push_back
#define pf push_front
#define pii pair<int,int>
#define all(v) v.begin(),v.end()
#define F first
#define S second
#define mem(a,i) memset(a,i,sizeof(a))
#define sz(s) (int)s.size()
#define y1 yy
#define ppb pop_back
#define lb lower_bound
#define ub upper_bound
#define gcd(a,b) __gcd(a,b)
#define in insert
#define int ll
const int MAX=1e5+15;
const int B=300;
const int N=104;
const int block=400;
const int maxB=MAX/B+10;
const ll inf=2e18;
const int mod=998244353;
const int mod1=1e9+9;
const ld eps=1e-9;
int dx[8]={1,0,-1,0,1,-1,-1,1};
int dy[8]={0,1,0,-1,1,-1,1,-1};
int binpow(int a,int n){
if(!n)return 1;
if(n%2==1)return a*binpow(a,n-1)%mod;
int k=binpow(a,n/2);
return k*k%mod;
}
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int n,m;
vector<int> g[MAX];
int use[MAX];
int tin[MAX],fup[MAX],timer;
map<pii,int> br;
int cnt=0;
bool was;
void dfs(int v,int p=-1){
tin[v]=fup[v]=++timer;
use[v]=1;
cnt++;
for(auto to:g[v]){
if(to==p)continue;
if(!use[to]){
dfs(to,v);
fup[v]=min(fup[v],fup[to]);
if(fup[to]>tin[v]){
was=1;
br[{v,to}]=br[{to,v}]=1;
}
}
else{
fup[v]=min(fup[v],tin[to]);
}
}
}
struct DSU{
int f[MAX];
void init(int n){
for(int i=1;i<=n;i++){
f[i]=i;
}
}
int get(int v){
if(f[v]==v)return v;
return f[v]=get(f[v]);
}
void unite(int a,int b){
a=get(a);
b=get(b);
if(rng()%2)swap(a,b);
f[a]=b;
}
}D;
int s[MAX];
int S[MAX];
int pr[MAX];
int PR[MAX];
vector<int> g1[MAX];
void dfs1(int v,int st,int p=-1){
pr[v]=p;
PR[v]=st;
use[v]=1;
sort(all(g1[v]));
g1[v].erase(unique(all(g1[v])),g1[v].end());
S[v]=s[v];
for(auto to:g1[v]){
if(to==p)continue;
dfs1(to,st,v);
S[v]+=S[to];
}
}
void solve(){
cin>>n>>m;
for(int i=1;i<=m;i++){
int a,b;
cin>>a>>b;
g[a].pb(b);
g[b].pb(a);
}
int ans=0;
for(int i=1;i<=n;i++){
if(!use[i]){
dfs(i);
if(was)ans+=cnt*(cnt-1)*(cnt-2)/3;
else ans+=cnt*(cnt-1)*(cnt-2);
cnt=0;
was=0;
}
}
cout<<ans<<"\n";
return;
D.init(n);
for(int i=1;i<=n;i++){
for(auto to:g[i]){
if(!br.count({i,to})){
D.unite(i,to);
}
}
}
for(int i=1;i<=n;i++){
s[D.get(i)]++;
for(auto to:g[i]){
if(D.get(i)!=D.get(to)){
g1[D.get(i)].pb(D.get(to));
}
}
}
// cerr<<"Ok\n";
mem(use,0);
for(int i=1;i<=n;i++){
if(!use[D.get(i)]){
dfs1(D.get(i),D.get(i));
}
}
for(int i=1;i<=n;i++){
if(D.get(i)==i){
ans+=s[i]*(s[i]-1)*(s[i]-2)/6;
int up=S[PR[i]]-S[i];
int down=S[i]-s[i];
ans+=s[i]*up*down;
ans+=s[i]*(s[i]-1)*(S[PR[i]]-s[i]);
for(auto to:g1[i]){
if(to==pr[i])continue;
ans+=S[to]*s[i]*(S[i]-S[to]-s[i]);
}
}
}
cout<<ans*2<<"\n";
}
signed main(){
// freopen("help.in","r",stdin);
// freopen("help.out","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// prec();
int t=1;
// cin>>t;
while(t--)solve();
}
Compilation message (stderr)
count_triplets.cpp:3: warning: ignoring '#pragma optimize ' [-Wunknown-pragmas]
3 | #pragma optimize("Ofast")
|
count_triplets.cpp:4: warning: ignoring '#pragma target ' [-Wunknown-pragmas]
4 | #pragma target("avx2")
|
# | 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... |