# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
509873 | leinad2 | Making Friends on Joitter is Fun (JOI20_joitter2) | C++17 | 3 ms | 5000 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
int n, m, i, j, k, a, b, sz[100010], par[100010];
long long ans;
int Find(int v){return v==par[v]?v:par[v]=Find(par[v]);}
multiset<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);
a=Find(a);b=Find(b);
if(a!=b)
{
if(adj[a].find(b)==adj[a].end())
{
if(adj[b].find(a)==adj[b].end())
{
adj[b].insert(a);
ans+=sz[b];
}
}
else
{
ans-=1LL*(adj[a].size()-adj[a].count(b))*sz[a];
ans-=1LL*(adj[b].size())*sz[b];
ans+=1LL*sz[a]*sz[b];
ans+=1LL*(sz[b]-adj[a].count(b))*sz[a];
if(adj[a].size()<adj[b].size())swap(adj[a], adj[b]);
for(auto p:adj[b])adj[a].insert(p);
adj[b].clear();
adj[a].erase(b);
sz[a]+=sz[b];
par[b]=a;
ans+=1LL*adj[a].size()*sz[a];
}
}
printf("%lld\n", ans);
}
}
컴파일 시 표준 에러 (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... |