Submission #1369377

#TimeUsernameProblemLanguageResultExecution timeMemory
1369377adscoding철인 이종 경기 (APIO18_duathlon)C++20
23 / 100
1094 ms14608 KiB
#include <bits/stdc++.h>
#define pb emplace_back
#define SZ(x) ((int)x.size())
#define fi first
#define se second
#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 FORLL(i, a, b) for (ll i = a, _b = b; i <= _b; ++i)
#define FORDLL(i, a, b) for (ll i = a, _b = b; i >= _b; --i)
#define all(x) x.begin(), x.end()
#define uni(x) sort(all(x)), x.erase(unique(all(x)), x.end())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

#define dbg(...) debug(#__VA_ARGS__, __VA_ARGS__)

template<typename T>
void __prine_one(const char *&s, const T &x)
{
    while (*s == ' ') ++s;
    const char *p = s;
    int bal = 0;
    while (*s)
    {
        if (*s == '(') ++bal;
        else if (*s == ')') --bal;
        else if (*s == ',' && bal == 0) break;
        ++s;
    }
    cerr.write(p, s - p) << " = " << x;
    if (*s == ',')
    {
        cerr << "  ,  ";
        ++s;
    }
}

template<typename... Args>
void debug(const char *s, Args... args)
{
    cerr << "[  ";
    int dummy[] = {0, (__prine_one(s, args), 0)...};
    (void)dummy;
    cerr << "  ]\n\n";
}

template<class X>
bool maximize(X &a, const X &b)
{
    if (b > a)
    {
        a = b;
        return true;
    }
    return false;
}

template<class X>
bool minimize(X &a, const X &b)
{
    if (b < a)
    {
        a = b;
        return true;
    }
    return false;
}

// --------------------------------------------------------------------------------------------

constexpr int maxn = 1e5 + 3;
int n, m, sz[maxn];
vector<int> g[maxn];

// --------------------------------------------------------------------------------------------


void readInput()
{
    cin >> n >> m;
    FOR(i, 1, m)
    {
        int u, v; cin >> u >> v;
        if (u > v) swap(u, v);
        g[u].push_back(v);
        g[v].push_back(u);
    }
}

namespace subTree
{
    bool approve()
    {
        if (m != n - 1) return false;
        FOR(i, 1, n)
            if (SZ(g[i]) > 2) return false;
        return true;
    }


    bool vis[maxn];
    ll res = 0;

    vector<int> vec;

    void dfs_sz(int u, int p)
    {
        vis[u] = true;
        sz[u] = 1;
        vec.push_back(u);
        for (int v : g[u])
        {
            if (vis[v]) continue;
            dfs_sz(v, u);
            res += 1ll * (sz[u] - 1) * sz[v];
            sz[u] += sz[v];
        }
    }

    void solve()
    {
        FOR(i, 1, n)
        {
            if (vis[i]) continue;
            dfs_sz(i, -1);
            for (int u : vec)
            {
                res += 1ll * (sz[u] - 1) * (SZ(vec) - sz[u]); 
            }
            vec.clear();
        }
        res <<= 1;
        cout << res;
    }
}

namespace AC
{
    int num[maxn], low[maxn], tdfs, in[maxn];
    bool join[maxn];
    stack<int> st;
    int bcc;
    vector<int> BCT[maxn << 1];
    int root;
    bool vis[maxn << 1];
    ll res;
    vector<int> vec;

    void tarjan(int u, int p)
    {
        num[u] = low[u] = ++tdfs;
        st.push(u);
        int children = 0;
        for (int v : g[u])
        {
            if (!num[v])
            {
                tarjan(v, u);
                low[u] = min(low[u], low[v]);
                ++children;

                if (low[v] >= num[u])
                {
                    join[u] = true;
                    ++bcc;
                    while (st.size() && st.top() != u)
                    {
                        int v = st.top(); st.pop();
                        BCT[v].push_back(bcc);
                        BCT[bcc].push_back(v);
                    }
                    BCT[u].push_back(bcc);
                    BCT[bcc].push_back(u);
                }
            }
            else low[u] = min(low[u], num[v]);
        }
        if (u == root && children <= 1)
            join[u] = false;
    }

    ll sz_bct[maxn << 1], sz[maxn << 1];

    int cnt_bcc = 0;
    ll F(ll x) {return x * (x - 1) / 2;}

    void dfs_sz(int u, int p)
    {
        vis[u] = true;
        if (join[u] || u > n) vec.push_back(u);
        if (u <= n) ++cnt_bcc;
        sz[u] = sz_bct[u];
        for (int v : BCT[u])
        {
            if (vis[v]) continue;
            dfs_sz(v, u);
            // 2 ngoai 1 trong voi nhung nhanh duoi
            res += sz_bct[u] * (sz[u] - sz_bct[u]) * (sz[v]);
            sz[u] += sz[v];
        }
    }

    void solve()
    {
        bcc = n;
        FOR(i, 1, n)
        {
            if (num[i]) continue;
            root = i;
            tarjan(i, -1);
        }

        FOR(u, 1, n)
        {
            if (join[u])
            {
                sz_bct[u] = 1;
            }
        }

        FOR(u, n + 1, bcc)
        {
            for (int v : BCT[u])
            {
                sz_bct[u] += !join[v];
            }

            res += sz_bct[u] * (sz_bct[u] - 1) * (sz_bct[u] - 2) / 2;
            // dbg(u, sz_bct[u]);
        }

        FORD(u, bcc, 1)
        {
            if (vis[u] == false)
            {
                dfs_sz(u, -1);
                
                for (int x : vec)
                {
                    // 2 trong 1 ngoai
                    res += F(sz_bct[x]) * 2 * (cnt_bcc - sz_bct[x]);
                    // 2 ngoai 1 trong chung bicon (chi cho khop)
                    if (join[x])
                    {
                        for (int xj : BCT[x])
                            res += F(sz_bct[xj]);
                    }

                    // 2 ngoai 1 trong 1 nhanh duoi 1 nhanh len
                    res += sz_bct[x] * (sz[x] - sz_bct[x]) * (cnt_bcc - sz[x]);
                    // if (x == 1)
                    //     dbg(sz_bct[x], sz[x], cnt_bcc);
                }

                vec.clear();
                cnt_bcc = 0;
            } 
        }

        cout << res * 2;
    }
}

namespace Brute
{
    bool mark[105][105][105];
    bool used[105];
    vector<int> cur_path;

    ll res;

    void Try(int u)
    {
        if (SZ(cur_path) > 2)
        {
            FOR(i, 1, SZ(cur_path) - 2)
            {
                mark[cur_path[0]][cur_path.back()][cur_path[i]] = true;
            }
        }
        for (int v : g[u])
        {
            if (used[v]) continue;
            cur_path.push_back(v);
            used[v] = true;
            Try(v);
            used[v] = false;
            cur_path.pop_back();
        }
    }

    void solve()
    {
        FOR(u, 1, n)
            Try(u);
        FOR(u, 1, n)
            FOR(v, 1, n)
                FOR(k, 1, n)
                    res += mark[u][v][k];

        cout << res;
    }
}

void solve()
{
    if (n <= 10) Brute :: solve();
    else subTree :: solve();
}

signed main()
{
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
    #define TASK "TEST"
    if (fopen(TASK".INP", "r"))
    {
        freopen(TASK".INP", "r", stdin);
        freopen(TASK".ans", "w", stdout);
    }
    readInput();
    solve();
    return 0;
}

Compilation message (stderr)

count_triplets.cpp: In function 'int main()':
count_triplets.cpp:320:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  320 |         freopen(TASK".INP", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
count_triplets.cpp:321:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  321 |         freopen(TASK".ans", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...
#Result Execution timeMemoryGrader output
Fetching results...