#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<pii, int> ppi;
#define ff first
#define ss second
const int N = 1e5 + 9;
int n, m;
vector<int> g[N];
namespace subtask1 {
bool avail[1 << 10][10][10];
vector<int> g2[10][10];
void solve() {
for (int i = 0; i < n; ++i) avail[1 << i][i][i] = true;
for (int msk = 1; msk < (1 << n); ++msk)
for (int s = 0; s < n; ++s) if (msk >> s & 1)
for (int f = 0; f < n; ++f) if ((msk >> f & 1) && f != s) {
for (int e : g[f + 1]) if (msk >> (e - 1) & 1)
avail[msk][s][f] |= avail[msk ^ (1 << f)][s][e - 1];
if (avail[msk][s][f]) g2[s][f].push_back(msk);
}
ll ans(0);
for (int s = 0; s < n; ++s)
for (int c = 0; c < n; ++c) if (c != s)
for (int f = 0; f < n; ++f) if (f != s && f != c) {
bool check(false);
for (int msk1 : g2[s][c]) {
for (int msk2 : g2[c][f]) {
if ((msk1 & msk2) == (1 << c)) check = true;
if (check) break;
}
if (check) break;
}
ans += check;
}
cout << ans << '\n';
}
}
// bridge tree (WA for subtask 89)
namespace subtask34567 {
int num[N], low[N], tme;
int lab[N], cnt_scc, sz[N];
int path[N], top;
void tarjan(int u, int p) {
num[u] = low[u] = ++tme;
path[++top] = u;
for (int v : g[u]) if (v != p) {
if (num[v]) low[u] = min(low[u], num[v]);
else {
tarjan(v, u);
low[u] = min(low[u], low[v]);
}
}
if (num[u] == low[u]) { // chot
++cnt_scc;
int v;
do {
v = path[top--];
num[v] = low[v] = N;
lab[v] = cnt_scc;
++sz[cnt_scc];
} while (v != u);
}
}
vector<ppi> g2[N];
void init_graph() {
for (int u = 1; u <= n; ++u) for (int v : g[u])
if (lab[u] != lab[v]) g2[lab[u]].emplace_back(pii(u, v), lab[v]);
}
ll ans, dp[N][3];
bool vis[N];
void dfs(int u, int p, int vertex) {
vis[u] = true;
sort(g2[u].begin(), g2[u].end());
/* sort(g2[u].begin(), g2[u].end(), [=](const ppi &a, const ppi &b) {
if (a.ff.ff == vertex && b.ff.ff == vertex) return false;
if (a.ff.ff == vertex) return true;
if (b.ff.ff == vertex) return false;
return a.ff < b.ff;
});
*/
dp[u][1] = sz[u];
dp[u][2] = (ll)(sz[u] - 1) * (sz[u] - 1);
ans += (ll)sz[u] * (sz[u] - 1) * (sz[u] - 2);
ll cnt(0), dp1(0), dp2(0);
int pre(0);
for (ppi x : g2[u]) if (x.ss != p) {
int v = x.ss;
if (pre != x.ff.ff) {
dp[u][2] += cnt, cnt = 0;
pre = x.ff.ff;
}
dfs(v, u, x.ff.ss);
ans += dp[u][1] * dp[v][2] * 2;
ans += dp[u][2] * dp[v][1] * 2;
dp[u][1] += dp[v][1];
dp[u][2] += dp[v][2];
dp[u][2] += dp[v][1];
if (vertex != x.ff.ff) {
cnt += dp[v][1] * (sz[u] - 1);
ans += dp1 * dp[v][1] * 2;
}
else dp1 += dp[v][1] * (sz[u] - 1);
}
dp[u][2] += cnt;
}
void solve() {
for (int i = 1; i <= n; ++i)
if (!num[i]) tarjan(i, 0);
init_graph();
dfs(4, 0, 0);
for (int u = 1; u <= cnt_scc; ++u)
if (!vis[u]) dfs(u, 0, 0);
cout << ans << '\n';
}
}
bool f[N];
int main() {
cin.tie(NULL)->sync_with_stdio(false);
cin >> n >> m;
for (int i = 0, u, v; i < m; ++i) {
cin >> u >> v;
g[u].push_back(v);
g[v].push_back(u);
}
if (n <= 10)
subtask1::solve();
else subtask34567::solve();
}
/** /\_/\
* (= ._.)
* / >0 \>1
**/
/*
==================================================+
INPUT: |
--------------------------------------------------|
15 19
1 2
2 3
3 1
4 5
5 6
6 4
7 8
8 9
9 7
10 11
11 12
12 10
13 14
14 15
15 13
1 10
1 4
1 7
2 13
--------------------------------------------------|
==================================================+
OUTPUT: |
--------------------------------------------------|
726
--------------------------------------------------|
==================================================+
*/
Compilation message
count_triplets.cpp: In function 'void subtask34567::dfs(int, int, int)':
count_triplets.cpp:98:28: warning: unused variable 'dp2' [-Wunused-variable]
98 | ll cnt(0), dp1(0), dp2(0);
| ^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5076 KB |
Output is correct |
8 |
Correct |
4 ms |
5076 KB |
Output is correct |
9 |
Correct |
4 ms |
5204 KB |
Output is correct |
10 |
Correct |
4 ms |
5204 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
4 ms |
5040 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
4 ms |
5076 KB |
Output is correct |
15 |
Correct |
4 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 |
4 ms |
5076 KB |
Output is correct |
21 |
Correct |
4 ms |
5048 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
4948 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5076 KB |
Output is correct |
30 |
Correct |
3 ms |
4948 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5064 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5076 KB |
Output is correct |
8 |
Correct |
4 ms |
5076 KB |
Output is correct |
9 |
Correct |
4 ms |
5204 KB |
Output is correct |
10 |
Correct |
4 ms |
5204 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
4 ms |
5040 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
4 ms |
5076 KB |
Output is correct |
15 |
Correct |
4 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 |
4 ms |
5076 KB |
Output is correct |
21 |
Correct |
4 ms |
5048 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
4948 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5076 KB |
Output is correct |
30 |
Correct |
3 ms |
4948 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5064 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5048 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
3 ms |
4948 KB |
Output is correct |
38 |
Incorrect |
4 ms |
5044 KB |
Output isn't correct |
39 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
19032 KB |
Output is correct |
2 |
Correct |
53 ms |
19024 KB |
Output is correct |
3 |
Correct |
70 ms |
17892 KB |
Output is correct |
4 |
Correct |
81 ms |
18068 KB |
Output is correct |
5 |
Correct |
72 ms |
15064 KB |
Output is correct |
6 |
Correct |
65 ms |
18856 KB |
Output is correct |
7 |
Correct |
69 ms |
15172 KB |
Output is correct |
8 |
Correct |
60 ms |
16976 KB |
Output is correct |
9 |
Correct |
57 ms |
14028 KB |
Output is correct |
10 |
Correct |
80 ms |
14296 KB |
Output is correct |
11 |
Correct |
62 ms |
12772 KB |
Output is correct |
12 |
Correct |
48 ms |
12700 KB |
Output is correct |
13 |
Correct |
52 ms |
12872 KB |
Output is correct |
14 |
Correct |
44 ms |
12660 KB |
Output is correct |
15 |
Correct |
35 ms |
12876 KB |
Output is correct |
16 |
Correct |
37 ms |
12672 KB |
Output is correct |
17 |
Correct |
7 ms |
9044 KB |
Output is correct |
18 |
Correct |
8 ms |
9044 KB |
Output is correct |
19 |
Correct |
9 ms |
9040 KB |
Output is correct |
20 |
Correct |
7 ms |
9044 KB |
Output is correct |
21 |
Correct |
6 ms |
9044 KB |
Output is correct |
22 |
Correct |
6 ms |
9044 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 |
5204 KB |
Output is correct |
5 |
Correct |
3 ms |
5204 KB |
Output is correct |
6 |
Correct |
3 ms |
5124 KB |
Output is correct |
7 |
Correct |
3 ms |
5204 KB |
Output is correct |
8 |
Correct |
3 ms |
5204 KB |
Output is correct |
9 |
Correct |
4 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 |
5128 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
4 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 |
4 ms |
5204 KB |
Output is correct |
19 |
Correct |
4 ms |
5204 KB |
Output is correct |
20 |
Correct |
3 ms |
5204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
16420 KB |
Output is correct |
2 |
Correct |
79 ms |
16468 KB |
Output is correct |
3 |
Correct |
80 ms |
16512 KB |
Output is correct |
4 |
Correct |
76 ms |
16500 KB |
Output is correct |
5 |
Correct |
71 ms |
16424 KB |
Output is correct |
6 |
Correct |
81 ms |
26444 KB |
Output is correct |
7 |
Correct |
84 ms |
20152 KB |
Output is correct |
8 |
Correct |
85 ms |
19152 KB |
Output is correct |
9 |
Correct |
82 ms |
18104 KB |
Output is correct |
10 |
Correct |
66 ms |
16464 KB |
Output is correct |
11 |
Correct |
76 ms |
16516 KB |
Output is correct |
12 |
Correct |
76 ms |
16476 KB |
Output is correct |
13 |
Correct |
93 ms |
16500 KB |
Output is correct |
14 |
Correct |
80 ms |
16132 KB |
Output is correct |
15 |
Correct |
56 ms |
15496 KB |
Output is correct |
16 |
Correct |
35 ms |
13780 KB |
Output is correct |
17 |
Correct |
59 ms |
16992 KB |
Output is correct |
18 |
Correct |
72 ms |
16836 KB |
Output is correct |
19 |
Correct |
79 ms |
16816 KB |
Output is correct |
20 |
Correct |
67 ms |
16840 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 |
5076 KB |
Output is correct |
5 |
Correct |
3 ms |
5076 KB |
Output is correct |
6 |
Correct |
4 ms |
5076 KB |
Output is correct |
7 |
Correct |
5 ms |
5076 KB |
Output is correct |
8 |
Correct |
5 ms |
5076 KB |
Output is correct |
9 |
Correct |
3 ms |
5076 KB |
Output is correct |
10 |
Correct |
4 ms |
5076 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
4 ms |
5080 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 |
5140 KB |
Output is correct |
17 |
Correct |
3 ms |
5076 KB |
Output is correct |
18 |
Correct |
3 ms |
5076 KB |
Output is correct |
19 |
Correct |
4 ms |
5076 KB |
Output is correct |
20 |
Correct |
3 ms |
5076 KB |
Output is correct |
21 |
Correct |
3 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 |
5076 KB |
Output is correct |
26 |
Correct |
3 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
16508 KB |
Output is correct |
2 |
Correct |
71 ms |
16204 KB |
Output is correct |
3 |
Correct |
70 ms |
13980 KB |
Output is correct |
4 |
Correct |
73 ms |
12156 KB |
Output is correct |
5 |
Correct |
78 ms |
10752 KB |
Output is correct |
6 |
Correct |
65 ms |
10256 KB |
Output is correct |
7 |
Correct |
48 ms |
10012 KB |
Output is correct |
8 |
Correct |
44 ms |
9828 KB |
Output is correct |
9 |
Correct |
53 ms |
9744 KB |
Output is correct |
10 |
Correct |
49 ms |
9628 KB |
Output is correct |
11 |
Correct |
69 ms |
9468 KB |
Output is correct |
12 |
Correct |
57 ms |
9552 KB |
Output is correct |
13 |
Correct |
44 ms |
9716 KB |
Output is correct |
14 |
Correct |
50 ms |
11992 KB |
Output is correct |
15 |
Correct |
81 ms |
17608 KB |
Output is correct |
16 |
Correct |
82 ms |
16576 KB |
Output is correct |
17 |
Correct |
110 ms |
16216 KB |
Output is correct |
18 |
Correct |
83 ms |
15176 KB |
Output is correct |
19 |
Correct |
60 ms |
12064 KB |
Output is correct |
20 |
Correct |
62 ms |
12168 KB |
Output is correct |
21 |
Correct |
80 ms |
12028 KB |
Output is correct |
22 |
Correct |
76 ms |
11748 KB |
Output is correct |
23 |
Correct |
56 ms |
11340 KB |
Output is correct |
24 |
Correct |
62 ms |
14148 KB |
Output is correct |
25 |
Correct |
71 ms |
14164 KB |
Output is correct |
26 |
Correct |
67 ms |
12864 KB |
Output is correct |
27 |
Correct |
102 ms |
12916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5076 KB |
Output is correct |
8 |
Correct |
4 ms |
5076 KB |
Output is correct |
9 |
Correct |
4 ms |
5204 KB |
Output is correct |
10 |
Correct |
4 ms |
5204 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
4 ms |
5040 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
4 ms |
5076 KB |
Output is correct |
15 |
Correct |
4 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 |
4 ms |
5076 KB |
Output is correct |
21 |
Correct |
4 ms |
5048 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
4948 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5076 KB |
Output is correct |
30 |
Correct |
3 ms |
4948 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5064 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5048 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
3 ms |
4948 KB |
Output is correct |
38 |
Incorrect |
4 ms |
5044 KB |
Output isn't correct |
39 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
4948 KB |
Output is correct |
2 |
Correct |
3 ms |
4948 KB |
Output is correct |
3 |
Correct |
3 ms |
4948 KB |
Output is correct |
4 |
Correct |
3 ms |
4948 KB |
Output is correct |
5 |
Correct |
3 ms |
4948 KB |
Output is correct |
6 |
Correct |
3 ms |
5076 KB |
Output is correct |
7 |
Correct |
4 ms |
5076 KB |
Output is correct |
8 |
Correct |
4 ms |
5076 KB |
Output is correct |
9 |
Correct |
4 ms |
5204 KB |
Output is correct |
10 |
Correct |
4 ms |
5204 KB |
Output is correct |
11 |
Correct |
3 ms |
5076 KB |
Output is correct |
12 |
Correct |
4 ms |
5040 KB |
Output is correct |
13 |
Correct |
3 ms |
5076 KB |
Output is correct |
14 |
Correct |
4 ms |
5076 KB |
Output is correct |
15 |
Correct |
4 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 |
4 ms |
5076 KB |
Output is correct |
21 |
Correct |
4 ms |
5048 KB |
Output is correct |
22 |
Correct |
4 ms |
5076 KB |
Output is correct |
23 |
Correct |
4 ms |
5076 KB |
Output is correct |
24 |
Correct |
3 ms |
4948 KB |
Output is correct |
25 |
Correct |
3 ms |
5076 KB |
Output is correct |
26 |
Correct |
4 ms |
5076 KB |
Output is correct |
27 |
Correct |
3 ms |
5076 KB |
Output is correct |
28 |
Correct |
4 ms |
5076 KB |
Output is correct |
29 |
Correct |
3 ms |
5076 KB |
Output is correct |
30 |
Correct |
3 ms |
4948 KB |
Output is correct |
31 |
Correct |
3 ms |
5076 KB |
Output is correct |
32 |
Correct |
3 ms |
5076 KB |
Output is correct |
33 |
Correct |
4 ms |
5064 KB |
Output is correct |
34 |
Correct |
3 ms |
5076 KB |
Output is correct |
35 |
Correct |
3 ms |
5048 KB |
Output is correct |
36 |
Correct |
3 ms |
5076 KB |
Output is correct |
37 |
Correct |
3 ms |
4948 KB |
Output is correct |
38 |
Incorrect |
4 ms |
5044 KB |
Output isn't correct |
39 |
Halted |
0 ms |
0 KB |
- |