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. 认识Eureka (F版)

    Spring Cloud 为开发者提供了在分布式系统中的一些常用的组件(例如配置管理,服务发现,断路器,智能路由,微代理,控制总线,一次性令牌,全局锁定,决策竞选,分布式会话集群状态).使用Sprin ...

  2. (八十九)c#Winform自定义控件-自定义滚动条(treeview、panel、datagridview、listbox、listview、textbox)

    官网 http://www.hzhcontrols.com/ 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kw ...

  3. eIQ WSL下工具及环境配置

    1. 配置WSL 参考[https://www.cnblogs.com/hayley111/p/12844337.html] 2. 配置VScode 参考[https://zhuanlan.zhihu ...

  4. 【error fixed】E: Package 'oracle-java8-installer' has no installation candidate

    问题:安装oracle-java8-installer按照如下指南失败: How To Install Java with Apt-Get on Ubuntu 16.04[https://www.di ...

  5. 个人对于flask中蓝图的理解

    什么是蓝图? 蓝图可以理解为,是一种对项目中的代码进行模块化管理的工具,相当于python中的包为什么要使用蓝图? 在一个py文件中具有多个功能代码,不利于维护和管理. 如果在其他的模块中去调用视图函 ...

  6. Web前端开发未来的六大趋势

    说起Web前端开发想必你一定不会陌生,因为现在的前端开发学习的培训机构也是层出不穷.下面济南优就业IT培训给大家总结出了未来Web前端开发的六大趋势从中可以大致看出来Web前端未来的发展前景. 趋势一 ...

  7. 调试HotSpot源代码

    之前的文章在Ubuntu 16.04上编译OpenJDK8的源代码 已经介绍过在Ubuntu上编译OpenJDK8的源代码,这一篇将介绍在Ubuntu上调试OpenJDK8源代码的2种方式. 1.GD ...

  8. 线性动归之Wooden Sticks

    题面:现在有n(n<5000)个木头,每个木头都有长度l和重量w(l<10000,w<10000),现在你要对木头进行加工: 1.第一根木头需要先花费1min: 2.加工完第i跟木头 ...

  9. Python and or not 优先级

    not > and >or 1 or 5 and 4: -> 1 or 4-> 1 (1 or 5) and 4: ->1 and 4 ->4 x or y . x ...

  10. 数据可视化基础专题(五):Pandas基础(四) 生成对象

    引言 先介绍下 Pandas 的数据结构,毕竟数据结构是万物的基础. Pandas 有两种主要的数据结构: Series 和 DataFrame 模块导入 首先我们在代码中引入 Pandas 和 Nu ...