题目描述:链接点此

这套题的github地址(里面包含了数据,题解,现场排名):点此

题目描述

由于系统限制,C题无法在此评测,此题为现场赛的D题

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.

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

输入例子:
4
1 2
2 3
3 4
输出例子:
10

-->

示例1

输入

4
1 2
2 3
3 4

输出

10

说明

For a chain, there are ten different connected subgraphs: 

 
题意说明:给你一个树,然后让你求树中的所有联通子树个数
 
树形dp
 
维护一个数组dp[i]表示以i为根节点且含有i的联通子图个数,然后从树的下层向上传递并进行计数,我们可以知道以当前节点的子节点为根节点且含有当前节点的子节点的联通子图个数,然后逐个遍历子节点并定义一个变量temp表示遍历过前面的子节点过后可以凑成的联通子图数,那么当遍历到新的一个子节点时,在以当前节点为根且使用当前遍历过所有子节点可以得到的新联通子图数为temp*dp[v],总联通子图数为temp*dp[v]+temp(v表示当前遍历的子节点),就这样逐个累计即可获得答案。
 
 代码:
/*
data:2018.04.22
author:gsw
link:https://www.nowcoder.com/acm/contest/104#question
tip:武大校赛--补题
*/
#define IO ios::sync_with_stdio(false);
#define ll long long
#define mod 10000007
#define maxn 200005 #include<iostream>
#include<string.h>
#include<math.h>
#include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
int dp[maxn];
vector<int> g[maxn];
int a,b,n,ans=;
void init()
{
memset(dp,,sizeof(dp));
} void dfs(int node,int fa)
{
ll t=;
for(int i=;i<g[node].size();i++)
{
if(g[node][i]==fa)continue;
dfs(g[node][i],node);
t=(t+t*dp[g[node][i]])%mod;
}
dp[node]=t;
ans=(ans+t)%mod;
}
int main()
{
init();
scanf("%d",&n);
for(int i=;i<n-;i++)
{
scanf("%d%d",&a,&b);
g[a].push_back(b);g[b].push_back(a);
}
dfs(,);
printf("%d\n",ans);
}
 

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

  1. “今日头条杯”首届湖北省大学程序设计竞赛--F. Flower Road

    题目链接:点这 github链接:(包含数据和代码,题解):点这 链接:https://www.nowcoder.com/acm/contest/104/E来源:牛客网 题目描述 (受限于评测机,此题 ...

  2. “今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛 )--E. DoveCCL and Resistance

    题目描述:链接点此 这套题的github地址(里面包含了数据,题解,现场排名):点此 链接:https://www.nowcoder.com/acm/contest/104/D来源:牛客网 题目描述 ...

  3. A. Srdce and Triangle--“今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛)

    如下图这是“今日头条杯”首届湖北省大学程序设计竞赛的第一题,作为赛后补题 题目描述:链接点此 这套题的github地址(里面包含了数据,题解,现场排名):点此 Let  be a regualr tr ...

  4. I. Five Day Couple--“今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛)

    题目描述:链接点此 这套题的github地址(里面包含了数据,题解,现场排名):点此 链接:https://www.nowcoder.com/acm/contest/104/H来源:牛客网 题目描述 ...

  5. H. GSS and Simple Math Problem--“今日头条杯”首届湖北省大学程序设计竞赛(网络同步赛)

    题目描述:链接点此 这套题的github地址(里面包含了数据,题解,现场排名):点此 题目描述 Given n positive integers , your task is to calculat ...

  6. “东信杯”广西大学第一届程序设计竞赛(同步赛)H

    链接:https://ac.nowcoder.com/acm/contest/283/H来源:牛客网 题目描述 由于临近广西大学建校90周年校庆,西大开始了喜闻乐见的校园修缮工程! 然后问题出现了,西 ...

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

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

  8. 2019年广东工业大学腾讯杯新生程序设计竞赛(同步赛)E-缺席的神官

    链接:https://ac.nowcoder.com/acm/contest/3036/E 来源:牛客网 题目描述 面前的巨汉,让我想起了多年前的那次,但这个巨汉身上散布着让人畏惧害怕的黑雾.即使看不 ...

  9. 牛客竞赛-Who killed Cock Robin

    Who killed Cock Robin? I, said the Sparrow, With my bow and arrow,I killed Cock Robin. Who saw him d ...

随机推荐

  1. Python基础教程(006)--Python的特点

    前言 了解Python背景,明白Python在目前社会中的标准库是有成千上万的Python爱好者共同维护的. 知识点 Python是完全面相对象的语言 函数,模块,数字,字符串都是对象,在Python ...

  2. nginx 配置多服务器代理

    找到 nginx > conf目录中nginx.conf #user nobody; worker_processes 1; #error_log logs/error.log; #error_ ...

  3. (18)C++ string和标准模板库

    一.stringl类 1.string构造函数 string a1("abc");//初始化字符串 cout<<a1<<endl;//abc , '#'); ...

  4. Hexo next博客的pjax一个Bug引发的关于pjax用法的小技巧-----pjax后图片点击放大的js失效

    文章目录 广告: 背景 发现 解决 get技能 广告: 本人博客地址:https://mmmmmm.me 源码:https://github.com/dataiyangu/dataiyangu.git ...

  5. canvas简单画图板

    <!DOCTYPE html> <html lang='en'> <head> <meta charset='UTF-8'> <title> ...

  6. interleave two text files with specified lines

    a_file=$1 a_step=$2 b_file=$3 b_step=$4 a_start=1 let a_end=$a_start+$a_step b_start=1 let b_end=$b_ ...

  7. C. Ancient Berland Circus(三点确定最小多边形)

    题目链接:https://codeforces.com/problemset/problem/1/C 题意:对于一个正多边形,只给出了其中三点的坐标,求这个多边形可能的最小面积,给出的三个点一定能够组 ...

  8. 使用pip 时报错 Fatal error in launcher: Unable to create process using '"D:\pytghon2.7\python.exe" "D:\python2.7\S

    无法创建使用pip.exe创建进程,说白了就是无法启动pip安装插件. 解决方法升级pip: python -m pip install -U pip  

  9. sql 查询库是否存在

    网上查了很多,但是都是不完整的,很多坑,后面终于摸索出来了:DROP DATABASE IF EXISTS 库名(不要加引号); 这句话的意思就是如果库存在,就删除库,然后再新建库就行了.

  10. @Validated和@Valid区别:Spring validation验证框架对入参实体进行嵌套验证必须在相应属性(字段)加上@Valid而不是@Validated

    Spring Validation验证框架对参数的验证机制提供了@Validated(Spring's JSR-303规范,是标准JSR-303的一个变种),javax提供了@Valid(标准JSR- ...