#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 1e5 + 10;
int n,m;
vector <int> adj[maxn],adj2[maxn];
int dp[maxn],dp2[maxn];
stack <int> st;
int l[maxn],t[maxn],cnt2;
int scc, id[maxn], s[maxn];
int rt[maxn];
bool dau[maxn];
vector <int> root;
int ans = 0LL;
void dfs(int x, int p)
{
dau[x] = 1;
dp[x] = 0;
for (int i:adj[x])
if (i!=p)
{
rt[i] = rt[x];
dfs(i,x);
dp[x] += dp[i]+s[i];
}
}
void dfs2(int x, int p, int val)
{
dp2[x] = val;
val+=s[x];
vector <int> pre,suf;
for (int i:adj[x])
if (i!=p)
{
pre.push_back(dp[i]+s[i]);
suf.push_back(dp[i]+s[i]);
}
for (int i=1; i<pre.size(); i++) pre[i]+=pre[i-1];
for (int i=suf.size()-2; i>=0; i--) suf[i]+=suf[i+1];
int cnt=0;
for (int i:adj[x])
if (i!=p)
{
int val2 = val;
if (cnt>0) val2 += pre[cnt-1];
if (cnt<(int)suf.size()-1) val2+=suf[cnt+1];
cnt++;
dfs2(i,x,val2);
}
}
void dfs3(int x, int p, int sz)
{
int val = dp2[x];
int temp = (sz - s[x] - val);
if (temp>0&&val>0)
ans += val*temp*s[x];
for (int i:adj[x])
if (i!=p)
{
dfs3(i,x,sz);
val = dp[i]+s[i];
temp = sz - s[x] - val;
if (temp>0&&val>0) ans+=val*temp*s[x];
}
}
void build(int x, int p)
{
st.push(x);
l[x]=t[x]=++cnt2;
for (int i:adj2[x])
if (i!=p)
{
if (t[i]==0)
{
build(i,x);
l[x]=min(l[x],l[i]);
}
else l[x]=min(l[x],t[i]);
}
if (l[x]==t[x])
{
scc++; int temp;
do
{
temp = st.top(); st.pop();
s[scc] ++ ;
id[temp] = scc;
}
while (temp!=x);
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin>>n>>m;
for (int i=1; i<=m; i++)
{
int u,v; cin>>u>>v;
adj2[u].push_back(v);
adj2[v].push_back(u);
}
scc=cnt2=0;
for (int i=1; i<=n; i++) if (!t[i]) build(i,i);
for (int i=1; i<=n; i++)
for (int j:adj2[i])
if (id[i]!=id[j])
adj[id[i]].push_back(id[j]);
n = scc;
fill_n(dau,n+1,0);
for (int i=1; i<=n; i++) if (!dau[i]) rt[i] = i, dfs(i,i), root.push_back(i);
for (int i:root) dfs2(i,i,0);
for (int i=1; i<=n; i++) if (s[i]>=3) ans += s[i]*(s[i]-1)*(s[i]-2);
for (int i=1; i<=n; i++)
for (int j=1; j<=n; j++)
if (i!=j&&rt[i]==rt[j])
{
ans += (s[i]-1)*(s[i]-1)*s[j]*2;
}
for (int i:root) dfs3(i,i,dp[i]+s[i]);
cout<<ans<<'\n';
}
Compilation message
count_triplets.cpp: In function 'void dfs2(long long int, long long int, long long int)':
count_triplets.cpp:39:20: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
39 | for (int i=1; i<pre.size(); i++) pre[i]+=pre[i-1];
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
17484 KB |
Output is correct |
2 |
Correct |
54 ms |
17568 KB |
Output is correct |
3 |
Execution timed out |
1087 ms |
26164 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
5076 KB |
Output is correct |
2 |
Correct |
6 ms |
5076 KB |
Output is correct |
3 |
Correct |
4 ms |
5076 KB |
Output is correct |
4 |
Correct |
5 ms |
5332 KB |
Output is correct |
5 |
Correct |
5 ms |
5316 KB |
Output is correct |
6 |
Correct |
5 ms |
5192 KB |
Output is correct |
7 |
Correct |
5 ms |
5332 KB |
Output is correct |
8 |
Correct |
5 ms |
5204 KB |
Output is correct |
9 |
Correct |
5 ms |
5204 KB |
Output is correct |
10 |
Correct |
4 ms |
5076 KB |
Output is correct |
11 |
Correct |
6 ms |
5076 KB |
Output is correct |
12 |
Correct |
4 ms |
5076 KB |
Output is correct |
13 |
Correct |
5 ms |
5076 KB |
Output is correct |
14 |
Correct |
5 ms |
5076 KB |
Output is correct |
15 |
Correct |
4 ms |
5176 KB |
Output is correct |
16 |
Correct |
5 ms |
5076 KB |
Output is correct |
17 |
Correct |
4 ms |
5204 KB |
Output is correct |
18 |
Correct |
5 ms |
5204 KB |
Output is correct |
19 |
Correct |
5 ms |
5204 KB |
Output is correct |
20 |
Correct |
4 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1069 ms |
18052 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
5076 KB |
Output is correct |
2 |
Correct |
7 ms |
5076 KB |
Output is correct |
3 |
Incorrect |
3 ms |
5076 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1064 ms |
17992 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
4948 KB |
Output is correct |
7 |
Incorrect |
3 ms |
4948 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |