题意:现在有n个集合 每个集合大小为1 现在你可以把集合合并m次 每次会告诉你哪个集合合并 让你输出每次从不同的四个集合里各选出四个的组合方案

思路:我们可以想到用并查集模拟集合的合并 对于方案数 我们可以发现 其实就是合并之前的答案 减掉两个集合内的数的组合的方案数(详情理解代码)

#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];
}
int main(){
ios::sync_with_stdio(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; //组合数
for(ll i=0;i<N;i++){
f[i]=i;
size[i]=1;
}
ll sum=n; //记录有多少种两两组合的方案(这里是可重复的)
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;
}

2019牛客暑期多校训练营(第九场)E.All men are brothers(并查集+排列组合)的更多相关文章

  1. 2019牛客暑期多校训练营(第九场) D Knapsack Cryptosystem

    题目 题意: 给你n(最大36)个数,让你从这n个数里面找出来一些数,使这些数的和等于s(题目输入),用到的数输出1,没有用到的数输出0 例如:3  4 2 3 4 输出:0 0 1 题解: 认真想一 ...

  2. 2019牛客暑期多校训练营(第二场) H-Second Large Rectangle(单调栈)

    题意:给出由01组成的矩阵,求求全是1的次大子矩阵. 思路: 单调栈 全是1的最大子矩阵的变形,不能直接把所有的面积存起来然后排序取第二大的,因为次大子矩阵可能在最大子矩阵里面,比如: 1 0 0 1 ...

  3. 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题)

    layout: post title: 2019牛客暑期多校训练营(第五场)G - subsequeue 1 (一题我真的不会的题) author: "luowentaoaa" c ...

  4. 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)

    题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9:  对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可.     后者mod=1e9,5才 ...

  5. [状态压缩,折半搜索] 2019牛客暑期多校训练营(第九场)Knapsack Cryptosystem

    链接:https://ac.nowcoder.com/acm/contest/889/D来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言52428 ...

  6. 2019牛客暑期多校训练营(第一场)A题【单调栈】(补题)

    链接:https://ac.nowcoder.com/acm/contest/881/A来源:牛客网 题目描述 Two arrays u and v each with m distinct elem ...

  7. 2019牛客暑期多校训练营(第一场) B Integration (数学)

    链接:https://ac.nowcoder.com/acm/contest/881/B 来源:牛客网 Integration 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 5242 ...

  8. 2019牛客暑期多校训练营(第一场) A Equivalent Prefixes ( st 表 + 二分+分治)

    链接:https://ac.nowcoder.com/acm/contest/881/A 来源:牛客网 Equivalent Prefixes 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/ ...

  9. 2019牛客暑期多校训练营(第二场)F.Partition problem

    链接:https://ac.nowcoder.com/acm/contest/882/F来源:牛客网 Given 2N people, you need to assign each of them ...

  10. 2019牛客暑期多校训练营(第八场)E.Explorer

    链接:https://ac.nowcoder.com/acm/contest/888/E来源:牛客网 Gromah and LZR have entered the fifth level. Unli ...

随机推荐

  1. Tomca7t服务器 配置HTTP和HTTPS 同时访问

    (首先你要有 ssl 证书 ,我是阿里的 ) 查看申请ssl证书(https://www.cnblogs.com/lxf-mw/p/14261303.html) 一.下载 tomcat证书(两个文件) ...

  2. Jenkins-自动部署,备份

    Jenkins-自动部署,备份 问题导入: 环境: CentOS 7,   Tomcat 8.5,   Jdk 1.8,   Maven 3.25 ,Jenkins war包 2.x 原因: 每次部署 ...

  3. Approach for Unsupervised Bug Report Summarization 无监督bug报告汇总方法

    AUSUM: approach for unsupervised bug report summarization 1. Abstract 解决的bug被归类以便未来参考 缺点是还是需要手动的去细读很 ...

  4. Hbase snapshot数据迁移

    # 在源集群中创建快照(linux shell) hbase snapshot -t <table_name> -n <snapshot_name> 或(hbase shell ...

  5. Error: Could not open input file: /usr/java/jdk1.7.0_07/jre/lib/jsse.pack

    [root@localhost ~]# rpm -ivh jdk-7u7-linux-i586.rpm Preparing... ################################### ...

  6. update 表名 set 某列名=now() where user in('user1','user2','user3');

    update 表名  set 某列名=now() where user in('user1','user2','user3');

  7. 攻防世界 - Web(三)

    PHP2: 1.进入页面,进行抓包或后台扫描都没有什么发现,然后网上查一波wp,发现是关于.phps文件,进入index.phps,弹出一段代码,查看源代码, <?php if("ad ...

  8. library cache pin解决方法

    library cache pin大部分都是因为编译存储过程造成的 查找造成问题的数据库对象(一般为存储过程) SELECT * FROM v$session_wait WHERE event = ' ...

  9. 1.8V转5V电平转换芯片,1.8V转5V的电源芯片

    1.8V是一个比较低的电压,在电压供电电压中,1.8V电压的过于小了,在一些电子模块或者MCU中,无法达到供电电压,和稳压作用,PW5100就是可以在1.8V转5V的电平转换电路和芯片,最大可提供50 ...

  10. 关于springboot2.X使用外部tomcat服务器进行部署的操作详细步骤

    1.修改pom.xml文件(4个地方) ①<packaging>war</packaging>将其中的jar该为war ②<dependency> <grou ...