// Zende bad Shoma nasime faghat !
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
const ll Mod = 1000000007LL;
const int N = 1e5 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;
ll ans;
int n, m;
map<pll, int> mp;
int par[N], sz[N];
set<int> st[N];
ll Calc(int com){
ll res = 0;
res = 1ll * sz[com] * st[com].size();
res -= sz[com];
return res;
}
int Find(int u){
if(par[u] == u) return u;
return par[u] = Find(par[u]);
}
void Unite(int u, int v){
u = Find(u); v = Find(v);
if(u == v) return ;
if(st[u].size() > st[v].size()) swap(u, v);
ans -= Calc(u); ans -= Calc(v);
for(auto x : st[u]) st[v].insert(x);
st[u].clear();
//st[u].shrink_to_fit();
par[u] = v;
sz[v] += sz[u];
ans += Calc(v);
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
iota(par, par + N, 0);
fill(sz, sz + N, 1);
cin >> n >> m;
for(int i = 1; i <= n; i++) st[i].insert(i);
int u, v;
for(int i = 0; i < m; i++){
cin >> u >> v;
if(st[Find(u)].count(v)){
//debug("Sal");
Unite(u, v);
} else {
if(Find(u) != Find(v)){
ans -= Calc(Find(v));
st[Find(v)].insert(u);
ans += Calc(Find(v));
}
}
mp[{u, v}] = 1;
cout << ans << '\n';
}
return 0;
}
/*
3 4
1 2
2 3
3 2
3 1
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
5760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
5760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
5760 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |