Submission #245421

#TimeUsernameProblemLanguageResultExecution timeMemory
245421TadijaSebezMaking Friends on Joitter is Fun (JOI20_joitter2)C++11
0 / 100
15 ms19072 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back const int N=100050; set<int> in[N],all[N],E[N],R[N]; int my[N]; ll ans; ll Calc(int cmp){ return (ll)all[cmp].size()*((int)all[cmp].size()-1)+(ll)in[cmp].size()*all[cmp].size(); } void Del(int cmp){ ans-=Calc(cmp); } void Add(int cmp){ ans+=Calc(cmp); } int Mrg(int A,int B){ if(all[A].empty())return B; if(all[B].empty())return A; Del(A);Del(B); E[B].erase(A); R[A].erase(B); if(all[A].size()+in[A].size()+E[A].size()+R[A].size()>all[B].size()+in[B].size()+E[B].size()+R[B].size())swap(A,B); for(int x:all[A]){ if(in[B].count(x))in[B].erase(x); all[B].insert(x); my[x]=B; } all[A].clear(); for(int x:in[A]){ if(!all[B].count(x))in[B].insert(x); } in[A].clear(); vector<int> nxt; for(int x:E[A]){ if(R[B].count(x))nxt.pb(x); R[x].erase(A); if(!E[B].count(x)){ E[B].insert(x); R[x].insert(B); } } E[A].clear(); for(int x:R[A]){ if(E[B].count(x))nxt.pb(x); E[x].erase(A); if(!R[B].count(x)){ R[B].insert(x); E[x].insert(B); } } R[A].clear(); Add(B); sort(nxt.begin(),nxt.end()); nxt.erase(unique(nxt.begin(),nxt.end()),nxt.end()); for(int x:nxt){ B=Mrg(B,x); } return B; } int main(){ int n,m;scanf("%i %i",&n,&m); for(int i=1;i<=n;i++)all[i].insert(i),my[i]=i; for(int i=1;i<=m;i++){ int u,v;scanf("%i %i",&u,&v); int A=my[u],B=my[v]; if(E[B].count(A)){ Mrg(A,B); }else{ Del(B); in[B].insert(u); E[A].insert(B); R[B].insert(A); Add(B); } printf("%lld\n",ans); } return 0; }

Compilation message (stderr)

joitter2.cpp: In function 'int main()':
joitter2.cpp:63:15: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  int n,m;scanf("%i %i",&n,&m);
          ~~~~~^~~~~~~~~~~~~~~
joitter2.cpp:66:16: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   int u,v;scanf("%i %i",&u,&v);
           ~~~~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...