#include <bits/stdc++.h>
#define nl "\n"
#define no "NO"
#define yes "YES"
#define fi first
#define se second
#define vec vector
#define task "main"
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())
using namespace std;
template<typename U, typename V> bool maxi(U &a, V b) {
if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool mini(U &a, V b) {
if (a > b) { a = b; return 1; } return 0;
}
const int N = (int)2e5 + 9;
const int mod = (int)1e9 + 7;
void prepare(); void main_code();
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
const bool MULTITEST = 0; prepare();
int num_test = 1; if (MULTITEST) cin >> num_test;
while (num_test--) { main_code(); }
}
void prepare() {};
long long ans = 0;
int par[N], s[N];
int rt(int x) {
return x == par[x] ? x : par[x] = rt(par[x]);
}
set<pair<int, int>> in[N], out[N];
queue<pair<int, int>> q;
bool check(int x, int y) {
x = rt(x), y = rt(y);
auto it1 = out[x].lower_bound({y, 0});
auto it2 = out[y].lower_bound({x, 0});
return (it1 != out[x].end() and it2 != out[y].end()
and it1->first == y and it2->first == x);
}
void unite(int x, int y) {
x = rt(x), y = rt(y);
if (x == y) return ;
if (sz(in[x]) + sz(out[x]) < sz(in[y]) + sz(out[y]))
swap(x, y);
ans -= 1LL * s[x] * (s[x] - 1) + 1LL * s[x] * sz(in[x]);
ans -= 1LL * s[y] * (s[y] - 1) + 1LL * s[y] * sz(in[y]);
// cerr << x << ' ' << y << ' ' << ans << nl;
for(ii v : in[y]) {
if (v.first == x) {
continue ;
}
out[v.first].erase(_mp(y, v.second));
out[v.first].insert(_mp(x, v.second));
in[x].insert(v);
if (check(x, v.first)) {
q.push(_mp(x, v.first));
}
}
for(ii v : out[y]) {
if (v.first == x) {
in[x].erase(_mp(y, v.second));
continue ;
}
in[v.first].erase(_mp(y, v.second));
in[v.first].insert(_mp(x, v.second));
out[x].insert(v);
if (check(x, v.first)) {
q.push(_mp(x, v.first));
}
}
par[y] = x, s[x] += s[y];
ans += 1LL * s[x] * (s[x] - 1) + 1LL * s[x] * sz(in[x]);
cerr << s[x] << ' ' << sz(in[x]) << nl;
}
void main_code() {
int n, m; cin >> n >> m;
for(int i = 1; i <= n; ++i) s[i] = 1, par[i] = i;
for(int i = 1; i <= m; ++i) {
int x, y; cin >> x >> y;
int u=x, v=y;
x = rt(x), y = rt(y);
if (x != y) {
// cerr << "i: " << i << ' ' << x << ' ' << u << nl;
ans -= 1LL * s[y] * sz(in[y]);
out[x].insert(_mp(y, u));
in[y].insert(_mp(x, u));
ans += 1LL * s[y] * sz(in[y]);
if (check(x, y)) {
q.push(_mp(x, y));
}
while (!q.empty()) {
auto c = q.front(); q.pop();
unite(c.first, c.second);
}
}
cout << ans << nl;
}
}
/* Let the river flows naturally */
Compilation message (stderr)
joitter2.cpp: In function 'int main()':
joitter2.cpp:36:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
36 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
joitter2.cpp:37:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
37 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |