제출 #1213349

#제출 시각아이디문제언어결과실행 시간메모리
1213349spike1236철인 이종 경기 (APIO18_duathlon)C++20
0 / 100
49 ms30912 KiB
#include <bits/stdc++.h>
using namespace std;
using int64 = long long;

const int MAXN = 100'000;
const int MAXM = 200'000;

int n, m;

vector<int> g[MAXN];                
vector<int> T[MAXN + MAXM];         
int timer = 0, bcc_cnt = 0;
int tin[MAXN], low[MAXN];
int st[MAXN], top = 0;              

void dfs1(int u, int p) {
    tin[u] = low[u] = ++timer;
    st[top++] = u;

    for (int v : g[u]) {
        if (!tin[v]) {                          
            dfs1(v, u);
            low[u] = min(low[u], low[v]);

            if (low[v] >= tin[u]) {             
                int block = n + bcc_cnt++;      
                T[u].push_back(block);
                T[block].push_back(u);
                while (true) {
                    int x = st[--top];
                    T[x].push_back(block);
                    T[block].push_back(x);
                    if (x == v) break;
                }
            }
        }
        else if (v != p)                        
            low[u] = min(low[u], tin[v]);
    }
}

int64 comp_sz;                     
int64 answer = 0;
int64 sub_sz[MAXN + MAXM];         

void dfs2(int u, int parent, bool isRootBlock) {
    sub_sz[u] = (u < n);           

    for (int v : T[u]) if (v != parent) {
        dfs2(v, u, false);
        sub_sz[u] += sub_sz[v];

        if (u >= n) {              
            int deg = T[u].size();
            answer -= int64(deg - isRootBlock) * sub_sz[v] * (sub_sz[v] - 1);
        }
    }

    if (u >= n) {                  
        int deg = T[u].size();
        int64 rem = comp_sz - sub_sz[u];
        answer -= int64(deg) * rem * (rem - 1);
    }
}

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

    cin >> n >> m;
    while (m--) {
        int u, v;  cin >> u >> v;  --u; --v;
        g[u].push_back(v);
        g[v].push_back(u);
    }

    for (int i = 0; i < n; ++i) if (!tin[i]) {

        int old_bcc = bcc_cnt;
        timer = 0; top = 0;
        dfs1(i, -1);
        comp_sz = timer;                       

        answer += comp_sz * (comp_sz - 1) * (comp_sz - 2);

        dfs2(i, -1, true);

    }

    cout << answer << '\n';
}
#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...