CF 852E Casinos and travel

题目链接

\(Desccription\)
给定一棵树,John从任意一个点开始,每次走向一个未到达过的点。每个点都可以有或没有赌场,每经过一个赌场心情都会反转,旅行开始前心情很好。
问有多少种方案使得旅行结束后心情很好。

\(n≤10^5\)


\(Solution\)
把题目抽象一下:
每个点随机一个\(0/1\)的权值,随机选择一个点作为根,有多少种方案使得根节点到所有叶节点路径上的异或和为\(0\)

发现叶节点到根的路径的权值异或和可以由叶节点确定。记叶节点数为\(tot\)
1.若所选的根节点不是叶节点,那么除叶节点外的\(n-tot\)个点的权值可以随便选,方案数为\((n-tot)\times 2^{n-tot}\)
2.若所选的根节点是叶节点,那么除其他\(n-tot-1\)个叶节点外的所有的点权值可以随便选,方案数为\(tot\times 2^{n-tot+1}\)

\(ans=(n-tot)\times 2^{n-tot}+tot\times 2^{n-tot+1}=(n+tot)\times 2^{n-tot}\)

#include<complex>
#include<cstdio>
using namespace std;
const int mod=1e9+7;
const int N=1e5+7;
int n,tot;
int in[N];
int qread()
{
	int x=0;
	char ch=getchar();
	while(ch<'0' || ch>'9')ch=getchar();
	while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}
	return x;
}
long long Fpow(long long b,int p)
{
	long long res=1;
	for(;p;p>>=1,b=b*b%mod)
		if(p&1)res=res*b%mod;
	return res;
}
int main()
{
	scanf("%d",&n);
	for(int i=1;i<n;i++)
		in[qread()]++,in[qread()]++;
	for(int i=1;i<=n;i++)
		if(in[i]==1)tot++;
	printf("%d\n",(n+tot)*Fpow(2,n-tot)%mod);
	return 0;
}
posted @ 2019-01-25 11:25  LeTri  阅读(244)  评论(0编辑  收藏  举报