답안 #1100801

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1100801 2024-10-14T18:29:57 Z imarn 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++14
0 / 100
3 ms 14928 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];
int sz[mxn]{0},pr[mxn]{0};
int get(int r){
    return pr[r]==r?r:pr[r]=get(pr[r]);
}vector<pii>qr;
void add(int a,int b){
    a=get(a),b=get(b);
    if(a==b)return;
    g[a].insert(b);
    gr[b].insert(a);
    if(g[b].count(a))qr.pb({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);
    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(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.back().f,qr.back().s);qr.pop_back();
        }cout<<rs<<'\n';
    }
}
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14928 KB Output is correct
2 Correct 3 ms 14928 KB Output is correct
3 Incorrect 3 ms 14928 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14928 KB Output is correct
2 Correct 3 ms 14928 KB Output is correct
3 Incorrect 3 ms 14928 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 3 ms 14928 KB Output is correct
2 Correct 3 ms 14928 KB Output is correct
3 Incorrect 3 ms 14928 KB Output isn't correct
4 Halted 0 ms 0 KB -