2020HDU多校第三场 1005 Little W and Contest
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
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).
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.
#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; }