// MDSPro
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx")
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using ld = long double;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#ifdef ONLINE_JUDGE
#define debug(x...) 42
#endif
const ld PI = 3.141592653589793;
const int MOD = 1e9+7;
const int INF = 1e9;
const ll INFLL = 4e18;
const double EPS = 1e-9;
const int MAXN = 1000*1007;
struct DSU {
int n;
vector<int> p, link;
vector<set<int>> v;
vector<set<int>> st;
ll ans;
DSU(int sz):n(sz) {
ans = 0;
p.resize(n); iota(p.begin(), p.end(), 0);
link.resize(n); iota(link.begin(), link.end(), 0);
v.resize(n); st.resize(n);
for(int i = 0; i < n; ++i) v[i].insert(i);
}
inline ll contr(int x) {
int sz = v[x].size();
int fr = st[link[x]].size();
return 1LL * sz * (sz - 1 + fr);
}
void unite(int a, int b) {
a = p[a]; b = p[b];
if(b == a) return;
ans -= contr(a);
ans -= contr(b);
int la = link[a], lb = link[b];
if(v[a].size() > v[b].size()) swap(a, b);
if(st[la].size() > st[lb].size()) swap(la, lb);
for(int x: st[la]) {
if(p[x] != b && p[x] != a) st[lb].insert(x);
}
st[la].clear();
for(int x: v[a]) {
if(st[lb].count(x)) st[lb].erase(x);
p[x] = b;
v[b].insert(x);
}
v[a].clear();
link[b] = lb;
link[a] = 0;
ans += contr(b);
}
void add_edge(int x, int y) {
y = p[y];
if(p[x] != y) {
if(!st[link[y]].count(x)) {
st[link[y]].insert(x);
ans += v[y].size();
}
}
}
ll get_ans() {
return ans;
}
void print() {
for(int i = 1; i < n; ++i) {
debug(i, link[i]);
debug(v[i]);
debug(st[link[i]]);
}
debug(ans);
}
};
void solve(int NT){
int n, m; cin >> n >> m;
DSU dsu(n + 1);
set<pair<int, int>> st;
dsu.print();
cerr << '\n';
for(int i = 0; i < m; ++i) {
int x, y; cin >> x >> y;
st.insert({x, y});
if(st.count({y, x})) dsu.unite(x, y);
else dsu.add_edge(x, y);
cout << dsu.get_ans() << '\n';
}
}
// #define TESTCASES
int main() {
cin.tie(0)->sync_with_stdio(0);
int t = 1;
#ifdef TESTCASES
cin >> t;
#endif
for(int i = 1; i <= t; ++i){
solve(i);
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |