이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// Zende bad Shoma nasime faghat !
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
typedef long long ll;
typedef long double ld;
typedef string str;
typedef pair<ll, ll> pll;
const ll Mod = 1000000007LL;
const int N = 1e5 + 10;
const ll Inf = 2242545357980376863LL;
const ll Log = 30;
ll ans;
int n, m;
set<int> I[N], O[N];
int par[N], sz[N];
set<int> st[N];
vector<pll> U;
ll Calc(int com){
ll res = 0;
res = 1ll * sz[com] * st[com].size();
res -= sz[com];
return res;
}
int Find(int u){
if(par[u] == u) return u;
return par[u] = Find(par[u]);
}
void AddE(int u, int v){
v = Find(v);
ans -= Calc(v);
st[v].insert(u);
ans += Calc(v);
u = Find(u); v = Find(v);
O[u].insert(v);
I[v].insert(u);
if(I[u].count(v)){
U.pb({u, v});
}
}
void Unite(int u, int v){
u = Find(u); v = Find(v);
if(u == v) return ;
if(sz[u] > sz[v]) swap(u, v);
ans -= Calc(u); ans -= Calc(v);
for(auto x : st[u]){
//AddE(x, v);
st[v].insert(x);
}
for(auto x : I[u]){
if(O[v].count(x)){
U.pb({v, x});
}
I[v].insert(x);
O[x].erase(u);
O[x].insert(v);
}
for(auto x : O[u]){
if(I[v].count(x)){
U.pb({v, x});
}
O[v].insert(x);
I[x].erase(u);
I[x].insert(v);
}
st[u].clear(); I[u].clear(); O[u].clear();
//st[u].shrink_to_fit();
par[u] = v;
sz[v] += sz[u];
ans += Calc(v);
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
iota(par, par + N, 0);
fill(sz, sz + N, 1);
cin >> n >> m;
for(int i = 1; i <= n; i++) st[i].insert(i);
int u, v;
for(int i = 0; i < m; i++){
cin >> u >> v;
AddE(u, v);
int f1, f2;
while(!U.empty()){
f1 = U.back().F; f2 = U.back().S;
U.pop_back();
Unite(f1, f2);
//cerr << "# " << f1 << ' ' << f2 << '\n';
}
cout << ans << '\n';
}
return 0;
}
/*
4 6
1 2
2 1
3 4
4 3
1 3
4 2
8 14
1 2
2 1
3 4
4 5
5 4
4 3
6 7
7 8
8 7
7 6
1 3
4 7
6 1
8 5
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |