Submission #1043277

#TimeUsernameProblemLanguageResultExecution timeMemory
1043277vjudge1Duathlon (APIO18_duathlon)C++17
24 / 100
52 ms12244 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

const int N = 1e5 + 10;
int n, m;
vector<int> g[N], comp;
bool vis[N], possible[105][105][105];

void dfs_brute(int v, int y, int z){
    if (v == z) return;
    vis[v] = 1;
    for (int u : g[v])
        if (!vis[u]) dfs_brute(u, y, z);
}

void dfs(int v){
    vis[v] = 1;
    comp.push_back(v);
    for (int u : g[v]){
        if (vis[u]) continue;
        dfs(u);
    }
}

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

    int total = n * (n - 1) * (n - 2);

    if (n <= 100 and m <= 100){
        for (int u = 1; u <= n; u ++){
            for (int v = 1; v <= n; v ++){
                for (int e = 1; e <= n; e ++){
                    for (int x = 1; x <= n; x ++)
                        vis[x] = 0;
                    dfs_brute(u, v, e);
                    if (vis[v])
                        possible[u][v][e] = 1;
                }
            }
        }

        for (int c = 1; c <= n; c ++){
            for (int s = 1; s <= n; s ++){
                for (int f = 1; f <= n; f ++){
                    if (c == s or c == f or s == f) continue;
                    bool bad = 0;
                    for (int v = 1; v <= n; v ++){
                        if (v == c) continue;
                        if (!possible[s][c][v] and !possible[c][f][v])
                            bad = 1;
                    }
                    total -= bad;
                }
            }
        }

        cout << total << endl;
        return 0;
    }

    ll ans = 0;
    for (int v = 1; v <= n; v ++){
        if (vis[v]) continue;
        comp.clear();
        dfs(v);
        int edges = 0;
        for (int x : comp)
            edges += g[x].size();
        edges /= 2;

        ll nodes = comp.size();
        if (nodes == edges){
            ans += nodes * (nodes - 1) * (nodes - 2);
            continue;
        }

        ans += (nodes * (nodes - 1) * (nodes - 2)) / 3;
    }

    cout << ans << endl;
}
#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...