#include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define _size(x) (int)x.size()
#define BIT(i, x) ((x >> i) & 1)
#define MASK(n) ((1 << n) - 1)
#define REP(i, n) for (int i = 0, _n = (n); i < _n; i++)
#define FOR(i, a, b) for (int i = a, _b = (b); i <= _b; i++)
#define FORD(i, a, b) for (int i = a, _b = (b); i >= _b; i--)
#define FORB1(i, mask) for (int i = mask; i > 0; i ^= i & - i)
#define FORB0(i, n, mask) for (int i = ((1 << n) - 1) ^ mask; i > 0; i ^= i & - i)
#define FORALL(i, a) for (auto i: a)
#define fastio ios_base::sync_with_stdio(0); cin.tie(0);
int n, m;
vector<vector<int>> adj;
namespace subtask1 {
void main() {
vector<vector<vector<int>>> f(n + 1, vector<vector<int>>(n + 1, vector<int>(n + 1)));
FOR(i, 1, n) {
vector<vector<int>> g((1 << n), vector<int>(n));
FORALL(j, adj[i]) {
if (j == i) continue;
g[(1 << (i - 1)) | (1 << (j - 1))][j - 1] = 1;
}
FOR(mask, 0, MASK(n)) {
FORB1(_mask, mask) {
int u = __builtin_ctz(_mask);
if (!g[mask][u]) continue;
FORB1(__mask, mask) {
int v = __builtin_ctz(__mask);
if (i == u + 1 || i == v + 1 || u == v) continue;
f[i][v + 1][u + 1]++;
}
FORALL(v, adj[u + 1]) {
if (BIT(v - 1, mask)) continue;
g[mask | (1 << (v - 1))][v - 1] = 1;
}
}
}
}
int ans = 0;
FOR(i, 1, n) FOR(j, 1, n) FOR(z, 1, n) {
if (i == j || i == z || j == z) continue;
ans += (f[i][j][z] > 0);
}
cout << ans << '\n';
}
}
namespace subtask2 {
void main() {
}
}
bool checkSub3() {
vector<int> deg(n + 1);
FOR(u, 1, n) FORALL(v, adj[u]) deg[v]++;
FOR(u, 1, n) if (deg[u] > 2) return false;
return true;
}
namespace subtask3 {
const int inf = 1e9;
int t_time = 0;
vector<int> num, low, child, depth, vtx, sz;
vector<vector<int>> p;
void dfs(int _p, int u) {
vtx.push_back(u);
num[u] = ++t_time;
low[u] = inf;
FORALL(v, adj[u]) {
if (v == _p) continue;
if (num[v]) low[u] = min(low[u], num[v]);
else {
depth[v] = depth[u] + 1;
p[0][v] = u;
dfs(u, v);
low[u] = min(low[u], low[v]);
child[u] += child[v];
}
}
}
int jump(int u, int l) {
FORB1(mask, l) u = p[__builtin_ctz(mask)][u];
return u;
}
void main() {
num.resize(n + 1);
low.resize(n + 1);
child.assign(n + 1, 1);
sz.resize(n + 1);
depth.resize(n + 1);
p.assign(20, vector<int>(n + 1));
long long ans = 0;
FOR(u, 1, n) {
if (num[u]) continue;
vtx.clear();
dfs(u, u);
int cnt = _size(vtx);
if (low[u] == inf) ans += 1LL * cnt * (cnt - 1) * (cnt - 2);
else ans += 1LL * cnt * (cnt - 1) * (cnt - 2);
FORALL(v, vtx) sz[v] = cnt;
}
FOR(j, 1, 19) FOR(i, 1, n) p[j][i] = p[j - 1][p[j - 1][i]];
vector<int> f(n + 1);
FOR(u, 1, n) {
if (!p[0][u]) continue;
if (low[u] < num[p[0][u]]) f[p[0][u]] += child[u];
}
FOR(u, 1, n) {
if (low[u] != inf) continue;
FORALL(v, adj[u]) {
if (depth[u] > depth[v]) {
int value = sz[u] - child[u] + f[u];
ans -= 1LL * value * (value - 1);
}
else {
int r = jump(v, depth[u] - depth[v] - 1);
int value = child[r];
if (low[r] < num[u]) value += sz[u] - child[u];
ans -= 1LL * value * (value - 1);
}
}
}
cout << ans << '\n';
}
}
vector<int> vis_check;
int dfs_check(int p, int u) {
int res = 1;
vis_check[u]++;
FORALL(v, adj[u]) {
if (v == p) continue;
if (vis_check[v]) return 0;
res = min(res, dfs_check(u, v));
}
return res;
}
bool checkSub45() {
vis_check.resize(n + 1);
int res = 1;
FOR(u, 1, n) if (!vis_check[u]) res = min(res, dfs_check(- 1, u));
if (res) return true;
return false;
}
namespace subtask4 {
vector<int> vis, child;
vector<vector<int>> a;
void dfs(int p, int u) {
a[p][u]++;
vis[u]++;
FORALL(v, adj[u]) {
if (vis[v]) continue;
dfs(p, v);
child[u] += child[v];
}
}
void main() {
a.assign(n + 1, vector<int>(n + 1));
vis.resize(n + 1);
child.resize(n + 1);
int ans = 0;
FOR(u, 1, n) {
FOR(i, 1, n) vis[i] = 0, child[i] = 1;
dfs(u, u);
int cnt = 0;
FOR(v, 1, n) {
if (v == u) continue;
cnt += (a[u][v] > 0);
}
ans += cnt * (cnt - 1);
FORALL(v, adj[u]) ans -= child[v] * (child[v] - 1);
}
cout << ans << '\n';
}
}
namespace subtask5 {
const int inf = 1e9;
int t_time = 0;
vector<int> num, low, child, depth, vtx, sz;
vector<vector<int>> p;
void dfs(int _p, int u) {
vtx.push_back(u);
num[u] = ++t_time;
low[u] = inf;
FORALL(v, adj[u]) {
if (v == _p) continue;
if (num[v]) low[u] = min(low[u], num[v]);
else {
depth[v] = depth[u] + 1;
p[0][v] = u;
dfs(u, v);
low[u] = min(low[u], low[v]);
child[u] += child[v];
}
}
}
int jump(int u, int l) {
FORB1(mask, l) u = p[__builtin_ctz(mask)][u];
return u;
}
void main() {
num.resize(n + 1);
low.resize(n + 1);
child.assign(n + 1, 1);
sz.resize(n + 1);
depth.resize(n + 1);
p.assign(20, vector<int>(n + 1));
long long ans = 0;
FOR(u, 1, n) {
if (num[u]) continue;
vtx.clear();
dfs(u, u);
int cnt = _size(vtx);
ans += 1LL * cnt * (cnt - 1) * (cnt - 2);
FORALL(v, vtx) sz[v] = cnt;
}
FOR(j, 1, 19) FOR(i, 1, n) p[j][i] = p[j - 1][p[j - 1][i]];
vector<int> f(n + 1);
FOR(u, 1, n) {
if (!p[0][u]) continue;
if (low[u] < num[p[0][u]]) f[p[0][u]] += child[u];
}
FOR(u, 1, n) {
FORALL(v, adj[u]) {
if (depth[u] > depth[v]) {
int value = sz[u] - child[u] + f[u];
ans -= 1LL * value * (value - 1);
}
else {
int r = jump(v, depth[u] - depth[v] - 1);
int value = child[r];
if (low[r] < num[u]) value += sz[u] - child[u];
ans -= 1LL * value * (value - 1);
}
}
}
cout << ans << '\n';
}
}
int main() {
fastio;
cin >> n >> m;
adj.resize(n + 1);
REP(i, m) {
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
FOR(u, 1, n) {
sort(adj[u].begin(), adj[u].end());
adj[u].resize(unique(adj[u].begin(), adj[u].end()) - adj[u].begin());
}
if (n <= 10 && m <= 100) subtask1::main();
else if (n <= 50 && m <= 100) subtask2::main();
else if (checkSub3()) subtask3::main();
else if (checkSub45()) {
if (n <= 1000) subtask4::main();
else subtask5::main();
}
return 0;
}
Compilation message
count_triplets.cpp: In function 'void subtask1::main()':
count_triplets.cpp:41:35: warning: suggest parentheses around '-' inside '>>' [-Wparentheses]
41 | if (BIT(v - 1, mask)) continue;
| ~~^~~
count_triplets.cpp:7:26: note: in definition of macro 'BIT'
7 | #define BIT(i, x) ((x >> i) & 1)
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
496 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
496 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
22936 KB |
Output is correct |
2 |
Correct |
51 ms |
24272 KB |
Output is correct |
3 |
Correct |
51 ms |
20940 KB |
Output is correct |
4 |
Correct |
52 ms |
22732 KB |
Output is correct |
5 |
Correct |
51 ms |
19916 KB |
Output is correct |
6 |
Correct |
44 ms |
19836 KB |
Output is correct |
7 |
Correct |
49 ms |
18892 KB |
Output is correct |
8 |
Correct |
42 ms |
19400 KB |
Output is correct |
9 |
Correct |
46 ms |
18124 KB |
Output is correct |
10 |
Correct |
47 ms |
18808 KB |
Output is correct |
11 |
Correct |
37 ms |
17404 KB |
Output is correct |
12 |
Correct |
36 ms |
17352 KB |
Output is correct |
13 |
Correct |
34 ms |
17108 KB |
Output is correct |
14 |
Correct |
47 ms |
17356 KB |
Output is correct |
15 |
Correct |
28 ms |
16428 KB |
Output is correct |
16 |
Correct |
32 ms |
16396 KB |
Output is correct |
17 |
Correct |
8 ms |
13264 KB |
Output is correct |
18 |
Correct |
8 ms |
13268 KB |
Output is correct |
19 |
Correct |
8 ms |
13268 KB |
Output is correct |
20 |
Correct |
8 ms |
13268 KB |
Output is correct |
21 |
Correct |
10 ms |
13292 KB |
Output is correct |
22 |
Correct |
9 ms |
13264 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
4444 KB |
Output is correct |
2 |
Correct |
11 ms |
4476 KB |
Output is correct |
3 |
Correct |
10 ms |
4480 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
13 ms |
4444 KB |
Output is correct |
6 |
Correct |
13 ms |
4496 KB |
Output is correct |
7 |
Correct |
1 ms |
604 KB |
Output is correct |
8 |
Correct |
13 ms |
4444 KB |
Output is correct |
9 |
Correct |
13 ms |
4444 KB |
Output is correct |
10 |
Correct |
9 ms |
4444 KB |
Output is correct |
11 |
Correct |
9 ms |
4480 KB |
Output is correct |
12 |
Correct |
7 ms |
4444 KB |
Output is correct |
13 |
Correct |
5 ms |
4440 KB |
Output is correct |
14 |
Correct |
4 ms |
4440 KB |
Output is correct |
15 |
Correct |
3 ms |
4444 KB |
Output is correct |
16 |
Correct |
4 ms |
4420 KB |
Output is correct |
17 |
Correct |
8 ms |
4444 KB |
Output is correct |
18 |
Correct |
10 ms |
4444 KB |
Output is correct |
19 |
Correct |
9 ms |
4488 KB |
Output is correct |
20 |
Correct |
9 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
17240 KB |
Output is correct |
2 |
Correct |
50 ms |
17364 KB |
Output is correct |
3 |
Correct |
49 ms |
17356 KB |
Output is correct |
4 |
Correct |
44 ms |
17276 KB |
Output is correct |
5 |
Correct |
51 ms |
17372 KB |
Output is correct |
6 |
Correct |
51 ms |
20292 KB |
Output is correct |
7 |
Correct |
50 ms |
19404 KB |
Output is correct |
8 |
Correct |
49 ms |
18892 KB |
Output is correct |
9 |
Correct |
59 ms |
18420 KB |
Output is correct |
10 |
Correct |
45 ms |
17100 KB |
Output is correct |
11 |
Correct |
44 ms |
17096 KB |
Output is correct |
12 |
Correct |
52 ms |
16924 KB |
Output is correct |
13 |
Correct |
52 ms |
16844 KB |
Output is correct |
14 |
Correct |
50 ms |
16920 KB |
Output is correct |
15 |
Correct |
38 ms |
16656 KB |
Output is correct |
16 |
Correct |
28 ms |
15988 KB |
Output is correct |
17 |
Correct |
39 ms |
17476 KB |
Output is correct |
18 |
Correct |
40 ms |
17440 KB |
Output is correct |
19 |
Correct |
36 ms |
17472 KB |
Output is correct |
20 |
Correct |
37 ms |
17612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
4696 KB |
Output is correct |
2 |
Correct |
11 ms |
4480 KB |
Output is correct |
3 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
17372 KB |
Output is correct |
2 |
Correct |
50 ms |
17096 KB |
Output is correct |
3 |
Incorrect |
32 ms |
6284 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
496 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
600 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
2 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
344 KB |
Output is correct |
20 |
Correct |
1 ms |
348 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
496 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
0 ms |
348 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
0 ms |
348 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
348 KB |
Output is correct |
36 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
37 |
Halted |
0 ms |
0 KB |
- |