树包含N个点和N-1条边。树的边有2中颜色红色(‘r’)和黑色(‘b’)。给出这N-1条边的颜色,求有多少节点的三元组(a,b,c)满足:节点a到节点b、节点b到节点c、节点c到节点a的路径上,每条路径都至少有一条边是红色的。
注意(a,b,c), (b,a,c)以及所有其他排列被认为是相同的三元组。输出结果对1000000007取余的结果。
找补集,快乐容斥一下就好了
#include<bits/stdc++.h>
#define re return
#define inc(i,l,r) for(int i=l;i<=r;++i)
using namespace std;
template<typename T>inline void rd(T&x)
{
char c;bool f=0;
while((c=getchar())<'0'||c>'9')if(c=='-')f=1;
x=c^48;
while((c=getchar())>='0'&&c<='9')x=x*10+(c^48);
if(f)x=-x;
}
const int maxn=50005;
#define ll long long
ll n,m,cnt,hd[maxn],vis[maxn];
struct edge{
int to,nt;
}e[maxn<<1];
ll k,mod=1000000007;
inline void add(int x,int y)
{
e[++k].to=y;e[k].nt=hd[x];hd[x]=k;
e[++k].to=x;e[k].nt=hd[y];hd[y]=k;
}
inline void dfs(int x,int fa)
{
++cnt;
vis[x]=1;
for(int i=hd[x];i;i=e[i].nt)
{
int v=e[i].to;
if(!vis[v])dfs(v,x);
}
}
char qwq[10];
int main()
{
freopen("in.txt","r",stdin);
int x,y;
rd(n);
inc(i,2,n)
{
rd(x),rd(y);
scanf("%s",qwq);
if(qwq[0]=='b')
//只加黑边
add(x,y);
}
ll ans=0;
inc(i,1,n)
if(!vis[i])
{
cnt=0;
dfs(i,i);
ans=(ans+cnt*(cnt-1)/2*(n-cnt)+cnt*(cnt-1)*(cnt-2)/6)%mod;
//容斥
}
ans=n*(n-1)*(n-2)/6-ans;
printf("%lld",ans%mod);
re 0;
}