답안 #509878

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
509878 2022-01-14T11:28:14 Z leinad2 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
0 / 100
3 ms 4940 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]);}
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];
                }
            }
            else
            {
                ans-=1LL*(adj[a].size()-adj[a][b].size())*sz[a];
                ans-=1LL*(adj[b].size())*sz[b];
                ans+=1LL*sz[a]*sz[b];
                ans+=1LL*(sz[b]-adj[a][b].size())*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:14:18: warning: unused variable 'y' [-Wunused-variable]
   14 |         int x=a, y=b;
      |                  ^
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 4940 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 4940 KB Output isn't correct
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 3 ms 4940 KB Output isn't correct
2 Halted 0 ms 0 KB -