多校第三场——1005 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

题意:n个人互不认识,经过一次介绍,两人认识,且a对b认识,b对c认识,则a对c认识,每个人都具有技能1或2,从n个人中选择三个人参加比赛,且至少有两个具有技能2,且三个人互不认识。

思路:第一天都不认识,可以选2 2 1和2 2 2。
后面的几天,利用并查集,可以将人分成三种,当天认识的u,v和其他的w,因为u和v肯定是不互相认识的,所以认识u的,认识v的,和其他的人w,很明显当有人认识后就会出现需要除去的方案,分别从u,v,w中选
也就是这四种情况
在这里插入图片描述
代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod = 1e9 + 7;
const int N = 1e5 + 5;
int T, n;
int p[N], p1[N], p2[N];
int f(int x)
{
	if (p[x] != x)
	{
		p[x] = f(p[x]);
	}
	return p[x];
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin >> T;
	while (T--)
	{
		cin >> n;
		int n1 = 0;
		int n2 = 0;
		for (int i = 1; i <= n; i++)
		{
			int temp;
			cin >> temp;
			p[i] = i;
			if (temp == 1)
			{
				n1++;
				p1[i] = 1;
				p2[i] = 0;
			}
			else
			{
				n2++;
				p2[i] = 1;
				p1[i] = 0;
			}
		}
		int ans = ((ll)n2*(n2 - 1)*(n2 - 2) / 6 % mod + (ll)n1*n2%mod*(n2 - 1) / 2 % mod) % mod;
		cout << ans << endl;
		for (int i = 1; i < n; i++)
		{
			int num = 0;
			int u;
			int v;
			cin >> u >> v;
			int a = f(u);
			int b = f(v);
			num = (num + (ll)p1[a] * p2[b] * (n2 - p2[a] - p2[b])) % mod;
			num = (num + (ll)p2[a] * p1[b] * (n2 - p2[a] - p2[b])) % mod;
			num = (num + (ll)p2[a] * p2[b] * (n2 - p2[a] - p2[b])) % mod;
			num = (num + (ll)p2[a] * p2[b] * (n1 - p1[a] - p1[b])) % mod;
			ans = (ans - num + mod) % mod;
			p[b] = a;
			p1[a] += p1[b];
			p2[a] += p2[b];
			p1[b] = 0;
			p2[b] = 0;
			cout << ans << endl;
		}

	}
}

猜你喜欢

转载自blog.csdn.net/weixin_43330910/article/details/107738106