Colorful Tree
时间:2019-09-03
本文章向大家介绍Colorful Tree,主要包括Colorful Tree使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
Colorful Tree
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 3373 Accepted Submission(s): 1461
Problem Description
There is a tree with n nodes, each of which has a type of color represented by an integer, where the color of node i is ci.
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.
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.
Input
The input contains multiple test cases.
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.
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.
Output
For each test case, output "Case #x: y" in one line (without quotes), where x indicates the case number starting from 1 and y 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
Source
#pragma GCC optimize(2) #include <bits/stdc++.h> #define lowbit(x) x&(-x) typedef long long ll; using namespace std; const int maxn = 2e5 + 100000; ll n; ll siz[maxn], sum[maxn], cnt; ll color[maxn], vis[maxn]; struct node { int to, nx; } o[maxn << 1]; int head[maxn]; ll res; inline void add_edge(int u, int v) { o[++cnt] = (node) {v, head[u]}; head[u] = cnt; } inline void solve(int cur, int fa) { siz[cur] = 1; ll pre = sum[color[cur]]; ll e = 0; for (register int i = head[cur]; i; i = o[i].nx) { int to = o[i].to; if (to == fa)continue; solve(to, cur); siz[cur] += siz[to]; ll nx = sum[color[cur]] - pre; ll fur = nx; nx = siz[to] - nx; res -= nx * (nx - 1) / 2; pre = sum[color[cur]]; e += fur; } sum[color[cur]] += siz[cur] - e; } int Case; int main() { #ifndef ONLINE_JUDGE freopen("1.txt", "r", stdin); #endif while (scanf("%lld", &n) != EOF) { ll tot = 0; cnt = 0; memset(head, 0, sizeof(head)); memset(vis, 0, sizeof(vis)); memset(sum,0,sizeof(sum)); for (register int i = 1; i <= n; ++i) { scanf("%d", &color[i]); if (!vis[color[i]]) { ++tot; vis[color[i]] = 1; } } for (register int i = 1, u, v; i < n; ++i) { scanf("%d%d", &u, &v); add_edge(u, v); add_edge(v, u); } res = tot * n * (n - 1) / 2; solve(1, -1); for (register int i = 1; i <= n; ++i) { if (!sum[i])continue; ll leave = n - sum[i]; res -= leave * (leave - 1) / 2; } printf("Case #%d: ", ++Case); printf("%lld\n", res); } return 0; }
原文地址:https://www.cnblogs.com/czy-power/p/11451657.html
- 如何买卖股票?不要慌,我有妙招!
- 2017.11.7解题报告
- TensorFlow从0到1 - 11 - 74行Python实现手写体数字识别
- 让priority_queue支持小根堆的几种方法
- 一招解决4道leetcode hard题,动态规划在字符串匹配问题中的应用
- 细数Python中的数据类型以及他们的方法
- 洛谷 P3807 【模板】卢卡斯定理
- 数据城堡参赛代码实战篇(六)---使用sklearn进行数据标准化及参数寻优
- 震惊!Vector两行代码求逆序对,六行代码过普通平衡树
- 数据城堡参赛代码实战篇(五)---使用sklearn解决分类问题
- 洛谷P1894 [USACO4.2]完美的牛栏The Perfect Stall
- [编程经验]Python生成器、迭代器与yield语句小结
- TensorFlow从0到1 - 12 - TensorFlow构建3层NN玩转MNIST
- 数据城堡参赛代码实战篇(四)---使用pandas合并数据表
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 如何使用python记录室友的抖音在线时间
- 使用keras实现BiLSTM+CNN+CRF文字标记NER
- 图解MyBatis的SQL执行流程
- Keras: model实现固定部分layer,训练部分layer操作
- 在Keras中CNN联合LSTM进行分类实例
- DeepWalk:图网络与NLP的巧妙融合
- PHP扩展mcrypt实现的AES加密功能示例
- PHP PDOStatement::bindColumn讲解
- PHP PDOStatement::fetchAll讲解
- PHP PDOStatement::bindParam讲解
- PHP date()格式MySQL中插入datetime方法
- Python sklearn中的.fit与.predict的用法说明
- 基于python实现ROC曲线绘制广场解析
- Python建造者模式案例运行原理解析
- PHP PDOStatement::errorInfo讲解