2020杭电多校第三场 Little W and Contest(并查集,组合数学)

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

Source
2020 Multi-University Training Contest 3

题意:
n个点。每个点有权值1或者2。
组成三元组的条件是至少两个2,且相互之间不可达。
n个点依次加边,最后成为一棵树,求初始结果和每次加边后的结果。

思路:
初始三元组的个数为
B ∗ ( B − 1 ) ∗ A / 2 ; B * (B - 1) * A / 2; B(B1)A/2;
B ∗ ( B − 1 ) ∗ ( B − 2 ) / 6 ; B * (B - 1) * (B - 2) / 6; B(B1)(B2)/6;
B代表权值为2点的个数,A代表权值为1的点的个数

维护并查集内权值1点的数目和权值2点的数目。
那么合并两个并查集减少的三元组个数为
A 2 ∗ B 2 ∗ ( A − A 1 − B 1 ) ; A2 * B2 * (A - A1 - B1); A2B2(AA1B1);
A 2 ∗ B 1 ∗ ( B − A 2 − B 2 ) ; A2 * B1 * (B - A2 - B2); A2B1(BA2B2);
A 1 ∗ B 2 ∗ ( B − A 2 − B 2 ) ; A1 * B2 * (B - A2 - B2); A1B2(BA2B2);
A 2 ∗ B 2 ∗ ( B − A 2 − B 2 ) ; A2 * B2 * (B - A2 - B2); A2B2(BA2B2);

A1,A2代表第一个并查集的点
B1,B2代表第二个并查集的点

#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

typedef long long ll;
const int maxn = 1e5 + 7;
const int mod = 1e9 + 7;

int fa[maxn];
ll sizA[maxn],sizB[maxn];
ll A,B,N,ans;

int findset(int x) {
    if(x == fa[x]) return x;
    return fa[x] = findset(fa[x]);
}

void get(int rx,int ry) {
    ll A1 = sizA[rx],A2 = sizB[rx];
    ll B1 = sizA[ry],B2 = sizB[ry];
    ans -= A2 * B2 * (A - A1 - B1);
    ans -= A2 * B1 * (B - A2 - B2);
    ans -= A1 * B2 * (B - A2 - B2);
    ans -= A2 * B2 * (B - A2 - B2);
}

void Union(int x,int y) {
    int rx = findset(x),ry = findset(y);
    if(rx != ry) {
        fa[rx] = ry;
        get(rx,ry);
        sizA[ry] += sizA[rx];
        sizB[ry] += sizB[rx];
    }
}

int main() {
    int T;scanf("%d",&T);
    while(T--) {
        scanf("%d",&N);
        A = B = ans = 0;
        for(int i = 1;i <= N;i++) {
            int x;scanf("%d",&x);
            fa[i] = i;
            if(x == 1) {
                A++;
                sizA[i] = 1;
                sizB[i] = 0;
            } else {
                B++;
                sizA[i] = 0;
                sizB[i] = 1;
            }
        }
        if(B >= 1) ans += B * (B - 1) * A / 2;
        if(B >= 2) ans += B * (B - 1) * (B - 2) / 6;
        
        printf("%lld\n",ans % mod);
        for(int i = 1;i < N;i++) {
            int x,y;scanf("%d%d",&x,&y);
            Union(x,y);
            printf("%lld\n",ans % mod);
        }
    }
    return 0;
}

你可能感兴趣的:(#,其他比赛题目,#,并查集,#,组合数)