51 nod 1253 Kundu and Tree

树包含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;
	
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值