Submission #605230

# Submission time Handle Problem Language Result Execution time Memory
605230 2022-07-25T14:20:56 Z DJeniUp Making Friends on Joitter is Fun (JOI20_joitter2) C++17
0 / 100
13 ms 19084 KB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll,ll>pairll;
typedef pair<ll,pairll>pairlll;
typedef unsigned long long ull;
typedef long double ld;

#define fr first
#define sc second
#define pb push_back
#define INF 1000000007
#define N 200007
#define endl '\n'

ll n,M,f[100007],res,sz[100007],a[100007];

set<ll>s[100007],p[100007],d[100007];
set<ll>k;

map<ll,ll>m[100007];

ll P(ll x){
    if(f[x]!=x)f[x]=P(f[x]);
    return f[x];
}

void A(ll x,ll y){
    //cout<<x<<" "<<y<<" "<<a[x]<<" "<<a[y]<<" "<<sz[x]<<" "<<sz[y]<<endl;
    res-=a[x]*sz[x];
    res-=a[y]*sz[y];
    res-=sz[x]*(sz[x]-1);
    res-=sz[y]*(sz[y]-1);
    sz[x]+=sz[y];
    res+=sz[x]*(sz[x]-1);
    for(auto it:s[y]){
        s[x].insert(P(it));
    }
    k.clear();
    a[x]=0;
    for(auto it:p[x]){
        if(P(it)!=x && P(it)!=y){
            k.insert(it);
        }
    }
    swap(p[x],k);
    for(auto it:p[y]){
        if(P(it)!=x && P(it)!=y){
            p[x].insert(it);
        }
    }
    a[x]=p[x].size();
    res+=a[x]*sz[x];
    f[y]=x;
    return ;
}

int main()
{
    cin>>n>>M;
    for(int i=1;i<=n;i++){
        f[i]=i;
        sz[i]=1;
    }
    for(int i=1;i<=M;i++){
        ll x,y;
        cin>>x>>y;
        if(P(x)!=P(y)){
            //cout<<P(y)<<" "<<P(x)<<" "<<s[P(x)].count(P(y))<<endl;
            ll fl=0;
            for(auto it:s[P(y)]){
                if(P(it)==P(x)){
                    fl=1;
                    break;
                }
            }
            if(fl==1)A(P(x),P(y));
            else{
                if(p[P(y)].count(x)==0){
                    res+=sz[P(y)];
                    p[P(y)].insert(x);
                    a[P(y)]++;
                }
                s[P(x)].insert(P(y));
            }
        }
        cout<<res<<endl;

    }

    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19028 KB Output is correct
2 Correct 12 ms 19084 KB Output is correct
3 Incorrect 13 ms 19028 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19028 KB Output is correct
2 Correct 12 ms 19084 KB Output is correct
3 Incorrect 13 ms 19028 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 11 ms 19028 KB Output is correct
2 Correct 12 ms 19084 KB Output is correct
3 Incorrect 13 ms 19028 KB Output isn't correct
4 Halted 0 ms 0 KB -