Submission #1100833

# Submission time Handle Problem Language Result Execution time Memory
1100833 2024-10-14T18:57:35 Z imarn Making Friends on Joitter is Fun (JOI20_joitter2) C++14
0 / 100
4 ms 15952 KB
#include<bits/stdc++.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define plx pair<ll,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vl vector<ll>
#define vvi vector<vi>
using namespace std;
const int mxn=1e5+5;
set<int>ch[mxn],g[mxn],gr[mxn];
ll sz[mxn]{0},pr[mxn]{0};
int get(int r){
    return pr[r]==r?r:pr[r]=get(pr[r]);
}stack<pii>qr;
void add(int a,int b){
    if(a==b)return;
    g[a].insert(b);
    gr[b].insert(a);
    if(g[b].count(a))qr.push({a,b});
}
ll rs=0;
void uni(int a,int b){
    a=get(a),b=get(b);
    if(a==b)return;
    if(ch[a].size()+gr[a].size()+g[a].size()>ch[b].size()+gr[b].size()+g[b].size())swap(a,b);
    rs += ((ll)ch[a].size())*sz[b] + ((ll)ch[b].size()*sz[a]);
    pr[a]=b;sz[b]+=sz[a];
    for(auto it : ch[a]){
        if(ch[b].count(it))rs-=sz[b];
        else {
            ch[b].insert(it);
        }
    }g[a].erase(b);gr[a].erase(b);gr[b].erase(a);g[b].erase(a);
    for(auto it : g[a]){
        add(b,it);
    }
    for(auto it : gr[a]){
        add(it,b);
    }
}
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    int n,m;cin>>n>>m;
    for(int i=1;i<=n;i++)sz[i]=1,ch[i].insert(i),pr[i]=i;
    while(m--){
        int a,b;cin>>a>>b;b=get(b);
        if(a==b||ch[b].count(a)){
            cout<<rs<<'\n';continue;
        }rs+=sz[b];ch[b].insert(a);a=get(a);add(a,b);
        while(!qr.empty()){
            uni(qr.top().f,qr.top().s);qr.pop();
        }cout<<rs<<'\n';
    }
}
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15696 KB Output is correct
2 Correct 3 ms 15696 KB Output is correct
3 Incorrect 4 ms 15952 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15696 KB Output is correct
2 Correct 3 ms 15696 KB Output is correct
3 Incorrect 4 ms 15952 KB Output isn't correct
4 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 3 ms 15696 KB Output is correct
2 Correct 3 ms 15696 KB Output is correct
3 Incorrect 4 ms 15952 KB Output isn't correct
4 Halted 0 ms 0 KB -