【Codeforces-600E】Lomsat gelral(dsu on tree)

本文介绍了一种利用树形DSU数据结构解决支配颜色和问题的方法,该问题涉及在给定的树结构中,寻找每个节点子树内的支配颜色总和。通过巧妙的启发式合并策略,算法能在合理的时间复杂度内高效解决问题。

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.

Input
The first line contains integer n(1 ≤ n ≤ 105n (1 ≤ n ≤ 10^5n(1n105 $) — the number of vertices in the tree.

The second line contains n integersci(1&lt;=ci&lt;=n)c_i(1&lt;=c_i&lt;=n)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.

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

Examples
input
4
1 2 3 4
1 2
2 3
2 4
output
10 9 3 4

input
15
1 2 3 1 2 3 3 1 1 3 2 2 1 2 3
1 2
1 3
1 4
1 14
1 15
2 5
2 6
2 7
3 8
3 9
3 10
4 11
4 12
4 13
output
6 5 4 3 2 3 3 1 1 3 2 2 1 2 3

这题比较牛逼,我第一次接触这种类型的题,其实算是一种启发式合并的问题,dsu on tree可以解决一类静态树上的问题。
代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<set>
#define ll long long
#define maxx 100005
#define INF 0x7f7f7f7f
using namespace std;
int n;
int head[maxx],to[maxx<<1],_next[maxx<<1];
int edge;
inline void addEdge(int x,int y)
{
    to[++edge]=y,_next[edge]=head[x],head[x]=edge;
    to[++edge]=x,_next[edge]=head[y],head[y]=edge;
}
//����
int _size[maxx];
int son[maxx];
void dfs1(int u,int fa)
{
    _size[u]=1;
    int maxson=-1;
    for(int i=head[u];i;i=_next[i])
    {
        int v=to[i];
        if(v==fa)continue;
        dfs1(v,u);
        _size[u]+=_size[v];
        if(_size[v]>maxson)
        {
            son[u]=v;
            maxson=_size[v];
        }
    }
}
int l[maxx],r[maxx],_index;
int a[maxx],b[maxx];
void dfs2(int u,int fa)
{
    l[u]=++_index;
    a[_index]=b[u];
    for(int i=head[u];i;i=_next[i])
    {
        int v=to[i];
        if(v==fa)continue;
        dfs2(v,u);
    }
    r[u]=_index;
}
int cnt[maxx];
ll res=0;
int _max=0;
void del(int u)
{
    res=0;
    _max=0;
    for(int i=l[u];i<=r[u];i++)
        cnt[a[i]]=0;
}
void add(int u)
{
    for(int i=l[u];i<=r[u];i++)
    {
        cnt[a[i]]++;
        if(cnt[a[i]]>_max)
        {
            res=a[i];
            _max=cnt[a[i]];
        }
        else if(cnt[a[i]]==_max)res+=a[i];
    }
}
ll ans[maxx];
void dfs(int u,int fa)
{
    for(int i=head[u];i;i=_next[i])
    {
        int v=to[i];
        if(v==fa)continue;
        if(son[u]!=v)
        {
            dfs(v,u);
            del(v);
        }
    }
    if(son[u])dfs(son[u],u);
    for(int i=head[u];i;i=_next[i])
    {
        int v=to[i];
        if(v==fa)continue;
        if(son[u]!=v)add(v);
    }
    cnt[b[u]]++;
    if(cnt[b[u]]>_max)
    {
        res=b[u];
        _max=cnt[b[u]];
    }
    else if(cnt[b[u]]==_max)res+=b[u];
    ans[u]=res;
}
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)scanf("%d",b+i);
    int x,y;
    for(int i=1;i<n;i++)
    {
        scanf("%d%d",&x,&y);
        addEdge(x,y);
    }
    dfs1(1,0);
    dfs2(1,0);
    dfs(1,0);
    for(int i=1;i<n;i++)
        printf("%I64d ",ans[i]);
    printf("%I64d\n",ans[n]);
    return 0;
}

内容概要:本文介绍了基于贝叶斯优化的CNN-LSTM混合神经网络在时间序列预测中的应用,并提供了完整的Matlab代码实现。该模型结合了卷积神经网络(CNN)在特征提取方面的优势与长短期记忆网络(LSTM)在处理时序依赖问题上的强大能力,形成一种高效的混合预测架构。通过贝叶斯优化算法自动调参,提升了模型的预测精度与泛化能力,适用于风电、光伏、负荷、交通流等多种复杂非线性系统的预测任务。文中还展示了模型训练流程、参数优化机制及实际预测效果分析,突出其在科研与工程应用中的实用性。; 适合人群:具备一定机器学习基基于贝叶斯优化CNN-LSTM混合神经网络预测(Matlab代码实现)础和Matlab编程经验的高校研究生、科研人员及从事预测建模的工程技术人员,尤其适合关注深度学习与智能优化算法结合应用的研究者。; 使用场景及目标:①解决各类时间序列预测问题,如能源出力预测、电力负荷预测、环境数据预测等;②学习如何将CNN-LSTM模型与贝叶斯优化相结合,提升模型性能;③掌握Matlab环境下深度学习模型搭建与超参数自动优化的技术路线。; 阅读建议:建议读者结合提供的Matlab代码进行实践操作,重点关注贝叶斯优化模块与混合神经网络结构的设计逻辑,通过调整数据集和参数加深对模型工作机制的理解,同时可将其框架迁移至其他预测场景中验证效果。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值