【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 ...
随机推荐
- FreeBSD Here is how to upgrade
freebsd-version or uname -mrs freebsd-update fetch install pkg update && pkg upgrade -y fr ...
- LoadRunner对sockets报文进行压力测试(脚本设计)
1. LR新建一个windows sockets项目 2. action中写入测试代码 如: #include "lrs.h" Action() { char *recvbuf; ...
- VMware下的Centos7联网并设置固定IP(nat)
命令:vi /etc/sysconfig/network-scripts/ifcfg-ens33 如下图所示,加上这四行内容. IPADDR 需要是和网关在同一网段 GATEWAY 参考在虚拟机的na ...
- for循环实现九九乘法表
<!--for循环实现九九乘法表--> <table border="> <tbody> {% for x in range(1,10) %} <t ...
- Redis配置主从时报错“Could not connect to Redis at 192.168.0.50:6379: Connection refused not connected>”
配置Redis主从时,修改完从节点配置文件,然后报错 [root@Rich七哥-0-50 redis]# /opt/redis/redis-cli -h 192.168.0.50 Could not ...
- <<C++ Primer>> 术语表 (总) (待补充)
术语表 目录 第 1 章 开始 第 I 部分 C++基础 第 2 章 变量和基本类型 第 3 章 字符串, 向量和数组 第 4 章 表达式 第 5 章 语句 第 6 章 函数 第 7 章 类 第 II ...
- E - 卿学姐与城堡的墙(树状数组求逆序数)
卿学姐与城堡的墙 Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Submit ...
- Android 开源项目及库汇总(2)
Android 开源项目及库汇总(2) ListenToCode 2.7 2018.10.10 15:43 字数 8527 阅读 1001评论 0喜欢 29 地图 百度地图– Android百度地图 ...
- ArcGIS 在VS2010中 ESRI.ArcGIS.SOESupport.dll 无法正常加载的处理
转自 http://blog.csdn.net/tnt123688/article/details/23186973 问题描述: 打开ArcGIS的SOE模板后,提示 错误 命名空间“ESRI.A ...
- 06.AutoMapper 之内联映射(Inline Mapping)
https://www.jianshu.com/p/623655d7cb34 内联映射(Inline Mapping) AutoMapper在 6.2 以上版本将动态创建类型映射. 当第一次调用Map ...