Submission #849042

# Submission time Handle Problem Language Result Execution time Memory
849042 2023-09-14T02:21:36 Z hngwlog Duathlon (APIO18_duathlon) C++14
28 / 100
63 ms 21684 KB
#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() {

    return false;
}


namespace subtask3 {

    void main() {


    }
}

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 1 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 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 544 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 2 ms 344 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 0 ms 344 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 504 KB Output is correct
30 Correct 0 ms 344 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 344 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 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 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 544 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 2 ms 344 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 0 ms 344 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 504 KB Output is correct
30 Correct 0 ms 344 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 344 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
36 Incorrect 0 ms 344 KB Output isn't correct
37 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 29 ms 10832 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 4440 KB Output is correct
2 Correct 11 ms 4440 KB Output is correct
3 Correct 11 ms 4440 KB Output is correct
4 Correct 15 ms 4440 KB Output is correct
5 Correct 13 ms 4444 KB Output is correct
6 Correct 13 ms 4440 KB Output is correct
7 Correct 15 ms 4440 KB Output is correct
8 Correct 13 ms 4440 KB Output is correct
9 Correct 13 ms 4440 KB Output is correct
10 Correct 10 ms 4440 KB Output is correct
11 Correct 9 ms 4444 KB Output is correct
12 Correct 6 ms 4440 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 4440 KB Output is correct
16 Correct 3 ms 4444 KB Output is correct
17 Correct 8 ms 4440 KB Output is correct
18 Correct 9 ms 4440 KB Output is correct
19 Correct 8 ms 4440 KB Output is correct
20 Correct 9 ms 4440 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 41 ms 17148 KB Output is correct
2 Correct 45 ms 18376 KB Output is correct
3 Correct 50 ms 18320 KB Output is correct
4 Correct 46 ms 18384 KB Output is correct
5 Correct 43 ms 18376 KB Output is correct
6 Correct 63 ms 21684 KB Output is correct
7 Correct 46 ms 20420 KB Output is correct
8 Correct 45 ms 19912 KB Output is correct
9 Correct 46 ms 19400 KB Output is correct
10 Correct 50 ms 18120 KB Output is correct
11 Correct 41 ms 18348 KB Output is correct
12 Correct 54 ms 18212 KB Output is correct
13 Correct 48 ms 18120 KB Output is correct
14 Correct 38 ms 17572 KB Output is correct
15 Correct 37 ms 17340 KB Output is correct
16 Correct 26 ms 16284 KB Output is correct
17 Correct 36 ms 18504 KB Output is correct
18 Correct 45 ms 18624 KB Output is correct
19 Correct 34 ms 18492 KB Output is correct
20 Correct 35 ms 18636 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 11 ms 4440 KB Output is correct
2 Correct 11 ms 4440 KB Output is correct
3 Incorrect 1 ms 348 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 43 ms 16996 KB Output is correct
2 Correct 44 ms 18116 KB Output is correct
3 Incorrect 32 ms 7764 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 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 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 544 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 2 ms 344 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 0 ms 344 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 504 KB Output is correct
30 Correct 0 ms 344 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 344 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
36 Incorrect 0 ms 344 KB Output isn't correct
37 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 1 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 344 KB Output is correct
5 Correct 0 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 544 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 2 ms 344 KB Output is correct
10 Correct 2 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 348 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 600 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 344 KB Output is correct
22 Correct 1 ms 348 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 0 ms 344 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 0 ms 344 KB Output is correct
28 Correct 0 ms 348 KB Output is correct
29 Correct 1 ms 504 KB Output is correct
30 Correct 0 ms 344 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 344 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 1 ms 344 KB Output is correct
36 Incorrect 0 ms 344 KB Output isn't correct
37 Halted 0 ms 0 KB -