John has just bought a new car and is planning a journey around the country. Country has N cities, some of which are connected by bidirectional roads. There are N - 1 roads and every city is reachable from any other city. Cities are labeled from 1 to N.
John first has to select from which city he will start his journey. After that, he spends one day in a city and then travels to a randomly choosen city which is directly connected to his current one and which he has not yet visited. He does this until he can't continue obeying these rules.
To select the starting city, he calls his friend Jack for advice. Jack is also starting a big casino business and wants to open casinos in some of the cities (max 1 per city, maybe nowhere). Jack knows John well and he knows that if he visits a city with a casino, he will gamble exactly once before continuing his journey.
He also knows that if John enters a casino in a good mood, he will leave it in a bad mood and vice versa. Since he is John's friend, he wants him to be in a good mood at the moment when he finishes his journey. John is in a good mood before starting the journey.
In how many ways can Jack select a starting city for John and cities where he will build casinos such that no matter how John travels, he will be in a good mood at the end? Print answer modulo 109 + 7.
In the first line, a positive integer N (1 ≤ N ≤ 100000), the number of cities.
In the next N - 1 lines, two numbers a, b (1 ≤ a, b ≤ N) separated by a single space meaning that cities a and b are connected by a bidirectional road.
Output one number, the answer to the problem modulo 109 + 7.
2 1 2
4
3 1 2 2 3
10
Example 1: If Jack selects city 1 as John's starting city, he can either build 0 casinos, so John will be happy all the time, or build a casino in both cities, so John would visit a casino in city 1, become unhappy, then go to city 2, visit a casino there and become happy and his journey ends there because he can't go back to city 1. If Jack selects city 2 for start, everything is symmetrical, so the answer is 4.
Example 2: If Jack tells John to start from city 1, he can either build casinos in 0 or 2 cities (total 4 possibilities). If he tells him to start from city 2, then John's journey will either contain cities 2 and 1 or 2 and 3. Therefore, Jack will either have to build no casinos, or build them in all three cities. With other options, he risks John ending his journey unhappy. Starting from 3 is symmetric to starting from 1, so in total we have 4 + 2 + 4 = 10 options.
题目的意思是,现在给你一棵树,树节点编号从1到n,每个节点可以选择为0或者1,问你依次选取所有节点,走到所有可能的叶子节点权值之和为偶数的可能情况有多少种。
看到题目的瞬间,我想到了树状DP...然后这题就没做出来。
后经高人点拨,如拨开云雾见青天。
我们开始选择节点的时候,要么起点在叶子节点上,要么在非叶子节点上。在这里我们要注意的是,在从起点到某个终点的权值累加是奇数和偶数,和路径上各个点是1还是0关系不是很大,而是完全取决于最后一个节点是0还是1。要是前面累加为1,那它也是1,要是前面为0,它也为0,这样就能保证结果始终是偶数。那么既然我们知道叶子节点是固定的,那么其他所有非叶子节点可以选择的情况就是2的n-l次方种了,期中l是叶子的数量。另外,当起点为某一个叶子的时候,这是可选情况就是2的n-l+1种。
所以,总的公式就是 answer = l * 2^(n-l+1) + (n-l)*2^(n-l) = (n+l) * 2^(n-l);
为了节约计算2的n-l次方,我们使用快速幂。
#include <iostream>
#include <cstring>
using namespace std;
typedef long long ll;
const ll mod = 1e9+7;
ll asd[100004];
ll n;
ll pow(ll t,ll p){
ll a = 1;
while(p){
if(p&1) a=(a*t) % mod;
p>>=1;
t=(t*t)%mod;
}
return a;
}
int main(){
cin>>n;
memset(asd,0,sizeof(asd));
for(int i = 0;i<n-1;i++){
ll a,b;
cin>>a>>b;
asd[a]++;
asd[b]++;
}
ll l = 0;
for(int i = 1;i<=n;i++){
if(asd[i]==1)
l++;
}
ll tem = pow(2,n-l);
cout<<(n+l)*tem%mod<<endl;
return 0;
}