#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e5;
int N, M;
vector<int> adj[MAXN+10];
int idx[MAXN+10], low[MAXN+10], cnt;
ll ans;
void dfs(int now, int bef)
{
idx[now]=low[now]=++cnt;
for(auto nxt : adj[now])
{
if(nxt==bef) continue;
if(!idx[nxt])
{
dfs(nxt, now);
low[now]=min(low[now], low[nxt]);
}
else
{
low[now]=min(low[now], idx[nxt]);
}
}
}
bool vis[MAXN+10];
int sz[2*MAXN+10];
vector<int> bcc[MAXN+10], adj2[MAXN+10];
int ccnt;
void color(int now, int col)
{
if(col)
{
bcc[now].push_back(col);
sz[col]++;
}
vis[now]=true;
for(int nxt : adj[now])
{
if(vis[nxt]) continue;
if(idx[now]<=low[nxt])
{
ccnt++;
bcc[now].push_back(ccnt);
sz[ccnt]++;
color(nxt, ccnt);
}
else
{
color(nxt, col);
}
}
}
bool vis2[2*MAXN+10];
void dfs2(int now)
{
vis2[now]=true;
for(auto nxt : adj2[now])
{
if(vis2[nxt]) continue;
dfs2(nxt);
sz[now]+=sz[nxt];
}
//printf("!%d %d\n", now, sz[now]);
}
void dfs3(int now, int bef)
{
ll sum=0, val=0;
for(int nxt : adj2[now])
{
if(nxt==bef) continue;
sz[now]-=sz[nxt];
sz[nxt]+=sz[now];
dfs3(nxt, now);
sz[nxt]-=sz[now];
sz[now]+=sz[nxt];
}
for(int nxt : adj2[now])
{
val-=(ll)sz[nxt]*sz[nxt];
sum+=sz[nxt];
//printf("!!!!%d / %d %d\n", now, nxt, sz[nxt]);
}
val+=sum*sum;
//printf("%d %lld %lld\n", now, val, sum);
if(now>N) ans+=val;
else ans+=(sz[now]-sum)*val+2*(sz[now]-sum)*((sum-adj2[now].size())*(sz[now]-sum-1)+(sum-adj2[now].size())*(adj2[now].size()-1));//, printf("WOW %d %lld %lld\n", now, (sz[now]-sum)*val, 2*(sz[now]-sum)*((sum-adj2[now].size())*(sz[now]-sum-1)+(sum-adj2[now].size())*(adj2[now].size()-1)));
}
int main()
{
int i, j;
scanf("%d%d", &N, &M);
for(i=1; i<=M; i++)
{
int u, v;
scanf("%d%d", &u, &v);
adj[u].push_back(v);
adj[v].push_back(u);
}
for(i=1; i<=N; i++)
{
if(vis[i]) continue;
dfs(i, i);
color(i, 0);
}
for(i=1; i<=N; i++)
{
if(bcc[i].size()>1)
{
for(auto j : bcc[i]) adj2[i+N].push_back(j), adj2[j].push_back(i+N);
sz[i+N]=1;
}
}
for(i=1; i<=ccnt; i++) ans+=(ll)(sz[i])*(sz[i]-1)*(sz[i]-2); //printf("ANS %lld\n", ans);
for(i=1; i<=ccnt; i++) sz[i]-=adj2[i].size();
/*
for(i=1; i<=2*N; i++)
{
printf("%d : (%d)", i, sz[i]);
for(auto j : adj2[i]) printf("%d ", j);
printf("\n");
}
*/
for(i=1; i<=ccnt; i++)
{
if(vis2[i]) continue;
dfs2(i);
dfs3(i, i);
}
printf("%lld", ans);
}
Compilation message
count_triplets.cpp: In function 'int main()':
count_triplets.cpp:104:12: warning: unused variable 'j' [-Wunused-variable]
int i, j;
^
count_triplets.cpp:106:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &N, &M);
~~~~~^~~~~~~~~~~~~~~~
count_triplets.cpp:110:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &u, &v);
~~~~~^~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7416 KB |
Output is correct |
2 |
Correct |
8 ms |
7444 KB |
Output is correct |
3 |
Correct |
8 ms |
7416 KB |
Output is correct |
4 |
Correct |
8 ms |
7416 KB |
Output is correct |
5 |
Correct |
8 ms |
7416 KB |
Output is correct |
6 |
Correct |
8 ms |
7420 KB |
Output is correct |
7 |
Incorrect |
8 ms |
7416 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7416 KB |
Output is correct |
2 |
Correct |
8 ms |
7444 KB |
Output is correct |
3 |
Correct |
8 ms |
7416 KB |
Output is correct |
4 |
Correct |
8 ms |
7416 KB |
Output is correct |
5 |
Correct |
8 ms |
7416 KB |
Output is correct |
6 |
Correct |
8 ms |
7420 KB |
Output is correct |
7 |
Incorrect |
8 ms |
7416 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
23636 KB |
Output is correct |
2 |
Correct |
125 ms |
23516 KB |
Output is correct |
3 |
Runtime error |
162 ms |
38796 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
7588 KB |
Output is correct |
2 |
Correct |
11 ms |
7616 KB |
Output is correct |
3 |
Correct |
11 ms |
7544 KB |
Output is correct |
4 |
Correct |
11 ms |
7672 KB |
Output is correct |
5 |
Correct |
12 ms |
7672 KB |
Output is correct |
6 |
Correct |
11 ms |
7544 KB |
Output is correct |
7 |
Correct |
11 ms |
7544 KB |
Output is correct |
8 |
Correct |
11 ms |
7544 KB |
Output is correct |
9 |
Correct |
11 ms |
7544 KB |
Output is correct |
10 |
Correct |
11 ms |
7548 KB |
Output is correct |
11 |
Correct |
9 ms |
7676 KB |
Output is correct |
12 |
Correct |
9 ms |
7544 KB |
Output is correct |
13 |
Correct |
10 ms |
7544 KB |
Output is correct |
14 |
Correct |
9 ms |
7544 KB |
Output is correct |
15 |
Correct |
9 ms |
7544 KB |
Output is correct |
16 |
Correct |
8 ms |
7544 KB |
Output is correct |
17 |
Correct |
9 ms |
7544 KB |
Output is correct |
18 |
Correct |
9 ms |
7544 KB |
Output is correct |
19 |
Correct |
9 ms |
7544 KB |
Output is correct |
20 |
Correct |
9 ms |
7544 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
129 ms |
31708 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
7672 KB |
Output is correct |
2 |
Correct |
9 ms |
7544 KB |
Output is correct |
3 |
Incorrect |
9 ms |
7592 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
121 ms |
31612 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7416 KB |
Output is correct |
2 |
Correct |
8 ms |
7444 KB |
Output is correct |
3 |
Correct |
8 ms |
7416 KB |
Output is correct |
4 |
Correct |
8 ms |
7416 KB |
Output is correct |
5 |
Correct |
8 ms |
7416 KB |
Output is correct |
6 |
Correct |
8 ms |
7420 KB |
Output is correct |
7 |
Incorrect |
8 ms |
7416 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
7416 KB |
Output is correct |
2 |
Correct |
8 ms |
7444 KB |
Output is correct |
3 |
Correct |
8 ms |
7416 KB |
Output is correct |
4 |
Correct |
8 ms |
7416 KB |
Output is correct |
5 |
Correct |
8 ms |
7416 KB |
Output is correct |
6 |
Correct |
8 ms |
7420 KB |
Output is correct |
7 |
Incorrect |
8 ms |
7416 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |