#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
const int N=1e5+10;
vector<int> ma[N];
// ll pre[N];
bool vis[N];
ll ans=0,sz[N],dp[N],dp_[N];
void dfs(int x,int p=-1)
{
sz[x]=1;
vis[x]=1;
for(auto y:ma[x])
{
if(y!=p)
{
dfs(y,x);
sz[x]+=sz[y];
dp[x]-=(sz[y]*sz[y]);
}
}
dp[x]+=((sz[x]-1)*(sz[x]-1));
}
void ChangeRoot(int x,int y)
{
//
dp[x]+=(sz[y]*sz[y]);
dp[x]-=((sz[x]-1)*(sz[x]-1));
dp[y]-=((sz[y]-1)*(sz[y]-1));
sz[x]-=sz[y];
sz[y]+=sz[x];
dp[x]+=((sz[x]-1)*(sz[x]-1));
dp[y]+=((sz[y]-1)*(sz[y]-1));
dp[y]-=(sz[x]*sz[x]);
}
void dfs_(int x,int p=-1)
{
vis[x]=1;
ans+=dp[x];
for(auto y:ma[x])
{
if(y!=p)
{
ChangeRoot(x,y);
dfs_(y,x);
ChangeRoot(y,x);
}
}
}
bool cycle=0;
int nodes=0;
void _dfs(int x,int p=-1)
{
vis[x]=1;
nodes++;
for(auto y:ma[x])
{
if(y!=p)
{
if(vis[y])
{
cycle=1;
}
else
{
_dfs(y,x);
}
}
}
}
int f_cyc(int x)
{
return (x*(x-1)*(x-2));
}
// int f_lin(int x)
// {
// ll cur=(x-1)*(x-1)*(x-1);
// return cur-(pre[x-1]);
// }
int f_lin_brute(int x)
{
ll ans=0;
ll sp=(x-1)*(x-1);
for(int j=1;j<=x;j++)
ans+=(sp-((j-1)*(j-1))-((x-j)*(x-j)));
return ans;
}
bool senpai[51][51][51];
vector<int> path;
void full(int x)
{
vis[x]=1;
for(auto y:ma[x])
{
if(!vis[y])
{
path.push_back(y);
full(y);
path.pop_back();
}
}
for(int i=1;(i+1)<path.size();i++)
senpai[path[0]][path[i]][path.back()]=1;
vis[x]=0;
}
void solve()
{
// int n,m;
// cin>>n;
// // m=n-1;
int n,m;
cin>>n>>m;
vector<int> deg(n+2);
for(int i=0;i<m;i++)
{
int x=i+1,y=i+2;
cin>>x>>y;
deg[x]++;
deg[y]++;
ma[x].push_back(y);
ma[y].push_back(x);
}
int mx=0;
for(int i=1;i<=n;i++)
mx=max(mx,deg[i]);
if(mx<=2)
{
// Solve a Cycle or a Path
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
cycle=0;
nodes=0;
_dfs(i);
if(cycle)
{
ans+=f_cyc(nodes);
}
else
{
ans+=f_lin_brute(nodes);
}
}
}
}
else
{
bool sub=1;
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
cycle=0;
_dfs(i);
if(cycle)
{
sub=0;
break;
}
}
}
for(int j=1;j<=n;j++)
{
vis[j]=0;
}
if(sub)
{
// Solve on Tree
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
dfs(i);
}
}
for(int i=1;i<=n;i++)
vis[i]=0;
for(int i=1;i<=n;i++)
{
if(!vis[i])
{
dfs_(i);
}
}
}
else
{
for(int i=1;i<=n;i++)
{
path.push_back(i);
full(i);
path.pop_back();
}
for(int a=1;a<=n;a++)
for(int b=1;b<=n;b++)
for(int c=1;c<=n;c++)
ans+=senpai[a][b][c];
}
}
cout<<ans<<'\n';
}
signed main()
{
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
// pre[0]=0;
// ll asp=1e5+1;
// for(int i=1;i<=asp;i++)
// pre[i]=pre[i-1]+(i*i);
solve();
}
Compilation message
count_triplets.cpp: In function 'void full(long long int)':
count_triplets.cpp:103:19: 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]
103 | for(int i=1;(i+1)<path.size();i++)
| ~~~~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3160 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
1 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Correct |
1 ms |
2908 KB |
Output is correct |
6 |
Correct |
1 ms |
2968 KB |
Output is correct |
7 |
Correct |
1 ms |
3416 KB |
Output is correct |
8 |
Correct |
1 ms |
3164 KB |
Output is correct |
9 |
Correct |
9 ms |
3164 KB |
Output is correct |
10 |
Correct |
244 ms |
3156 KB |
Output is correct |
11 |
Correct |
1 ms |
3160 KB |
Output is correct |
12 |
Correct |
1 ms |
3164 KB |
Output is correct |
13 |
Correct |
1 ms |
3164 KB |
Output is correct |
14 |
Correct |
1 ms |
3164 KB |
Output is correct |
15 |
Correct |
1 ms |
3164 KB |
Output is correct |
16 |
Correct |
1 ms |
3164 KB |
Output is correct |
17 |
Correct |
1 ms |
3164 KB |
Output is correct |
18 |
Correct |
1 ms |
3164 KB |
Output is correct |
19 |
Correct |
1 ms |
2904 KB |
Output is correct |
20 |
Correct |
1 ms |
2904 KB |
Output is correct |
21 |
Correct |
1 ms |
2908 KB |
Output is correct |
22 |
Correct |
1 ms |
3164 KB |
Output is correct |
23 |
Correct |
1 ms |
3180 KB |
Output is correct |
24 |
Correct |
1 ms |
3164 KB |
Output is correct |
25 |
Correct |
1 ms |
3164 KB |
Output is correct |
26 |
Correct |
1 ms |
3164 KB |
Output is correct |
27 |
Correct |
1 ms |
3164 KB |
Output is correct |
28 |
Correct |
1 ms |
3164 KB |
Output is correct |
29 |
Correct |
1 ms |
3416 KB |
Output is correct |
30 |
Correct |
1 ms |
3164 KB |
Output is correct |
31 |
Correct |
1 ms |
3164 KB |
Output is correct |
32 |
Correct |
1 ms |
3164 KB |
Output is correct |
33 |
Correct |
1 ms |
3164 KB |
Output is correct |
34 |
Correct |
1 ms |
2908 KB |
Output is correct |
35 |
Correct |
1 ms |
2908 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3160 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
1 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Correct |
1 ms |
2908 KB |
Output is correct |
6 |
Correct |
1 ms |
2968 KB |
Output is correct |
7 |
Correct |
1 ms |
3416 KB |
Output is correct |
8 |
Correct |
1 ms |
3164 KB |
Output is correct |
9 |
Correct |
9 ms |
3164 KB |
Output is correct |
10 |
Correct |
244 ms |
3156 KB |
Output is correct |
11 |
Correct |
1 ms |
3160 KB |
Output is correct |
12 |
Correct |
1 ms |
3164 KB |
Output is correct |
13 |
Correct |
1 ms |
3164 KB |
Output is correct |
14 |
Correct |
1 ms |
3164 KB |
Output is correct |
15 |
Correct |
1 ms |
3164 KB |
Output is correct |
16 |
Correct |
1 ms |
3164 KB |
Output is correct |
17 |
Correct |
1 ms |
3164 KB |
Output is correct |
18 |
Correct |
1 ms |
3164 KB |
Output is correct |
19 |
Correct |
1 ms |
2904 KB |
Output is correct |
20 |
Correct |
1 ms |
2904 KB |
Output is correct |
21 |
Correct |
1 ms |
2908 KB |
Output is correct |
22 |
Correct |
1 ms |
3164 KB |
Output is correct |
23 |
Correct |
1 ms |
3180 KB |
Output is correct |
24 |
Correct |
1 ms |
3164 KB |
Output is correct |
25 |
Correct |
1 ms |
3164 KB |
Output is correct |
26 |
Correct |
1 ms |
3164 KB |
Output is correct |
27 |
Correct |
1 ms |
3164 KB |
Output is correct |
28 |
Correct |
1 ms |
3164 KB |
Output is correct |
29 |
Correct |
1 ms |
3416 KB |
Output is correct |
30 |
Correct |
1 ms |
3164 KB |
Output is correct |
31 |
Correct |
1 ms |
3164 KB |
Output is correct |
32 |
Correct |
1 ms |
3164 KB |
Output is correct |
33 |
Correct |
1 ms |
3164 KB |
Output is correct |
34 |
Correct |
1 ms |
2908 KB |
Output is correct |
35 |
Correct |
1 ms |
2908 KB |
Output is correct |
36 |
Correct |
1 ms |
2908 KB |
Output is correct |
37 |
Correct |
1 ms |
4956 KB |
Output is correct |
38 |
Correct |
3 ms |
3164 KB |
Output is correct |
39 |
Execution timed out |
1085 ms |
3136 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
11604 KB |
Output is correct |
2 |
Correct |
30 ms |
11604 KB |
Output is correct |
3 |
Correct |
29 ms |
9308 KB |
Output is correct |
4 |
Correct |
30 ms |
10588 KB |
Output is correct |
5 |
Correct |
28 ms |
8540 KB |
Output is correct |
6 |
Correct |
27 ms |
8532 KB |
Output is correct |
7 |
Correct |
27 ms |
7776 KB |
Output is correct |
8 |
Correct |
27 ms |
8276 KB |
Output is correct |
9 |
Correct |
28 ms |
7504 KB |
Output is correct |
10 |
Correct |
26 ms |
7764 KB |
Output is correct |
11 |
Correct |
26 ms |
6996 KB |
Output is correct |
12 |
Correct |
31 ms |
6748 KB |
Output is correct |
13 |
Correct |
24 ms |
6744 KB |
Output is correct |
14 |
Correct |
20 ms |
6796 KB |
Output is correct |
15 |
Correct |
15 ms |
6492 KB |
Output is correct |
16 |
Correct |
16 ms |
6236 KB |
Output is correct |
17 |
Correct |
2 ms |
3932 KB |
Output is correct |
18 |
Correct |
2 ms |
3932 KB |
Output is correct |
19 |
Correct |
2 ms |
3932 KB |
Output is correct |
20 |
Correct |
2 ms |
3932 KB |
Output is correct |
21 |
Correct |
2 ms |
3932 KB |
Output is correct |
22 |
Correct |
2 ms |
3932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
5208 KB |
Output is correct |
3 |
Correct |
1 ms |
5212 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Correct |
1 ms |
5212 KB |
Output is correct |
6 |
Correct |
1 ms |
5212 KB |
Output is correct |
7 |
Correct |
1 ms |
3164 KB |
Output is correct |
8 |
Correct |
2 ms |
5212 KB |
Output is correct |
9 |
Correct |
1 ms |
5212 KB |
Output is correct |
10 |
Correct |
1 ms |
5212 KB |
Output is correct |
11 |
Correct |
1 ms |
5212 KB |
Output is correct |
12 |
Correct |
1 ms |
5212 KB |
Output is correct |
13 |
Correct |
1 ms |
5212 KB |
Output is correct |
14 |
Correct |
1 ms |
5212 KB |
Output is correct |
15 |
Correct |
1 ms |
5212 KB |
Output is correct |
16 |
Correct |
1 ms |
5212 KB |
Output is correct |
17 |
Correct |
2 ms |
5208 KB |
Output is correct |
18 |
Correct |
2 ms |
5208 KB |
Output is correct |
19 |
Correct |
1 ms |
5212 KB |
Output is correct |
20 |
Correct |
1 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
9564 KB |
Output is correct |
2 |
Correct |
33 ms |
9552 KB |
Output is correct |
3 |
Correct |
33 ms |
9564 KB |
Output is correct |
4 |
Correct |
33 ms |
9564 KB |
Output is correct |
5 |
Correct |
32 ms |
9564 KB |
Output is correct |
6 |
Correct |
27 ms |
9564 KB |
Output is correct |
7 |
Correct |
38 ms |
12368 KB |
Output is correct |
8 |
Correct |
45 ms |
11600 KB |
Output is correct |
9 |
Correct |
36 ms |
10932 KB |
Output is correct |
10 |
Correct |
33 ms |
9564 KB |
Output is correct |
11 |
Correct |
34 ms |
9552 KB |
Output is correct |
12 |
Correct |
32 ms |
9556 KB |
Output is correct |
13 |
Correct |
33 ms |
9556 KB |
Output is correct |
14 |
Correct |
30 ms |
9300 KB |
Output is correct |
15 |
Correct |
26 ms |
9048 KB |
Output is correct |
16 |
Correct |
18 ms |
8284 KB |
Output is correct |
17 |
Correct |
22 ms |
9680 KB |
Output is correct |
18 |
Correct |
21 ms |
9804 KB |
Output is correct |
19 |
Correct |
24 ms |
9816 KB |
Output is correct |
20 |
Correct |
23 ms |
9804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5208 KB |
Output is correct |
2 |
Correct |
2 ms |
5212 KB |
Output is correct |
3 |
Runtime error |
286 ms |
10528 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
40 ms |
9684 KB |
Output is correct |
2 |
Correct |
35 ms |
9556 KB |
Output is correct |
3 |
Runtime error |
42 ms |
20052 KB |
Execution killed with signal 11 |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3160 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
1 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Correct |
1 ms |
2908 KB |
Output is correct |
6 |
Correct |
1 ms |
2968 KB |
Output is correct |
7 |
Correct |
1 ms |
3416 KB |
Output is correct |
8 |
Correct |
1 ms |
3164 KB |
Output is correct |
9 |
Correct |
9 ms |
3164 KB |
Output is correct |
10 |
Correct |
244 ms |
3156 KB |
Output is correct |
11 |
Correct |
1 ms |
3160 KB |
Output is correct |
12 |
Correct |
1 ms |
3164 KB |
Output is correct |
13 |
Correct |
1 ms |
3164 KB |
Output is correct |
14 |
Correct |
1 ms |
3164 KB |
Output is correct |
15 |
Correct |
1 ms |
3164 KB |
Output is correct |
16 |
Correct |
1 ms |
3164 KB |
Output is correct |
17 |
Correct |
1 ms |
3164 KB |
Output is correct |
18 |
Correct |
1 ms |
3164 KB |
Output is correct |
19 |
Correct |
1 ms |
2904 KB |
Output is correct |
20 |
Correct |
1 ms |
2904 KB |
Output is correct |
21 |
Correct |
1 ms |
2908 KB |
Output is correct |
22 |
Correct |
1 ms |
3164 KB |
Output is correct |
23 |
Correct |
1 ms |
3180 KB |
Output is correct |
24 |
Correct |
1 ms |
3164 KB |
Output is correct |
25 |
Correct |
1 ms |
3164 KB |
Output is correct |
26 |
Correct |
1 ms |
3164 KB |
Output is correct |
27 |
Correct |
1 ms |
3164 KB |
Output is correct |
28 |
Correct |
1 ms |
3164 KB |
Output is correct |
29 |
Correct |
1 ms |
3416 KB |
Output is correct |
30 |
Correct |
1 ms |
3164 KB |
Output is correct |
31 |
Correct |
1 ms |
3164 KB |
Output is correct |
32 |
Correct |
1 ms |
3164 KB |
Output is correct |
33 |
Correct |
1 ms |
3164 KB |
Output is correct |
34 |
Correct |
1 ms |
2908 KB |
Output is correct |
35 |
Correct |
1 ms |
2908 KB |
Output is correct |
36 |
Correct |
1 ms |
2908 KB |
Output is correct |
37 |
Correct |
1 ms |
4956 KB |
Output is correct |
38 |
Correct |
3 ms |
3164 KB |
Output is correct |
39 |
Execution timed out |
1085 ms |
3136 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3160 KB |
Output is correct |
2 |
Correct |
1 ms |
3164 KB |
Output is correct |
3 |
Correct |
1 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
3164 KB |
Output is correct |
5 |
Correct |
1 ms |
2908 KB |
Output is correct |
6 |
Correct |
1 ms |
2968 KB |
Output is correct |
7 |
Correct |
1 ms |
3416 KB |
Output is correct |
8 |
Correct |
1 ms |
3164 KB |
Output is correct |
9 |
Correct |
9 ms |
3164 KB |
Output is correct |
10 |
Correct |
244 ms |
3156 KB |
Output is correct |
11 |
Correct |
1 ms |
3160 KB |
Output is correct |
12 |
Correct |
1 ms |
3164 KB |
Output is correct |
13 |
Correct |
1 ms |
3164 KB |
Output is correct |
14 |
Correct |
1 ms |
3164 KB |
Output is correct |
15 |
Correct |
1 ms |
3164 KB |
Output is correct |
16 |
Correct |
1 ms |
3164 KB |
Output is correct |
17 |
Correct |
1 ms |
3164 KB |
Output is correct |
18 |
Correct |
1 ms |
3164 KB |
Output is correct |
19 |
Correct |
1 ms |
2904 KB |
Output is correct |
20 |
Correct |
1 ms |
2904 KB |
Output is correct |
21 |
Correct |
1 ms |
2908 KB |
Output is correct |
22 |
Correct |
1 ms |
3164 KB |
Output is correct |
23 |
Correct |
1 ms |
3180 KB |
Output is correct |
24 |
Correct |
1 ms |
3164 KB |
Output is correct |
25 |
Correct |
1 ms |
3164 KB |
Output is correct |
26 |
Correct |
1 ms |
3164 KB |
Output is correct |
27 |
Correct |
1 ms |
3164 KB |
Output is correct |
28 |
Correct |
1 ms |
3164 KB |
Output is correct |
29 |
Correct |
1 ms |
3416 KB |
Output is correct |
30 |
Correct |
1 ms |
3164 KB |
Output is correct |
31 |
Correct |
1 ms |
3164 KB |
Output is correct |
32 |
Correct |
1 ms |
3164 KB |
Output is correct |
33 |
Correct |
1 ms |
3164 KB |
Output is correct |
34 |
Correct |
1 ms |
2908 KB |
Output is correct |
35 |
Correct |
1 ms |
2908 KB |
Output is correct |
36 |
Correct |
1 ms |
2908 KB |
Output is correct |
37 |
Correct |
1 ms |
4956 KB |
Output is correct |
38 |
Correct |
3 ms |
3164 KB |
Output is correct |
39 |
Execution timed out |
1085 ms |
3136 KB |
Time limit exceeded |
40 |
Halted |
0 ms |
0 KB |
- |