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>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 2e5;
const int MAXM = 2e5;
int N, M;
vector<int> adj[MAXN+10];
int dfn[MAXN+10], low[MAXN+10], cnt;
void dfs(int now, int bef)
{
low[now]=dfn[now]=++cnt;
for(int nxt : adj[now])
{
if(nxt==bef) continue;
if(dfn[nxt]) low[now]=min(low[now], dfn[nxt]);
else
{
dfs(nxt, now);
low[now]=min(low[now], low[nxt]);
}
}
}
int bcnt, vis[MAXN+10];
vector<int> bcc[MAXN+10];
void dfs2(int now, int col)
{
if(col) bcc[now].push_back(col);
vis[now]=true;
for(int nxt : adj[now])
{
if(vis[nxt]) continue;
if(low[nxt]>=dfn[now])
{
bcc[now].push_back(++bcnt);
dfs2(nxt, bcnt);
}
else
{
dfs2(nxt, col);
}
}
}
int sz[MAXN+10], dp[MAXN+10], sum, par[MAXN+10];
ll dp2[MAXN+10];
vector<int> adj2[MAXN+10];
bool vis2[MAXN+10];
ll ans;
vector<int> V;
void dfs3(int now, int bef)
{
vis2[now]=true;
par[now]=bef;
V.push_back(now);
sum+=sz[now];
dp[now]=sz[now];
for(int nxt : adj2[now])
{
if(nxt==bef) continue;
dfs3(nxt, now);
dp[now]+=dp[nxt];
}
}
int f(int now, int bef)
{
if(par[now]==bef) return dp[now];
return sum-dp[bef];
}
int main()
{
scanf("%d%d", &N, &M);
for(int i=1; i<=M; i++)
{
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
for(int i=1; i<=N; i++) if(dfn[i]==0) dfs(i, i);
for(int i=1; i<=N; i++) if(vis[i]==0) dfs2(i, 0);
for(int i=1; i<=N; i++)
{
if(bcc[i].size()==1)
{
sz[bcc[i][0]+N]++;
}
else
{
sz[i]++;
for(auto nxt : bcc[i])
{
int u=i, v=nxt+N;
adj2[u].push_back(v);
adj2[v].push_back(u);
//printf("%d %d\n", u, v);
}
}
}
for(int i=1; i<=bcnt; i++)
{
if(vis2[N+i]) continue;
V.clear(); sum=0;
dfs3(N+i, N+i);
//printf("SUM %d\n", sum);
//for(auto it : V) printf("%d ", it); printf("\n");
for(auto now : V)
{
//printf("!!%d\n", now);
if(now>N)
{
ans+=(ll)(sum-2)*(sz[now])*(sz[now]-1);
//printf("%lld\n", (ll)(sum-2)*(sz[now])*(sz[now]-1));
for(auto nxt : adj2[now])
{
int t=f(nxt, now);
dp2[now]+=(ll)t*(t-1);
ans+=(ll)(sz[now])*t*(sum-t-1);
//printf("%lld\n", (ll)(sz[now])*t*(sum-t-1));
}
}
}
for(auto now : V)
{
if(now<=N)
{
for(auto nxt : adj2[now])
{
int t=f(nxt, now), t2=f(now, nxt);
ans+=(ll)t*(sum-t-1);
ans+=(ll)t*(t-1)-dp2[nxt]+(ll)t2*(t2-1);
}
}
}
}
printf("%lld\n", ans);
}
Compilation message (stderr)
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:83:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~
count_triplets.cpp:87:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~
# | 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... |