#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;
int ttt = 0;
queue <pair<int, int>> q;
map<pair<int, int>, ll> mp;
struct dsu{
int n;
vector<int> p;
vector<ll> sz;
vector<set<int>> aa, bb;
vector<ll> cnt;
ll ans;
dsu(int nn){
n = nn;
ans = 0;
sz.resize(n, 1);
p.resize(n);
aa.resize(n);
bb.resize(n);
cnt.resize(n, 0);
for(int i = 0; i < n; i++) p[i] = i;
}
int get(int x){
if(p[x] != x) p[x] = get(p[x]);
return p[x];
}
void unite(int a, int b){
ttt++;
a = get(a), b = get(b);
if(a == b) return;
if(sz[b] + aa[b].size() + bb[b].size() > sz[a] + aa[a].size() + bb[a].size()) swap(a, b);
ans-=sz[a]*(sz[a]-1);
ans-=sz[a]*bb[a].size();
ans-=sz[b]*(sz[b]-1);
ans-=sz[b]*bb[b].size();
aa[a].erase(b);
aa[b].erase(a);
bb[a].erase(b);
bb[b].erase(a);
for(int x: aa[b]){
mp[{b, x}]--;
if(aa[a].find(x) == aa[a].end()) mp[{a, x}]++;
bb[x].erase(b);
bb[x].insert(a);
aa[a].insert(x);
}
aa[b].clear();
for(int x: bb[b]){
mp[{x, b}]--;
if(bb[a].find(x) == bb[a].end()) mp[{x, a}]++;
aa[x].erase(b);
aa[x].insert(a);
bb[a].insert(x);
}
bb[b].clear();
sz[a]+=sz[b];
sz[b] = 0;
p[b] = a;
ans+=sz[a]*bb[a].size();
ans+=sz[a]*(sz[a]-1);
}
void add_edge(int a, int b){
a = get(a), b = get(b);
if(a == b) return;
if(aa[a].find(b) != aa[a].end()) return;
aa[a].insert(b);
bb[b].insert(a);
ans+=sz[b];
cnt[b]++;
mp[{a, b}]++;
if(aa[b].find(a) != aa[b].end()) unite(a, b);
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n, m; cin >> n >> m;
dsu ds(n);
for(int i = 0; i < m; i++){
int a, b; cin >> a >> b; a--, b--;
ds.add_edge(a, b);
cout << ds.ans << "\n";
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |