#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;
void dfs(int v,int p=-1){
tin[v]=fup[v]=++timer;
use[v]=1;
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]){
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];
vector<int> g1[MAX];
void dfs1(int v,int p=-1){
pr[v]=p;
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,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);
}
dfs(1);
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));
}
}
}
dfs1(D.get(1));
int ans=0;
for(int i=1;i<=n;i++){
if(D.get(i)==i){
ans+=s[i]*(s[i]-1)*(s[i]-2)/6;
// cout<<s[i]*(s[i]-1)*(s[i]-2)<<"\n";
int up=n-S[i];
int down=S[i]-s[i];
// cout<<s[i]*up*down<<"\n";
ans+=s[i]*up*down;
ans+=s[i]*(s[i]-1)*(n-s[i]);
for(auto to:g1[i]){
if(to==pr[i])continue;
// cout<<S[to]<<" "<<s[i]<<" "<<(S[i]-S[to]-s[i])<<"\n";
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
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")
|
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
48 ms |
29012 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
10844 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
161 ms |
31644 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
10840 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
147 ms |
31824 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
10588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |