Who killed Cock Robin?

I, said the Sparrow, With my bow and arrow,I killed Cock Robin.

Who saw him die?

I, said the Fly.With my little eye,I saw him die.

Who caught his blood?

I, said the Fish,With my little dish,I caught his blood.

Who'll make his shroud?

I, said the Beetle,With my thread and needle,I'll make the shroud.

.........

All the birds of the air

Fell a-sighing and a-sobbing.

When they heard the bell toll.

For poor Cock Robin.

March 26, 2018

Sparrows are a kind of gregarious animals,sometimes the relationship between them can be represented by a tree.

The Sparrow is for trial, at next bird assizes,we should select a connected subgraph from the whole tree of sparrows as trial objects.

Because the relationship between sparrows is too complex, so we want to leave this problem to you. And your task is to calculate how many different ways can we select a connected subgraph from the whole tree.

输入描述:

The first line has a number n to indicate the number of sparrows. n\le 2\times 10^5n≤2×10

5

The next n-1 row has two numbers x and y per row, which means there is an undirected edge between x and y.

输出描述:

The output is only one integer, the answer module 10000007 (107+7) in a line

简而言之 计算我们能用多少种不同的方式从整个树中选择一个连通的子图。


#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N=2e5+10,mod=1e7+7;
#define int long long
int Next[N*2],head[N],go[N*2],tot;
inline void add(int u,int v){
Next[++tot]=head[u];head[u]=tot;go[tot]=v;
Next[++tot]=head[v];head[v]=tot;go[tot]=u;
}
int dp[N][2],n;
inline void dfs(int u,int fa){
dp[u][1]=1,dp[u][0]=0;
for(int i=head[u];i;i=Next[i]){
int v=go[i];
if(v==fa)continue;
dfs(v,u);
dp[u][0]=dp[u][0]+(dp[v][1]+dp[v][0])%mod;
dp[u][1]=(dp[u][1]*(dp[v][1]+1))%mod;
}
}
signed main(){
cin>>n;
for(int i=1,u,v;i<n;i++){scanf("%lld%lld",&u,&v);add(u,v);}
dfs(1,1);
cout<<(dp[1][1]+dp[1][0])%mod<<endl;
}

牛客竞赛-Who killed Cock Robin的更多相关文章

  1. D. Who killed Cock Robin 湖北省大学程序设计竞赛

    链接:https://www.nowcoder.com/acm/contest/104/C来源:牛客网 The Sparrow is for trial, at next bird assizes,w ...

  2. 2020牛客竞赛 DP F 碎碎念

    作者:儒生雄才1链接:https://ac.nowcoder.com/discuss/366644来源:牛客网 题目连接:https://ac.nowcoder.com/acm/contest/300 ...

  3. 牛客竞赛&&mjt的毒瘤赛

    题目链接 https://ac.nowcoder.com/acm/contest/368/F 思路 询问可以离线. 然后每个节点上建32个权值线段树(权值不大,其实只要20颗) 记录每一位权值为x(如 ...

  4. 牛客竞赛(gcd,快速幂)

    一.最大公约数和最小公倍数问题 题目描述: 输入2个正整数x0,y0(2<=x0<100000,2<=y0<=1000000),求出满足下列条件的P,Q的个数. 条件:1.P, ...

  5. 2019牛客竞赛第六场D Move 宏观单调,部分不单调

    Move 题意 有k个体积相同的箱子,有个憨憨有固定的装箱策略,每次都只装可以装的重量中最大的东西,求箱子的最小提及 分析 看起来可以二分,但由于他的装箱策略有点蠢,所以只在宏观上满足单调性,在特别小 ...

  6. 牛客竞赛第二场D Kth Minimum Clique 贪心+bitmap

    Kth Minimum Clique 题意 给出n(n<100)个点的邻接表,和n个点的权值,求第k大的团(完全子图) 分析 n很小,并且好像没有什么算法和这个有关系,所以可以往暴力枚举的方向想 ...

  7. D. Who killed Cock Robin--“今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛)

    题目描述:链接点此 这套题的github地址(里面包含了数据,题解,现场排名):点此 题目描述 由于系统限制,C题无法在此评测,此题为现场赛的D题 Who killed Cock Robin? I, ...

  8. 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 G.路径-带条件的树的直径变形-边权最大,边数偶数的树上的最长路径-树形dp

    链接:https://ac.nowcoder.com/acm/contest/558/G 来源:牛客网 路径 小猫在研究树. 小猫在研究路径. 给定一棵N个点的树,每条边有边权,请你求出最长的一条路径 ...

  9. 牛客网 桂林电子科技大学第三届ACM程序设计竞赛 D.寻找-树上LCA(树上a到b的路径上离c最近的点)

    链接:https://ac.nowcoder.com/acm/contest/558/D来源:牛客网 寻找 小猫在研究树. 小猫在研究树上的距离. 给定一棵N个点的树,每条边边权为1. Q次询问,每次 ...

随机推荐

  1. ASP.NET Core 3.x 并发限制

    前言 Microsoft.AspNetCore.ConcurrencyLimiter AspNetCore3.0后增加的,用于传入的请求进行排队处理,避免线程池的不足. 我们日常开发中可能常做的给某w ...

  2. Spring Boot 2.X(十六):应用监控之 Spring Boot Actuator 使用及配置

    Actuator 简介 Actuator 是 Spring Boot 提供的对应用系统的自省和监控功能.通过 Actuator,可以使用数据化的指标去度量应用的运行情况,比如查看服务器的磁盘.内存.C ...

  3. Python实现自动化监控远程服务器

    最近发现Python课器做很多事情,在监控服务器有其独特的优势,耗费资源少,开发周期短. 首先我们做一个定时或者实时脚本timedtask.py,让其定时监控目标服务器,两种方式: 第一种: #!/u ...

  4. Project Euler 60: Prime pair sets

    素数3, 7, 109, 673很有意思,从中任取两个素数以任意顺序拼接起来形成的仍然是素数.例如,取出7和109,7109和1097都是素数.这四个素数的和是792,是具有这样性质的四个素数的最小的 ...

  5. Docker学习-Kubernetes - 集群部署

    Docker学习 Docker学习-VMware Workstation 本地多台虚拟机互通,主机网络互通搭建 Docker学习-Docker搭建Consul集群 Docker学习-简单的私有Dock ...

  6. 小程序 数字过千 以K显示

    先新建一个 wxs 文件 每一个 .wxs 文件和 <wxs> 标签都是一个单独的模块. 每个模块都有自己独立的作用域.即在一个模块里面定义的变量与函数,默认为私有的,对其他模块不可见. ...

  7. [笔记]IDEA使用笔记

    1.IDEA的目录结构 2.所有的源文件都必须写在src文件夹下, 3.输入psvm再按回车,就会生成主函数: 4.输入sout就会生成输出语句的格式: 5.ALT+4   调出上次运行的结果出来看看 ...

  8. 使用JSP脚本在页面输出九九乘法表

    <% int i,j; for(i=1;i<10;i++) { for(j=1;j<=i;j++) { out.println(i+"*"+j+"=&q ...

  9. Python3.7.1学习(二)使用schedule模块定时执行任务

    python中有一个轻量级的定时任务调度的库:schedule.他可以完成每分钟,每小时,每天,周几,特定日期的定时任务.因此十分方便我们执行一些轻量级的定时任务. 1 安装  1.1在cmd中输入p ...

  10. 力扣(LeetCode)旋转字符串 个人题解

    给定两个字符串, A 和 B. A 的旋转操作就是将 A 最左边的字符移动到最右边. 例如, 若 A = 'abcde',在移动一次之后结果就是'bcdea' .如果在若干次旋转操作之后,A 能变成B ...