2017 Multi-University Training Contest - Team 1 03Colorful Tree
地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6035
题面:
Colorful Tree
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1651 Accepted Submission(s): 675
The path between each two different nodes is unique, of which we define the value as the number of different colors appearing in it.
Calculate the sum of values of all paths on the tree that has n(n−1)2 paths in total.
For each test case, the first line contains one positive integers n, indicating the number of node. (2≤n≤200000)
Next line contains n integers where the i-th integer represents ci, the color of node i. (1≤ci≤n)
Each of the next n−1 lines contains two positive integers x,y (1≤x,y≤n,x≠y), meaning an edge between node x and node y.
It is guaranteed that these edges form a tree.
1 2 1
1 2
2 3
6
1 2 1 3 2 1
1 2
1 3
2 4
2 5
3 6
Case #2: 29
思路:很巧妙的树形dp!
具体看这个博客把,我也是看了这个才懂的http://blog.csdn.net/Bahuia/article/details/76141574
#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];
LL n,cnt,ans,sz[K],col[K],vis[K],sum[K]; void dfs(int x,int f)
{
LL tmp,add,all=;
sz[x]=;
for(auto v:mp[x])
if(v!=f)
{
tmp=sum[col[x]];
dfs(v,x);
sz[x]+=sz[v];
add=sum[col[x]]-tmp;
ans+=(sz[v]-add)*(sz[v]-add-)/;
all+=sz[v]-add;
}
sum[col[x]]+=all+;
} int main(void)
{
int cs=;
while(~scanf("%lld",&n))
{
cnt=,ans=;
memset(mp,,sizeof mp);
memset(sum,,sizeof sum);
memset(sz,,sizeof sz);
for(int i=;i<=n;i++) scanf("%d",col+i),vis[col[i]]=;
for(int i=,x,y;i<n;i++) scanf("%d%d",&x,&y),mp[x].PB(y),mp[y].PB(x);
dfs(,);
for(int i=;i<=n;i++)
if(vis[i])
ans+=(n-sum[i]-)*(n-sum[i])/,vis[i]=,cnt++;
printf("Case #%d: %lld\n",cs++,cnt*n*(n-1LL)/2LL-ans);
}
return ;
}
2017 Multi-University Training Contest - Team 1 03Colorful Tree的更多相关文章
- 2017 Multi-University Training Contest - Team 9 1005&&HDU 6165 FFF at Valentine【强联通缩点+拓扑排序】
FFF at Valentine Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- 2017 Multi-University Training Contest - Team 9 1004&&HDU 6164 Dying Light【数学+模拟】
Dying Light Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tot ...
- 2017 Multi-University Training Contest - Team 9 1003&&HDU 6163 CSGO【计算几何】
CSGO Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Subm ...
- 2017 Multi-University Training Contest - Team 9 1002&&HDU 6162 Ch’s gift【树链部分+线段树】
Ch’s gift Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- 2017 Multi-University Training Contest - Team 9 1001&&HDU 6161 Big binary tree【树形dp+hash】
Big binary tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- 2017 Multi-University Training Contest - Team 1 1003&&HDU 6035 Colorful Tree【树形dp】
Colorful Tree Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- 2017 Multi-University Training Contest - Team 1 1006&&HDU 6038 Function【DFS+数论】
Function Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total ...
- 2017 Multi-University Training Contest - Team 1 1002&&HDU 6034 Balala Power!【字符串,贪心+排序】
Balala Power! Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- 2017 Multi-University Training Contest - Team 1 1011&&HDU 6043 KazaQ's Socks【规律题,数学,水】
KazaQ's Socks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
随机推荐
- VC++中CEdit控件实现回车换行
1.通过回车Enter换行: 这里要有两个设置 <1>.将控件的属性设置为Mutilines->true; <2>.将控件的另一个属性设置为Want return-> ...
- 《C++ Primer Plus》学习笔记 2.1.3 C++预处理器和iostream文件
程序清单2-1 myfirst.cpp // myfirst.cpp -- displays a message #include <iostream> // a PREPROCESSOR ...
- Node.js 搭建Web
Express Express 是整个 Node.js 之中最为常见的一个框架(开发包),可以帮助我们快速构建一个WEB项目.(http://expressjs.com) 1.在 F 盘新建 node ...
- 160311、mybatis sql语句中转义字符
问题: 在mapper ***.xml中的sql语句中,不能直接用大于号.小于号要用转义字符 解决方法: 1.转义字符串 小于号 < < 大于号 > & ...
- 用angular引入复杂的json文件2
昨天我们也说了一下angular引入复杂json文件的方法,今天我们再来学习一种方法,而且更简单,更快捷. 首先我们引入一个angular插件,并且写上引入模块和控制台,在html中书写上模块名和控制 ...
- org.hibernate.ObjectDeletedException: deleted object would be re-saved by cascade 解决方案 (网络转载)
前提是配置了cascade=all,依然报这种错误,其实出现这个错误的大多数情况根本不是像网上的帖子所说的是什么级联删除的问题,而且hibernate session关于实体生命周期操作的原因,这里明 ...
- WebKit HTML、CSS、JS
开发者需要了解的WebKit https://www.infoq.cn/article/webkit-for-developers 开发者需要了解的 WebKit 彭超 2013 年 3 月 18 ...
- 【推荐】CentOS安装Subversion-1.8.17+HTTP协议支持配置
注:以下所有操作均在CentOS 6.5 x86_64位系统下完成. 我们需要搭建一个自己的SVN服务器. 此外,搭建好的SVN服务器除了需要支持svn协议外,最好还需要支持HTTP协议和HTTPS协 ...
- windows accounts
Some built-in groups are used for management purposes. You control which > users belong to these ...
- datasnap 授权验证DSAuthenticationManager方法应用
服务端 1.服务端只需要增加DSAuthenticationManager1并且把dshttpservice的AuthenticationManager属性设置为DSAuthenticationMan ...