给你一棵N个节点的树(无向无环连通图),每个节点有一个颜色。你知道树的根节点(树的根节点是一个特殊的节点,是树节点中唯一没有父节点的)。颜色用1到109范围内的整数表示。现在,你需要回答M个查询,每个查询询问以节点s为根的子树节点中,不同颜色的数目。(s会在输入中给出)。

输入格式

第一行包含3个空格分隔的整数 (N M root)表示树的节点数、查询数和根节点。

接下来N行,每行包含着2个空格分隔的整数(a b)表示一条从节点a到节点b的边。

接下来N行, 第i行表示第i个节点的颜色。

接下来M行,每行包含一个整数s。

输出格式

输出M行,每行输出对应查询的结果。

约束条件

0=< M <= 105
1=< N <= 105
1=< root <= N
1=< 节点颜色 <= 109

样例

输入样例

4 2 1
1 2
2 4
2 3
10
20
20
30
1
2
输出样例

3
2
解释

查询1 以1为根的子树就是整个树,包含颜色10 20 20 30, 所以结果是3(10,20 and 30)。

查询2 以2为节点的子树包含颜色20 20 30, 所以答案是2(20 and 30)。

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <set>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 100010;struct Edge {int v, next;Edge() { }Edge(int _v, int _next) : v(_v), next(_next) { }
}edges[2*maxn];
int head[maxn], edge_num;
int color[maxn], ans[maxn], id[maxn];
set<int> ver[maxn];
int n, m, r;void init_graph() {edge_num = 0;memset(head, -1, sizeof(head));
}void add_edge(int u, int v) {edges[edge_num].v = v;edges[edge_num].next = head[u];head[u] = edge_num++;edges[edge_num].v = u;edges[edge_num].next = head[v];head[v] = edge_num++;
}void merge(int &u, int &v) {if(ver[u].size() < ver[v].size()) swap(u, v);for(set<int>::iterator it = ver[v].begin(); it != ver[v].end(); ++it) {ver[u].insert(*it);}return ;
}void dfs(int u, int f) {ver[id[u]].insert(color[u]);for(int i = head[u]; i != -1; i = edges[i].next) {int v = edges[i].v;if(v == f) continue;dfs(v, u);merge(id[u], id[v]);    }ans[u] = ver[id[u]].size();return ;
}int main() {/* Enter your code here. Read input from STDIN. Print output to STDOUT */   int u, v;init_graph();scanf("%d %d %d", &n, &m, &r);for(int i = 1; i < n; ++i) {scanf("%d %d", &u, &v);add_edge(u, v);}for(int i = 1; i <= n; ++i) {id[i] = i;scanf("%d", &color[i]);}dfs(r, -1);for(int i = 0; i < m; ++i) {scanf("%d", &u);printf("%d\n", ans[u]);}return 0;
}

Coloring Tree相关推荐

  1. [GitHub] 75+的 C# 数据结构和算法实现

    C#中标准数据结构和算法的即插即用类库项目 GitHub:https://github.com/aalhour/C-Sharp-Algorithms Watch: 307  Star: 3.4k Fo ...

  2. 牛客网暑期ACM多校训练营(第三场)

    牛客网暑期ACM多校训练营(第三场) A. PACM Team 01背包,输出方案,用bool存每种状态下用的哪一个物品,卡内存.官方题解上,说用char或者short就行了.还有一种做法是把用的物品 ...

  3. 数论三之排列组合Ⅱ——Virus Tree 2,RGB Coloring,123 Triangle,排列计数,排队,卡农

    丝且人一口 Virus Tree 2 description solution code RGB Coloring description solution code 123 Triangle des ...

  4. Leetcode-5148 Binary Tree Coloring Game(二叉树着色游戏)

    这题还是有点意思的,因为只能从第一个染色节点开始拓展,所以作为后手的B,一定是将A的第一个染色节点的父亲或者两个儿子染色,堵住A的路,这样才有可能获胜. 所以B占据的区域要么是除A占据的子树以外的部分 ...

  5. POJ 2054 Color a Tree解题报告

    题干 Bob is very interested in the data structure of a tree. A tree is a directed graph in which a spe ...

  6. POJ 2054 Color a Tree

    贪心....                    Color a Tree Time Limit: 1000MS   Memory Limit: 30000K Total Submissions:  ...

  7. CodeForces 711C - Coloring Trees DP

    /*http://codeforces.com/problemset/problem/711/C http://codeforces.com/blog/entry/46830官方题解:We compu ...

  8. 机器学习论文:《LightGBM: A Highly Efficient Gradient Boosting Decision Tree》

    翻译自<LightGBM: A Highly Efficient Gradient Boosting Decision Tree> 摘要 Gradient Boosting Decisio ...

  9. 107. Binary Tree Level Order Traversal II

    题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...

  10. 102. Binary Tree Level Order Traversal

    题目 Binary Tree Level Order Traversal 层次遍历二叉树 链接 Given a binary tree, return the level order traversa ...

最新文章

  1. NODEJS项目实践0.4 [domain,pm2,log4js,md5]
  2. 保护模式及其编程——8086系统寄存器和系统指令
  3. Configuration Extensions - 简化配置,让你配置支持变量
  4. python循环经典例题_python练习题:循环打印嵌套列表
  5. ubuntu软件安装 caffe相关软件安装 学习笔记
  6. 贝叶斯信念网络和马尔科夫链有什么区别
  7. html+lt;input+file样式,不同内核的浏览器中文件选择控件的外观也不相同
  8. Python菜鸟入门:day18编程学习
  9. TopCoder中插件的用法
  10. scala中的if esle使用
  11. MATLAB 图像处理基础(1)
  12. poj 2406 Power Strings(KMP)
  13. opengl 纹理贴到对应的位置_OpenGL常用命令备忘录(Part B)
  14. tsp问题——遗传算法解决
  15. python中,获取设备网卡接口(ethx, mgmt)
  16. BUUCTF misc 二维码1解题思路
  17. 练习题:千克转换成磅
  18. android设备判断是否支持NFC功能
  19. 从键盘读入一个字符,如果该字符是大写字母则转小写,如果该字符是小写字母则转大写,如果不是字符则输出不是字母。
  20. 读取xlsx文件到list

热门文章

  1. 标准差(Standard Deviation) 和 标准误差(Standard Error)
  2. 腾达路由器登录远端服务器无响应,腾达路由器 192.168.0.1(tendawifi.com)打不开,怎么办?...
  3. python+pillow不规则图片合成合并
  4. 天猫为海澜之家打造“智慧门店”;东方网力联手电子科技大学,共建人工智能联合实验室...
  5. linux教程试卷_linux基础教程试卷及答案.doc
  6. windows xp sp3
  7. 计算机无线鼠标重启才能连上,无线鼠标电脑重启后无反应
  8. Outlook添加新浪邮箱时的配置细节——登录密码
  9. 六、T100固定资产之固定资产月结处理
  10. Andriod.mk用法