답안 #989123

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
989123 2024-05-27T14:42:42 Z vjudge2 철인 이종 경기 (APIO18_duathlon) C++17
0 / 100
69 ms 41928 KB
#include <bits/stdc++.h>
using namespace std;

#define int long long
const int N = 200001;
int n, m, st[N], low[N], k, ans = 0, tot = 0, sz[N], dp[N], timer = 0, siz[N], f[N], sizes[N];
bool vis[N << 1], in[N];
vector<int> adj[N], g[N << 1], stk;

int find(int u) {
    return f[u] == u ? u : f[u] = find(f[u]);
}

void merge(int u, int v) {
    u = find(u), v = find(v);
    if (u == v) return;
    if (siz[u] < siz[v]) swap(u, v);
    siz[u] += siz[v];
    f[v] = u;
}

void dfs(int u) {
    st[u] = low[u] = ++timer;
    stk.push_back(u);
    for (int v : adj[u]) {
        if (!st[v]) {
            dfs(v);
            low[u] = min(low[u], low[v]);
            if (low[v] == st[u]) {
                ++k;
                for (int x = -1; x != v; stk.pop_back()) {
                    x = stk.back();
                    g[x].push_back(k);
                    g[k].push_back(x);
                    // cout << x << " " << k << '\n';
                }
                g[u].push_back(k);
                g[k].push_back(u);
                // cout << u << " " << k << '\n';
            }
        }
        low[u] = min(low[u], st[v]);
    }
}

void compute(int u, int p, int S) {
    vis[u] = 1;
    int sum = 0, sdp = 0;
    if (in[u]) sz[u] = 1;
    for (int v : g[u]) {
        if (v == p) continue;
        compute(v, u, S);
        sz[u] += sz[v];
        if (in[u]) ans += dp[v] * 4;
        dp[u] += dp[v];
        sdp += dp[v];
        sum += sz[v];
    }
    if (in[u]) {
        dp[u] += (sz[u] - 1);
    }
    ans += (sum * sum) * (S - sz[u] - 1);
    for (int v : g[u]) {
        if (v == p) continue;
        // ans += (sdp - dp[v]) * (sz[v]) * 2;
        ans -= sz[v] * sz[v] * (S - sz[u] - 1);
    }
    // cout << u << ' ' << ans << '\n';
}

int32_t main() {
    ios::sync_with_stdio(0); cin.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i++) f[i] = i, siz[i] = 1;
    k = n;
    for (int i = 1; i <= m; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
        in[u] = in[v] = 1;
        merge(u, v);
    }
    for (int i = 1; i <= n; i++) sizes[find(i)] = 1;
    int tot = 0;
    for (int i = 1; i <= n; i++) {
        if (!sizes[i]) continue;
        tot += siz[i] * (siz[i] - 1) * (siz[i] - 2);
    }
    for (int i = 1; i <= n; i++) if (!st[i]) dfs(i);
    for (int i = 1; i <= n; i++) if (!vis[i]) compute(i, -1, siz[find(i)]);
    cout << tot - ans << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 21080 KB Output is correct
2 Correct 3 ms 21116 KB Output is correct
3 Correct 3 ms 21084 KB Output is correct
4 Correct 3 ms 21080 KB Output is correct
5 Correct 3 ms 21084 KB Output is correct
6 Correct 3 ms 21084 KB Output is correct
7 Incorrect 3 ms 21084 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 21080 KB Output is correct
2 Correct 3 ms 21116 KB Output is correct
3 Correct 3 ms 21084 KB Output is correct
4 Correct 3 ms 21080 KB Output is correct
5 Correct 3 ms 21084 KB Output is correct
6 Correct 3 ms 21084 KB Output is correct
7 Incorrect 3 ms 21084 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 50 ms 41928 KB Output is correct
2 Correct 60 ms 41928 KB Output is correct
3 Incorrect 54 ms 39888 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 21084 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 69 ms 34216 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 21084 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 55 ms 34384 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 21080 KB Output is correct
2 Correct 3 ms 21116 KB Output is correct
3 Correct 3 ms 21084 KB Output is correct
4 Correct 3 ms 21080 KB Output is correct
5 Correct 3 ms 21084 KB Output is correct
6 Correct 3 ms 21084 KB Output is correct
7 Incorrect 3 ms 21084 KB Output isn't correct
8 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 21080 KB Output is correct
2 Correct 3 ms 21116 KB Output is correct
3 Correct 3 ms 21084 KB Output is correct
4 Correct 3 ms 21080 KB Output is correct
5 Correct 3 ms 21084 KB Output is correct
6 Correct 3 ms 21084 KB Output is correct
7 Incorrect 3 ms 21084 KB Output isn't correct
8 Halted 0 ms 0 KB -