https://vjudge.net/contest/386223#problem/E

There are nn 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 11. The second part contains only coders who code and test programs all the time, and similarly, we define the power of a coder as 22.
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 55 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 AA, BB, and CC, if AA is familiar with BB, and BB is familiar with CC, then AA will be familiar with CC through BB 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)(n−1) introductions.
Now, for i=1,2,…,ni=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)(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)(109+7).

InputThere are several test cases.
The first line contains an integer TT (1≤T≤101≤T≤10), denoting the number of test cases. Then follow all the test cases.
For each test case, the first line contains an integer nn (1≤n≤105)(1≤n≤105), denoting the number of members in this club.
The second line contains nn integers consisting of only 11 and 22, where the ii-th integer represents the power of the ii-th member.
The next (n−1)(n−1) lines describe all introductions in chronological order of occurrence, where each line contains two integers uu and vv (1≤u,v≤n,u≠v)(1≤u,v≤n,u≠v), representing an introduction between the uu-th member and the vv-th member, who are currently strangers to each other.
It is guaranteed that the sum of nn is no larger than 106106.
OutputFor each test case, output nn lines, where the ii-th line contains an integer, denoting the number of combinations of three club members, in modulo (109+7)(109+7), that can form a new team after the first (i−1)(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

Sponsor

题意:

  n个人互不认识,经过一次介绍,两人认识,认识关系具有传递性,n-1次介绍后两联认识;

  每个人都具有属性1或2,从n个人中选择三个人参加比赛,且至少有两个具有属性2,且三个人互不认识。

  问每给一次介绍后有多少种凑出来一个队的组合

思路;

利用并查集,将所有的点分成三个部分(三种连通块),每次去不同块中取不满足的情况,

求出总的方案数

merge先计算贡献。合并集合产生四种情况

去合并的负贡献:每次用前一次的减去不满足的情况个数,得到结果。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include <vector>
#include <iterator>
#include <utility>
#include <sstream>
#include <limits>
#include <numeric>
#include <functional>
using namespace std;
#define gc getchar()
#define mem(a) memset(a,0,sizeof(a))
//#define sort(a,n,int) sort(a,a+n,less<int>()) #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0); typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pii;
typedef char ch;
typedef double db; const double PI=acos(-1.0);
const double eps=1e-6;
const int inf=0x3f3f3f3f;
const int maxn=1e5+10;
const int maxm=100+10;
const int N=2e5+10;
const int mod=1e9+7;
ll n = 0;
ll p[100005] = {0};
ll p1[100005] = {0};
ll p2[100005] = {0};
ll In, K;
ll temp;
ll ans;
ll x, y, k = 0;
ll inv(ll a)
{
return a == 1 ? 1 : (ll)(mod-mod/a)*inv(mod%a)%mod;
}
ll comb(ll n,ll m)
{
if(m < 0)return 0;
if(n < m)return 0;
if(m > n-m) m = n-m;
ll up = 1;
ll down = 1;
for(ll i = 0;i<m;i++)
{
up = up * (n-i) % mod;
down = down * (i+1) % mod;
}
return up*inv(down) %mod;
}
ll find(ll x)
{
if(x != p[x])
return p[x] = find(p[x]);
return x;
} void merge(ll x, ll y)
{
x = find(x);
y = find(y);
p1[y] += p1[x];
p2[y] += p2[x];
p[x] = y;
}
int main()
{
int T = 0;
cin >> T;
while(T--)
{
cin >> n;
In = 0;
K = 0;
for(ll i = 1;i<=n;i++)
{
p[i] = i;
cin >> temp;
if(temp == 1)
{
In += 1;
p1[i] = 1;
p2[i] = 0;
}
else
{
K += 1;
p1[i] = 0;
p2[i] = 1;
}
}
ans = (In*comb(K, 2)%mod + comb(K, 3))%mod; cout << ans <<endl;
for(int i = 1;i<=n-1;i++)
{
k = 0;
cin >> x >> y;
x = find(x);
y = find(y);
k = (k + p2[x]*p2[y] * (In- p1[x]- p1[y]))%mod;
k = (k + p2[x]*p2[y] * (In- p2[x]- p2[y]))%mod;
k = (k + p1[x]*p2[y] * (In- p2[x]- p2[y]))%mod;
k = (k + p2[x]*p1[y] * (In- p2[x]- p2[y]))%mod;
merge(x, y);
ans += mod - k;
ans %= mod;
cout << ans << endl;
}
}
}

  

E - Little W and Contest的更多相关文章

  1. 2020HDU多校第三场 1005 Little W and Contest

    Little W and Contest Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/O ...

  2. hdu 6795 Little W and Contest 并查集+排列组合

    题意: t组输入,有n个人,刚开始谁也不认识谁.每一个人有一个权值w[i](1<=w[i]<=2),你要挑选3个互相不认识的人组成一个队,且要保证3个人权值之和大于等于5(也就意味着最少要 ...

  3. lucene入门创建索引——(二)

    1.程序宏观结构图

  4. hdu-5988 Coding Contest(费用流)

    题目链接: Coding Contest Time Limit: 2000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Ot ...

  5. ZOJ 3703 Happy Programming Contest

    偏方记录背包里的物品.....每个背包的价值+0.01 Happy Programming Contest Time Limit: 2 Seconds      Memory Limit: 65536 ...

  6. 校际联合Contest

    每次开一个坑都像是重新被碾压的预感 最近的新闻,以前很喜欢乔任梁的<复活>...然后他就死了...感觉我再多愁善感一点的话...就要悲伤逆流成河了吧... Contest 09/24(乐滋 ...

  7. 2016 Multi-University Training Contest 1

    8/11 2016 Multi-University Training Contest 1 官方题解 老年选手历险记 最小生成树+线性期望 A Abandoned country(BH) 题意: 1. ...

  8. 2016 Multi-University Training Contest 2

    8/13 2016 Multi-University Training Contest 2官方题解 数学 A Acperience(CYD)题意: 给定一个向量,求他减去一个  α(>=0)乘以 ...

  9. 2016 Multi-University Training Contest 3

    5/11 2016 Multi-University Training Contest 3官方题解 2016年多校训练第三场 老年选手历险记 暴力 A Sqrt Bo(CYD) 题意:问进行多少次开根 ...

  10. 2016 Multi-University Training Contest 4

    6/12 2016 Multi-University Training Contest 4官方题解 KMP+DP A Another Meaning(CYD) 题意: 给一段字符,同时给定你一个单词, ...

随机推荐

  1. 聊一聊 C# NativeAOT 多平台下的函数导出

    一:背景 1. 讲故事 昨晚训练营里有一位朋友提到一个问题,说 C# AOT程序能否编译为一个dll,供其他语言调用,其实这个是完全没有问题的,也确实我的的文章体系中没有涉及到这块,那今天就补充完整吧 ...

  2. 重磅开源 基于AI大语言模型的AI 助手全套开源解决方案 AI开源平台

    介绍 GeekAI 基于AI大语言模型的AI 助手全套开源解决方案,自带运营管理后台,开箱即用.集成了 OpenAI, Claude, 通义千问,Kimi,DeepSeek等多个平台的大语言模型. 基 ...

  3. RBMQ案例二:工作队列模式

    工作队列模式 工作队列(又名:任务队列)背后的主要思想是避免立即执行资源密集型任务而不得不等待它完成.相反,我们安排任务稍后完成.我们将任务封装 为消息并将其发送到队列.在后台运行的工作进程将弹出任务 ...

  4. 「Note」字符串方向 - 自动机相关

    1. AC 自动机 ACAM 1.1. 简介 AC 自动机用于解决多模式串匹配问题,例如求多个模式串在文本串中的出现次数.显著地,它的应用实际上非常广泛. 借助 KMP 的思想,我们对 Trie 树上 ...

  5. P4602 [CTSC2018] 混合果汁

    贪心思想,整体二分+权值线段树解决. \(Step\ 1\) 首先将所有果汁的美味度按从大到小排序,若美味度高的果汁可以满足小朋友的两个需求,则储存答案. \(Step\ 2\) 不断二分果汁,并且枚 ...

  6. 由MySQL的Explain 看闭环 看交付

    今天在业务上有一个列表页的需求,列表页需要进行统计记录的求和情况,也就是需要用到sum语句.如: 自己在date_time 上面加了索引,那么我们对a字段求和的时候是否会使用到索引呢? select ...

  7. Ding!您有一份ChunJun实用指南,请查收

    ChunJun是易用.稳定.高效的批流一体的数据集成框架,主要应用于大数据开发平台的数据同步/数据集成模块,使大数据开发人员可简洁.快速的完成数据同步任务开发,供企业数据业务使用. 本文主要整理Chu ...

  8. 袋鼠云数栈UI5.0体验升级背后的故事:可用性原则与交互升级

    最近,我们袋鼠云的UED部⻔小伙伴们,不声不响地⼲了⼀件⼤事--升级了全新设计语言「数栈UI5.0」. 众所周知,用户在使用产品时,是一个动态的过程,用户和产品之间进行交互的可用性,能否让用户愉悦.快 ...

  9. Cursor 1.2重磅更新,这个痛点终于被解决了!

    大家好,我是程序员鱼皮.分享一个重磅消息,AI 编程工具 Cursor 1.2 版本正式发布了! 感觉最近 Cursor 团队像打了鸡血一样,从 1.0 到 1.1 再到 1.2,短短一个月更新了 2 ...

  10. cmd命令行下怎么禁用和启用网络

    https://jingyan.baidu.com/article/d3b74d64b293525e76e6092a.html 执行netsh命令. interface show interface ...