#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;
int temp = (dp[i]+1);
int temp2 = (n-temp-1);
ans += temp*temp2;
}
}
void dfs2(int x, int p, int val)
{
dp2[x] = val;
int temp = n-val-1;
ans+=val*temp;
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);
}
}
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;
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);
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 |
Runtime error |
445 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
445 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1093 ms |
633296 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 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 |
2772 KB |
Output is correct |
9 |
Correct |
2 ms |
2772 KB |
Output is correct |
10 |
Incorrect |
2 ms |
2644 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
8064 KB |
Output is correct |
2 |
Correct |
66 ms |
8060 KB |
Output is correct |
3 |
Correct |
53 ms |
8044 KB |
Output is correct |
4 |
Correct |
49 ms |
8072 KB |
Output is correct |
5 |
Correct |
51 ms |
8012 KB |
Output is correct |
6 |
Correct |
79 ms |
20192 KB |
Output is correct |
7 |
Correct |
87 ms |
16440 KB |
Output is correct |
8 |
Correct |
72 ms |
14196 KB |
Output is correct |
9 |
Correct |
68 ms |
12212 KB |
Output is correct |
10 |
Incorrect |
49 ms |
8072 KB |
Output isn't correct |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2644 KB |
Output is correct |
3 |
Runtime error |
554 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
8068 KB |
Output is correct |
2 |
Correct |
55 ms |
7900 KB |
Output is correct |
3 |
Runtime error |
666 ms |
1048576 KB |
Execution killed with signal 9 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
445 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
445 ms |
1048576 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |