답안 #938768

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
938768 2024-03-05T14:11:06 Z guagua0407 조이터에서 친구를 만드는건 재밌어 (JOI20_joitter2) C++17
0 / 100
14 ms 39556 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define all(x) x.begin(),x.end()
#define abcorz ios_base::sync_with_stdio(false);cin.tie(0);
const int N=200005;
const int mod=1e9+7;
const int INF=1e9;
queue<pair<int,int>> q;
namespace{
    vector<int> pa(N,-1);
    int Find(int k){
        return pa[k]<0?k:pa[k]=Find(pa[k]);
    }
    void onion(int a,int b,vector<set<int>> &adj,vector<set<int>> &to,vector<set<int>> &adj1,vector<set<int>> &to1){
        a=Find(a),b=Find(b);
        if(a==b)return;
        if(adj[a].size()>adj[b].size())swap(a,b);
        pa[b]+=pa[a];
        pa[a]=b;
        for(int j:to[a]){
            to[b].insert(j);
            if(adj[b].find(j)!=adj[b].end()){
                q.push({b,j});
            }
        }
        for(int j:adj[a]){
            adj[b].insert(j);
            if(to[b].find(j)!=to[b].end()){
                q.push({b,j});
            }
        }
        for(int j:to1[a]){
            to1[b].insert(j);
        }
        for(int j:adj1[a]){
            adj1[b].insert(j);
        }
    }
};
int32_t main(){
    abcorz;
    int n,m;cin>>n>>m;
    vector<set<int>> adj(N),to(N),adj1(N),to1(N);
    for(int j=1;j<=n;j++){
        adj[j].insert(j);
        to[j].insert(j);
    }
    for(int i=0;i<m;i++){
        int a,b;cin>>a>>b;
        adj[Find(b)].insert(a);
        to[Find(a)].insert(b);
        adj1[Find(b)].insert(Find(a));
        to1[Find(a)].insert(Find(b));
        if(adj1[Find(a)].find(Find(b))!=adj1[Find(a)].end()){
            q.push({a,b});
            while(q.size()){
                auto [a,b]=q.front();
                q.pop();
                //cout<<"sir "<<a<<' '<<b<<'\n';
                onion(a,b,adj,to,adj1,to1);
            }
        }
        int ans=-n;
        for(int j=1;j<=n;j++){
            if(Find(j)!=j)continue;
            ans+=(-pa[j])*(int)(adj[j].size());
        }
        cout<<ans<<endl;
    }



    return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 39516 KB Output is correct
2 Correct 12 ms 39556 KB Output is correct
3 Incorrect 11 ms 39552 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 39516 KB Output is correct
2 Correct 12 ms 39556 KB Output is correct
3 Incorrect 11 ms 39552 KB Output isn't correct
4 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 14 ms 39516 KB Output is correct
2 Correct 12 ms 39556 KB Output is correct
3 Incorrect 11 ms 39552 KB Output isn't correct
4 Halted 0 ms 0 KB -