链接:https://ac.nowcoder.com/acm/contest/904/E

来源:牛客网

DongDong数颜色

时间限制:C/C++ 1秒,其他语言2秒

空间限制:C/C++ 524288K,其他语言1048576K

64bit IO Format: %lld

题目描述

DongDong是个喜欢数颜色的女孩子,她已经熟练地掌握了在序列上数颜色的操作,现在她开始学习如何在树上数颜色,现在给定一个n个点,n-1条边的树形图(视1号店为根),每个点有一个颜色,每次询问以x为根的子树中有多少种不同的颜色,DongDong轻松地解决了这个问题,但她想考考会编程的你。

输入描述:

第一行两个整数n,m

第二行n个整数,表示每个点的颜色

接下来n-1行每行u,v,表示存在一条从u到v的双向边(保证最终图形是树形图)

2<=n<=100000,1<=m,color<=n,

输出描述:

共m行:每行输出相应询问的答案

示例1

输入

复制

4 3

1 1 2 3

1 2

2 3

1 4

1

2

4

输出

复制

3

2

1

思路:

询问的都是子树的问题,很经典的dsu on tree,

和这题比较像:https://www.cnblogs.com/qieqiemin/p/11309973.html

把add 里维护答案的部分改一下即可。

细节见代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
inline void getInt(int* p);
const int maxn = 100010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/ int n;
std::vector<int> son[maxn];
int wson[maxn];
int SZ[maxn];
int a[maxn];
void dfs1(int x,int pre)
{
SZ[x]=1;
int maxson=-1;
for(auto y:son[x])
{
if(y!=pre)
{
dfs1(y,x);
SZ[x]+=SZ[y];
if(maxson<SZ[y])
{
maxson=SZ[y];
wson[x]=y;
}
}
}
}
ll ans[maxn];
ll sum;
int isson;
int m;
ll cnt[maxn];
void add(int x,int pre,int val)
{
if(cnt[a[x]])
{
if(val==-1)
{
cnt[a[x]]=0;
sum--;
}
}
if(cnt[a[x]]==0)
{
if(val==1)
{
cnt[a[x]]=1;
sum++;
}
}
for(auto y:son[x])
{
if(y==pre||y==isson)
continue;
add(y,x,val);
}
}
void dfs2(int x,int pre,int op)
{
for(auto y:son[x])
{
if(y==pre||y==wson[x])
{
continue;
}
dfs2(y,x,0);
}
if(wson[x])
{
dfs2(wson[x],x,1);
isson=wson[x];
}
add(x,pre,1);
isson=0;
ans[x]=sum;
if(op==0)
{
add(x,pre,-1);
sum=0;
m=0;
}
}
int main()
{
//freopen("D:\\code\\text\\input.txt","r",stdin);
//freopen("D:\\code\\text\\output.txt","w",stdout);
gg(n);
int q;
gg(q);
repd(i,1,n)
{
gg(a[i]);
}
int u,v;
repd(i,2,n)
{
gg(u);gg(v);
son[u].pb(v);
son[v].pb(u);
}
dfs1(1,0);
dfs2(1,0,0);
int x;
repd(i,1,q)
{
gg(x);
printf("%lld\n",ans[x] );
}
// printf("\n"); return 0;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '0');
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 - ch + '0';
}
}
else {
*p = ch - '0';
while ((ch = getchar()) >= '0' && ch <= '9') {
*p = *p * 10 + ch - '0';
}
}
}

牛客练习赛47 E DongDong数颜色 (树上启发式合并)的更多相关文章

  1. 牛客练习赛47 E DongDong数颜色 (树状数组维护区间元素种类数)

    链接:https://ac.nowcoder.com/acm/contest/904/E 来源:牛客网 DongDong数颜色 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 5242 ...

  2. 【Luogu U41492】树上数颜色——树上启发式合并(dsu on tree)

    (这题在洛谷主站居然搜不到--还是在百度上偶然看到的) 题目描述 给一棵根为1的树,每次询问子树颜色种类数 输入输出格式 输入格式: 第一行一个整数n,表示树的结点数 接下来n-1行,每行一条边 接下 ...

  3. 牛客练习赛47 D DongDong坐飞机 (分层最短路)

    链接:https://ac.nowcoder.com/acm/contest/904/D 来源:牛客网 DongDong坐飞机 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 5242 ...

  4. 牛客练习赛47 A DongDong破密码 (异或性质,递推)

    链接:https://ac.nowcoder.com/acm/contest/904/A 来源:牛客网 DongDong破密码 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1310 ...

  5. 牛客练习赛47 DongDong数颜色 (莫队算法)

    链接:https://ac.nowcoder.com/acm/contest/904/E 来源:牛客网 DongDong数颜色 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 5242 ...

  6. 牛客挑战赛40 VMware和基站 set 二分 启发式合并 区间覆盖

    LINK:VMware和基站 一道 做法并不常见的题目 看起来很难写 其实set维护线段就可以解决了. 容易想到 第二个操作借用启发式合并可以得到一个很不错的复杂度 不过利用线段树维护这个东西 在区间 ...

  7. 牛客-DongDong数颜色 及其相似题

    大佬博客 ps:在牛客上做到这题不会,学会之后补了两道相关题.顺便记录一下. 牛客-DongDong数颜色 sol:dfs序+莫队,先把树上的点标上dfs序,因为子树的dfs序是连续的,所以子树可以表 ...

  8. 牛客练习赛31 B 赞迪卡之声妮莎与奥札奇 逻辑,博弈 B

    牛客练习赛31 B 赞迪卡之声妮莎与奥札奇 https://ac.nowcoder.com/acm/contest/218/B 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 2621 ...

  9. 牛客练习赛51 **E-数列** (二分,贪心,构造)

    牛客练习赛51 E-数列 链接:https://ac.nowcoder.com/acm/contest/1083/E来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 327 ...

随机推荐

  1. yum安装epel源

    国内yum源的安装(163,阿里云,epel)   国内yum源的安装(163,阿里云,epel) ----阿里云镜像源 1.备份 mv /etc/yum.repos.d/CentOS-Base.re ...

  2. python配置主机名

    .准备hosts模板 mkdir -p /k8s/profile cat >/k8s/profile/hosts<<EOF 192.168.0.91 test1 192.168.0. ...

  3. Tor路径选择说明

    Tor Path Specification Roger Dingledine Nick Mathewson Note: This is an attempt to specify Tor as cu ...

  4. 【ABAP系列】SAP Smartforms 设置纸张打印格式

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP Smartforms 设 ...

  5. 【AMAD】django-model-utils -- Django model使用的mixin和utils

    动机 简介 个人评分 动机 为django model系统提供一些可重用的mixin和utils. 简介 django-model-utils1为Django Model提供了下嘛几种分类的utils ...

  6. 【DSP开发】【VS开发】PCIE设备扫描过程

    初步了解完PCI总线标准之后,我们接下来正式开始PCIe设备的漫游之旅.从我们按下PC的电源按钮开始,BIOS就接管系统控制权开始工作,它会先进行一些内存和设备的初始化工作(当然,也包括我们的PCI设 ...

  7. 【VS开发】【DSP开发】TCP和UDP数据包结构

    TCP (Transport Control Protocol)传输控制协议: 1.TCP数据包的分组格式: A,源端口:标识源端应用进程. B, 目的端口:标识目的端应用进程. C, 序号:在SYN ...

  8. Mysql事务代码

    /// <summary> /// 删除相册 /// </summary> /// <param name="id"></param> ...

  9. linux下安转nodejs

    转载自:https://www.cnblogs.com/zhuawang/p/7617176.html 在Linux系统安装Nodejs 最简单步骤 1.去官网下载和自己系统匹配的文件: 英文网址:h ...

  10. RPC基本原理

    RPC非常重要,很多人面试的时候都挂在了这个地方!你要是还不懂RPC是什么?他的基本原理是什么?你一定要把下边的内容记起来!好好研究一下!特别是文中给出的一张关于RPC的基本流程图,重点中的重点,Du ...