이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <ios>
#include<iostream>
#include <queue>
#include <set>
#include <vector>
#define ll long long
#define ff first
#define ss second
#define ull unsigned ll
#define ln "\n"
#define pll pair<ll, ll>
using namespace std;
struct DSU{
vector<ll> p;
vector<multiset<ll>> in, out, mem;
queue<pair<ll, ll>> links;
ll pedge;
DSU(ll N){
p.resize(N+1, -1);
in.resize(N+1);
out.resize(N+1);
mem.resize(N+1);
for (ll i=1; i<=N; i++) mem[i].insert(i);
pedge=0;
}
ll get(ll x){
return p[x]==-1?x:p[x]=get(p[x]);
}
void unite(ll u, ll v){
if (in[u].size()+out[u].size()+mem[u].size()<in[v].size()+out[v].size()+mem[v].size()) swap(u, v);
// cout << "Unity: " << u << " " << v << ln;
// cout << "Pre: " << pedge << ln;
p[v]=u;
pedge+=mem[u].size()*mem[v].size()*2+in[u].size()*mem[v].size();
for (auto ind:in[v]){
pedge-=mem[v].size();
out[ind].erase(out[ind].find(v));
link(ind, u);
}
in[v].clear();
for (auto outd:out[v]){
pedge-=mem[outd].size();
in[outd].erase(in[outd].find(v));
link(outd, u);
}
out[v].clear();
for (auto child:mem[v]){
mem[u].insert(child);
}
mem[v].clear();
// cout << "Post: " << pedge << ln;
}
void follow(ll u, ll v){
link(u, v);
while (!links.empty()){
unite(links.front().ff, links.front().ss);
links.pop();
}
// cout << "end\n";
}
void link(ll u, ll v){
// cout << "Linking: " << u << " - " << v << ln;
u=get(u);
v=get(v);
if (out[u].find(v)!=out[u].end()) return;
if (u==v) return;
if (out[v].find(u)!=out[v].end()){
pedge-=mem[u].size()*out[v].count(u);
out[v].erase(u);
in[u].erase(v);
links.push({u, v});
return;
}else{
pedge+=mem[v].size();
out[u].insert(v);
in[v].insert(u);
}
}
};
void solve(){
ll n, m; cin >> n >> m;
DSU tr(n);
for (ll i=0; i<m; i++){
ll u, v; cin >> u >> v;
tr.follow(u, v);
cout << tr.pedge << ln;
}
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
ll t=1;
// cin >> t;
while (t--) {
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |