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. 『题解』洛谷P2170 选学霸

    更好的阅读体验 Portal Portal1: Luogu Description 老师想从\(N\)名学生中选\(M\)人当学霸,但有\(K\)对人实力相当,如果实力相当的人中,一部分被选上,另一部 ...

  2. 关于BootStrap的相关介绍

    一.Bootstrap Bootstrap的官网:www.bootcss.com 1.响应式布局 Responsive web page 响应式/自适应的网页 可以根据浏览器设备的不同(pc,pad, ...

  3. [ PyQt入门教程 ] PyQt5中数据表格控件QTableWidget使用方法

    如果你想让你开发的PyQt5工具展示的数据显得整齐.美观.好看,显得符合你的气质,可以考虑使用QTableWidget控件.之前一直使用的是textBrowser文本框控件,数据展示还是不太美观.其中 ...

  4. element - ui tree

    一行代码两行泪,没有外网真可怕!好久没写博客了,更新一把. <template> <div> <el-tree :data="data" :props ...

  5. jsp页面时间的转换js

    /**                            * 日期 转换为 Unix时间戳              * @param <string> 2014-01-01 20:2 ...

  6. 网站搭建-IIS Windows系统搭建网站 (不小心看到自己的密码 - 怎么找回网站记住的密码)

    上一期说到IIS可以用自己喜欢的网站来直接玩,然后得得瑟瑟将自己的博客园账号首页拿过去玩(今天第一天水博客园). 然后自己访问啊,访问啊,然后就一直点啊点的,当然,其实后面的链接都是跳转到博客园里面去 ...

  7. [LC]219题 Contains Duplicate II (存在重复元素 II )

    ①英文题目: Given an array of integers and an integer k, find out whether there are two distinct indices ...

  8. java编程思想第四版第八章总结

    1. 多态的含义 面向对象的三大基本特征: 封装,继承,多态. 多态是干什么的? 多态将做什么和怎么做分离开.从另一个角度将接口是实现类分离开. 多态的作用 消除类型之间耦合的关系 使用多态的好处? ...

  9. Mybatis 关联对象不能输出的解决办法

    Mybatis 关联对象不能输出的解决办法 1.如图所示,现在进行查询的时候并没有得到来自另一张表address项 2.我们进行如下配置: (1).在mybatis-config.xml 文件中配置, ...

  10. TestNg练习001

    15分钟入门TestNG 阅读目录 TestNG介绍 在Eclipse中在线安装TestNG 在Eclipse中离线安装TestNg TestNG最简单的测试 TestNG的基本注解 TestNG中如 ...