#include <bits/stdc++.h>
#define For(i, a, b) for(int i = a; i <= b; i++)
#define Forr(i, a, b) for(int i = a; i >= b; i--)
#define F first
#define S second
#define eb emplace_back
#define all(x) x.begin(), x.end()
#define sz(x) ((int)x.size())
#define int LL
using namespace std;
using LL = long long;
using pii = pair<int, int>;
const int MAXN = 100010;
vector<int> adj[MAXN];
int par[MAXN];
int dep[MAXN];
int id[MAXN];
vector<pii> bcc[MAXN];
int siz[MAXN];
int sum[MAXN];
int cur_bcc = 0;
void dfs_bcc(int n, int p, int d) {
par[n] = p;
dep[n] = d;
for(auto &i:adj[n]) if(i != p) {
if(!par[i]) {
// tree edge
dfs_bcc(i, n, d + 1);
} else if(dep[i] < dep[n]) {
// back edge
cur_bcc++;
int t = n;
while(true) {
id[t] = cur_bcc;
if(t == i) break;
t = par[t];
}
}
}
if(!id[n]) id[n] = ++cur_bcc;
for(auto &i:adj[n]) if(dep[i] > dep[n] && id[i] != id[n]) {
bcc[id[i]].eb(i, id[n]);
bcc[id[n]].eb(n, id[i]);
}
siz[id[n]]++;
}
void dfs_siz(int n, int p) {
sum[n] = siz[n];
// assert(siz[n] == 1);
for(auto &i:bcc[n]) if(i.S != p) {
dfs_siz(i.S, n);
sum[n] += sum[i.S];
}
}
int ans;
#define C(x) ((x) * ((x) - 1))
void dfs_ans(int n, int p, int tot_n) {
int t = C(tot_n - 1) * siz[n];
int t1 = t;
map<int, int> mp;
for(auto &i:bcc[n]) {
if(!mp.count(i.F)) mp[i.F] = 0;
int s = sum[i.S];
if(i.S == p) s = tot_n - sum[n];
mp[i.F] += s;
t -= C(s);
if(i.S != p) dfs_ans(i.S, n, tot_n);
}
for(auto &i:mp) {
int k = i.S;
// cout << "! " << i.F << " " << i.S << '\n';
t -= C(k + 1) * (siz[n] - 1);
}
// cout << t1 << " " << t << "\n";
ans += t;
}
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0);
// =^-w-^= nya
int n, m; cin >> n >> m;
while(m--) {
int a, b; cin >> a >> b;
adj[a].eb(b);
adj[b].eb(a);
}
ans = 0;
For(i, 1, n) if(!par[i]) {
For(j, 1, cur_bcc) {
bcc[j].clear();
siz[j] = 0;
}
cur_bcc = 0;
dfs_bcc(i, i, 1);
dfs_siz(1, 1);
dfs_ans(1, 1, sum[1]);
// cout << cur_bcc << "\n";
// For(j, 1, cur_bcc) {
// cout << j << " = " << siz[j] << ", " << sum[j] << ":";
// for(auto &k:bcc[j]) cout << " " << k;
// cout << "\n";
// }
// cout << flush;
}
cout << ans << "\n";
return 0;
}
/*
4 3
1 2
2 3
3 4
= 8
4 4
1 2
2 3
3 4
4 2
= 14
5 5
1 2
2 3
3 1
3 4
3 5
= 24
*/
Compilation message
count_triplets.cpp: In function 'void dfs_ans(LL, LL, LL)':
count_triplets.cpp:65:9: warning: unused variable 't1' [-Wunused-variable]
65 | int t1 = t;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
59 ms |
18212 KB |
Output is correct |
2 |
Correct |
53 ms |
18308 KB |
Output is correct |
3 |
Correct |
71 ms |
25420 KB |
Output is correct |
4 |
Correct |
59 ms |
19440 KB |
Output is correct |
5 |
Correct |
63 ms |
20320 KB |
Output is correct |
6 |
Correct |
76 ms |
26956 KB |
Output is correct |
7 |
Correct |
74 ms |
16512 KB |
Output is correct |
8 |
Correct |
78 ms |
21176 KB |
Output is correct |
9 |
Correct |
59 ms |
13476 KB |
Output is correct |
10 |
Correct |
70 ms |
15436 KB |
Output is correct |
11 |
Correct |
47 ms |
10456 KB |
Output is correct |
12 |
Correct |
44 ms |
10264 KB |
Output is correct |
13 |
Correct |
47 ms |
10256 KB |
Output is correct |
14 |
Correct |
48 ms |
10236 KB |
Output is correct |
15 |
Correct |
30 ms |
9812 KB |
Output is correct |
16 |
Correct |
46 ms |
9816 KB |
Output is correct |
17 |
Correct |
7 ms |
7380 KB |
Output is correct |
18 |
Correct |
7 ms |
7380 KB |
Output is correct |
19 |
Correct |
8 ms |
7372 KB |
Output is correct |
20 |
Correct |
6 ms |
7380 KB |
Output is correct |
21 |
Correct |
6 ms |
7380 KB |
Output is correct |
22 |
Correct |
6 ms |
7380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5332 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5204 KB |
Output is correct |
7 |
Correct |
3 ms |
5352 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
3 ms |
5204 KB |
Output is correct |
10 |
Correct |
3 ms |
5076 KB |
Output is correct |
11 |
Correct |
4 ms |
5076 KB |
Output is correct |
12 |
Correct |
3 ms |
5076 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
3 ms |
5076 KB |
Output is correct |
15 |
Correct |
3 ms |
5076 KB |
Output is correct |
16 |
Correct |
3 ms |
5076 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5076 KB |
Output is correct |
20 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
77 ms |
17804 KB |
Output is correct |
2 |
Correct |
72 ms |
17720 KB |
Output is correct |
3 |
Correct |
67 ms |
17788 KB |
Output is correct |
4 |
Correct |
72 ms |
17736 KB |
Output is correct |
5 |
Correct |
76 ms |
17740 KB |
Output is correct |
6 |
Correct |
84 ms |
40144 KB |
Output is correct |
7 |
Correct |
80 ms |
25956 KB |
Output is correct |
8 |
Correct |
105 ms |
23812 KB |
Output is correct |
9 |
Correct |
83 ms |
21772 KB |
Output is correct |
10 |
Correct |
64 ms |
15732 KB |
Output is correct |
11 |
Correct |
66 ms |
17096 KB |
Output is correct |
12 |
Correct |
61 ms |
14028 KB |
Output is correct |
13 |
Correct |
70 ms |
13516 KB |
Output is correct |
14 |
Correct |
47 ms |
10852 KB |
Output is correct |
15 |
Correct |
44 ms |
10592 KB |
Output is correct |
16 |
Correct |
29 ms |
9684 KB |
Output is correct |
17 |
Correct |
42 ms |
17488 KB |
Output is correct |
18 |
Correct |
43 ms |
17432 KB |
Output is correct |
19 |
Correct |
45 ms |
17936 KB |
Output is correct |
20 |
Correct |
50 ms |
17656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
5076 KB |
Output is correct |
2 |
Correct |
3 ms |
5076 KB |
Output is correct |
3 |
Correct |
3 ms |
5076 KB |
Output is correct |
4 |
Correct |
3 ms |
5048 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
3 ms |
5076 KB |
Output is correct |
8 |
Correct |
3 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
3 ms |
5040 KB |
Output is correct |
11 |
Correct |
3 ms |
5044 KB |
Output is correct |
12 |
Correct |
3 ms |
5176 KB |
Output is correct |
13 |
Correct |
3 ms |
5168 KB |
Output is correct |
14 |
Correct |
3 ms |
5112 KB |
Output is correct |
15 |
Correct |
4 ms |
5172 KB |
Output is correct |
16 |
Correct |
4 ms |
5076 KB |
Output is correct |
17 |
Correct |
4 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
3 ms |
5040 KB |
Output is correct |
20 |
Correct |
3 ms |
5040 KB |
Output is correct |
21 |
Correct |
4 ms |
5076 KB |
Output is correct |
22 |
Correct |
3 ms |
5076 KB |
Output is correct |
23 |
Correct |
3 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
5076 KB |
Output is correct |
25 |
Correct |
3 ms |
5008 KB |
Output is correct |
26 |
Correct |
3 ms |
4948 KB |
Output is correct |
27 |
Correct |
3 ms |
5040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
17680 KB |
Output is correct |
2 |
Correct |
71 ms |
17620 KB |
Output is correct |
3 |
Correct |
91 ms |
15564 KB |
Output is correct |
4 |
Correct |
102 ms |
14844 KB |
Output is correct |
5 |
Correct |
105 ms |
13276 KB |
Output is correct |
6 |
Correct |
62 ms |
12680 KB |
Output is correct |
7 |
Correct |
83 ms |
12520 KB |
Output is correct |
8 |
Correct |
82 ms |
12120 KB |
Output is correct |
9 |
Correct |
96 ms |
11968 KB |
Output is correct |
10 |
Correct |
50 ms |
11832 KB |
Output is correct |
11 |
Correct |
56 ms |
11820 KB |
Output is correct |
12 |
Correct |
50 ms |
11724 KB |
Output is correct |
13 |
Correct |
65 ms |
11748 KB |
Output is correct |
14 |
Correct |
72 ms |
13584 KB |
Output is correct |
15 |
Correct |
88 ms |
23904 KB |
Output is correct |
16 |
Correct |
103 ms |
21556 KB |
Output is correct |
17 |
Correct |
91 ms |
21308 KB |
Output is correct |
18 |
Correct |
88 ms |
19304 KB |
Output is correct |
19 |
Correct |
72 ms |
14656 KB |
Output is correct |
20 |
Correct |
71 ms |
13880 KB |
Output is correct |
21 |
Correct |
72 ms |
13648 KB |
Output is correct |
22 |
Correct |
69 ms |
12120 KB |
Output is correct |
23 |
Correct |
81 ms |
11664 KB |
Output is correct |
24 |
Correct |
69 ms |
16320 KB |
Output is correct |
25 |
Correct |
80 ms |
16408 KB |
Output is correct |
26 |
Correct |
65 ms |
15384 KB |
Output is correct |
27 |
Correct |
74 ms |
15192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
4948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |