Description

Input

Output

Sample Input

input 1:

5
1 1 1 1 1
1 2
2 3
3 4
4 5

input 2:

5
0 1 0 1 0
1 2
2 3
3 4
4 5

Sample Output

output 1:

15

output 2:

12

Data Constraint

Solution

  • 正难则反,这题我们可以转换一下思路。姑且称 可能 符合条件的一种方案为 一个匹配

  • 设 A= 不加限制的匹配数(总量), B= 不符合条件的匹配数(部分),则答案即为 A-B 。

  • 那么我们如何求出 A 和 B 呢?可以用到 树形DP 的方法。

  • 设 FiF_i 表示以 ii 为根的子树所产生的匹配数。则有转移方程式:

    Fi=Fi∗Fj+Fj(j∈Soni)

    F_i=F_i*F_j+F_j(j∈Son_i)(逐一组合)

  • 处理完上述式子后,再有:Fi=Fi+1 F_i=F_i+1 \ (单独自己也是一种匹配)

  • 最后可以求出 A :

    A=∑i=1nFi

    A=\sum_{i=1}^{n}F_i

  • 求 B 也同理,不过当 jj 为最值点时不转移 ii 、当 ii 为最值点时 FiF_i 不 +1 。

  • 最后 A、B 相减即可得出答案,总时间复杂度是 O(N)O(N) 。

Code

#include<cstdio>
#include<cstring>
using namespace std;
const int N=100001,mo=998244353;
int tot,mx=-2147483647;
int first[N],next[N<<1],en[N<<1];
int a[N];
long long f[N],g[N];
long long ans;
inline int read()
{int X=0,w=1; char ch=0;while(ch<'0' || ch>'9') {if(ch=='-') w=-1;ch=getchar();}while(ch>='0' && ch<='9') X=(X<<3)+(X<<1)+ch-'0',ch=getchar();return X*w;
}
inline void insert(int x,int y)
{next[++tot]=first[x];first[x]=tot;en[tot]=y;
}
inline void dfs1(int x,int y)
{for(int i=first[x];i;i=next[i])if(en[i]!=y){dfs1(en[i],x);(f[x]+=f[x]*f[en[i]]%mo+f[en[i]])%=mo;}ans+=++f[x];
}
inline void dfs2(int x,int y)
{for(int i=first[x];i;i=next[i])if(en[i]!=y){dfs2(en[i],x);if(a[en[i]]<mx) (g[x]+=g[x]*g[en[i]]%mo+g[en[i]])%=mo;}if(a[x]<mx) (ans+=mo-++g[x])%=mo;
}
int main()
{int n=read();for(int i=1;i<=n;i++)if((a[i]=read())>mx) mx=a[i];for(int i=1;i<n;i++){int x=read(),y=read();insert(x,y);insert(y,x);}dfs1(1,0),dfs2(1,0);printf("%lld",ans);return 0;
}

JZOJ 5186. 【NOIP2017提高组模拟6.30】tty's home相关推荐

  1. JZOJ 5185. 【NOIP2017提高组模拟6.30】tty's sequence

    Description Input Output Sample Input input 1: 6 3 1 1 1 0 0 0 input 2: 6 3 1 1 0 1 0 0 input 3: 6 3 ...

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

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

  3. JZOJ 5182. 【NOIP2017提高组模拟6.29】码灵鼠

    Description 码零鼠是一只很喜欢mx数学的神犇,上面那个不是ta本人的样子.这天,ta在研究一个神奇的数列,这个数列是这样的: a0 = 1 an = ai + aj (n>=1, i ...

  4. JZOJ 4932. 【NOIP2017提高组模拟12.24】B

    Description 现在你有 NN 个数,分别为 A1,A2,-,ANA1,A2,-,AN ,现在有M组询问需要你回答.每个询问将会给你一个L和R(L<=R)(L,保证 MaxAi−MinA ...

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

    Description Input Output Sample Input 3 Sample Output 1 Data Constraint Solution 这题我的方法是打表找规律,可以发现答案 ...

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

    Description Input Output Sample Input 7 3 Sample Output 4 Data Constraint Solution 这是一道经典的DP问题了,也可以把 ...

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

    Description Input Output Solution 这题看上去有些 01背包,但是细节上却大有不同. 先将价格从小到大排序,由于剩余的钱什么也不能买, 所以当没购买的物品中价格最小的为 ...

  8. JZOJ 5183. 【NOIP2017提高组模拟6.29】小T的钢琴

    Description Input Output Sample Input 9 3 A1 B2 C3 D4 E5 D4.0 C3.0 B2.0 A1.0[nya] 3 A1 A1.0 E5[qwert ...

  9. JZOJ 5177. 【NOIP2017提高组模拟6.28】TRAVEL

    Description Input Output Sample Input 4 4 1 2 1 10 2 4 3 5 1 3 1 5 2 4 2 7 Sample Output 6 2 3 4 5 6 ...

最新文章

  1. Fedora 30系统的升级方法
  2. angularjs与后台传值接收值
  3. make changes to the open source project?
  4. DISCUZ 嵌入点 输出插件模版内容
  5. Lua基础---lua字符串库函数详解,实例及正则表达式
  6. Hadoop Hbase 模型结构
  7. 用友NCCloud 补丁下载/用友ncc 补丁下载/nccloud 补丁下载
  8. wps2019怎么调整字间距_wps2019表格和文字间距太大怎么调整?
  9. word文档怎么画流程图
  10. 淘宝优惠券商城赚钱是真的吗?购物最便宜的app
  11. html新闻公告滚动效果,好用的滚动公告HTML代码
  12. 数独-- 一个高效率生成数独的算法
  13. ping命令 ——测试主机间网络连通性
  14. 2019最新14个国外知名开发者社区
  15. 【统一数据开发平台】-OLAP分析平台和实时数仓实践和优化
  16. 基于LSTM实现股票预测
  17. 随心所欲b超工作站图像处理_彩超工作站B超随心所欲XP版超声随心所欲XP版
  18. 【小沐学qt】生成二维码
  19. Oracle数据库深入学习
  20. Python代码出现UnicodeEncodeError问题

热门文章

  1. CUDA学习日志:常量内存和纹理内存
  2. vivado实现异步复位的D触发器
  3. 傅里叶变换函数FFT的使用方法
  4. loss=nan解决办法
  5. 科大星云诗社动态20210827
  6. oracle 如何形成死锁,ORACLE死锁
  7. adf检验代码 python_第22期:向量自回归(VAR)模型预测——Python实现
  8. Matlab神经网络十讲(7): Self-Organizing and LVQ Networks
  9. Qt Creator和VS2010中添加槽信号_差异与详解
  10. 项目经理应该把30%的时间用在编程上