题目

分析

如果直接求方案数很麻烦。

但是,我们可以反过来做:先求出所有的方案数,在减去不包含的方案数。

由于所有的路径连在一起,

于是\(设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的更多相关文章

  1. 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 ...

  2. [JZOJ5185] 【NOIP2017提高组模拟6.30】tty's sequence

    Description

  3. JZOJ 5184. 【NOIP2017提高组模拟6.29】Gift

    5184. [NOIP2017提高组模拟6.29]Gift (Standard IO) Time Limits: 1000 ms  Memory Limits: 262144 KB  Detailed ...

  4. NOIP2017提高组 模拟赛15(总结)

    NOIP2017提高组 模拟赛15(总结) 第一题 讨厌整除的小明 [题目描述] 小明作为一个数学迷,总会出于数字的一些性质喜欢上某个数字,然而当他喜欢数字k的时候,却十分讨厌那些能够整除k而比k小的 ...

  5. NOIP2017提高组 模拟赛13(总结)

    NOIP2017提高组 模拟赛13(总结) 第一题 函数 [题目描述] [输入格式] 三个整数. 1≤t<10^9+7,2≤l≤r≤5*10^6 [输出格式] 一个整数. [输出样例] 2 2 ...

  6. NOIP2017提高组模拟赛4 (总结)

    NOIP2017提高组模拟赛4 (总结) 第一题 约数 设K是一个正整数,设X是K的约数,且X不等于1也不等于K. 加了X后,K的值就变大了,你可以重复上面的步骤.例如K= 4,我们可以用上面的规则产 ...

  7. JZOJ 5196. 【NOIP2017提高组模拟7.3】B

    5196. [NOIP2017提高组模拟7.3]B Time Limits: 1000 ms  Memory Limits: 262144 KB  Detailed Limits   Goto Pro ...

  8. JZOJ 5197. 【NOIP2017提高组模拟7.3】C

    5197. [NOIP2017提高组模拟7.3]C Time Limits: 1000 ms  Memory Limits: 262144 KB  Detailed Limits   Goto Pro ...

  9. JZOJ 5195. 【NOIP2017提高组模拟7.3】A

    5195. [NOIP2017提高组模拟7.3]A Time Limits: 1000 ms  Memory Limits: 262144 KB  Detailed Limits   Goto Pro ...

随机推荐

  1. java:Hibernate框架2(关联映射(多对一,一对多,一对多双向,一对一主键,一对一唯一外键,多对多双向))

      hibernate.cfg.xml: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE h ...

  2. Kafka集群搭建和配置

    Kafka配置优化 https://www.jianshu.com/p/f62099d174d9 1.安装&配置 下载tar包 解压后即可使用 修改配置文件 将server.propertie ...

  3. 磊哥的密码箱icpc11526

    问题 D: 磊哥的密码箱 时间限制: 1 Sec  内存限制: 128 MB提交: 238  解决: 61[提交] [状态] [命题人:admin] 题目描述 磊哥有个密码箱,里面装的都是令磊哥羞羞的 ...

  4. 【DSP开发】ccsv6添加simulator功能

    ccsv5更新到ccsv6后,ti去掉了simulator功能,具体的说法是"CCSv6 does NOT have any simulators. Texas Instruments is ...

  5. magento form.html不显示 window 和 Linux下的区别

    window 无大小写区别,所以可以显示表框 Linux 大小写敏感,显示不了 \app\code\community\Company\BabyPay\Model\Payment.php 里定义了fo ...

  6. [Python3] 029 常用模块 timeit

    目录 timeit 直接举例 1. 测量生成列表的时间 2. 测量函数运行时间(一) 3. 测量函数运行时间(二) timeit 直接举例 必要的导入 import timeit 1. 测量生成列表的 ...

  7. Java集合简单解析

    一. Collection 1. List a. ArrayList b. Vector c. LinkedList 首先要对List的三种实现进行一个简单的异同比较: 同: *ArrayList和V ...

  8. HDU 2586——How far away ?

    Time limit 1000 ms Memory limit 32768 kB Description There are n houses in the village and some bidi ...

  9. hugo搭建个人博客

    本地先安装git 1. 下载hugo,并配置好环境变量 我这里win7 64位,选择该版本下载 将解压后的hugo.exe,配置到环境变量中,如下图所示表明配置成功 hugo version 2. 生 ...

  10. 51nod 2589 快速讨伐

    51nod 如果不考虑升级操作,只有买装备操作和打怪操作,那么首先一定要先买装备,然后可以打死1级的怪,这些怪被打死的时间只要在第一次买装备后面好了,因为现在总操作是\(n+\sum a_i\)个,所 ...