#include <bits/stdc++.h>
#define int long long
using namespace std;
const int maxn = 1e5 + 10;
int n,m;
vector <int> adj[maxn];
int dp[maxn],dp2[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);
// cout<<x<<" :: "<<val<<' '<<temp<<endl;
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;
// cout<<i<<" "<<x<<" :: "<<val<<" :: "<<temp<<endl;
if (temp>0&&val>0) ans+=val*temp;
}
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
// freopen("dualthon.inp","r",stdin);
// freopen("dualthon.out","w",stdout);
cin>>n>>m;
for (int i=1; i<=m; i++)
{
int u,v; cin>>u>>v;
adj[u].push_back(v);
adj[v].push_back(u);
}
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:34: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]
34 | for (int i=1; i<pre.size(); i++) pre[i]+=pre[i-1];
| ~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
501 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
501 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1072 ms |
469640 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2644 KB |
Output is correct |
2 |
Correct |
3 ms |
2644 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2900 KB |
Output is correct |
5 |
Correct |
2 ms |
2772 KB |
Output is correct |
6 |
Correct |
2 ms |
2772 KB |
Output is correct |
7 |
Correct |
2 ms |
2772 KB |
Output is correct |
8 |
Correct |
2 ms |
2788 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2688 KB |
Output is correct |
13 |
Correct |
2 ms |
2684 KB |
Output is correct |
14 |
Correct |
2 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2680 KB |
Output is correct |
16 |
Correct |
2 ms |
2644 KB |
Output is correct |
17 |
Correct |
2 ms |
2772 KB |
Output is correct |
18 |
Correct |
2 ms |
2772 KB |
Output is correct |
19 |
Correct |
2 ms |
2772 KB |
Output is correct |
20 |
Correct |
2 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
8000 KB |
Output is correct |
2 |
Correct |
73 ms |
8256 KB |
Output is correct |
3 |
Correct |
77 ms |
8208 KB |
Output is correct |
4 |
Correct |
81 ms |
8200 KB |
Output is correct |
5 |
Correct |
72 ms |
8300 KB |
Output is correct |
6 |
Correct |
102 ms |
20428 KB |
Output is correct |
7 |
Correct |
87 ms |
16588 KB |
Output is correct |
8 |
Correct |
100 ms |
14464 KB |
Output is correct |
9 |
Correct |
111 ms |
12436 KB |
Output is correct |
10 |
Correct |
78 ms |
8300 KB |
Output is correct |
11 |
Correct |
70 ms |
8092 KB |
Output is correct |
12 |
Correct |
79 ms |
8200 KB |
Output is correct |
13 |
Correct |
85 ms |
8108 KB |
Output is correct |
14 |
Correct |
73 ms |
8140 KB |
Output is correct |
15 |
Correct |
66 ms |
8016 KB |
Output is correct |
16 |
Correct |
36 ms |
7384 KB |
Output is correct |
17 |
Correct |
32 ms |
9984 KB |
Output is correct |
18 |
Correct |
38 ms |
10488 KB |
Output is correct |
19 |
Correct |
45 ms |
10984 KB |
Output is correct |
20 |
Correct |
43 ms |
10020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Runtime error |
562 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
8048 KB |
Output is correct |
2 |
Correct |
69 ms |
8080 KB |
Output is correct |
3 |
Runtime error |
692 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
501 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
501 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |