Submission #265442

# Submission time Handle Problem Language Result Execution time Memory
265442 2020-08-14T20:45:29 Z DS007 Duathlon (APIO18_duathlon) C++14
0 / 100
1000 ms 42360 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

const int N = 1e5, M = 2e5;
vector<int> adj[N], tree[N];
bool explored[N], up[N], down[N];
int n, m, dp[N][3], ans, cid[N], cnt[N], dep[N], sz[N], comp[N], compno;
set<pair<int, int>> br;

void dfs2(int v, int p = -1) {
    if (sz[v] == 0)
        return;

    explored[v] = true;
    int s1 = 0, s2 = 0;
    dp[v][0] = sz[v];
    dp[v][1] = sz[v] * (sz[v] - 1) - (sz[v] - 1);
    dp[v][2] = sz[v] * (sz[v] - 1) * (sz[v] - 2);

    for (int i : tree[v]) {
        if (i != p) {
            dfs2(i, v);
            dp[v][0] += dp[i][0];
            dp[v][1] += dp[i][0] * sz[v] + dp[i][1];
            dp[v][2] += dp[i][1] * sz[v] * 2 + (sz[v] * (sz[v] - 1) - (sz[v] - 1)) * dp[i][1] * 2;
            s2 += dp[i][1];
            s1 += dp[i][0];
        }
    }

    for (int i : tree[v]) {
        if (i != p) {
            s2 -= dp[i][1];
            s1 -= dp[i][0];
            dp[v][2] += s2 * dp[i][0] * 2;
            dp[v][2] += s1 * dp[i][0];
            s2 += dp[i][1];
            s1 += dp[i][0];
        }
    }

    ans += dp[v][2];
}

void dfs1(int v) {
    int curcmp = compno;
    comp[v] = curcmp;

    queue<int> q;
    q.push(v);
    explored[v] = true;

    while (!q.empty()) {
        int u = q.front();
        sz[curcmp]++;
        q.pop();

        for (int i : adj[u]) {
            if (explored[i])
                continue;

            if (br.count({u, i}) + br.count({i, u})) {
                compno++;
                tree[curcmp].push_back(compno);
                tree[compno].push_back(curcmp);
                dfs1(i);
            } else {
                q.push(i);
                explored[i] = true;
            }
        }
    }
}

void dfs(int v, int p = -1, int d = 0) {
    explored[v] = true;
    dep[v] = d;

    for (int i : adj[v]) {
        if (!explored[i])
            dfs(i, v, d + 1), cnt[v] += cnt[i];
        else if (i != p && dep[i] < dep[v])
            cnt[v]++;
        else if (i != p)
            cnt[v]--;
    }

    if (cnt[v] == 0 && p != -1)
        br.insert({p, v}), br.insert({v, p});
}

int solveTestCase() {
    cin >> n >> m;
    for (int i = 0; i < m; i++) {
        int u, v;
        cin >> u >> v;
        u--, v--;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }

    for (int i = 0; i < n; i++) {
        if (!explored[i])
            dfs(i);
        cerr << cnt[i] << " ";
    }
    cerr << "\n";
    assert(br.size() == m * 2);

    for (auto i : br)
        cerr << i.first << " " << i.second << "\n";

    fill(explored, explored + n, false);
    for (int i = 0; i < n; i++) {
        if (!explored[i])
            dfs1(i);
    }
    
    for (int i = 0; i < n; i++)
        assert(sz[i] == 1);

    fill(explored, explored + n, false);
    for (int i = 0; i < n; i++) {
        if (!explored[i])
            dfs2(i);
        cerr << dp[i][0] << " " << dp[i][1] << " " << dp[i][2] << endl;
    }

    cout << ans;
    return 0;
}

signed main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    int t = 1;
    //cin >> t;
    while (t--)
        solveTestCase();
}


Compilation message

In file included from /usr/include/c++/9/cassert:44,
                 from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:33,
                 from count_triplets.cpp:1:
count_triplets.cpp: In function 'long long int solveTestCase()':
count_triplets.cpp:109:22: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
  109 |     assert(br.size() == m * 2);
      |            ~~~~~~~~~~^~~~~~~~
# Verdict Execution time Memory Grader output
1 Runtime error 10 ms 10112 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 10 ms 10112 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 525 ms 42360 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 41 ms 5376 KB Output is correct
2 Correct 40 ms 5304 KB Output is correct
3 Correct 41 ms 5368 KB Output is correct
4 Correct 52 ms 6008 KB Output is correct
5 Correct 41 ms 5752 KB Output is correct
6 Correct 42 ms 5692 KB Output is correct
7 Correct 39 ms 5760 KB Output is correct
8 Correct 53 ms 5624 KB Output is correct
9 Correct 40 ms 5504 KB Output is correct
10 Runtime error 36 ms 10616 KB Execution killed with signal 11
11 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1059 ms 23736 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 41 ms 5308 KB Output is correct
2 Correct 44 ms 5308 KB Output is correct
3 Runtime error 18 ms 10368 KB Execution killed with signal 11
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Execution timed out 1087 ms 23884 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 10 ms 10112 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 10 ms 10112 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -