# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
938356 | guagua0407 | Making Friends on Joitter is Fun (JOI20_joitter2) | C++17 | 1 ms | 344 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
vector<map<int,int>> S1,S2,R1,R2;
ll ans=0;
struct DSU{
vector<int> e;
DSU(int n){
e=vector<int>(n,-1);
}
int find(int x){
return (e[x]<0?x:e[x]=find(e[x]));
}
bool same(int a,int b){
return find(a)==find(b);
}
int sz(int x){
return -e[find(x)];
}
bool unite(int a,int b){
a=find(a);
b=find(b);
if(a==b) return false;
for(auto v:R1[a]){
if(find(v.f)==b){
R1[a].erase(v.f);
}
}
for(auto v:R1[b]){
if(find(v.f)==a){
R1[b].erase(v.f);
}
}
ans+=2ll*sz(a)*sz(b)-1ll*S2[a][b]*sz(b)-1ll*S2[b][a]*sz(a);
ans+=1ll*(int)R1[a].size()*sz(b)+1ll*(int)R1[b].size()*sz(a);
if(S2[a].size()+R1[a].size()+R2[a].size()<S2[b].size()+R1[b].size()+R2[b].size()) swap(a,b);
for(auto v:S2[b]){
S2[a][v.f]+=v.s;
R2[v.f][a]+=v.s;
}
for(auto v:R1[b]){
S1[v.f][a]+=v.s;
R1[a][v.f]+=v.s;
}
for(auto v:R2[b]){
S2[v.f][a]+=v.s;
R2[a][v.f]+=v.s;
}
e[a]+=e[b];
e[b]=a;
return true;
}
};
int main() {_
int n,m;
cin>>n>>m;
S1.resize(n);
S2.resize(n);
R1.resize(n);
R2.resize(n);
DSU dsu(n);
for(int i=0;i<m;i++){
int a,b;
cin>>a>>b;
a--;
b--;
if(dsu.same(a,b)){
cout<<ans<<'\n';
continue;
}
int pa=dsu.find(a);
int pb=dsu.find(b);
if(S1[a].find(pb)!=S1[a].end()){
cout<<ans<<'\n';
continue;
}
S1[a][pb]++;
S2[pa][pb]++;
R1[pb][a]++;
R2[pb][pa]++;
ans+=dsu.sz(pb);
//cout<<"-"<<'\n';
if(S2[pb].find(pa)!=S2[pb].end()){
dsu.unite(pa,pb);
}
cout<<ans<<'\n';
}
return 0;
}
//maybe its multiset not set
//yeeorz
//laborz
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |