Submission #380932

#TimeUsernameProblemLanguageResultExecution timeMemory
380932usachevd0Duathlon (APIO18_duathlon)C++14
31 / 100
155 ms49256 KiB
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define all(a) (a).begin(), (a).end()
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
using pil = pair<int, ll>;
using pli = pair<ll, int>;
using pll = pair<ll, ll>;
using ld = long double;
template<typename T1, typename T2> bool chkmin(T1 &x, T2 y) { return y < x ? (x = y, true) : false; }
template<typename T1, typename T2> bool chkmax(T1 &x, T2 y) { return y > x ? (x = y, true) : false; }
void debug_out() { cerr << endl; }
template<typename T1, typename... T2> void debug_out(T1 A, T2... B) { cerr << ' ' << A; debug_out(B...); }
template<typename T> void mdebug_out(T* a, int n) { for (int i = 0; i < n; ++i) cerr << a[i] << ' '; cerr << endl; }
#ifdef DEBUG
    #define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
    #define mdebug(a, n) cerr << #a << ": ", mdebug_out(a, n)
#else
    #define debug(...) 1337
    #define mdebug(a, n) 1337
#endif
template<typename T> ostream& operator << (ostream& stream, const vector<T> &v) { for (auto x : v) stream << x << ' '; return stream; }
template<typename T1, typename T2> ostream& operator << (ostream& stream, const pair<T1, T2>& p) { return stream << p.first << ' ' << p.second; }

const int N = 100005;
int n, m;
vector<int> G[N];
int tin[N], fup[N], timer;
int used[3 * N];
bool cut[N];

vector<int> block[2 * N];
int blocks;

vector<int> stk;
void dfs1(int v, int p = -1)
{
    used[v] = 1;
    tin[v] = fup[v] = timer++;
    stk.push_back(v);
    int cnt_down = 0;
    for (int& u : G[v])
    {
        if (u == p) continue;
        if (!used[u])
        {
            dfs1(u, v);
            chkmin(fup[v], fup[u]);
            if (fup[u] >= tin[v])
            {
                // v is a cut
                ++cnt_down;
                cut[v] = true;
                auto &b = block[blocks++];
                for (; stk.back() != v; stk.pop_back())
                    b.push_back(stk.back());
                b.push_back(v);
            }
        }
        else
        {
            chkmin(fup[v], tin[u]);
        }
    }
    if (p == -1 && cnt_down <= 1)
        cut[v] = false;
}

vector<int> T[3 * N]; // block-cut tree
ll C[3 * N];
ll cnt[3 * N];
ll sum[3 * N];
ll sum2[3 * N];
ll ans;

void add_edge(int u, int v)
{
//    debug(u, v);
    T[u].pb(v);
    T[v].pb(u);
}

void dfs2(int v, int p = -1)
{
    ans += C[v] * (C[v] - 1) * (C[v] - 2); // case 1

    used[v] = 1;
    
    cnt[v] = 1;
    sum[v] = C[v];
    sum2[v] = 0;
    for (int& u : T[v]) if (u != p)
    {
        dfs2(u, v);
        ans += sum2[u] * sum[v] * 2; // case 3
        ans += sum[u] * sum2[v] * 2; // case 3
        cnt[v] += cnt[u];
        sum[v] += sum[u];
        sum2[v] += sum2[u] + sum[u] * C[v];
    }
//    debug(v, cnt[v], sum[v], sum2[v]);
}

ll curSum;
void dfs3(int v, int p = -1)
{
    ans += C[v] * (C[v] - 1) * (curSum - C[v]) * 2; // case 2
    for (int& u : T[v]) if (u != p)
    {
        dfs3(u, v);
    }
    if (v > n) // v --- block
    {
        for (int& u : T[v])
        {
            if (u <= n) // u --- cut
            {
                ans += C[v] * (C[v] - 1); // case 4
            }
        }
    }
}

signed main()
{
#ifdef DEBUG
    freopen("in", "r", stdin);
#endif
    ios::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> m;
    for (int i = 0; i < m; ++i)
    {
        int a, b;
        cin >> a >> b;
        G[a].push_back(b);
        G[b].push_back(a);
    }
    for (int v = 1; v <= n; ++v) if (!used[v])
    {
        // new component
        dfs1(v);
    }
    for (int bi = 0; bi < blocks; ++bi)
    {
        auto& b = block[bi];
        vector<int> new_block;
        for (auto& v : b)
        {
            if (cut[v])
            {
                add_edge(v, bi + n + 1);
            }
            else
            {
                new_block.push_back(v);
            }
        }
        swap(b, new_block);
        C[bi + n + 1] = b.size();
    }
    for (int v = 1; v <= n; ++v)
        if (cut[v])
            C[v] = 1;
//    mdebug(C + 1, n + blocks);
    ans = 0;
    memset(used, 0, sizeof used);
    for (int bi = 0; bi < blocks; ++bi)
    {
        int v = bi + n + 1;
        if (!used[v])
        {
            dfs2(v);
            curSum = sum[v];
            dfs3(v);
        }
    }
    cout << ans << '\n';
    
    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...