当前位置: 首页 > news >正文

Lomsat gelral 树上启发式合并

题目描述

You are given a rooted tree with root in vertex 1 . Each vertex is coloured in some colour.

Let's call colour c dominating in the subtree of vertex v if there are no other colours that appear in the subtree of vertex v more times than colour c . So it's possible that two or more colours will be dominating in the subtree of some vertex.

The subtree of vertex v is the vertex v and all other vertices that contains vertex v in each path to the root.

For each vertex v find the sum of all dominating colours in the subtree of vertex v .

输入格式

The first line contains integer n ( 1<=n<=105 ) — the number of vertices in the tree.

The second line contains n integers ci​ ( 1<=ci​<=n ), ci​ — the colour of the i -th vertex.

Each of the next n−1 lines contains two integers xj​,yj​ ( 1<=xj​,yj​<=n ) — the edge of the tree. The first vertex is the root of the tree.

输出格式

Print n integers — the sums of dominating colours for each vertex.

隐藏翻译

题意翻译

  • 有一棵 n 个结点的以 1 号结点为根的有根树
  • 每个结点都有一个颜色,颜色是以编号表示的, i 号结点的颜色编号为 ci​。
  • 如果一种颜色在以 x 为根的子树内出现次数最多,称其在以 x 为根的子树中占主导地位。显然,同一子树中可能有多种颜色占主导地位。
  • 你的任务是对于每一个 i∈[1,n],求出以 i 为根的子树中,占主导地位的颜色的编号和。
  • n≤105,ci​≤n

洛谷崩掉了,cf交的超时,我也不知道怎么办了

好的现在不超时了。dfs_son函数 原本是return int值 改成void就不超时了

原本的单链表存储改成vector了

代码 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
int n,color[N],cnt[N];
int sz[N],son[N],idx=0;
int mx;
ll ans[N],sum;
vector<vector<int>>g(N);void dfs_son(int u,int father){sz[u]=1;for(auto i:g[u]){if(i==father)continue;dfs_son(i,u);sz[u]+=sz[i];if(sz[i]>sz[son[u]])son[u]=i;}
}void update(int u,int father,int sign,int pson){int c=color[u];cnt[c]+=sign;if(cnt[c]>mx)mx=cnt[c],sum=c;else if(cnt[c]==mx)sum+=c;for(int i:g[u]){if(i==father||i==pson)continue;update(i,u,sign,pson);}
}void dfs(int u,int father,int op){for(auto i:g[u]){if(i==father||i==son[u])continue;dfs(i,u,0);}if(son[u])dfs(son[u],u,1);update(u,father,1,son[u]);ans[u]=sum;if(!op)update(u,father,-1,0),sum=mx=0;
}int main(){ios::sync_with_stdio(false);cin>>n;for(int i=1;i<=n;++i)cin>>color[i];	int x,y;for(int i=0;i<n-1;++i){cin>>x>>y;g[x].push_back(y);g[y].push_back(x);}dfs_son(1,0);dfs(1,0,1);for(int i=1;i<=n;++i)cout<<ans[i]<<" ";return 0;
}

单链表版 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5*2+10;
int n,color[N],cnt[N];
int h[N],e[N],ne[N],sz[N],son[N],idx=0;
int mx;
ll ans[N],sum;void add(int x,int y){e[idx]=y,ne[idx]=h[x],h[x]=idx++;
}void dfs_son(int u,int father){sz[u]=1;for(int i=h[u];~i;i=ne[i]){int j=e[i];if(j==father)continue;dfs_son(j,u);sz[u]+=sz[j];if(sz[j]>sz[son[u]])son[u]=j;}
}void update(int u,int father,int sign,int pson){int c=color[u];cnt[c]+=sign;if(cnt[c]>mx)mx=cnt[c],sum=c;else if(cnt[c]==mx)sum+=c;for(int i=h[u];~i;i=ne[i]){int j=e[i];if(j==father||j==pson)continue;update(j,u,sign,pson);}
}void dfs(int u,int father,int op){for(int i=h[u];~i;i=ne[i]){int j=e[i];if(j==father||j==son[u])continue;dfs(j,u,0);}if(son[u])dfs(son[u],u,1);update(u,father,1,son[u]);ans[u]=sum;if(!op)update(u,father,-1,0),sum=mx=0;
}int main(){scanf("%d",&n);for(int i=1;i<=n;++i)scanf("%d",&color[i]);memset(h,-1,sizeof(h));int x,y;for(int i=0;i<n-1;++i){scanf("%d%d",&x,&y);add(x,y);add(y,x);}dfs_son(1,-1);dfs(1,-1,1);for(int i=1;i<=n;++i)printf("%lld ",ans[i]);return 0;
}

http://www.lryc.cn/news/606530.html

相关文章:

  • Coze是什么?能做什么?
  • Dify插件安装失败,一直处于安装状态?
  • 【智能体agent】入门之--1.初体验
  • OpenCV学习day2
  • RabbitMQ的特点和消息可靠性保障
  • 【neo4j】跨版本升级数据库
  • 《Java 程序设计》第 14 章 - JavaFX 基础
  • MySQL 8.0 OCP 1Z0-908 题目解析(42)
  • 企业级部署 (基于tomcat与nginx)
  • Linux和shell
  • 【运维基础】Linux 文件系统基本管理
  • Side band ECC、Inline ECC、On-die ECC、Link ECC
  • chrome.storage 和 localStorage
  • Android 基础入门学习目录(持续更新)
  • kettle插件-kettle http client plus插件,轻松解决https接口无法调用文件流下载问题
  • 面试笔记【16:9区域问题】
  • SQL注入SQLi-LABS 靶场less25a-28a详细通关攻略
  • ESP32 外设驱动开发指南 (ESP-IDF框架)——GPIO篇:基础配置、外部中断与PWM(LEDC模块)应用
  • 机械学习--逻辑回归
  • 第1章:基础篇——第1节:基础操作与认识界面
  • Vercel 全面介绍与网站部署指南
  • 老旧远程控制管理模块(物联网设备)渗透实战:SNMP泄露+内核提权攻击链深度解析
  • Gold 序列
  • 7月31号打卡
  • nvm安装nodejs后提示No installations recognized
  • 爱普生002墨水与004墨水基本参数及支持机型
  • pyspark使用
  • 火焰图(Flame Graph)深度指南:CPU性能分析与瓶颈定位
  • STM32——HAL 库MDK工程创建
  • 计算机网络知识【推荐!!!】按照OSI七层模型梳理