All men are brothers

All men are brothers

牛客多校第九场E

给定n个人,起初互不认识

然后m各阶段

每个阶段有两个人x、y认识

求每个阶段选出四个人互不认识的方式

并查集

 

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
ll f[100004];
ll siz[100005];
///维护当前连通分支大小

ll n,m;
ll K;
ll tm=0;///所有连通分支大小的平方求和
void init()
{
    for(ll i=1;i<=n;i++){
        f[i]=i;
        siz[i]=1;
        tm++;
    }
}
ll cal(ll n)///算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;///连通分支个数不变,对答案不产生贡献
    ///两个连通分支合并
    ///产生的影响:原来答案中,可能选了一个属于a,一个属于b的情况,所以减去这部分情形
    ///即a中选一个,b中选一个,剩下的其他连通分支中选2个
    ///产生的贡献即为siz[a]*siz[b]*其他分支选两个
    ///其他分支计数,即(剩余的元素的个数的平方-其他分支的元素个数平方之和)/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';
        }
    }

}

 

转载于:https://www.cnblogs.com/liulex/p/11359678.html

马丁路德金的"I have a dream"节选存放在"freedom.txt"中: I have a dream that one day this nation will rise up, live up to the true meaning of its creed: “We hold these truths to be self-evident; that all men are created equal.” I have a dream that one day on the red hills of Georgia the sons of former slaves and the sons of former slave-owners will be able to sit down together at the table of br otherhood. I have a dream that one day even the state of Mississippi, a state sweltering with th e heat of injustice, sweltering with the heat of oppression, will be transformed into an oasis of freedom and justice. I have a dream that my four children will one day live in a nation where they will no t be judged by the color if their skin but by the content of their character. I have a dream today. I have a dream that one day down in Alabama with its governor having his lips drippin g with the words of interposition and nullification, one day right down in Alabama li ttle black boys and black girls will be able to join hands with little white boys and white girls as sisters and brothers. I have a dream today. I have a dream that one day every valley shall be exalted, every hill and mountain sh all be made low, the rough places will be made plain, and the crooked places will be made straight, and the glory of the Lord shall be revealed, and all flesh shall see i t together. 编程实现词汇表,计算每一个单词出现的次数,大小写不区分,输出到"dic.txt" 文件保存。
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值