Submission #49245

# Submission time Handle Problem Language Result Execution time Memory
49245 2018-05-24T03:11:21 Z BThero Duathlon (APIO18_duathlon) C++17
25 / 100
1000 ms 19540 KB
// Why am I so stupid? :c
#include <bits/stdc++.h>

#define pb push_back
#define mp make_pair

#define all(x) (x).begin(), (x).end()

#define fi first
#define se second

typedef long long ll;

using namespace std;

struct edge {
    int u, v;

    edge() {
        u = v = 0;
    }
} e[200005];

int tin[100005], fup[100005];

vector <int> adj[100005];

vector <int> cp[100005];

bool bad[200005];

int col[100005];

bool u[200005];

int timer, sz;

int n, m;

ll ans;

void dfs(int v, int pr) {
    tin[v] = fup[v] = ++timer;
    u[v] = 1;

    for (int id : adj[v]) {
        int to;

        if (v == e[id].u) {
            to = e[id].v;
        }
        else {
            to = e[id].u;
        }

        if (to == pr) {
            continue;
        }

        if (u[to]) {
            fup[v] = min(fup[v], tin[to]);
        }
        else {
            dfs(to, v);
            fup[v] = min(fup[v], fup[to]);

            if (fup[to] > tin[v]) {
                bad[id] = 1;
            }
        }
    }
}

void dfs2(int v) {
    cp[sz].pb(v);
    u[v] = 1;

    for (int to : adj[v]) {
        if (u[to]) {
            continue;
        }

        dfs2(to);
    }
}

void calc(int v, int st, int mlt, int cur) {
    if (st != v) {
        int a = cp[st].size();
        int b = cp[v].size();

        ans += 1ll * (a - 1) * (b - 1) * (mlt + a - 1 + b - 1);
        ans += 1ll * (b - 1) * (mlt + b - 1);
        ans += 1ll * (a - 1) * (mlt + a - 1);
        ans += 1ll * mlt;
    }

    u[v] = 1;

    for (int id : adj[v]) {
        bool good = 1;
        int to, nxt;

        if (col[e[id].v] == v) {
            if (e[id].v == cur || cur == -1) {
                good = 0;
            }

            nxt = e[id].u;
        }
        else {
            if (e[id].u == cur || cur == -1) {
                good = 0;
            }

            nxt = e[id].v;
        }

        to = col[nxt];

        if (u[to]) {
            continue;
        }

        calc(to, st, mlt + 1 + good * (cp[v].size() - 1), nxt);
    }
}

ll f(ll x) {
    return x * (x - 1) * (x - 2);
}

void solve() {
    scanf("%d %d", &n, &m);

    for (int i = 1; i <= m; ++i) {
        scanf("%d %d", &e[i].u, &e[i].v);

        adj[e[i].u].pb(i);
        adj[e[i].v].pb(i);
    }

    for (int i = 1; i <= n; ++i) {
        if (u[i]) {
            continue;
        }

        dfs(i, -1);
    }

    for (int i = 1; i <= n; ++i) {
        adj[i].clear();
        u[i] = 0;
    }

    for (int i = 1; i <= m; ++i) {
        if (!bad[i]) {
            adj[e[i].u].pb(e[i].v);
            adj[e[i].v].pb(e[i].u);
        }
    }

    for (int i = 1; i <= n; ++i) {
        if (u[i]) {
            continue;
        }

        ++sz; dfs2(i);

        for (int it : cp[sz]) {
            col[it] = sz;
        }
    }

    for (int i = 1; i <= n; ++i) {
        adj[i].clear();
        u[i] = 0;
    }

    for (int i = 1; i <= m; ++i) {
        if (bad[i]) {
            adj[col[e[i].v]].pb(i);
            adj[col[e[i].u]].pb(i);
        }
    }

    for (int i = 1; i <= sz; ++i) {
        ans += f(cp[i].size());

        for (int j = 1; j <= sz; ++j) {
            u[j] = 0;
        }

        calc(i, i, -1, -1);
    }

    printf("%lld\n", ans);
}

int main() {
#ifdef BThero
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif // BThero

    int tt = 1;

    while (tt--) {
        solve();
    }

    return 0;
}

Compilation message

count_triplets.cpp: In function 'void solve()':
count_triplets.cpp:134:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
     scanf("%d %d", &n, &m);
     ~~~~~^~~~~~~~~~~~~~~~~
count_triplets.cpp:137:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
         scanf("%d %d", &e[i].u, &e[i].v);
         ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 8 ms 6644 KB Output is correct
2 Correct 8 ms 6644 KB Output is correct
3 Correct 8 ms 6688 KB Output is correct
4 Correct 7 ms 6740 KB Output is correct
5 Correct 7 ms 6796 KB Output is correct
6 Correct 10 ms 6796 KB Output is correct
7 Correct 7 ms 6800 KB Output is correct
8 Incorrect 7 ms 6800 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 6644 KB Output is correct
2 Correct 8 ms 6644 KB Output is correct
3 Correct 8 ms 6688 KB Output is correct
4 Correct 7 ms 6740 KB Output is correct
5 Correct 7 ms 6796 KB Output is correct
6 Correct 10 ms 6796 KB Output is correct
7 Correct 7 ms 6800 KB Output is correct
8 Incorrect 7 ms 6800 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 178 ms 19540 KB Output is correct
2 Correct 177 ms 19540 KB Output is correct
3 Execution timed out 1064 ms 19540 KB Time limit exceeded
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 42 ms 19540 KB Output is correct
2 Correct 41 ms 19540 KB Output is correct
3 Correct 54 ms 19540 KB Output is correct
4 Correct 53 ms 19540 KB Output is correct
5 Correct 51 ms 19540 KB Output is correct
6 Correct 69 ms 19540 KB Output is correct
7 Correct 48 ms 19540 KB Output is correct
8 Correct 72 ms 19540 KB Output is correct
9 Correct 52 ms 19540 KB Output is correct
10 Correct 41 ms 19540 KB Output is correct
11 Correct 31 ms 19540 KB Output is correct
12 Correct 23 ms 19540 KB Output is correct
13 Correct 14 ms 19540 KB Output is correct
14 Correct 9 ms 19540 KB Output is correct
15 Correct 10 ms 19540 KB Output is correct
16 Correct 8 ms 19540 KB Output is correct
17 Correct 33 ms 19540 KB Output is correct
18 Correct 32 ms 19540 KB Output is correct
19 Correct 34 ms 19540 KB Output is correct
20 Correct 33 ms 19540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1047 ms 19540 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 40 ms 19540 KB Output is correct
2 Correct 43 ms 19540 KB Output is correct
3 Correct 22 ms 19540 KB Output is correct
4 Correct 12 ms 19540 KB Output is correct
5 Correct 10 ms 19540 KB Output is correct
6 Correct 9 ms 19540 KB Output is correct
7 Correct 8 ms 19540 KB Output is correct
8 Correct 7 ms 19540 KB Output is correct
9 Correct 8 ms 19540 KB Output is correct
10 Correct 9 ms 19540 KB Output is correct
11 Correct 8 ms 19540 KB Output is correct
12 Correct 25 ms 19540 KB Output is correct
13 Correct 23 ms 19540 KB Output is correct
14 Correct 16 ms 19540 KB Output is correct
15 Correct 18 ms 19540 KB Output is correct
16 Correct 13 ms 19540 KB Output is correct
17 Correct 11 ms 19540 KB Output is correct
18 Correct 9 ms 19540 KB Output is correct
19 Correct 8 ms 19540 KB Output is correct
20 Correct 8 ms 19540 KB Output is correct
21 Correct 18 ms 19540 KB Output is correct
22 Correct 21 ms 19540 KB Output is correct
23 Correct 14 ms 19540 KB Output is correct
24 Correct 17 ms 19540 KB Output is correct
25 Correct 8 ms 19540 KB Output is correct
26 Correct 8 ms 19540 KB Output is correct
27 Correct 8 ms 19540 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1050 ms 19540 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 6644 KB Output is correct
2 Correct 8 ms 6644 KB Output is correct
3 Correct 8 ms 6688 KB Output is correct
4 Correct 7 ms 6740 KB Output is correct
5 Correct 7 ms 6796 KB Output is correct
6 Correct 10 ms 6796 KB Output is correct
7 Correct 7 ms 6800 KB Output is correct
8 Incorrect 7 ms 6800 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 6644 KB Output is correct
2 Correct 8 ms 6644 KB Output is correct
3 Correct 8 ms 6688 KB Output is correct
4 Correct 7 ms 6740 KB Output is correct
5 Correct 7 ms 6796 KB Output is correct
6 Correct 10 ms 6796 KB Output is correct
7 Correct 7 ms 6800 KB Output is correct
8 Incorrect 7 ms 6800 KB Output isn't correct
9 Halted 0 ms 0 KB -