This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// O O O O O O O O O O O O O O O OO O OO O OO O O O TTCH O TTTCH O TTTCH O O O O
#pragma GCC optimize("Ofast")
#pragma GCC optimize("no-stack-protector")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
#pragma GCC target("sse,sse2,sse3,ssse3,popcnt,abm,mmx")
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <stdio.h>
#include <cstdio>
#include <math.h>
#include <cmath>
#include <string>
#include <cstring>
#include <queue>
#include <deque>
// #include <random>
#include <iomanip>
#include <bitset>
#include <cassert>
using namespace std;
#define int long long
#define y1 y11
#define double long double
#define less less228
#define left left228
#define right right228
#define list list228
#define all(v) v.begin(), v.end()
template<typename T> void uin(T &a, T b) {
if (b < a) a = b;
}
template<typename T> void uax(T &a, T b) {
if (b > a) a = b;
}
const int N = 100 * 1000 + 228;
int p[N], sz[N];
vector<int> in[N];
void init(int n) {
for (int i = 1; i <= n; ++i) {
p[i] = i;
sz[i] = 1;
in[i].emplace_back(i);
}
}
int getp(int v){
if(v==p[v])return v;
p[v]=getp(p[v]);
return p[v];
}
int n,m;
int ans = 0;
map< pair<int,int>, bool> vc, is;
map< pair<int,int>,int> between;
set<int> vic[N], iic[N], outc[N];
void squeeze(int c1, int c2) {
ans -= between[{c1, c2}] * sz[c2];
ans -= between[{c2, c1}] * sz[c1];
ans += 2LL * sz[c1] * sz[c2];
if (sz[c1] > sz[c2]) swap(c1, c2);
for (int x : in[c1]) {
if (vic[c2].find(x) != vic[c2].end()) {
vic[c2].erase(x);
}
}
ans -= (int)vic[c2].size() * sz[c2];
for (int x : vic[c1]) {
if (vic[c2].find(x) != vic[c2].end()) vic[c2].erase(x);
if (getp(x) == c2) continue;
ans -= sz[c1];
if (!vc[{x, c2}]) {
vc[{x, c2}] = 1;
vic[c2].insert(x);
}
}
ans += (int)vic[c2].size() * (sz[c1] + sz[c2]);
vector<int>suspicion;
for (int c : iic[c1]) {
if (getp(c) == c2) continue;
if (!is[{c, c2}]) {
is[{c, c2}] = 1;
iic[c2].insert(c);
between[{c, c2}] += between[{c, c1}];
suspicion.emplace_back(c);
}
}
for (int c : outc[c1]) {
if (getp(c) == c2) continue;
if (!is[{c2, c}]) {
is[{c2, c}] = 1;
outc[c2].insert(c);
between[{c2, c}] += between[{c1, c}];
suspicion.emplace_back(c);
}
}
for (int x : in[c1]) {
if (outc[c2].find(x) != outc[c2].end()) {
outc[c2].erase(x);
}
}
p[c1] = c2;
sz[c2] += sz[c1];
for (int x : in[c1]) {
in[c2].emplace_back(x);
}
for (int c : suspicion) {
if (is[{getp(c2), getp(c)}] && is[{getp(c), getp(c2)}]) {
squeeze(getp(c), getp(c2));
}
}
}
void add(int a, int b) {
int c1 = getp(a);
int c2 = getp(b);
if (c1 == c2)return;
// c1 --> c2
if (!is[{c2, c1}]) {
is[{c1, c2}] = 1;
iic[c2].insert(c1);
outc[c1].insert(c2);
if (!vc[{a, c2}]) {
++between[{c1, c2}];
ans += sz[c2];
vc[{a, c2}] = 1;
vic[c2].insert(a);
}
} else {
squeeze(c1, c2);
}
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> m;
init(n);
for (int i = 0; i < m; ++i) {
int u, v;
cin >> u >> v;
add(u, v);
cout << ans << '\n';
}
return 0;
}
// RU_023
/*
4 6
1 2
2 3
3 2
1 3
3 4
4 3
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |