# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
509906 | leinad2 | Making Friends on Joitter is Fun (JOI20_joitter2) | C++17 | 3 ms | 4940 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.
#include<bits/stdc++.h>
using namespace std;
int n, m, i, j, k, a, b, sz[100010], indeg[100010], par[100010];
long long ans;
int Find(int v){return v==par[v]?v:par[v]=Find(par[v]);}
map<int, set<int> >adj[100010];
main()
{
scanf("%d %d", &n, &m);
for(i=0;i++<n;)sz[i]=1,par[i]=i;
while(m--)
{
scanf("%d %d", &a, &b);
int x=a, y=b;
a=Find(a);b=Find(b);
if(a!=b)
{
if(adj[a].find(b)==adj[a].end())
{
if(adj[b][a].find(x)==adj[b][a].end())
{
adj[b][a].insert(x);
ans+=sz[b];
indeg[b]++;
}
}
else
{
ans-=1LL*(indeg[a]-(int)adj[a][b].size())*sz[a];
ans-=1LL*(indeg[b])*sz[b];
ans+=1LL*sz[a]*sz[b];
ans+=1LL*(sz[b]-adj[a][b].size())*sz[a];
if(indeg[a]<indeg[b])swap(adj[a], adj[b]);
for(auto p:adj[b])
{
for(auto q:p.second)
adj[a][p.first].insert(q);
}
adj[b].clear();
indeg[a]-=adj[a][b].size();
adj[a].erase(b);
sz[a]+=sz[b];
indeg[a]+=indeg[b];
par[b]=a;
ans+=1LL*indeg[a]*sz[a];
}
}
printf("%lld\n", ans);
}
}
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... |