All men are brothers

All men are brothers

Cattle off more school ninth field E

Given n people, at first did not know each

Then each stage m

Each stage has two x, y know

Each stage of seeking ways to select four people did not know each

Disjoint-set

 

#include <bits / STDC ++ H.>
 the using  namespace STD; 
typedef unsigned Long  Long LL; 
LL F [ 100004 ]; 
LL SIZ [ 100005 ];
 /// maintain the current size of the connected components 

LL n-, m; 
LL K; 
LL (TM) = 0 ; /// all connected components sized square sum 
void the init () 
{ 
    for (I = LL . 1 ; I <= n-; I ++ ) { 
        F [I] = I; 
        SIZ [I] = . 1 ; 
        (TM) ++ ; 
    } 
} 
LL CAL (n-LL) ///算C (n,4)
{
   ll ans=(ll)(((n*(n-1))/2*(n-2))/3*(n-3))/4;
   return ans;
}
ll get(int x)
{
    return f[x]=(f[x]==x? x:get(f[x]));
}
ll uni(int x,int y)
{
    int a=get(x);
    int b=get(y);
    if(a==b)return 0;///The number of branches of the same communication, to answer no contribution
     /// two connected components merge
     /// impact of: the original answer, you might choose a part of a, b, a part of the case, so subtract this part of the case
     /// i.e. a selected a, b select one of the remaining communication branch selected other two
     /// contribution that is generated siz [a] * siz [b ] * is selected from two other branches
     /// other branches count , i.e., (the number of squares remaining elements - the square of the number of elements and the other branches) / 2 
    (TM) - = (SIZ [a] * SIZ [a] + SIZ [B] * SIZ [B]); 
    LL _ = SIZ [A] * SIZ [B]; 
    SIZ [B] + = SIZ [A]; 
    LL O = N- SIZ [B]; 
    O = (O-O * (TM)) / 2 ; 
    _ * = O; 
    SIZ [A] = 0 ; 
    F [A] = B; 
    K -;
    tm+=siz[b]*siz[b];
    return _;
}
int main()
{
    scanf("%lld%lld",&n,&m);
    init();
    K=n;
    ll _y=cal(n);
    cout<<_y<<'\n';
    ll x,y,t;
    for(int i=0;i<m;i++){
        scanf("%lld%lld",&x,&y);
     
        if(K<4)cout<<0<<'\n';
        else{
           ll t= uni(x,y);
            _y-=t;
            cout<<_y<<'\n';
        }
    }

}

 

Guess you like

Origin www.cnblogs.com/liulex/p/11359678.html