【ZJOJ5186】【NOIP2017提高组模拟6.30】tty's home
题目
分析
如果直接求方案数很麻烦。
但是,我们可以反过来做:先求出所有的方案数,在减去不包含的方案数。
由于所有的路径连在一起,
于是\(设f[i]表示以i为根的子树中,连接到i的方案数\)
则\(f[i]=f[son]+(f[i]+1)\)表示从子树son分别到i和i其他儿子的子树的路径方案数。
由于每棵子树互不影响,\(ans=\sum_{i=1}^nf[i]\)
对于不包含的,就是当son为最大值就不转移到父亲上,且当i为最大值不加入ans。
#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
const int maxlongint=2147483647;
const long long mo=998244353;
const long long N=100005;
using namespace std;
struct arr
{
int v,p;
}b[N];
long long f[N],n,last[N*2],to[N*2],next[N*2],tot,ans,size[N],ans1;
bool bz[N];
bool cmp(arr x,arr y)
{
return x.v>y.v;
}
void bj(int x,int y)
{
next[++tot]=last[x];
last[x]=tot;
to[tot]=y;
}
void dg(int x,int fa)
{
for(int i=last[x];i;i=next[i])
{
int j=to[i];
if(j!=fa) dg(j,x),(f[x]+=f[j]+f[x]*f[j]%mo)%=mo;
}
(ans+=++f[x])%=mo;
}
void dg1(int x,int fa)
{
for(int i=last[x];i;i=next[i])
{
int j=to[i];
if(j!=fa)
{
dg1(j,x);
if(!bz[j]) (f[x]+=f[j]+f[x]*f[j]%mo)%=mo;
}
}
if(!bz[x]) (ans1+=++f[x])%=mo;
}
int main()
{
scanf("%lld",&n);
for(int i=1;i<=n;i++) scanf("%d",&b[i].v),b[i].p=i;
for(int i=1,x,y;i<=n-1;i++) scanf("%d%d",&x,&y),bj(x,y),bj(y,x);
sort(b+1,b+1+n,cmp);
bz[b[1].p]=true;
for(int i=2;i<=n;i++)
if(b[i].v==b[i-1].v) bz[b[i].p]=true;
else break;
dg(1,0);
memset(f,0,sizeof(f));
dg1(1,0);
printf("%lld",(ans-ans1+2*mo)%mo);
}
【ZJOJ5186】【NOIP2017提高组模拟6.30】tty's home的更多相关文章
- JZOJ 5185. 【NOIP2017提高组模拟6.30】tty's sequence
5185. [NOIP2017提高组模拟6.30]tty's sequence (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB ...
- [JZOJ5185] 【NOIP2017提高组模拟6.30】tty's sequence
Description
- JZOJ 5184. 【NOIP2017提高组模拟6.29】Gift
5184. [NOIP2017提高组模拟6.29]Gift (Standard IO) Time Limits: 1000 ms Memory Limits: 262144 KB Detailed ...
- NOIP2017提高组 模拟赛15(总结)
NOIP2017提高组 模拟赛15(总结) 第一题 讨厌整除的小明 [题目描述] 小明作为一个数学迷,总会出于数字的一些性质喜欢上某个数字,然而当他喜欢数字k的时候,却十分讨厌那些能够整除k而比k小的 ...
- NOIP2017提高组 模拟赛13(总结)
NOIP2017提高组 模拟赛13(总结) 第一题 函数 [题目描述] [输入格式] 三个整数. 1≤t<10^9+7,2≤l≤r≤5*10^6 [输出格式] 一个整数. [输出样例] 2 2 ...
- NOIP2017提高组模拟赛4 (总结)
NOIP2017提高组模拟赛4 (总结) 第一题 约数 设K是一个正整数,设X是K的约数,且X不等于1也不等于K. 加了X后,K的值就变大了,你可以重复上面的步骤.例如K= 4,我们可以用上面的规则产 ...
- JZOJ 5196. 【NOIP2017提高组模拟7.3】B
5196. [NOIP2017提高组模拟7.3]B Time Limits: 1000 ms Memory Limits: 262144 KB Detailed Limits Goto Pro ...
- JZOJ 5197. 【NOIP2017提高组模拟7.3】C
5197. [NOIP2017提高组模拟7.3]C Time Limits: 1000 ms Memory Limits: 262144 KB Detailed Limits Goto Pro ...
- JZOJ 5195. 【NOIP2017提高组模拟7.3】A
5195. [NOIP2017提高组模拟7.3]A Time Limits: 1000 ms Memory Limits: 262144 KB Detailed Limits Goto Pro ...
随机推荐
- 【SSM】---增删改查
20:43:06 package com.chinasofti.dao; import java.util.List; import com.chinasofti.entity.User; publi ...
- Spring 中如何自动创建代理(spring中的三种自动代理创建器)
Spring 提供了自动代理机制,可以让容器自动生成代理,从而把开发人员从繁琐的配置中解脱出来 . 具体是使用 BeanPostProcessor 来实现这项功能. 这三种自动代理创建器 为:Bean ...
- cocos2dx[3.2](10) 新回调函数std::bind
在2.x中处理事件需要用到委托代理(delegate),相信学过2.x的触摸事件的同学,都知道创建和移除的流程十分繁琐. 而在3.x中由于加入了C++11的特性,而对事件的分发机制通过事件分发器Eve ...
- MyView 的无奈问题
这不是为难我吗这种问题 据说这是内置库!!!!!可是: 找不到呀!!!!!!!!!! 后期补充: 是因为我的一个目录名字起了冲突
- redis缓存与数据一致性
目录 缓存 缓存穿透 缓存雪崩(缓存失效) 缓存击穿(热点key) 缓存并发竞争(并发set) 数据一致性 缓存(双写)一致性 Redis集群(Redis-cluster)一致性原理 哨兵(Senti ...
- 【VS开发】在VS2010中开发ActiveX控件设置测试容器的方式
在VS2010中开发ActiveX控件设置测试容器的方式 借鉴文章http://blog.csdn.net/waxgourd0/article/details/7374669 在VS2010中开发MF ...
- Connection is read-only. Queries leading to data modification are not allowed 错误原因
因为我再spring 中使用了AOP进行事务管理,有如下配置 <tx:advice id="txAdvice" transaction-manager="trans ...
- Maven - Maven3实战学习笔记(2)坐标和依赖
1.maven坐标元素 maven坐标元素包括:groupId.artifactId.version.packaging.classifier. classifier:定义输出的附属构件.groupI ...
- linux-yum-downloadonly 下载rpm安装包到本地
注意 注意1:如果机器,本来就安装了相应的rpm包,则该rpm包不会下载. 参考 centos7离线安装rpm包自动解决依赖 查看linux系统版本信息(Oracle Linux.Centos Lin ...
- Android-Widget桌面小组件
1, 掌握Widget的用:Widget的用途,能够添加到手机桌面的程序 2, Widget的特点和用法步骤: 特点:快捷,方便,个性化,可自定义功能,可及时控制更新Widget显示内容 3, 用法步 ...