이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
#define fi first
#define se second
#define rep(a, b) for(int a = 0; a < (b); a++)
using namespace std;
typedef long long ll;
typedef vector<ll> vi;
typedef vector<ll> vl;
const int N=1e5+10;
vi f[N];
int fau[N];
ll w[N];
set<int> ini[N];
set<int> outi[N];
set<int> ini2[N];
bool vis[N];
ll res;
vi Union(int a,int b){
res-=(ll)(f[a].size())*(ll)(f[a].size()-1);
res-=(ll)(f[b].size())*(ll)(f[b].size()-1);
a=fau[a],b=fau[b];
vi p;
if(f[a].size()>f[b].size()) swap(a,b);
rep(i,f[a].size()){
fau[f[a][i]]=b,f[b].push_back(f[a][i]);
auto it=ini2[b].lower_bound(f[a][i]);
if(it!=ini2[b].end() and (*it)==f[a][i]) ini2[b].erase(f[a][i]);
}
f[a].clear();
for(auto it=ini2[a].begin();it!=ini2[a].end();it++) if(fau[(*it)]!=b) ini2[b].insert(*it);
ini2[a].clear();
for(auto it=ini[a].begin();it!=ini[a].end();it++){
auto it2=outi[b].lower_bound(*it);
if(!vis[*it2] and it2!=outi[b].end() and *it2==*it) p.push_back(*it2),vis[*it2]=1;
}
for(auto it=outi[a].begin();it!=outi[a].end();it++){
auto it2=ini[b].lower_bound(*it);
if(!vis[*it2] and it2!=ini[b].end() and *it==*it2) p.push_back(*it2),vis[*it2]=1;
}
for(auto it=ini[a].begin();it!=ini[a].end();it++){
if((*it)!=b){
outi[*it].erase(a);
outi[*it].insert(b);
}
}
for(auto it=outi[a].begin();it!=outi[a].end();it++){
if((*it)!=b){
ini[*it].erase(a);
ini[*it].insert(b);
}
}
for(auto it=ini[a].begin();it!=ini[a].end();it++) if(*it!=b) ini[b].insert(*it);
for(auto it=outi[a].begin();it!=outi[a].end();it++) if(*it!=b) outi[b].insert(*it);
outi[b].erase(a),ini[b].erase(a);
ini[a].clear(),outi[a].clear();
res-=w[a],res-=w[b];
w[b]=(ll)(ini2[b].size())*(ll)(f[b].size());
res+=w[b];
res+=(ll)(f[b].size())*(ll)(f[b].size()-1);
return p;
}
void solve(){
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++) fau[i]=i,f[i].push_back(i);
rep(i,q){
int a,b;
cin>>a>>b;
b=fau[b];
auto it2=ini2[b].lower_bound(a);
if((it2!=ini2[b].end() and (*it2)==a) or fau[a]==b){cout<<res<<"\n";continue;}
ini2[b].insert(a);
a=fau[a];
res-=w[b];
outi[a].insert(b),ini[b].insert(a);
w[b]=(ll)(ini2[b].size())*(ll)(f[b].size());
res+=w[b];
auto it=outi[b].lower_bound(a);
if(it==outi[b].end() or (*it)!=a){cout<<res<<"\n"; continue;}
vi xd=Union(a,b);
int v=fau[a];
while(xd.size()){
vi pom=Union(v,xd.back());
vis[xd.back()]=0;
xd.pop_back();
rep(j,pom.size()) xd.push_back(pom[j]);
v=fau[v];
}
cout<<res<<"\n";
}
}
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
solve();
}
컴파일 시 표준 에러 (stderr) 메시지
joitter2.cpp: In function 'vi Union(int, int)':
joitter2.cpp:4:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
4 | #define rep(a, b) for(int a = 0; a < (b); a++)
| ^
joitter2.cpp:24:2: note: in expansion of macro 'rep'
24 | rep(i,f[a].size()){
| ^~~
joitter2.cpp: In function 'void solve()':
joitter2.cpp:4:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
4 | #define rep(a, b) for(int a = 0; a < (b); a++)
| ^
joitter2.cpp:86:4: note: in expansion of macro 'rep'
86 | rep(j,pom.size()) xd.push_back(pom[j]);
| ^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |