Submission #229719

# Submission time Handle Problem Language Result Execution time Memory
229719 2020-05-06T03:34:21 Z lyc Duathlon (APIO18_duathlon) C++14
49 / 100
108 ms 37108 KB
#include <bits/stdc++.h>
using namespace std;

#define TRACE(x) cerr << #x << " :: " << x << endl
#define _ << " " <<
#define SZ(x) (int)(x).size()
#define ALL(x) (x).begin(),(x).end()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define RFOR(i,a,b) for (int i=(a);i>=(b);--i)

const int MX_N = 1e5+5;
int N, M;
vector<int> al[MX_N];

namespace BCT {
    int pre[MX_N], low[MX_N], cnt, root, rootChildren;

    vector<int> stk;
    int comp[MX_N], sz[MX_N];
    int nap, nv;

    vector<int> tree[MX_N];

    void dfs(int u, int p) {
        pre[u] = low[u] = ++cnt;
        for (int v : al[u]) if (v != p) {
            if (pre[v] == -1) {
                dfs(v,u);
                if (u == root) ++rootChildren;
                else if (comp[u] == -1 && low[v] >= pre[u]) comp[u] = ++nv, sz[nv] = 1;
                low[u] = min(low[u],low[v]);
            } else {
                low[u] = min(low[u],pre[v]);
            }
        }
    }

    void addEdge(int u, int v) {
        tree[u].push_back(v);
        tree[v].push_back(u);
    }

    void decomp(int u, int p) {
        pre[u] = ++cnt;
        stk.push_back(u);
        for (int v : al[u]) if (v != p) {
            if (pre[v] == -1) {
                int st = stk.back();
                decomp(v,u);

                if (comp[u] != -1 && low[v] >= pre[u]) {
                    sz[++nv] = 1;
                    addEdge(comp[u],nv);
                    while (stk.back() != st) {
                        int& w = stk.back(); stk.pop_back();
                        ++sz[nv];
                        if (comp[w] == -1) comp[w] = nv;
                        else addEdge(comp[w],nv);
                    }
                }
            }
        }
    }

    void run() {
        fill_n(comp,N+1,-1); nv = 0;

        fill_n(pre,N+1,-1); cnt = 0;
        FOR(u,1,N) if (pre[u] == -1) {
            root = u, rootChildren = 0;
            dfs(u,0);
            if (rootChildren > 1) comp[u] = ++nv, sz[nv] = 1;
        }
        nap = nv;

        fill_n(pre,N+1,-1); cnt = 0;
        FOR(u,1,N) if (pre[u] == -1) {
            stk.clear();
            decomp(u,0);
            if (comp[u] == -1) {
                sz[++nv] = 0;
                while (!stk.empty()) {
                    int& w = stk.back(); stk.pop_back();
                    ++sz[nv];
                    if (comp[w] == -1) comp[w] = nv;
                    else addEdge(comp[w],nv);
                }
            }
        }
    }

    void dbg() {
        FOR(i,1,N){ TRACE(i _ pre[i] _ low[i] _ comp[i]); }
        TRACE(nv);
        cout << "APs: "; FOR(i,1,N){ if (comp[i] <= nap) cout << i << ' '; } cout << endl;
        FOR(i,1,nv){ cout << i << " (" << sz[i] << "): "; for(int v : tree[i]) { cout << v << ' '; } cout << endl; }
    }

    long long ans;
    int sub[MX_N];

    void precomp(int u, int p) {
        sub[u] = sz[u];
        for (int v : tree[u]) if (v != p) {
            precomp(v,u);
            sub[u] += sub[v];
            --sub[u];
        }
        //TRACE(u _ sz[u] _ u _ sub[u]);
    }

    void trav(int u, int p, int all) {
        //TRACE("trav" _ u _ p _ all);
        if (u <= nap) {
            int other = all - 1;
            for (int v : tree[u]) {
                int cur;
                if (v == p) {
                    cur = all-sub[u];
                } else {
                    cur = sub[v]-1;
                    trav(v,u,all);
                }
                //long long two = 1LL * cur * (other-cur);
                //long long one = 1LL * (sz[v]-1) * (sz[v]-2);
                //long long snk = 2LL * (cur+1-sz[v]) * (sz[v]-2);
                //TRACE(u _ v _ "add" _ cur _ (other-cur) _ "res" _ two _ one _ snk);
                ans += 1LL * cur * (other-cur);     // ep in diff
                ans += 1LL * (sz[v]-1) * (sz[v]-2); // ep in same
                ans += 2LL * (cur+1-sz[v]) * (sz[v]-2); // ep in same subtree
            }
        } else {
            int inside = (sz[u] - SZ(tree[u]));
            int other = all - inside;
            for (int v : tree[u]) {
                other -= sz[v];
            }
            for (int v : tree[u]) {
                int cur;
                if (v == p) {
                    cur = all-sub[u]-(sz[p]-1);
                } else {
                    cur = sub[v]-sz[v];
                    trav(v,u,all);
                }
                //long long two = inside * cur * (other-cur);
                //long long one = 2LL * inside * cur * (sz[u]-2);
                //long long snk = 1LL * (SZ(tree[u])-2) * cur * (other-cur);
                //TRACE(u _ v _ "add" _ cur _ (other-cur) _ "res" _ two _ one _ snk);
                ans += 1LL * inside * cur * (other-cur);// both ep outside
                ans += 2LL * inside * cur * (sz[u]-2);  // one ep outside
                ans += 1LL * (SZ(tree[u])-2) * cur * (other-cur); // both ep outside, mid is ap
            }
            //long long zero = 1LL * inside * (sz[u]-1) * (sz[u]-2);
            //TRACE(u _ zero);
            if (sz[u] >= 3) ans += 1LL * inside * (sz[u]-1) * (sz[u]-2);
        }
    }

    long long solve() {
        ans = 0;
        fill_n(sub,N+1,-1);
        FOR(i,1,nv) if (sub[i] == -1) {
            precomp(i,0);
            trav(i,0,sub[i]);
        }
        return ans;
    }
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    cin >> N >> M;
    FOR(i,1,M){
        int U, V; cin >> U >> V;
        al[U].push_back(V);
        al[V].push_back(U);
    }

    BCT::run();
    //BCT::dbg();
    cout << BCT::solve() << '\n';
}

# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 7 ms 4992 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 7 ms 5120 KB Output is correct
5 Correct 7 ms 5120 KB Output is correct
6 Correct 7 ms 4992 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 7 ms 4992 KB Output is correct
9 Correct 7 ms 5120 KB Output is correct
10 Correct 7 ms 4992 KB Output is correct
11 Correct 7 ms 4992 KB Output is correct
12 Correct 7 ms 4992 KB Output is correct
13 Correct 7 ms 5120 KB Output is correct
14 Correct 7 ms 4992 KB Output is correct
15 Correct 7 ms 4992 KB Output is correct
16 Correct 8 ms 4992 KB Output is correct
17 Correct 8 ms 4992 KB Output is correct
18 Correct 7 ms 4992 KB Output is correct
19 Correct 7 ms 4992 KB Output is correct
20 Correct 7 ms 4992 KB Output is correct
21 Correct 8 ms 4992 KB Output is correct
22 Correct 7 ms 4992 KB Output is correct
23 Correct 7 ms 5120 KB Output is correct
24 Correct 7 ms 5120 KB Output is correct
25 Correct 7 ms 4992 KB Output is correct
26 Correct 7 ms 5120 KB Output is correct
27 Correct 7 ms 4992 KB Output is correct
28 Correct 7 ms 4992 KB Output is correct
29 Correct 7 ms 4992 KB Output is correct
30 Correct 8 ms 4992 KB Output is correct
31 Correct 7 ms 4992 KB Output is correct
32 Correct 7 ms 5120 KB Output is correct
33 Correct 7 ms 4992 KB Output is correct
34 Correct 7 ms 4992 KB Output is correct
35 Correct 7 ms 4992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 7 ms 4992 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 7 ms 5120 KB Output is correct
5 Correct 7 ms 5120 KB Output is correct
6 Correct 7 ms 4992 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 7 ms 4992 KB Output is correct
9 Correct 7 ms 5120 KB Output is correct
10 Correct 7 ms 4992 KB Output is correct
11 Correct 7 ms 4992 KB Output is correct
12 Correct 7 ms 4992 KB Output is correct
13 Correct 7 ms 5120 KB Output is correct
14 Correct 7 ms 4992 KB Output is correct
15 Correct 7 ms 4992 KB Output is correct
16 Correct 8 ms 4992 KB Output is correct
17 Correct 8 ms 4992 KB Output is correct
18 Correct 7 ms 4992 KB Output is correct
19 Correct 7 ms 4992 KB Output is correct
20 Correct 7 ms 4992 KB Output is correct
21 Correct 8 ms 4992 KB Output is correct
22 Correct 7 ms 4992 KB Output is correct
23 Correct 7 ms 5120 KB Output is correct
24 Correct 7 ms 5120 KB Output is correct
25 Correct 7 ms 4992 KB Output is correct
26 Correct 7 ms 5120 KB Output is correct
27 Correct 7 ms 4992 KB Output is correct
28 Correct 7 ms 4992 KB Output is correct
29 Correct 7 ms 4992 KB Output is correct
30 Correct 8 ms 4992 KB Output is correct
31 Correct 7 ms 4992 KB Output is correct
32 Correct 7 ms 5120 KB Output is correct
33 Correct 7 ms 4992 KB Output is correct
34 Correct 7 ms 4992 KB Output is correct
35 Correct 7 ms 4992 KB Output is correct
36 Correct 7 ms 4992 KB Output is correct
37 Correct 7 ms 4992 KB Output is correct
38 Correct 8 ms 5120 KB Output is correct
39 Correct 9 ms 4992 KB Output is correct
40 Correct 7 ms 5120 KB Output is correct
41 Correct 7 ms 4992 KB Output is correct
42 Correct 7 ms 4992 KB Output is correct
43 Correct 7 ms 4992 KB Output is correct
44 Correct 7 ms 4992 KB Output is correct
45 Correct 7 ms 4992 KB Output is correct
46 Correct 7 ms 4992 KB Output is correct
47 Correct 7 ms 5120 KB Output is correct
48 Correct 7 ms 4992 KB Output is correct
49 Correct 7 ms 5120 KB Output is correct
50 Correct 7 ms 4992 KB Output is correct
51 Correct 7 ms 4992 KB Output is correct
52 Correct 7 ms 4992 KB Output is correct
53 Correct 7 ms 5120 KB Output is correct
54 Correct 7 ms 5120 KB Output is correct
55 Correct 7 ms 4992 KB Output is correct
56 Correct 7 ms 4992 KB Output is correct
57 Correct 8 ms 5120 KB Output is correct
58 Correct 7 ms 4992 KB Output is correct
59 Correct 7 ms 4992 KB Output is correct
60 Correct 7 ms 4992 KB Output is correct
61 Correct 7 ms 5120 KB Output is correct
62 Correct 8 ms 4992 KB Output is correct
63 Correct 7 ms 4992 KB Output is correct
64 Correct 7 ms 4992 KB Output is correct
65 Correct 7 ms 4992 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 91 ms 20976 KB Output is correct
2 Correct 87 ms 20976 KB Output is correct
3 Runtime error 108 ms 37108 KB Execution killed with signal 11 (could be triggered by violating memory limits)
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5120 KB Output is correct
2 Correct 8 ms 5248 KB Output is correct
3 Correct 7 ms 5248 KB Output is correct
4 Correct 8 ms 5376 KB Output is correct
5 Correct 8 ms 5248 KB Output is correct
6 Correct 8 ms 5248 KB Output is correct
7 Correct 8 ms 5376 KB Output is correct
8 Correct 8 ms 5248 KB Output is correct
9 Correct 9 ms 5248 KB Output is correct
10 Correct 8 ms 5120 KB Output is correct
11 Correct 8 ms 5120 KB Output is correct
12 Correct 8 ms 5120 KB Output is correct
13 Correct 8 ms 5120 KB Output is correct
14 Correct 8 ms 5120 KB Output is correct
15 Correct 8 ms 5120 KB Output is correct
16 Correct 8 ms 5120 KB Output is correct
17 Correct 7 ms 5120 KB Output is correct
18 Correct 7 ms 5120 KB Output is correct
19 Correct 7 ms 5120 KB Output is correct
20 Correct 8 ms 5168 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 81 ms 25856 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 8 ms 5120 KB Output is correct
2 Correct 8 ms 5120 KB Output is correct
3 Correct 8 ms 5120 KB Output is correct
4 Correct 8 ms 5120 KB Output is correct
5 Correct 8 ms 5120 KB Output is correct
6 Correct 8 ms 5120 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 8 ms 5120 KB Output is correct
9 Correct 8 ms 5120 KB Output is correct
10 Correct 8 ms 5120 KB Output is correct
11 Correct 8 ms 5120 KB Output is correct
12 Correct 8 ms 5248 KB Output is correct
13 Correct 8 ms 5248 KB Output is correct
14 Correct 8 ms 5248 KB Output is correct
15 Correct 8 ms 5248 KB Output is correct
16 Correct 8 ms 5120 KB Output is correct
17 Correct 8 ms 5120 KB Output is correct
18 Correct 8 ms 5120 KB Output is correct
19 Correct 8 ms 5120 KB Output is correct
20 Correct 7 ms 5120 KB Output is correct
21 Correct 8 ms 5248 KB Output is correct
22 Correct 8 ms 5120 KB Output is correct
23 Correct 8 ms 5120 KB Output is correct
24 Correct 8 ms 5120 KB Output is correct
25 Correct 7 ms 5120 KB Output is correct
26 Correct 7 ms 5120 KB Output is correct
27 Correct 8 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Runtime error 93 ms 25976 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 7 ms 4992 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 7 ms 5120 KB Output is correct
5 Correct 7 ms 5120 KB Output is correct
6 Correct 7 ms 4992 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 7 ms 4992 KB Output is correct
9 Correct 7 ms 5120 KB Output is correct
10 Correct 7 ms 4992 KB Output is correct
11 Correct 7 ms 4992 KB Output is correct
12 Correct 7 ms 4992 KB Output is correct
13 Correct 7 ms 5120 KB Output is correct
14 Correct 7 ms 4992 KB Output is correct
15 Correct 7 ms 4992 KB Output is correct
16 Correct 8 ms 4992 KB Output is correct
17 Correct 8 ms 4992 KB Output is correct
18 Correct 7 ms 4992 KB Output is correct
19 Correct 7 ms 4992 KB Output is correct
20 Correct 7 ms 4992 KB Output is correct
21 Correct 8 ms 4992 KB Output is correct
22 Correct 7 ms 4992 KB Output is correct
23 Correct 7 ms 5120 KB Output is correct
24 Correct 7 ms 5120 KB Output is correct
25 Correct 7 ms 4992 KB Output is correct
26 Correct 7 ms 5120 KB Output is correct
27 Correct 7 ms 4992 KB Output is correct
28 Correct 7 ms 4992 KB Output is correct
29 Correct 7 ms 4992 KB Output is correct
30 Correct 8 ms 4992 KB Output is correct
31 Correct 7 ms 4992 KB Output is correct
32 Correct 7 ms 5120 KB Output is correct
33 Correct 7 ms 4992 KB Output is correct
34 Correct 7 ms 4992 KB Output is correct
35 Correct 7 ms 4992 KB Output is correct
36 Correct 7 ms 4992 KB Output is correct
37 Correct 7 ms 4992 KB Output is correct
38 Correct 8 ms 5120 KB Output is correct
39 Correct 9 ms 4992 KB Output is correct
40 Correct 7 ms 5120 KB Output is correct
41 Correct 7 ms 4992 KB Output is correct
42 Correct 7 ms 4992 KB Output is correct
43 Correct 7 ms 4992 KB Output is correct
44 Correct 7 ms 4992 KB Output is correct
45 Correct 7 ms 4992 KB Output is correct
46 Correct 7 ms 4992 KB Output is correct
47 Correct 7 ms 5120 KB Output is correct
48 Correct 7 ms 4992 KB Output is correct
49 Correct 7 ms 5120 KB Output is correct
50 Correct 7 ms 4992 KB Output is correct
51 Correct 7 ms 4992 KB Output is correct
52 Correct 7 ms 4992 KB Output is correct
53 Correct 7 ms 5120 KB Output is correct
54 Correct 7 ms 5120 KB Output is correct
55 Correct 7 ms 4992 KB Output is correct
56 Correct 7 ms 4992 KB Output is correct
57 Correct 8 ms 5120 KB Output is correct
58 Correct 7 ms 4992 KB Output is correct
59 Correct 7 ms 4992 KB Output is correct
60 Correct 7 ms 4992 KB Output is correct
61 Correct 7 ms 5120 KB Output is correct
62 Correct 8 ms 4992 KB Output is correct
63 Correct 7 ms 4992 KB Output is correct
64 Correct 7 ms 4992 KB Output is correct
65 Correct 7 ms 4992 KB Output is correct
66 Correct 8 ms 5120 KB Output is correct
67 Correct 7 ms 5120 KB Output is correct
68 Correct 8 ms 5120 KB Output is correct
69 Correct 9 ms 5120 KB Output is correct
70 Correct 8 ms 5248 KB Output is correct
71 Correct 8 ms 5120 KB Output is correct
72 Correct 8 ms 5120 KB Output is correct
73 Correct 7 ms 5120 KB Output is correct
74 Correct 8 ms 5120 KB Output is correct
75 Correct 8 ms 5120 KB Output is correct
76 Correct 7 ms 4992 KB Output is correct
77 Correct 8 ms 5120 KB Output is correct
78 Correct 8 ms 5120 KB Output is correct
79 Correct 7 ms 5120 KB Output is correct
80 Correct 8 ms 5120 KB Output is correct
81 Correct 8 ms 5120 KB Output is correct
82 Correct 8 ms 5248 KB Output is correct
83 Correct 8 ms 5248 KB Output is correct
84 Correct 8 ms 5120 KB Output is correct
85 Correct 7 ms 5120 KB Output is correct
86 Correct 8 ms 5120 KB Output is correct
87 Correct 9 ms 5120 KB Output is correct
88 Correct 8 ms 5120 KB Output is correct
89 Correct 8 ms 5120 KB Output is correct
90 Correct 8 ms 5120 KB Output is correct
91 Correct 8 ms 5120 KB Output is correct
92 Correct 7 ms 5120 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 7 ms 4992 KB Output is correct
2 Correct 7 ms 4992 KB Output is correct
3 Correct 7 ms 5120 KB Output is correct
4 Correct 7 ms 5120 KB Output is correct
5 Correct 7 ms 5120 KB Output is correct
6 Correct 7 ms 4992 KB Output is correct
7 Correct 7 ms 5120 KB Output is correct
8 Correct 7 ms 4992 KB Output is correct
9 Correct 7 ms 5120 KB Output is correct
10 Correct 7 ms 4992 KB Output is correct
11 Correct 7 ms 4992 KB Output is correct
12 Correct 7 ms 4992 KB Output is correct
13 Correct 7 ms 5120 KB Output is correct
14 Correct 7 ms 4992 KB Output is correct
15 Correct 7 ms 4992 KB Output is correct
16 Correct 8 ms 4992 KB Output is correct
17 Correct 8 ms 4992 KB Output is correct
18 Correct 7 ms 4992 KB Output is correct
19 Correct 7 ms 4992 KB Output is correct
20 Correct 7 ms 4992 KB Output is correct
21 Correct 8 ms 4992 KB Output is correct
22 Correct 7 ms 4992 KB Output is correct
23 Correct 7 ms 5120 KB Output is correct
24 Correct 7 ms 5120 KB Output is correct
25 Correct 7 ms 4992 KB Output is correct
26 Correct 7 ms 5120 KB Output is correct
27 Correct 7 ms 4992 KB Output is correct
28 Correct 7 ms 4992 KB Output is correct
29 Correct 7 ms 4992 KB Output is correct
30 Correct 8 ms 4992 KB Output is correct
31 Correct 7 ms 4992 KB Output is correct
32 Correct 7 ms 5120 KB Output is correct
33 Correct 7 ms 4992 KB Output is correct
34 Correct 7 ms 4992 KB Output is correct
35 Correct 7 ms 4992 KB Output is correct
36 Correct 7 ms 4992 KB Output is correct
37 Correct 7 ms 4992 KB Output is correct
38 Correct 8 ms 5120 KB Output is correct
39 Correct 9 ms 4992 KB Output is correct
40 Correct 7 ms 5120 KB Output is correct
41 Correct 7 ms 4992 KB Output is correct
42 Correct 7 ms 4992 KB Output is correct
43 Correct 7 ms 4992 KB Output is correct
44 Correct 7 ms 4992 KB Output is correct
45 Correct 7 ms 4992 KB Output is correct
46 Correct 7 ms 4992 KB Output is correct
47 Correct 7 ms 5120 KB Output is correct
48 Correct 7 ms 4992 KB Output is correct
49 Correct 7 ms 5120 KB Output is correct
50 Correct 7 ms 4992 KB Output is correct
51 Correct 7 ms 4992 KB Output is correct
52 Correct 7 ms 4992 KB Output is correct
53 Correct 7 ms 5120 KB Output is correct
54 Correct 7 ms 5120 KB Output is correct
55 Correct 7 ms 4992 KB Output is correct
56 Correct 7 ms 4992 KB Output is correct
57 Correct 8 ms 5120 KB Output is correct
58 Correct 7 ms 4992 KB Output is correct
59 Correct 7 ms 4992 KB Output is correct
60 Correct 7 ms 4992 KB Output is correct
61 Correct 7 ms 5120 KB Output is correct
62 Correct 8 ms 4992 KB Output is correct
63 Correct 7 ms 4992 KB Output is correct
64 Correct 7 ms 4992 KB Output is correct
65 Correct 7 ms 4992 KB Output is correct
66 Correct 91 ms 20976 KB Output is correct
67 Correct 87 ms 20976 KB Output is correct
68 Runtime error 108 ms 37108 KB Execution killed with signal 11 (could be triggered by violating memory limits)
69 Halted 0 ms 0 KB -