Submission #706566

# Submission time Handle Problem Language Result Execution time Memory
706566 2023-03-07T04:30:47 Z EthanKim8683 Making Friends on Joitter is Fun (JOI20_joitter2) C++17
0 / 100
5 ms 9684 KB
#include<bits/stdc++.h>
using namespace std;
using I=int;
using Lli=long long int;
const I N=100000;
set<pair<I,I>>viss;
set<I>incs[N],outs[N];
I pars[N];
Lli res=0;
I fnd(I i){
  return pars[i]<0?i:pars[i]=fnd(pars[i]);
}
I siz(I i){
  return-pars[fnd(i)];
}
void uni(I a,I b){
  if((a=fnd(a))==(b=fnd(b)))return;
  if(pars[a]>pars[b])swap(a,b);
  Lli siz1=siz(a),siz2=siz(b);
  res-=siz1*(siz1-1);
  res-=siz2*(siz2-1);
  res-=incs[a].size()*siz1;
  res-=incs[b].size()*siz2;
  for(auto c:incs[b]){
    outs[c].erase(b);
    if(fnd(c)!=a){
      outs[c].insert(a);
      incs[a].insert(c);
    }
  }
  for(auto c:outs[b]){
    incs[c].erase(b);
    if(fnd(c)!=a){
      incs[c].insert(a);
      outs[a].insert(c);
    }
  }
  pars[a]+=pars[b],pars[b]=a;
  Lli siz3=siz(a);
  res+=siz3*(siz3-1);
  res+=incs[a].size()*siz3;
}
I main(){
  cin.tie(0)->sync_with_stdio(0);
  I n,m;cin>>n>>m;
  fill_n(pars,n,-1);
  for(I i=0;i<m;i++){
    I a,b;cin>>a>>b,a--,b--;
    I c=fnd(a),d=fnd(b);
    if(!incs[d].count(c)){
      incs[d].insert(c);
      outs[c].insert(d);
      res+=siz(d);
    }
    viss.insert({a,b});
    if(viss.count({b,a}))uni(a,b);
    printf("%lli\n",res);
  }
}
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 9684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 9684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 9684 KB Output isn't correct
2 Halted 0 ms 0 KB -