地址:http://acm.uestc.edu.cn/#/problem/show/1559

题目:

B0n0 Path

Time Limit: 1500/500MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others)

There is a country with NN cities, there are roads between some pairs of cities.

For every pair of cities, there is exactly one path between them, on which there are no cities passed more than once.

So it is obvious that there are N−1N−1 roads and N(N−1)2N(N−1)2 paths.

The cities number from 11 to NN, and the ithith city has one number AiAi.

A path is Bono if and only if the numbers AiAi on the path are non-strictly increasing or decreasing.

i.e., if the numbers are Ab1,Ab2,…,AbmAb1,Ab2,…,Abm,

Abi≤Abi+1 ,∀1≤i<mAbi≤Abi+1 ,∀1≤i<m

or

Abi≥Abi+1 ,∀1≤i<mAbi≥Abi+1 ,∀1≤i<m

should be satisfied.

How many Bono paths in the country?

Input

The first line contains one integer NN.

The second line contains NN integers, where the ithith indicates AiAi.

For the next N−1N−1 lines, each line contains two integers u,vu,v, meaning that there is a road between uthuth city and vthvth city.

1≤N≤105,1≤u,v≤N,1≤Ai≤1091≤N≤105,1≤u,v≤N,1≤Ai≤109

Output

One integer indicating the number of bono paths in the country.

Sample input and output

Sample Input Sample Output
4
1 7 1 9
1 3
1 4
2 1
5
6
1 1 2 2 3 3
1 2
2 3
3 4
4 5
5 6
15

Hint

For sample 1:

The format of the text on ithith node is (i:Ai)(i:Ai).

There are 5 bono paths: (1,2),(1,3),(1,4),(2,1,3),(3,1,4)(1,2),(1,3),(1,4),(2,1,3),(3,1,4), while path (2,1,4)(2,1,4) is not bono path.

For sample 2:

All path are bono paths.

Source

The 15th UESTC Programming Contest Preliminary
思路:树形dp
  dp[x][0]:表示从x的子孙节点出发的路径,并且路径上所有点权值都相等的路径数
  dp[x][0]:表示从x的子孙节点出发的路径,并且路径上所有点权值是严格递增的路径数
  dp[x][0]:表示从x的子孙节点出发的路径,并且路径上所有点权值是严格递减的路径数
具体转移过程见代码吧:
 #include <bits/stdc++.h>

 using namespace std;

 #define MP make_pair
#define PB push_back
typedef long long LL;
typedef pair<int,int> PII;
const double eps=1e-;
const double pi=acos(-1.0);
const int K=1e6+;
const int mod=1e9+;
vector<int>mp[K];
int n,val[K];
LL dp[K][];
LL ans; void dfs(int x,int f)
{
for(int i=;i<mp[x].size();i++)
{
int v=mp[x][i];
if(v==f) continue;
dfs(v,x);
if(val[v]<val[x])
{
ans+=(dp[x][]+dp[x][])*(dp[v][]+dp[v][]+);
dp[x][]+=dp[v][]+dp[v][]+;
}
else if(val[v]>val[x])
{
ans+=(dp[x][]+dp[x][])*(dp[v][]+dp[v][]+);
dp[x][]+=dp[v][]+dp[v][]+;
}
else
{
ans+=dp[x][]*(dp[v][]+dp[v][]+dp[v][]+)+dp[x][]*(dp[v][]+dp[v][]+)+dp[x][]*(dp[v][]+dp[v][]+);
dp[x][]+=dp[v][]+;
dp[x][]+=dp[v][];
dp[x][]+=dp[v][];
}
}
ans+=dp[x][]+dp[x][]+dp[x][];
//cout<<"x="<<x<<" "<<dp[x][0]<<" "<<dp[x][1]<<" "<<dp[x][2]<<" "<<ans<<endl;
//printf("x=%I64d %I64d %I64d %I64d\n",x,dp[x][0],dp[x][1],dp[x][2]);
} int main(void)
{
cin>>n;
for(int i=;i<=n;i++)
scanf("%d",val+i);
for(int i=,x,y;i<n;i++)
scanf("%d%d",&x,&y),mp[x].PB(y),mp[y].PB(x);
dfs(,);
cout<<ans<<endl;
return ;
}

The 15th UESTC Programming Contest Preliminary B - B0n0 Path cdoj1559的更多相关文章

  1. The 15th UESTC Programming Contest Preliminary J - Jermutat1on cdoj1567

    地址:http://acm.uestc.edu.cn/#/problem/show/1567 题目: Jermutat1on Time Limit: 3000/1000MS (Java/Others) ...

  2. The 15th UESTC Programming Contest Preliminary C - C0ins cdoj1554

    地址:http://acm.uestc.edu.cn/#/problem/show/1554 题目: C0ins Time Limit: 3000/1000MS (Java/Others)     M ...

  3. The 15th UESTC Programming Contest Preliminary K - Kidd1ng Me? cdoj1565

    地址:http://acm.uestc.edu.cn/#/problem/show/1565 题目: Kidd1ng Me? Time Limit: 3000/1000MS (Java/Others) ...

  4. The 15th UESTC Programming Contest Preliminary M - Minimum C0st cdoj1557

    地址:http://acm.uestc.edu.cn/#/problem/show/1557 题目: Minimum C0st Time Limit: 3000/1000MS (Java/Others ...

  5. The 15th UESTC Programming Contest Preliminary G - GC?(X,Y) cdoj1564

    地址:http://acm.uestc.edu.cn/#/problem/show/1564 题目: G - GC?(X,Y) Time Limit: 3000/1000MS (Java/Others ...

  6. The 15th UESTC Programming Contest Preliminary H - Hesty Str1ng cdoj1551

    地址:http://acm.uestc.edu.cn/#/problem/show/1551 题目: Hesty Str1ng Time Limit: 3000/1000MS (Java/Others ...

  7. The 15th UESTC Programming Contest Preliminary D - Destr0y City cdoj1558

    地址:http://acm.uestc.edu.cn/#/problem/show/1558 题目: D - Destr0y City Time Limit: 3000/1000MS (Java/Ot ...

  8. 【set】【可持久化Trie】The 16th UESTC Programming Contest Preliminary K - Will the circle be broken

    题意:You are given an array A of N non-negative integers and an integer M. Find the number of pair(i,j ...

  9. 【字符串哈希】The 16th UESTC Programming Contest Preliminary F - Zero One Problem

    题意:给你一个零一矩阵,q次询问,每次给你两个长宽相同的子矩阵,问你它们是恰好有一位不同,还是完全相同,还是有多于一位不同. 对每行分别哈希,先一行一行地尝试匹配,如果恰好发现有一行无法对应,再对那一 ...

随机推荐

  1. 在VS中调试javascript脚本

    https://blog.csdn.net/u010228798/article/details/78207375

  2. Erlang语言学习入门

    这是一个命令行程序,可以直接在里面输入表达式进行计算,例如来一个简单的: Erlang R15B01 (erts-5.9.1) [smp:4:4] [async-threads:0] Eshell V ...

  3. iOS: NSObject中执行Selector的相关方法

    本文转载至 http://www.mgenware.com/blog/?p=463 1. 对当前Run Loop中Selector Sources的取消 NSObject中的performSelect ...

  4. CentOS 6.5 配置IP地址的三种方法

    1.自动获取IP地址虚拟机使用桥接模式,相当于连接到物理机的网络里,物理机网络有DHCP服务器自动分配IP地址.#dhclient 自动获取ip地址命令#ifconfig 查询系统里网卡信息,ip地址 ...

  5. 【BZOJ1898】[Zjoi2005]Swamp 沼泽鳄鱼 矩阵乘法

    [BZOJ1898][Zjoi2005]Swamp 沼泽鳄鱼 Description 潘塔纳尔沼泽地号称世界上最大的一块湿地,它地位于巴西中部马托格罗索州的南部地区.每当雨季来临,这里碧波荡漾.生机盎 ...

  6. ES6数组相关

    ES6数组新增的几个方法: 1. forEach() //forEach()遍历数组,无返回值,不改变原数组 var arr=[1,2,3,4] arr.forEach((item,index,arr ...

  7. KVM虚拟机添加硬盘

    1,创建硬盘 qemu-img create -f raw /opt/GlusterFS1_data.img 30G 硬盘名称为GlusterFS1_data.img 大小为30G 2,编辑虚拟机配置 ...

  8. response.setContentType()的String参数及对应类型(转)

    response.setContentType(MIME)的作用是使客户端浏览器,区分不同种类的数据,并根据不同的MIME调用浏览器内不同的程序嵌入模块来处理相应的数据. 例如web浏览器就是通过MI ...

  9. APNS/苹果推送服务

    Apple Push Notification Service Google Cloud Message/Google 云 消息 Firebase Cloud Messaging

  10. GraphicsMagick 1.3.25 Linux安装部署

    1.安装相关依赖包 yum install -y gcc libpng libjpeg libpng-devel libjpeg-devel ghostscript libtiff libtiff-d ...