题目

分析

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

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

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

于是\(设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. elasticsearch索引大小写的问题

    今天用elasticsearch创建索引时碰到一个问题,用大写创建的索引,ES前端插件可以看到用大写创建索引存入的数据 程序启动,搜索的时候,却不显示数据,索引变成了小写,导致数据无法封装传入. 在h ...

  2. Egret入门学习日记 --- 第五篇(书中 3.5节 内容)

    第五篇(书中 3.5节 内容) 今天得把昨天的问题解决了才行. 去了Q群,碰到一位大大,他给我解惑了.Thanks♪(・ω・)ノ 这是我之前按照书上写的方式写的,并没有效果. 然后大大给我解答了: 后 ...

  3. const char* to char*(当函数传递参数时)

    来自 https://blog.csdn.net/rongrongyaofeiqi/article/details/52442169 https://blog.csdn.net/hebbely/art ...

  4. Redis数据类型Strings、Lists常用操作指令

    Redis数据类型Strings.Lists常用操作指令 Strings常用操作指令 GET.SET相关操作 # GET 获取键值对 127.0.0.1:6379> get name (nil) ...

  5. poj2773(欧基里德算法 或 二分+容斥)

    题目链接:https://vjudge.net/problem/POJ-2773 题意:给定m,k,求与m互质的第k个数. 思路一:利用gcd(a,b)=gcd(b*t+a,b)知道,与m互质的数是以 ...

  6. C++学习——在C文件中调用C++文件中的函数

    1.CPP文件中的内容 #include "mytest.h" #include <iostream> using namespace std; int add(con ...

  7. hadoop-组件

    hadoop1.x 和 hadoop2.x 区别 HDFS 分布式文件存储系统 优点   缺点 MapReduce 分布式计算 详见我的博客 mapreduce YARN 计算资源管理器 主要了解两个 ...

  8. [wpf]wpf full screen.

    void window_KeyDown(object sender,KeyEventArgs e) { if(e.Key == Key.F11) { Window.ResizeMode = Resiz ...

  9. LOCATE()和FIND_IN_SET()

    locate(substr,str) 查找substr在str中第一次出现的位置,为空时返回0 SELECT login_name, LOCATE('ning',login_name) FROM us ...

  10. xml的解析及案例的分析和分享

    HTML的文档如下: <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset=& ...