牛客练习赛47 E DongDong数颜色 (树上启发式合并)
链接: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数颜色 (树上启发式合并)的更多相关文章
- 牛客练习赛47 E DongDong数颜色 (树状数组维护区间元素种类数)
链接:https://ac.nowcoder.com/acm/contest/904/E 来源:牛客网 DongDong数颜色 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 5242 ...
- 【Luogu U41492】树上数颜色——树上启发式合并(dsu on tree)
(这题在洛谷主站居然搜不到--还是在百度上偶然看到的) 题目描述 给一棵根为1的树,每次询问子树颜色种类数 输入输出格式 输入格式: 第一行一个整数n,表示树的结点数 接下来n-1行,每行一条边 接下 ...
- 牛客练习赛47 D DongDong坐飞机 (分层最短路)
链接:https://ac.nowcoder.com/acm/contest/904/D 来源:牛客网 DongDong坐飞机 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 5242 ...
- 牛客练习赛47 A DongDong破密码 (异或性质,递推)
链接:https://ac.nowcoder.com/acm/contest/904/A 来源:牛客网 DongDong破密码 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 1310 ...
- 牛客练习赛47 DongDong数颜色 (莫队算法)
链接:https://ac.nowcoder.com/acm/contest/904/E 来源:牛客网 DongDong数颜色 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 5242 ...
- 牛客挑战赛40 VMware和基站 set 二分 启发式合并 区间覆盖
LINK:VMware和基站 一道 做法并不常见的题目 看起来很难写 其实set维护线段就可以解决了. 容易想到 第二个操作借用启发式合并可以得到一个很不错的复杂度 不过利用线段树维护这个东西 在区间 ...
- 牛客-DongDong数颜色 及其相似题
大佬博客 ps:在牛客上做到这题不会,学会之后补了两道相关题.顺便记录一下. 牛客-DongDong数颜色 sol:dfs序+莫队,先把树上的点标上dfs序,因为子树的dfs序是连续的,所以子树可以表 ...
- 牛客练习赛31 B 赞迪卡之声妮莎与奥札奇 逻辑,博弈 B
牛客练习赛31 B 赞迪卡之声妮莎与奥札奇 https://ac.nowcoder.com/acm/contest/218/B 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 2621 ...
- 牛客练习赛51 **E-数列** (二分,贪心,构造)
牛客练习赛51 E-数列 链接:https://ac.nowcoder.com/acm/contest/1083/E来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 327 ...
随机推荐
- Spring学习之==>AOP
一.概述 AOP(Aspect Oriented Programming)称为面向切面编程,在程序开发中主要用来解决一些系统层面上的问题,比如日志,事务,权限等等,Struts2的拦截器设计就是基于A ...
- 基于Bootstrap 3可预览的HTML5文件上传插件
前端常用资源地址: http://www.htmleaf.com/ http://www.htmleaf.com/html5/html5muban/201505091801.html 源代码地址 ht ...
- XCTF (app1)
打开app.一个文本框,随便输入提示如下图. 打开JEB反编译. v2调用getPackageInfo获取版本信息.一般 Android 通过 PackageInfo 这个类来获取应用安装包信息,比如 ...
- Vi或者Vim下按了ctrl+s后终端卡住了咋办?
在Vi或者Vim下按了ctrl+s后终端卡住了咋办? 习惯了在windows下写程序,也习惯了按ctrl+s 保存代码. 在用vim的时候,也习惯性的按ctrl+s结果就是如同终端死掉了一样. 原因: ...
- TensorFlow自编码器(AutoEncoder)之MNIST实践
自编码器可以用于降维,添加噪音学习也可以获得去噪的效果. 以下使用单隐层训练mnist数据集,并且共享了对称的权重参数. 模型本身不难,调试的过程中有几个需要注意的地方: 模型对权重参数初始值敏感,所 ...
- TensorFlow实战第二课(添加神经层)
莫烦tensorflow实战教学 1.添加神经层 #add_layer() import tensorflow as tf def add_layer(inputs,in_size,out_size, ...
- finereport 填报 单元格 JS 触发 提交SQL 事件
var location = this.options.location; var cr = FR.cellStr2ColumnRow(location); var col = cr.col; var ...
- 关于虚拟机docker 启动mysql 启动成功但未挂载到端口
首先排查了防火墙和其他权限相关问题 然后检查了mysql 用户权限问题 docker logs 查看日志 正常应该是到3306 问题是我的mysql my.cnf 文件是挂在在本地.当第二次启动容器时 ...
- netcore发布的坑
当我选择目标运行时为Linux-64时,生成的接口为第二图, 而当我选择目标运行时为可移植或windows-64时,生成的接口则是正确的.和我写的代码,以及本地按F5启动调试的效果一致. 整个项目从v ...
- HDU 1260 Tickets (动态规划)
Tickets Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...