#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;
template <class T>
void debug(T s){
for(auto x: s) cerr << x << " ";
cerr << " d\n";
}
template <class TT>
void debug0(TT s){
cerr << s << "d\n";
}
struct dsu{
int n;
vector<int> p;
vector<ll> sz;
vector<set<int>> aa, bb;
vector<set<pair<int, int>>> cnt;
vector<ll> cnt2;
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);
cnt2.resize(n);
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++;
//cerr << ttt << " d\n";
a = get(a), b = get(b);
if(a == b) return;
if(sz[b] > sz[a]) swap(a, b);
ans-=sz[a]*(sz[a]-1);
ans-=cnt2[a]*sz[a];
ans-=sz[b]*(sz[b]-1);
ans-=cnt2[b]*sz[b];
auto it = cnt[a].lower_bound({b, 0});
if(it != cnt[a].end() && (*it).f == b){
cnt2[a]-=(*it).sc;
cnt[a].erase(it);
}
it = cnt[b].lower_bound({a, 0});
if(it != cnt[b].end() && (*it).f == a){
cnt2[a]-=(*it).sc;
cnt[b].erase(it);
}
aa[a].erase(b);
bb[a].erase(b);
aa[b].erase(a);
bb[b].erase(a);
auto exc = [&](int x, int y, int a0, int b0){
auto it = cnt[b0].lower_bound({x, 0});
if(it == cnt[b0].end() || (*it).f != x) return;
auto p = (*it);
p.f = y;
cnt[b0].erase(it);
it = cnt[a0].lower_bound({y, 0});
if(it == cnt[a0].end() || (*it).f != y){
cnt[a0].insert(p);
}
else{
p.sc+=(*it).sc;
cnt[a0].erase(it);
cnt[a0].insert(p);
}
};
while(!aa[b].empty()){
int x = *aa[b].begin();
aa[b].erase(aa[b].begin());
exc(x, x, a, b);
bb[x].erase(b);
bb[x].insert(a);
aa[a].insert(x);
}
while(!bb[b].empty()){
int x = *bb[b].begin();
bb[b].erase(bb[b].begin());
exc(a, b, x, x);
aa[x].erase(b);
aa[x].insert(a);
bb[a].insert(x);
}
sz[a]+=sz[b];
sz[b] = 0;
p[b] = a;
cnt2[a] = bb[a].size();
ans+=cnt2[a]*sz[a];
ans+=sz[a]*(sz[a]-1);
for(int x: aa[a]) if(bb[a].find(x) != bb[a].end()) unite(a, x);
}
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];
auto it = cnt[a].lower_bound({b, 0});
cnt2[b]++;
if(a == 4 && b == 2){
debug(bb[a]);
debug(aa[b]);
}
if(it == cnt[a].end() || (*it).f != b){
cnt[a].insert({b, 1});
}
else{
auto p = *it;
p.sc++;
cnt[a].erase(it);
cnt[a].insert(p);
}
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++){
cerr << " ----- \n";
cerr << "#get():\n";
for(int i = 0; i < n; i++) cerr << ds.get(i)+1 << " ";
cerr << "\n";
cerr << "#sz:\n";
for(int x: ds.sz) cerr << x << " ";
cerr << "\n";
cerr << "#aa:\n";
for(auto s: ds.aa){
for(int x: s) cerr << x+1 << " ";
cerr << "\n";
}
cerr << "#bb:\n";
for(auto s: ds.bb){
for(int x: s) cerr << x+1 << " ";
cerr << "\n";
}
cerr << "#cnt2:\n";
for(int x: ds.cnt2) cerr << x << " ";
cerr << "\n";
if(i < m){
int a, b; cin >> a >> b; a--, b--;
ds.add_edge(a, b);
}
cout << ds.ans << "\n";
cerr << "\n\n\n\n\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |