More than 2019 cattle off summer school camp (ninth field) E.All men are brothers (disjoint-set + permutations)

Meaning of the questions: There are now n sets each set of size 1 Now you can merge a set of m times each will tell you which allows you to merge a set of output each combination selected from each of four programs from four different collections in

Ideas: we can think to check with the merger set and a set of simulation programs for a number we can find that in fact is the answer before the merger the combined number minus the number of programs in the two collections (more details to understand the code)

#include <bits/stdc++.h>
using namespace std;
const double pi = acos(-1.0);
const int N = 1e6+7;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;
typedef unsigned long long ll;
const ll mod = 1e7+9;
ll f[N],size[N];
ll find(ll x){
    if(x!=f[x])
        f[x]=find(f[x]);
    return f[x];
}
intmain () { 
    iOS :: sync_with_stdio ( to false ); 
    cin.tie ( 0 ); cout.tie ( 0 ); 
    LL n-, m; CIN >> >> n- m; 
    LL ANS = (__ int128) (N- . 1 ) n-* / 2 * (N- 2 ) * (N- . 3 ) / 12 is ; // number of combinations 
    for (LL I = 0 ; I <N; I ++ ) { 
        F [I] = I; 
        size [I] = . 1 ; 
    } 
    LL SUM = n-; // record how many combinations of two of the program has (in this case repeatable) 
    COUT << << ANS "\n";
    for(ll i=1;i<=m;i++){
        ll x,y; cin>>x>>y;
        ll xx=find(x); ll yy=find(y);
        if(xx!=yy){
            sum-=(size[xx]*size[xx]);
            sum-=(size[yy]*size[yy]);
            ll tmp=size[xx]*size[yy]*((n-size[xx]-size[yy])*(n-size[xx]-size[yy])-sum)/2;
            f[xx]=yy;
            size[yy]+=size[xx];
            sum+=(size[yy]*size[yy]);
            ans-=tmp;            
        }
        cout<<ans<<"\n";
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/wmj6/p/11359841.html