#include<bits/stdc++.h>
#define pb emplace_back
using namespace std;
using ll = long long;
const int maxn = 300010;
int n, m;
vector<int> edge[maxn];
int low[maxn], st[maxn], stl, dep[maxn];
int cnt_b;
ll gsz;
vector<int> c[maxn];
bool is_ap[maxn];
ll res;
int h;
#define c2(i) ((ll)(i) * ((ll)(i)-1) / 2)
#define p3(i) ((ll)(i) * ((ll)(i)-1) * ((ll)(i)-2))
vector<int> tree_edge[maxn];
int sz[maxn];
void dfs2(int now, int last = 0){
sz[now] = c[now].size();
for (int u : tree_edge[now]) if(u != last) {
dfs2(u, now);
sz[now] += sz[u] - (now > n);
assert((sz[u] - (now)>n) >= 0);
}
if (now > n){
ll apc = 0;
for (int u : c[now])
apc += is_ap[u];
assert(apc > 0);
auto add = [&](ll a_with_ap, ll b_with_ap, ll all) {
res += (a_with_ap - apc) * (b_with_ap) * (a_with_ap - apc - 1) * 2;
res += (b_with_ap) * (all - a_with_ap - b_with_ap + 1) * (a_with_ap - apc);
};
for (int u : tree_edge[now]) if(u != last) {
add(c[now].size(), sz[u], gsz);
}
if (last)
add(c[now].size(), gsz - sz[now]+1, gsz);
res += p3(c[now].size());
}
else {
auto add = [&](ll a, ll all){
res += (a-1) * (all-a);
};
for (int u : tree_edge[now]) if(u != last)
add(sz[u], gsz);
if (last)
add(gsz - sz[now]+1, gsz);
}
}
void cal(){
for (int i = h;i < cnt_b;++i){
for (int u : c[i]) if (is_ap[u]) {
tree_edge[u].pb(i);
tree_edge[i].pb(u);
}
}
dfs2(h);
}
void dfs(int now, int last = 0){
static int d;
if (last == 0) {
gsz = 0;
h = cnt_b;
}
++gsz;
low[now] = dep[now] = ++d;
st[stl++] = now;
int cnt_c = 0;
for (int u : edge[now]) if (u != last) {
if (!low[u]){
dfs(u, now);
++cnt_c;
low[now] = min(low[now], low[u]);
if (low[u] >= dep[now]){
do {
c[cnt_b].pb( st[--stl] );
} while (st[stl] != u);
c[cnt_b++].pb(now);
is_ap[now] = true;
}
}
else
low[now] = min(low[now], dep[u]);
}
if (last == 0 && cnt_c < 2)
is_ap[now] = false;
--d;
if (last == 0)
cal();
}
signed main(){
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> m;
for ( int a, b, i = 0 ; i < m ; ++i) {
cin >> a >> b;
edge[a].pb(b);
edge[b].pb(a);
}
cnt_b = n+1;
for (int i = 1;i <= n;++i) if (!low[i])
dfs(i);
cout << res << '\n';
}
Compilation message
In file included from /usr/include/c++/7/cassert:44:0,
from /usr/include/x86_64-linux-gnu/c++/7/bits/stdc++.h:33,
from count_triplets.cpp:1:
count_triplets.cpp: In function 'void dfs2(int, int)':
count_triplets.cpp:24:28: warning: comparison of constant '0' with boolean expression is always true [-Wbool-compare]
assert((sz[u] - (now)>n) >= 0);
~~~~~~~~~~~~~~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
41 ms |
43384 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
41 ms |
43384 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
126 ms |
69104 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
18 ms |
21624 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
131 ms |
34400 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
18 ms |
21624 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
135 ms |
34424 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
41 ms |
43384 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
41 ms |
43384 KB |
Execution killed with signal 11 (could be triggered by violating memory limits) |
2 |
Halted |
0 ms |
0 KB |
- |