Submission #509972

# Submission time Handle Problem Language Result Execution time Memory
509972 2022-01-14T13:03:27 Z leinad2 Making Friends on Joitter is Fun (JOI20_joitter2) C++17
0 / 100
6 ms 9676 KB
#include<bits/stdc++.h>
using namespace std;
int n, m, i, j, k, a, b, sz[100010], par[100010];
set<int>in[100010];
set<pair<int, int> >out[100010];
long long ans;
int Find(int v){return v==par[v]?v:par[v]=Find(par[v]);}
void f(int a, int b)
{
    //printf("%d %d!!\n", a, b);
    int x=Find(a), y=Find(b);
    //printf("%d %d %d %d!\n", x, y, a, b);
    if(x==y)return;
    auto it=out[y].lower_bound({x, -2e9});
    vector<pair<int, int> >V;//recursion
    if(it!=out[y].end()&&(*it).first==x)
    {
        if(in[x].size()+out[x].size()<in[y].size()+out[y].size())swap(x, y);
        for(auto p:out[y])
        {
            in[p.first].erase(p.second);//cout<<sz[p.first]<<"!!!";
            if(p.first!=x)V.push_back({p.second, p.first});
            ans-=sz[p.first];
        }//cout<<ans;
        int prev=sz[x];//printf("%d %d\n", sz[x], sz[y]);
        ans+=2LL*sz[x]*sz[y];
        //printf("%lld\n", ans);
        ans-=1LL*sz[y]*(int)in[y].size();
        //printf("%lld\n", ans);
        sz[x]+=sz[y];
        ans+=1LL*(sz[x]-prev)*(int)in[x].size();
        //printf("%lld!\n", ans);
        for(auto p:in[y])
        {
            out[Find(p)].erase({y, p});
            //out[Find(p)].insert({x, p});
            V.push_back({p, x});
        }
        par[y]=x;
        for(auto p:V)f(p.first, p.second);
    }
    else
    {
        if(in[y].find(a)==in[y].end())
        {
            out[x].insert({y, b});
            in[y].insert(a);
            ans+=sz[y];
            //printf("%lld\n", ans);
        }
    }
}
main()
{
    scanf("%d %d", &n, &m);
    for(i=0;i++<n;)sz[i]=1,par[i]=i;
    while(m--)
    {
        scanf("%d %d", &a, &b);
        f(a, b);
        printf("%lld\n", ans);
    }
}

Compilation message

joitter2.cpp:53:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   53 | main()
      | ^~~~
joitter2.cpp: In function 'int main()':
joitter2.cpp:55:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   55 |     scanf("%d %d", &n, &m);
      |     ~~~~~^~~~~~~~~~~~~~~~~
joitter2.cpp:59:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   59 |         scanf("%d %d", &a, &b);
      |         ~~~~~^~~~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 9676 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 9676 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 6 ms 9676 KB Output isn't correct
2 Halted 0 ms 0 KB -