题目描述:链接点此

这套题的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. 快速搜索插件之quicksearch

    项目中如果遇到快速搜索table表格,select下拉框,ul>li列表等内容的时候,可以使用quicksearch快速搜索插件,举个栗子: 首先要引用jquery.js + jquery.qu ...

  2. [CSP-S模拟测试]:斯诺(snow)(数学+前缀和+树状数组)

    题目传送门(内部题37) 输入格式 第一行一个整数$n$,表示区间的长度. 第二行一个长度为$n$的只包含$0,1,2$的字符串,表示给出的序列. 输出格式 一行一个整数,表示革命的区间的数量. 样例 ...

  3. Linux系统Centos查看IP地址,不显示IP地址或者显示127.0.0.1

    1.桌面界面 右上角有个电脑的图标,鼠标悬停会显示no network connect 点击一下图标,选择连接的网络则ok 2.命令行界面 在命令行界面输入 vi  /etc/sysconfig/ne ...

  4. 安装纯净版debian!

    kali更新了1.1.0a,不知道新版的内核哪地方有bug,用着用着就卡死了,一怒之下卸载了装debian. 下载的netinst只有200M,基本上就是刚好能用,不要用硬盘装,会找不到网卡,无线也没 ...

  5. hive内部表&外部表介绍

    未被external修饰的是内部表(managed table),被external修饰的为外部表(external table):区别:内部表数据由Hive自身管理,外部表数据由HDFS管理:内部表 ...

  6. Jplayer用法

    引用js:jquery.jplayer.min.js; body里面必须有这个: <div id="jplayer"></div> jplayer停止方法 ...

  7. java并发编程笔记(一)——并发编程简介

    java并发编程笔记(一)--简介 线程不安全的类示例 public class CountExample1 { // 请求总数 public static int clientTotal = 500 ...

  8. JAVA StringUtils方法全集

    StringUtils方法全集 org.apache.commons.lang.StringUtils中方法的操作对象是java.lang.String类型的对象,是JDK提供 的String类型操作 ...

  9. 70、saleforce的Json输出

    List<Merchandise__c> merchandise = [select Id,Name,Price__c,Quantity__c from Merchandise__c li ...

  10. %matplotlib inline 被注释掉后,pycharm不能生成图

    目录 问题描述 解决方案 @ 问题描述 在 jupyter 编译器中 程序的开头,有这么一行 %matplotlib inline import numpy as np import matplotl ...