More than 2019 cattle off summer school camp (ninth field) -E All men are brothers

Topic link: https: //ac.nowcoder.com/acm/contest/889/E

The meaning of problems: n individual, m operations, combined two each, the combined seek to identify individuals 4, and the two programs are not the two numbers of a set.

Ideas: the beginning has C [n] [4] kinds of programs, we calculated each time the merger reduced the number how many programs can be. Reducing the product of the present two times the combined size of the set, multiplied by the number of the selected program is not in a set of two from the remaining set, which can be calculated by an arbitrary number of two programs, subtracting, in the same number two sets of programs available. Size set by disjoint-set operation and maintenance, and recording the selected prefix and thinking in this case two sets of the same program number sum. Pretreatment number of combinations obtained only with a pretreatment to C (n, 4) can be. The total complexity is O (mlogn).

AC Code:

#include<cstdio>
#include<algorithm>
#include<map>
using namespace std;

typedef long long LL;
const int maxn=1e5+5;
int n,m,root[maxn],siz[maxn];
LL ans,sum,C[maxn][5];

void init(){
    C[1][0]=1,C[1][1]=1;
    C[2][0]=1,C[2][1]=2,C[2][2]=1;
    C[3][0]=1,C[3][1]=3,C[3][2]=3,C[3][3]=1;
    for(int i=4;i<maxn;++i){
        C[i][0]=1;
        for(int j=1;j<=4;++j)
            C[i][j]=C[i-1][j-1]+C[i-1][j];
    }
}

int getr(int k){
    if(root[k]==k) return k;
    else return root[k]=getr(root[k]);
}

int main(){
    init();
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;++i)
        root[i]=i,siz[i]=1;
    ans=C[n][4];
    printf("%lld\n",ans);
    while(m--){
        int x,y;
        scanf("%d%d",&x,&y);
        int xr=getr(x),yr=getr(y);
        if(xr==yr){
            printf("%lld\n",ans);
            continue;
        }
        LL tx=0,ty=0;
        if(siz[xr]>=2) tx=C[siz[xr]][2];
        if(you [although]> =2) Ty = C [you [yr]] [ 2 ]; 
        sum - = tx + ty; 
        ans - = 1LL * you [xr] * you [yr] * (C [n-you [xr] -you [yr]] [ 2 ] - sum); 
        printf ( " % LLD \ n " , ans); 
        root [yr] = xr; 
        you [PR] + = you [yr]; 
        sum + = C [you [xr]] [ 2 ]; 
    } 
    Return  0 ; 
}

 

Guess you like

Origin www.cnblogs.com/FrankChen831X/p/11362450.html