Untitled

 avatar
unknown
plain_text
a year ago
1.0 kB
3
Indexable
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
#define endl "\n"
 
const double PI = 3.14159265358979;
const ll INF =  1e18 + 7;
const ll MOD = 1e9 + 7;
const ll nax = 100005;
const int LOG = 25;


vector<int> adj[nax];
vector<int> vis(nax, 0);

void dfs(int u, ll &componentSize) {
    vis[u] = 1;
    componentSize++;

    for (int v : adj[u]) {
        if (!vis[v]) {
            dfs(v, componentSize);
        }
    }
}

void solve() {
    int n , m;
    cin >> n >> m;

    for (int i = 1; i <= m; i++) {
        int x, y;
        cin >> x >> y;
        adj[x].emplace_back(y);
        adj[y].emplace_back(x);
    }

    ll ans = 0;
    for (int i = 1; i <= n; i++) {
        if (vis[i]) continue;
        ll componentSize = 0;
        dfs(i, componentSize);
        ans = ans + (componentSize * (n - componentSize));
    }

    cout << ans / 2;

}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);

    // int t; cin >> t; while(t--)
    solve();
    return 0;
}
Leave a Comment