Submission #260904

#TimeUsernameProblemLanguageResultExecution timeMemory
260904wiwihoDuathlon (APIO18_duathlon)C++14
0 / 100
1105 ms1048580 KiB
#include <bits/stdc++.h>

#define eb emplace_back
#define mp make_pair
#define F first
#define S second
#define pii pair<int, int>
#define pll pair<ll, ll>

using namespace std;

typedef long long ll;

const ll MAX = 2147483647;

vector<vector<int>> g;
vector<ll> sz;

ll ans = 0;
void dfs(int now, int p){
    sz[now] = 1;
    for(int i : g[now]){
        if(i == p) continue;
        dfs(i, now);
        sz[now] += sz[i];
    }
}

void dfs2(int now, int p){
    for(int i : g[now]){
        if(i == p) continue;
        dfs2(i, now);
    }
    ans += (sz[now] - 1) * (sz[p] - sz[now]) * 2;
    //cerr << now << " " << sz[now] << " " << p << " " << sz[p] << " " << sz[now] - 1 << " " << sz[p] - sz[now] << " " <<  (sz[now] - 1) * (sz[p] - sz[now]) * 2 << " " << ans << "\n";
}

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n, m;
    cin >> n >> m;

    g.resize(n + 1);
    sz.resize(n + 1);

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

    for(int i = 1; i <= n; i++){
        if(sz[i]) continue;
        //cerr << "test " << i << "\n";
        dfs(i, i);
        dfs2(i, i);
    }

    cout << ans << "\n";

    return 0;
}

#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...