Little W and Contest

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0

Problem Description
There are n members in our ACM club. Little W wants to select three persons from our club to form a new team taking part in provincial ACM contests, as it is known by all of us that any ACM contest requires a normal team to have three members.

Little W has divided our club members into two role groups. The first group contains only readers who dedicate themselves to reading problems during contests, though sometimes they may also prepare drinking and food for the team. For the sake of measurement, we define the power of a reader as 1. The second part contains only coders who code and test programs all the time, and similarly, we define the power of a coder as 2.

Little W thinks it will be a tremendous disaster when a team has two readers because in that case, the total power of this team is less than 5 and thus it has a high risk to fail the contest. To avoid that, Little W thinks a new team must have at least two coders.

Additionally, Little W defines the relationship between club members with transitivity. That is, for every three members A, B, and C, if A is familiar with B, and B is familiar with C, then A will be familiar with C through B instantly. Based on the definition, it is forbidden for the team to have any two members familiar with each other.

At first, no member of our club is familiar with any other, and then Little W will repeatedly make an introduction between two members who are currently strangers to each other until each member is familiar with all the others. During this process, there will be exactly (n−1) introductions.

Now, for i=1,2,…,n, Little W wants you to count the combinations of three club members that can form a new team after the first (i−1) introductions have been made. However, the numbers of combinations may be quite gigantic, so you just need to report each number in modulo (109+7).

 
Input
There are several test cases.

The first line contains an integer T (1≤T≤10), denoting the number of test cases. Then follow all the test cases.

For each test case, the first line contains an integer n (1≤n≤105), denoting the number of members in this club.

The second line contains n integers consisting of only 1 and 2, where the i-th integer represents the power of the i-th member.

The next (n−1) lines describe all introductions in chronological order of occurrence, where each line contains two integers u and v (1≤u,v≤n,u≠v), representing an introduction between the u-th member and the v-th member, who are currently strangers to each other.

It is guaranteed that the sum of n is no larger than 106.

 
Output
For each test case, output n lines, where the i-th line contains an integer, denoting the number of combinations of three club members, in modulo (109+7), that can form a new team after the first (i−1) introductions have been made.
 
Sample Input
1
5
2 2 2 1 1
4 5
1 4
2 1
3 2
 
Sample Output
7
7
3
0
0
题意:
ACM俱乐部有n名成员,每个成员可以担当读题或者编码的任务,读题用1表示,编码用2表示,一个队伍有三名成员,其中至少有两名编码选手,刚开始大家互不熟悉,每过一天就会有两个人变得相互熟悉,且双方会熟悉对方所熟悉的人,大概就是朋友的朋友就是我的朋友的意思,问从大家都不熟悉开始,到接下来n-1天,每一天能组队的方案数,输出mod 1e9+7后的结果。
思路:
考虑并查集,fat表示该成员所属的集合,s1表示该集合内读题选手的总人数,s2表示该集合内编码选手的总人数,每次找到两名在今天熟悉的选手的各自所属集合,考虑两名选手的身份减去相应的方案数,然后将两名选手所属的集合合并。
#include<bits/stdc++.h>

#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repp(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define perr(i,a,b) for(int i=a;i>b;i--)
#define pb push_back
#define eb push_back
#define mst(a,b) memset(a,b,sizeof(a))
using namespace std; typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF=0x3f3f3f3f;
const ll LINF=0x3f3f3f3f3f3f3f3f;
const double eps=1e-;
const double PI=acos(-1.0);
const double angcst=PI/180.0;
const ll mod=1e9+;
ll max_3(ll a,ll b,ll c){if(a>b&&a>c)return a;if(b>c)return b;return c;}
ll min_3(ll a,ll b,ll c){if(a<b&&a<c)return a;if(b<c)return b;return c;}
ll gcd(ll a,ll b){return b==?a:gcd(b,a%b);}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll qpow(ll a,ll n){ll r=;while(n){if(n&)r=(r*a)%mod;n>>=;a=(a*a)%mod;}return r;}
ll qmul(ll a,ll b){ll s=(long double)a/mod*b;s=a*b-s*mod;if(s<)s+=mod;if(s>=mod)s-=mod;return s;} template <typename _Tp> inline _Tp read(_Tp&x){
char c11=getchar(),ob=;x=;
while(c11^'-'&&!isdigit(c11))c11=getchar();if(c11=='-')c11=getchar(),ob=;
while(isdigit(c11))x=x*+c11-'',c11=getchar();if(ob)x=-x;return x;
} const int maxn=1e5+;
int fat[maxn],s1[maxn],s2[maxn],a[maxn];
ll cnt1,cnt2; int find(int x){return fat[x]==x?x:find(fat[x]);}
int main()
{
multiCase
{
cnt1=cnt2=;
int n;
read(n);
rep(i,,n)
{
read(a[i]);
if(a[i]==)s1[i]=,s2[i]=,cnt1++;
else s2[i]=,s1[i]=,cnt2++;
fat[i]=i;
}
ll ans=cnt2*(cnt2-)*(cnt2-)/+cnt2*(cnt2-)*cnt1/;
printf("%lld\n",ans%mod);
repp(j,,n)
{
int u,v;
read(u);read(v);
u=find(u);v=find(v);//找到u和v所属的集合
ans-=s2[u]*s2[v]*(cnt2-s2[u]-s2[v]);//这两个人都是2,所选为“2、2、2 ”的情况
ans-=s2[u]*s2[v]*(cnt1-s1[u]-s1[v]);//这两个人都是2,所选为“2、2、1 ”的情况
ans-=s1[u]*s2[v]*(cnt2-s2[u]-s2[v]);//两个人分别是1、2,所选为“1、2、2 ”的情况
ans-=s2[u]*s1[v]*(cnt2-s2[u]-s2[v]); //两个人分别是2、1,所选为“2、1、2 ”的情况
fat[u]=v;//将这两个人所处的集合合并 ,两集合中含1和含2的总人数也要合并
s1[v]+=s1[u];
s2[v]+=s2[u];
printf("%lld\n",ans%mod);
}
} return ;
}

2020HDU多校第三场 1005 Little W and Contest的更多相关文章

  1. 2014多校第三场1005 || HDU 4891 The Great Pan(模拟)

    题目链接 题意 : 给你n行字符串,问你有多少种理解方式.有两大类的理解 (1){A|B|C|D|...}代表着理解方式可以是A,可以是B或C或者D. (2)$blah blah$,在$$这两个符号中 ...

  2. 2018 HDU多校第三场赛后补题

    2018 HDU多校第三场赛后补题 从易到难来写吧,其中题意有些直接摘了Claris的,数据范围是就不标了. 如果需要可以去hdu题库里找.题号是6319 - 6331. L. Visual Cube ...

  3. 牛客多校第三场 F Planting Trees

    牛客多校第三场 F Planting Trees 题意: 求矩阵内最大值减最小值大于k的最大子矩阵的面积 题解: 矩阵压缩的技巧 因为对于我们有用的信息只有这个矩阵内的最大值和最小值 所以我们可以将一 ...

  4. 牛客多校第三场 G Removing Stones(分治+线段树)

    牛客多校第三场 G Removing Stones(分治+线段树) 题意: 给你n个数,问你有多少个长度不小于2的连续子序列,使得其中最大元素不大于所有元素和的一半 题解: 分治+线段树 线段树维护最 ...

  5. 2019 牛客暑期多校 第三场 F Planting Trees (单调队列+尺取)

    题目:https://ac.nowcoder.com/acm/contest/883/F 题意:求一个矩阵最大面积,这个矩阵的要求是矩阵内最小值与最大值差值<=m 思路:首先我们仔细观察范围,我 ...

  6. 2018多校第三场 hdu6331 M :Walking Plan

    题目链接 hdu6331 自我吐槽,这场多校大失败,开局签到因输入输出格式写错,wa了3发.队友C题wa了1个小时,还硬说自己写的没错,结果我随便造了个小数据,他都没跑对.然后跑对了后又进入了无限的卡 ...

  7. hdu-4893-Wow! Such Sequence!-线段树【2014多校第三场-J】

    题意:一个初始为0的数组,支持三种操作:1.向第k个数添加d,(|d| < 2^31);2.把[l, r]区间内的数字都换成与它最相近的Fibonacci数;3.询问[l, r]区间的和. 思路 ...

  8. 2018 Multi-University Training Contest 3 杭电多校第三场

    躺了几天 终于记得来填坑了 1001 Ascending Rating   (hdoj 6319) 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6319 ...

  9. 2019年杭电多校第三场 1011题Squrirrel(HDU6613+树DP)

    题目链接 传送门 题意 给你一棵无根树,要你寻找一个根节点使得在将一条边权变为\(0\)后,离树根最远的点到根节点的距离最小. 思路 本题和求树的直径很像,不过要记得的东西有点多,且状态也很多. \( ...

随机推荐

  1. spring boot 整合Thymeleaf模板

    SpringBoot 是为了简化 Spring 应用的创建.运行.调试.部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖 ...

  2. Xor_Sum 题解

    题目 You are given a positive integer \(N(1≦N≦10^{18})\). Find the number of the pairs of integers \(u ...

  3. day12 文件操作(下)

    目录 一.x模式(控制文件操作模式,与rwa同级) 1 特点 2 格式 二.b模式(控制文件读写内容的模式,与t同级) 1.b模式和t模式的区别 2 b模式应用 3 循环读取文件 三.文件操作的其他方 ...

  4. electron自定义最小化,最大化和关闭按钮

    Electron ipcRenderer 模块 ipcRenderer 模块是一个 EventEmitter 类的实例. 它提供了有限的方法,你可以从渲染进程向主进程发送同步或异步消息. 也可以收到主 ...

  5. 安装archlinux

    arch安装步骤 archlinux官方安装wiki 1.分区and格式化分区      (分区用fdisk,格式化分区用mkfs) 2.挂载分区  (mount命令) 3.安装archlinux   ...

  6. scrapy 基础组件专题(十二):scrapy 模拟登录

    1. scrapy有三种方法模拟登陆 1.1直接携带cookies 1.2找url地址,发送post请求存储cookie 1.3找到对应的form表单,自动解析input标签,自动解析post请求的u ...

  7. 数据可视化基础专题(七):Pandas基础(六) 数据增删改以及相关操作

    首先第一部还是导入 Pandas 与 NumPy ,并且要生成一个 DataFrame ,这里小编就简单的使用随机数的形式进行生成,代码如下: import numpy as np import pa ...

  8. python 装饰器(四):装饰器基础(三)叠放装饰器,参数化装饰器

    叠放装饰器 示例 7-19 演示了叠放装饰器的方式:@lru_cache 应用到 @clock 装饰fibonacci 得到的结果上.在示例 7-21 中,模块中最后一个函数应用了两个 @htmliz ...

  9. Tomcat的基本使用及相关知识的概述(超详细版)

    絮絮叨叨: 在深入了解Tomcat源码之前,本来是想亲自写一篇Tomcat的基本使用教程的,在网上兜兜转转发现了这篇博客:https://blog.csdn.net/weixin_40396459/a ...

  10. JavaScript图形实例:阿基米德螺线

    1.阿基米德螺线 阿基米德螺线亦称“等速螺线”.当一点P沿动射线OP以等速率运动的同时,该射线又以等角速度绕点O旋转,点P的轨迹称为“阿基米德螺线”. 阿基米德螺线的笛卡尔坐标方程式为: r=10*( ...