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 AB, 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 (n1) 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 (i1) 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 (1T10), denoting the number of test cases. Then follow all the test cases.

For each test case, the first line contains an integer n (1n105), 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 (n1) lines describe all introductions in chronological order of occurrence, where each line contains two integers u and v (1u,vn,uv), 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 (i1) 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-12;
const double PI=acos(-1.0);
const double angcst=PI/180.0;
const ll mod=1e9+7;
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==0?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=1;while(n){if(n&1)r=(r*a)%mod;n>>=1;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<0)s+=mod;if(s>=mod)s-=mod;return s;}


template <typename _Tp> inline _Tp read(_Tp&x){
    char c11=getchar(),ob=0;x=0;
    while(c11^'-'&&!isdigit(c11))c11=getchar();if(c11=='-')c11=getchar(),ob=1;
    while(isdigit(c11))x=x*10+c11-'0',c11=getchar();if(ob)x=-x;return x;
}





const int maxn=1e5+7;
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=0;
        int n;
        read(n);
        rep(i,1,n)
        {
            read(a[i]);
            if(a[i]==1)s1[i]=1,s2[i]=0,cnt1++;
            else s2[i]=1,s1[i]=0,cnt2++;
            fat[i]=i;
        }
        ll ans=cnt2*(cnt2-1)*(cnt2-2)/6+cnt2*(cnt2-1)*cnt1/2;
        printf("%lld\n",ans%mod);
        repp(j,1,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 0;
}

 

版权声明:本文为chuliyou原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/chuliyou/p/13393812.html