답안 #509873

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
509873 2022-01-14T11:22:33 Z leinad2 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
0 / 100
3 ms 5000 KB
#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);
    }
}

Compilation message

joitter2.cpp:7:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
    7 | main()
      | ^~~~
joitter2.cpp: In function 'int main()':
joitter2.cpp:9:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
    9 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
joitter2.cpp:13:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   13 |         scanf("%d %d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 5000 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 5000 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 5000 KB Output isn't correct
2 Halted 0 ms 0 KB -