/*树上启发式合并*/
import java.util.*;
public class Main {
static int maxn = 200005, n;
static int[] sz = new int[maxn], big = new int[maxn], col = new int[maxn], L = new int[maxn], R = new int[maxn];
static int totdfn = 0, totcolor = 0;
static int[] Node = new int[maxn], ans = new int[maxn], cnt = new int[maxn];
static ArrayList<ArrayList<Integer>> tr;
//全局变量用static,不可用var,指明变量类型
static void add(int u) {
if (cnt[col[u]] == 0) ++totcolor;
cnt[col[u]]++;
}
static void del(int u) {
cnt[col[u]]--;
if (cnt[col[u]] == 0) totcolor--;
}
static void dfs1(int u, int p) {
L[u] = ++totdfn;
Node[totdfn] = u;
sz[u] = 1;
for (var v : tr.get(u)) {
if (v != p) {
dfs1(v, u);
sz[u] += sz[v];
if (big[u] == 0 || sz[big[u]] < sz[v]) {
big[u] = v;
}
}
}
R[u] = totdfn;
}
static void dfs2(int u, int p, boolean keep) {
for (var v : tr.get(u)) {
if (v != p && v != big[u]) {
dfs2(v, u, false);
}
}
if (big[u] != 0) {
dfs2(big[u], u, true);
}
for (var v : tr.get(u)) {
if (v != p && v != big[u]) {
for (int i = L[v]; i <= R[v]; i++) {
add(Node[i]);
}
}
}
add(u);
ans[u] = totcolor;
if (!keep) {
for (int i = L[u]; i <= R[u]; i++) {
del(Node[i]);
}
}
}
public static void main(String[] args) {
var sc = new Scanner(System.in);
n = sc.nextInt();
tr = new ArrayList<>();//指明tr类型后,template可以缺省
for (int i = 1; i <= n + 1; i++) {
tr.add(new ArrayList<>());
}
for (int i = 1; i < n; i++) {
int u, v;
u = sc.nextInt();
v = sc.nextInt();
tr.get(u).add(v);
tr.get(v).add(u);
}
for (int i = 1; i <= n; i++) col[i] = sc.nextInt();
var m = sc.nextInt();
dfs1(1, 0);
dfs2(1, 0, false);
for (int i = 1; i <= m; i++) {
var q = sc.nextInt();
System.out.println(ans[q]);
}
}
};
没有合适的资源?快使用搜索试试~ 我知道了~
C++算法竞赛题目整理,模板归类
共863个文件
cpp:798个
java:23个
hpp:19个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
5星 · 超过95%的资源 1 下载量 97 浏览量
2024-01-26
07:48:31
上传
评论
收藏 745KB ZIP 举报
温馨提示
主要涵盖题目整理,模板归类等等。 题目索引 1. 英文网站名缩写 + (比赛 : 题号) / 题号 2. 比赛 + 题号 模板整理 数据结构与方法服从Java驼峰式命名。如quickPower(方法名), SegmentTree(数据结构名)等 封装便于多 Test 时在函数中创建对象, 避免暴力清空数组造成时间浪费。
资源推荐
资源详情
资源评论
收起资源包目录
C++算法竞赛题目整理,模板归类 (863个子文件)
NC2023多校7F.cpp 7KB
LGP3373.cpp 7KB
LC2617.cpp 6KB
ABC298E CF148D.cpp 5KB
LGP9453.cpp 5KB
NC练习赛104D.cpp 5KB
NC练习赛104D.cpp 5KB
ABC322 F.cpp 4KB
LGB2646.cpp 4KB
LC224 227 772.cpp 4KB
2023NC基础集训7I.cpp 4KB
NC231121.cpp 4KB
LC2646.cpp 4KB
NC231121.cpp 4KB
Libre10138.cpp 4KB
NC练习赛7C.cpp 4KB
LC2801.cpp 3KB
LGP4556.cpp 3KB
LGP4648.cpp 3KB
LGP4648.cpp 3KB
NC小白73E.cpp 3KB
ABC294G.cpp 3KB
LC2940.cpp 3KB
LC2646.cpp 3KB
CF 1884C.cpp 3KB
ABC322F.cpp 3KB
NC小白75F.cpp 3KB
ACW1171.cpp 3KB
CF896 C.cpp 3KB
LC2646.cpp 3KB
LC2646.cpp 3KB
OUC树的同构.cpp 3KB
LC2850.cpp 3KB
LGP1253.cpp 3KB
LC2852.cpp 3KB
2023NC基础集训4F.cpp 3KB
LC2916.cpp 3KB
2023NC多校5H.cpp 3KB
LC2827.cpp 3KB
LC2736.cpp 3KB
NC练习赛13E.cpp 3KB
CF 1833E.cpp 3KB
2023NC多校集训6E.cpp 3KB
NC21337.cpp 3KB
LGP3806.cpp 3KB
ABC194E.cpp 3KB
NC练习赛9F.cpp 3KB
NC练习赛9F.cpp 3KB
ABC282D.cpp 3KB
LG P3387.cpp 3KB
LG P3387.cpp 3KB
LG P3387.cpp 3KB
LGP2801.cpp 3KB
ATC diverta 2019 Programming Contest E.cpp 3KB
ABC326 F.cpp 3KB
NC练习赛60C.cpp 3KB
NC练习赛7E.cpp 3KB
NC练习赛35B.cpp 3KB
NC周赛16 D.cpp 3KB
ABC301E.cpp 3KB
NC练习赛49D.cpp 3KB
ABC285F.cpp 3KB
HeavyPathDecomposition.cpp 3KB
NC2023河南多校8I.cpp 3KB
LGP2704.cpp 3KB
NC2023河南多校8I.cpp 3KB
LC847.cpp 3KB
LC847.cpp 3KB
ABC305G.cpp 3KB
ABC235E.cpp 2KB
ABC305G.cpp 2KB
ABC222E.cpp 2KB
MTJBD202319.cpp 2KB
NC练习赛7E.cpp 2KB
LC2836.cpp 2KB
NC2020多校5 B.cpp 2KB
2023NC基础集训2C.cpp 2KB
LGP1314.cpp 2KB
NC周赛21 D.cpp 2KB
ACW214.cpp 2KB
ABC331 F.cpp 2KB
ABC314E.cpp 2KB
LC1044.cpp 2KB
Libre10229.cpp 2KB
ABC331 F.cpp 2KB
NC小白赛61C.cpp 2KB
NC小白赛61C.cpp 2KB
LC2699.cpp 2KB
NC练习赛25B.cpp 2KB
NC222283.cpp 2KB
ACW91.cpp 2KB
LC2286.cpp 2KB
ABC327F.cpp 2KB
ABC273F.cpp 2KB
NC练习赛4C.cpp 2KB
2023NC基础集训6B.cpp 2KB
LC150.cpp 2KB
牛客小白2D.cpp 2KB
ABC326E.cpp 2KB
LGP1725.cpp 2KB
共 863 条
- 1
- 2
- 3
- 4
- 5
- 6
- 9
资源评论
- weixin_401186372024-05-17资源简直太好了,完美解决了当下遇到的难题,这样的资源很难不支持~
十小大
- 粉丝: 9826
- 资源: 2556
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功