链接:https://www.nowcoder.com/acm/contest/104/C
来源:牛客网

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.

题意:给你一棵树,输出其所有子树的数量(连通块的数量)。

题解:手摸一下样例,再画一颗三叉树,找到一个公式:num[i]=(num[v1]+1)*(num[v2]+1)*````*(num[v0]+1) num[i]为以第i个节点为根的树的字数数量。v1```v0为其儿子。

    简单推导一下这个公式:对于n的每个儿子v,你选它时有num[v]种方法,、这是分步过程所以用乘法原理。但注意还有不选该儿子的一种情况,所以要+1.

    具体实现就是随便找一个点作为根dfs, 然后把所有num加起来

坑:我一发没过,改成ll 还是没过。比赛结束后发现 mod 没有改成ll orz

#define _CRT_SECURE_NO_WARNINGS
#include<cstring>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<string>
#include<stack>
#include<ctime>
#include<list>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<sstream>
#include<iostream>
#include<algorithm>
//#define INF 0x3f3f3f3f
#define eps 1e-6
#define pi acos(-1.0)
#define e exp(1.0)
#define rep(i,t,n) for(int i =(t);i<=(n);++i)
#define per(i,n,t) for(int i =(n);i>=(t);--i)
#define mp make_pair
#define pb push_back
//std::ios::sync_with_stdio(false);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
void smain();
#define ONLINE_JUDGE
int main() {
ios::sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
long _begin_time = clock();
#endif
smain();
#ifndef ONLINE_JUDGE
long _end_time = clock();
printf("time = %ld ms.", _end_time - _begin_time);
#endif
return ;
}
const int maxn = 2e5 + ;
const ll mod = 1e7 + ;
const ll INF = ()*(200000ll) + ; int n;
vector<int> E[maxn];
int num[maxn];
void dfs(int n,int fa) {
if (num[n] != )return;
int sz = E[n].size();
if (sz == && E[n][] == fa) {
num[n] = ;
}
rep(i, , sz - ) {
int v = E[n][i];
if (v ==fa)continue;
dfs(v,n);
num[n] = num[n]*(num[v] + )%mod;
}
}
void Run() {
dfs(,);
int ans=;
rep(i,,n) {
ans = (ans + num[i]) % mod;
}
cout << ans << endl; } void smain() { cin >> n;
rep(i,,n-) {
int a, b;
cin >> a >> b;
E[a].pb(b);
E[b].pb(a); }
rep(i, , n)num[i] = ;
Run(); }

D. Who killed Cock Robin 湖北省大学程序设计竞赛的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. 牛客竞赛-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 ...

  8. 第十四届浙江财经大学程序设计竞赛重现赛--A-A Sad Story

    链接:https://www.nowcoder.com/acm/contest/89/A 来源:牛客网 1.题目描述 The Great Wall story of Meng Jiangnv’s Bi ...

  9. 2018年第十届ACMICPC四川省大学程序设计竞赛

    ..拿金了 没给学校丢脸 A ....SB题啊 比赛的时候都没看 裸的一个bitset前缀和 先开一个1e4*1e4的二维bitset数组 初始第i个数组的值为1 << i (即B[i]= ...

随机推荐

  1. 你真的了解Python吗 ---Python的内存管理

    请看下面的一段代码: origin = {'a':100,'b':[1,2,34,5]} obj_copy ={}; print origin; obj_copy['key1']= origin; o ...

  2. C#:文件夹匹配

    //文件夹匹配:对比文件夹,相同的目录结构.所有文件名称小写相同,制定文件外的MD5值相同 ,则两个文件夹匹配成功! /// <summary> /// 批量匹配书籍H5资源包 /// & ...

  3. 阿里云 ssh 登陆请使用(公)ip

    一直以为要要登陆使用的是私有的ip,最后才发现是使用共有ip, 如图 47.52.69.151 > ssh root@47.52.69.151 > 输入密码

  4. hdoj:2037

    #include <iostream> using namespace std; struct Time { int start; int end; }; Time times[]; ]; ...

  5. 点云PCL中小细节

    计算点与点之间的距离的平局距离 double computeCloudResolution (const pcl::PointCloud<PointType>::ConstPtr & ...

  6. highCharts图表入门简介

    一.Highcharts简介 Highcharts:功能强大.开源.美观.图表丰富.兼容绝大多数浏览器的纯js图表库 Highcharts是一款纯javascript编写的图表库,能够很简单便捷的在W ...

  7. 仿迅雷播放器教程 -- 基于VLC的MFC播放器 (6)

        代码下载:http://download.csdn.net/detail/qq316293804/6409417   昨天的教程里写着预计MFC播放器会隔得久一点,但是今晚仔细看了下VLC的常 ...

  8. [Node.js] 00 - Where do we put Node.js

    Ref: 前后端分离的思考与实践(五篇软文) 其实就是在吹淘宝自己的Midway-ModelProxy架构. 第一篇 起因 为了提升开发效率,前后端分离的需求越来越被重视, 同一份数据接口,我们可以定 ...

  9. 8 -- 深入使用Spring -- 6...3 使用@Transactional

    8.6.3 使用@Transactional Spring还允许将事务配置放在Java类中定义,这需要借助于@Transactional注解,该注解即可用于修饰Spring Bean类,也可用于修饰B ...

  10. aws平台中为ec2实例添加双网卡

    亚马逊平台创建ec2实例时默认创建一个网卡primary网卡,主网卡是不能分离的. 在ec2服务控制台为ec2附加网卡. 平台资料中为正在使用的ec2添加网卡用到的操作系统是Amazon Linux ...