Submission #981572

# Submission time Handle Problem Language Result Execution time Memory
981572 2024-05-13T10:49:55 Z dimashhh New Home (APIO18_new_home) C++17
0 / 100
12 ms 25456 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

const int N = 2e5 + 5, MOD = 998244353;
typedef long double ld;
int n,m,tin[N],fup[N],timer;
bool vis[N];
vector<int> g[N];
void dfs(int v,int pr = -1){
    vis[v]=1;
    fup[v] = tin[v] = ++timer;
    for(int to:g[v]){
        if(to == pr) continue;
        if(!vis[to]){
            dfs(to,v);
            fup[v] = min(fup[v],fup[to]);
        }else{
            fup[v] = min(fup[v],tin[to]);
        }
    }
}
int mxc=0,c[N];
ll sz[N];
void paint(int v,int cl){
    c[v] = cl;
    sz[cl]++;
    for(int to:g[v]){
        if(!c[to]){
            if(fup[to] > tin[v]){
                mxc++;
                paint(to,mxc);
            }else{
                paint(to,cl);
            }
        }
    }
}
vector<int> G[N];
int P[N];
int get(int v){
    if(P[v] == v) return v;
    return P[v]= get(P[v]);
}
void merge(int a,int b){
    a = get(a);
    b = get(b);
    P[a] = b;
}
int sum[N];
void dfs2(int v,int pr = -1){
    sum[v] = sz[v];
    for(int to:G[v]){
        if(to == pr) continue;
        dfs2(to,v);
        sum[v] += sum[to];
    }
}
ll res = 0;
void dfs3(int v,int pr = -1,int S = 0){
    ll ss =0 ,ff = S - sum[v];
    for(int to:G[v]){
        if(to != pr){
            dfs3(to,v,S);
            ss += ff * 1ll * sum[to];
            ff += sum[to];
        }
    }
    ss *= 2;
    res += ss * 1ll * sz[v];
}
void test(){
    cin >> n >> m;
    for(int i = 1;i <= m;i++){
        int a,b;
        cin >> a >> b;
        g[a].push_back(b);
        g[b].push_back(a);
    }
    for(int i = 1;i <= n;i++){
        P[i] = i;
        if(!vis[i]){
            dfs(i);
        }
    }
    for(int i = 1;i <= n;i++){
        if(!c[i]){
            mxc++;
            paint(i,mxc);
        }
    }
    for(int i = 1;i <= n;i++){
        for(int to:g[i]){
            int x = c[i],y = c[to];
            if(get(x)!=get(y)){
                G[x].push_back(y);
                G[y].push_back(x);
                merge(x,y);
            }
        }
    }
    n = mxc;
    for(int i = 1;i <= n;i++) {
        vis[i]=0;
        res += sz[i] * (sz[i] - 1) * (sz[i] - 2);
    }
    ll A = 0;
    for(int i = 1;i <= n;i++){
        for(int j = 1;j <= n;j++){
            if(i == j) continue;
            A += sz[i] * sz[j] * (sz[j] - 1)/2 + (sz[j]-1) * (sz[j]-2) /2*sz[i];

        }
    }
    res += A*2;
    for(int i = 1;i <= n;i++){
        if(!sum[i]){
            dfs2(i);
            dfs3(i,-1,sum[i]);
        }
    }
    cout << res << '\n';
}
int main(){
    ios_base::sync_with_stdio(false);cin.tie(0);
    int T = 1;
//    cin >> T;
    while (T--){
        test();
    }
}
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 12632 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 12632 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 11 ms 25432 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Runtime error 12 ms 25456 KB Execution killed with signal 11
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 12632 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 2 ms 12632 KB Output isn't correct
2 Halted 0 ms 0 KB -