Casinos and travel[智力和快速冪]

Casinos and travel

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.

Input

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

Output one number, the answer to the problem modulo 109 + 7.

Examples
input
2
1 2
output
4
input
3
1 2
2 3
output
10
Note

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;
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章