codeforces 852E

原题链接:http://codeforces.com/problemset/problem/852/E

大致题意:一个人在树形的城市网络上旅行,旅行的规则是参观完一个城市后随机走向另外一个未曾到过的城市,直到不能走到其他未走过的城市时结束。他的朋友可以在某些城市开赌场,使得这个人在经过这些开了赌场的城市时,心情值由0变为1,由1变为0。问有多少种方法来选择这个人旅行的起点和开赌场的城市来使得这个人在旅行结束时心情值和旅行开始时一样是1。

显然这个人的旅行的终点一定是某个度为1的节点。所以不论他在除了叶子节点经过了多少赌场,叶子节点开不开赌场都是有唯一解的。所以总的方案数就是(总结点数+叶子节点数)*2^(总结点数-叶子节点数)。

代码:

#include <bits/stdc++.h>

using namespace std;
inline void read(int &x){
    char ch;
    bool flag=false;
    for (ch=getchar();!isdigit(ch);ch=getchar())if (ch=='-') flag=true;
    for (x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
    x=flag?-x:x;
}

inline void read(long long &x){
    char ch;
    bool flag=false;
    for (ch=getchar();!isdigit(ch);ch=getchar())if (ch=='-') flag=true;
    for (x=0;isdigit(ch);x=x*10+ch-'0',ch=getchar());
    x=flag?-x:x;
}


inline void write(int x){
    static const int maxlen=100;
    static char s[maxlen];
        if (x<0) {   putchar('-'); x=-x;}
    if(!x){ putchar('0'); return; }
    int len=0; for(;x;x/=10) s[len++]=x % 10+'0';
    for(int i=len-1;i>=0;--i) putchar(s[i]);
}

const int MAXN = 110000;
const int MAXM = 210000;
typedef long long ll;

int du[ MAXN ];
const long long P =1000000007ll;

int n;

int main(){
    read(n);
    for (int i=1;i<n;i++)
        {
            int a,b;
            read(a); read(b);
            du[ a ]++;du[b]++;
        }
    long long tmp=0;
    for (int i=1;i<=n;i++)
        if( du[i]==1)
            tmp++;
    long long ans=1;
    for (int i=1;i<=n-tmp;i++)
        ans=ans*2ll%P;
    printf("%I64d\n",1ll*(n+tmp)*ans%P);
    return 0;
}


猜你喜欢

转载自blog.csdn.net/u012602144/article/details/78035218