CF852E.Casinos and travel

提高+/省选-

通过率:0%

时间限制:1.00s

内存限制:256MB

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

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.

约翰刚刚买了一辆新车,正计划环游全国。该国共有 NN 座城市,其中一些城市由双向道路连接。总共有 N−1N-1 条道路,且任意两座城市之间均可互相到达(即整个国家构成一棵树)。城市编号为 11 到 NN。

约翰首先需选定旅程的出发城市。选定后,他每天停留在一座城市中,然后随机选择一个与当前城市直接相连、且尚未访问过的城市前往;他持续这一过程,直到无法再遵守上述规则为止(即当前城市所有邻接城市均已被访问过)。

为选定出发城市,约翰向好友杰克寻求建议。杰克正筹备一项大型赌场业务,计划在部分城市(每座城市至多建一座赌场,也可能完全不建)开设赌场。杰克非常了解约翰,并知道:若约翰访问了一座有赌场的城市,他将在继续旅程前恰好赌博一次。

杰克还知道:若约翰以好心情进入赌场,则离开时必为坏心情;反之,若以坏心情进入,则离开时必为好心情。作为约翰的朋友,杰克希望约翰在旅程结束的那一刻处于好心情。已知约翰在旅程开始前的心情是好的。

问:杰克有多少种方式选择约翰的出发城市以及建造赌场的城市,使得无论约翰如何随机旅行(即无论每次随机选择哪一未访问邻接城市),他最终都一定以好心情结束旅程?答案对 109+710^9 + 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.

第一行包含一个正整数 NN(1≤N≤1000001 \leq N \leq 100000),表示城市的数量。

接下来的 N−1N-1 行中,每行包含两个数字 aa、bb(1≤a,b≤N1 \leq a, b \leq N),以单个空格分隔,表示城市 aa 与城市 bb 之间由一条双向道路相连。

输出格式

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

输出一个数字,即该问题的答案对 109+710^9 + 7 取模的结果。

输入输出样例

  • 输入#1

    2
    1 2

    输出#1

    4
  • 输入#2

    3
    1 2
    2 3

    输出#2

    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:若 Jack 选择城市 1 作为 John 的起始城市,则他可以建造 0 个赌场(此时 John 始终保持开心),或者在两个城市均建造赌场(此时 John 先访问城市 1 的赌场而变得不开心,接着前往城市 2 并访问该处赌场而重新变得开心;由于无法返回城市 1,他的旅程在此结束)。若 Jack 选择城市 2 作为起始城市,则情况完全对称,因此答案为 4。

示例 2:若 Jack 让 John 从城市 1 出发,则他可选择在 0 个或 2 个城市建造赌场(共 4 种方案)。若让 John 从城市 2 出发,则 John 的旅程将仅包含城市 2 和 1,或城市 2 和 3。因此,Jack 要么一个赌场也不建,要么在全部三个城市都建造赌场;其余方案均可能导致 John 以不开心的状态结束旅程。从城市 3 出发的情况与从城市 1 出发对称,故总方案数为 4+2+4=104 + 2 + 4 = 10。

输入解题思路,AI测评打分。不知道怎么写?

首页