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 ;
}

最新文章

  1. Python 小白的新手教程(一)
  2. Html中自定义鼠标的形状
  3. 移动端Web适配的两种做法思路总结
  4. bzoj1854 游戏
  5. [转载]能不能同时用static和const修饰类的成员函数?
  6. XML标签
  7. Nginx学习笔记(八) Nginx进程启动分析
  8. AngularJS 初识笔记
  9. 用R在字符串中提取匹配的部分
  10. 新浪博客如何显示高亮代码,DIY
  11. 《Linux/Unix系统编程手册》读书笔记3
  12. 关于Extjs使用window.opener报错
  13. (转) oc static extern 和const
  14. js Date扩展Format()函数
  15. Documention
  16. 201521123005 《Java程序设计》 第十二周学习总结
  17. js自调用函数的实现方式
  18. jQuery Ajax 使用 ($.ajax、$.post、$.get)
  19. [No0000166]CPU的组成结构及其原理
  20. ZooKeeper单机伪集群搭建与启动

热门文章

  1. 谈谈javaSE中的==和equals的联系与区别
  2. 一.1搭建跨平台的统一python开发环境
  3. 07 . Kubernetes之Service
  4. C# 基于内容电影推荐项目(一)
  5. tabBar配置和修改
  6. C/C++编程语言制作《游戏内存外挂》
  7. WPF基于.Net Core
  8. HDFS客户端环境准备
  9. java 面向对象(四十二):反射(六)反射应用三:调用运行时类的指定结构
  10. bzoj2697特技飞行*