地址: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. WPF DataGrid DataGridTemplateColumn 控制模板中控件

    <DataGrid Name="DG">                <DataGrid.Columns>                    < ...

  2. Spring中Adivisor和Aspect的差别(自我理解)

    在AOP中有几个概念: - 方/切 面(Aspect):一个关注点的模块化,这个关注点实现可能另外横切多个对象.事务管理是J2EE应用中一个非常好的横切关注点样例. 方面用Spring的Advisor ...

  3. 编程之美 set 16 拈游戏分析(1)

    题目 N 个石头排成一行, 每块石头有固定的位置和编号, 两个玩家依次取石头, 每个玩家可以取其中的任一块石头, 或者相邻的两个石头. 石头在游戏过程中不能移位, 最后将剩下的石头依次取光的玩家获胜 ...

  4. Hibernate传递list参数的例子

    public Map<String, String> getAllFeedBack(Object[] obj){ Map<String, String> map = new H ...

  5. java enum(枚举)使用详解 + 总结(转载)

    enum 的全称为 enumeration, 是 JDK 1.5  中引入的新特性,存放在 java.lang 包中. 下面是我在使用 enum 过程中的一些经验和总结,主要包括如下内容: 1. 原始 ...

  6. Java构建网站多级菜单功能解决方案

    在网站开发的时候我们会对网站的栏目进行分类,一个栏目可以有多个子分类,一个子分类又可以有分裂,例如:新闻栏目下有每日早报和每日晚报两个栏目,其中每日早报下面又分为上海早报,北京早报,杭州早报,下面是京 ...

  7. ng2-file-upload上传附件同时传参

    由于业务需要,需要的场景是发某条公告的时候能够上传附件,不只是图片,图片的话可以直接用base64传给后台,但上传附件这个就不能这样干了, 与此同时,每条公告都有一个对应的唯一标识id, 附件以文件流 ...

  8. 使用Git分支开发新特性或修复Bug与使用Git分支开发新特性或修复Bug

    使用Git分支开发新特性或修复Bug 通过分支,可以在不影响原有代码的前提下改变代码,主要用于开发新功能新特性.下 一代产品 为已经发布的正式版修复bug 团队开发时为每个人建立一个分支,从而避免相互 ...

  9. 如何做rom,体验做rom过程,附图文教程,感谢各位romer

    http://bbs.gfan.com/android-5408130-1-1.html 有人问我,我简单的写一下,来源XDA,运行环境ubuntu 10.4. ubuntu安装很简单,在window ...

  10. org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter cannot be cast to javax.servlet.Filter

    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter cannot be cast to javax.servle ...