HDU
来源:互联网 发布:淘宝怎样用量子统计 编辑:程序博客网 时间:2024/06/18 18:04
There is a tree with nn nodes, each of which has a type of color represented by an integer, where the color of node ii is cici.
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)2n(n−1)2 paths in total.
Input
The input contains multiple test cases.
For each test case, the first line contains one positive integers nn, indicating the number of node. (2≤n≤200000)(2≤n≤200000)
Next line contains nn integers where the ii-th integer represents cici, the color of node ii. (1≤ci≤n)(1≤ci≤n)
Each of the next n−1n−1 lines contains two positive integers x,yx,y (1≤x,y≤n,x≠y)(1≤x,y≤n,x≠y), meaning an edge between node xx and node yy.
It is guaranteed that these edges form a tree.
Output
For each test case, output ” Case #xx: yy” in one line (without quotes), where xx indicates the case number starting from 11 and yy denotes the answer of corresponding case.
Sample Input
3
1 2 1
1 2
2 3
6
1 2 1 3 2 1
1 2
1 3
2 4
2 5
3 6
Sample Output
Case #1: 6
Case #2: 29
这个题如果按照题意直接做
下辈子也做不出来
首先转化成全部-非法
这种思路
然后去算非法
非法的就是在算颜色a的时候,把所有颜色a的点全都从图上擦掉
剩下的所有路径全都是非法的
但是显然不可能每次都擦掉复杂度爆炸
所以就有了he数组…
这个数组看上去很难理解
但是仔细想想可以理解成一个前缀和
一个存了所有搜过的点的连通块的点数的前缀和..
所以当前搜完子树正在处理的点的he值减掉处理之前的he值的差
代表的意义就是搜当前子树之后全部连通块点的个数-搜这个点之前的全部连通块的点的个数
也就是说这个差是当前子树搜过的连通块的点数
那么用这个子树的节点数减掉已经搜过的连通块的点数
就是靠当前节点最近的连通块的点数了
在他处理完全部这个点以后
再把所有新的连通块加入当前颜色
返回他爹节点
#include<bits/stdc++.h>using namespace std;typedef long long ll;#define int long long vector<int>tu[200001];int ys[200001],he[200001],n,q,w,mp[200001],sh=0,jd=0,zjd[200001],bj[200001],u=0;void dfs(int gen){ bj[gen]=1; zjd[gen]=1; int qb=0; int sg=he[ys[gen]]; for(int a=0;a<tu[gen].size();a++) { int zg=tu[gen][a]; if(bj[zg])continue; dfs(zg); int cha=he[ys[gen]]-sg; zjd[gen]+=zjd[zg]; sg=he[ys[gen]]; int suan=zjd[zg]-cha; jd+=suan*(suan-1)/2; qb+=suan; } he[ys[gen]]+=qb+1;}main(){ while(cin>>n) { sh=0; jd=0; for(int a=1;a<=n;a++)tu[a].clear(); memset(he,0,sizeof(he)); memset(mp,0,sizeof(mp)); memset(zjd,0,sizeof(zjd)); memset(bj,0,sizeof(bj)); for(int a=1;a<=n;a++)scanf("%lld",&ys[a]); for(int a=1;a<=n;a++) { if(mp[ys[a]])continue; mp[ys[a]]=1; sh++; } for(int a=1;a<n;a++) { scanf("%lld%lld",&q,&w); tu[q].push_back(w); tu[w].push_back(q); } ll dan=0; dan=sh*n; dan*=(n-1); dan/=2; dfs(1); dan-=jd; for(int a=1;a<=n;a++) { if(!mp[a])continue; // if(ys[1]==mp[a])continue; dan-=(n-he[a])*(n-he[a]-1)/2; } printf("Case #%lld: %lld\n",++u,dan); }}
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- Find a way HDU
- 光照效果
- 设计模式之禅笔记-模板方法模式
- String 类的常用方法 20170728
- MySQL数据表的权限分配
- HDU
- Linux系统运维工程该具备哪些素质
- Linux 设置程序开机自启动 (命令systemctl 和 chkconfig用法区别比较)
- Atitit 软件 开发 与互联网发展趋势 与一些原则 潮流就是社区化 o2o 各种服务化 xaas ##--------信息化建设的理念 1.1.兼容性(不同版本与项目兼容性有利
- Atitit 关于微服务的思考与理解 attilax总结 1.1. 架构的历史 微服务发展历史 Web》soa》msa1 1.2. 微服务最大特点 独立部署1 2. 微服务的优点1 2.1.
- JMock入门
- Atitit 企业文化之道 ---假日文化 attilax总结
- leetcode insertionSortList
- 菜鸟爬取中关村手机详情页参数及报价