#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];
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)
{
dfs(i,x);
dp[x] += dp[i]+1;
}
}
void dfs2(int x, int p, int val)
{
dp2[x] = val;
val++;
vector <int> pre,suf;
for (int i:adj[x])
if (i!=p)
{
pre.push_back(dp[i]+1);
suf.push_back(dp[i]+1);
}
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 - 1 - val);
if (temp>0&&val>0)
ans += val*temp;
for (int i:adj[x])
if (i!=p)
{
dfs3(i,x,sz);
val = dp[i]+1;
temp = sz - 1 - val;
if (temp>0&&val>0) ans+=val*temp;
}
}
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]) dfs(i,i), root.push_back(i);
for (int i:root) dfs2(i,i,0);
for (int i:root) dfs3(i,i,dp[i]+1);
cout<<ans<<'\n';
}
Compilation message
count_triplets.cpp: In function 'void dfs2(long long int, long long int, long long int)':
count_triplets.cpp:37: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]
37 | for (int i=1; i<pre.size(); i++) pre[i]+=pre[i-1];
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
57 ms |
17568 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5184 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5332 KB |
Output is correct |
5 |
Correct |
5 ms |
5204 KB |
Output is correct |
6 |
Correct |
4 ms |
5320 KB |
Output is correct |
7 |
Correct |
5 ms |
5364 KB |
Output is correct |
8 |
Correct |
5 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5204 KB |
Output is correct |
18 |
Correct |
3 ms |
5204 KB |
Output is correct |
19 |
Correct |
3 ms |
5116 KB |
Output is correct |
20 |
Correct |
4 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
17216 KB |
Output is correct |
2 |
Correct |
113 ms |
17356 KB |
Output is correct |
3 |
Correct |
84 ms |
17304 KB |
Output is correct |
4 |
Correct |
82 ms |
17228 KB |
Output is correct |
5 |
Correct |
87 ms |
17228 KB |
Output is correct |
6 |
Correct |
126 ms |
39540 KB |
Output is correct |
7 |
Correct |
123 ms |
25360 KB |
Output is correct |
8 |
Correct |
118 ms |
23308 KB |
Output is correct |
9 |
Correct |
99 ms |
21140 KB |
Output is correct |
10 |
Correct |
93 ms |
17284 KB |
Output is correct |
11 |
Correct |
105 ms |
17228 KB |
Output is correct |
12 |
Correct |
82 ms |
17272 KB |
Output is correct |
13 |
Correct |
85 ms |
17228 KB |
Output is correct |
14 |
Correct |
70 ms |
16972 KB |
Output is correct |
15 |
Correct |
94 ms |
16552 KB |
Output is correct |
16 |
Correct |
61 ms |
15076 KB |
Output is correct |
17 |
Correct |
50 ms |
19248 KB |
Output is correct |
18 |
Correct |
61 ms |
19784 KB |
Output is correct |
19 |
Correct |
62 ms |
20244 KB |
Output is correct |
20 |
Correct |
58 ms |
19172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 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 |
Correct |
90 ms |
17208 KB |
Output is correct |
2 |
Correct |
96 ms |
17004 KB |
Output is correct |
3 |
Incorrect |
98 ms |
15208 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |