hdu 5029 Relief grain(树剖好题)

Relief grain

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)
Total Submission(s): 3085    Accepted Submission(s): 903


Problem Description
The soil is cracking up because of the drought and the rabbit kingdom is facing a serious famine. The RRC(Rabbit Red Cross) organizes the distribution of relief grain in the disaster area.

We can regard the kingdom as a tree with n nodes and each node stands for a village. The distribution of the relief grain is divided into m phases. For each phases, the RRC will choose a path of the tree and distribute some relief grain of a certain type for every village located in the path.

There are many types of grains. The RRC wants to figure out which type of grain is distributed the most times in every village.
 

Input
The input consists of at most 25 test cases.

For each test case, the first line contains two integer n and m indicating the number of villages and the number of phases.

The following n-1 lines describe the tree. Each of the lines contains two integer x and y indicating that there is an edge between the x-th village and the y-th village.
  
The following m lines describe the phases. Each line contains three integer x, y and z indicating that there is a distribution in the path from x-th village to y-th village with grain of type z. (1 <= n <= 100000, 0 <= m <= 100000, 1 <= x <= n, 1 <= y <= n, 1 <= z <= 100000)

The input ends by n = 0 and m = 0.
 

Output
For each test case, output n integers. The i-th integer denotes the type that is distributed the most times in the i-th village. If there are multiple types which have the same times of distribution, output the minimal one. If there is no relief grain in a village, just output 0.
 

Sample Input
 
 
2 41 21 1 11 2 22 2 22 2 15 31 23 13 45 32 3 31 5 23 3 30 0
 

Sample Output
 
 
1223302


题意:现在有n个村庄,n-1条边,使村庄连接成树,m个操作,每次 u到v的最短路上的村庄w种类粮食都增加1,

最后输出每个村庄数量最大的粮食是哪个种类,如果没有粮食输出0


分析:由于m个操作中全是更新,没有查询,那么就可以离线处理,由于每次更新的都是一条重链或是一点(dfs序从小到大),可以像维护前缀和一样,u 该种类粮食数+1,v+1 该种类粮食数-1,到最后一块更新。由于粮食有很多种类,于是需要开个vetcor记录一下这点都进行了 哪些更新

最后从小到大扫过去,线段树维护当前点每个种类的粮食个数,个数最大的是哪个种类,依次输出

#include<bits/stdc++.h>
using namespace std;
const int N=1e5+200;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long ll;
struct edge
{
    int v,next;
} e[N<<1];
int siz[N],tid[N],rnk[N],top[N],faz[N],son[N],dep[N];
int first[N],tot,cnt,nn,ans[N];;
vector<pair<int,int> >g[N];
struct node
{
    int l,r,maxx,id;
    int mid()
    {
        return (l+r)>>1;
    }
} t[N<<2];
void add(int u,int v)
{
    e[tot].v=v;
    e[tot].next=first[u];
    first[u]=tot++;
}
void dfs1(int u,int father,int depth)
{
    siz[u]=1;
    dep[u]=depth;
    faz[u]=father;
    for(int i=first[u]; ~i; i=e[i].next)
    {
        int v=e[i].v;
        if(v!=father)
        {
            dfs1(v,u,depth+1);
            siz[u]+=siz[v];
            if(son[u]==-1||siz[v]>siz[son[u]])
                son[u]=v;
        }
    }
}
void dfs2(int u,int t)
{
    tid[u]=++cnt;
    rnk[cnt]=u;
    top[u]=t;
    if(son[u]==-1)
        return;
    dfs2(son[u],t);
    for(int i=first[u]; ~i; i=e[i].next)
    {
        int v=e[i].v;
        if(v!=faz[u]&&v!=son[u])
            dfs2(v,v);
    }
}
void pushup(int rt)
{
    t[rt].maxx=max(t[rt<<1].maxx,t[rt<<1|1].maxx);
    if(t[rt<<1].maxx>=t[rt<<1|1].maxx)
        t[rt].id=t[rt<<1].id;
    else
            t[rt].id=t[rt<<1|1].id;
}
void build(int l,int r,int rt)
{
    t[rt].l=l,t[rt].r=r;
    if(l==r)
    {
        t[rt].maxx=0;
        t[rt].id=l;
        return;
    }
    int m=t[rt].mid();
    build(l,m,rt<<1);
    build(m+1,r,rt<<1|1);
    pushup(rt);
}
void update(int q,int rt,int w)
{
    if(t[rt].l==t[rt].r)
    {
        t[rt].maxx+=w;
        return;
    }
    int m=t[rt].mid();
    if(q<=m)update(q,rt<<1,w);
    else update(q,rt<<1|1,w);
    pushup(rt);
}

void update_path(int u,int v,int w)
{
    while(top[u]!=top[v])
    {
        if(dep[top[u]]<dep[top[v]])swap(u,v);
        g[tid[top[u]]].push_back(make_pair(w,1));
        g[tid[u]+1].push_back(make_pair(w,-1));
        u=faz[top[u]];
    }
    if(tid[u]>tid[v])swap(u,v);
    g[tid[u]].push_back(make_pair(w,1));
    g[tid[v]+1].push_back(make_pair(w,-1));
}

void init(int n)
{
    for(int i=1; i<=n; i++)g[i].clear();
    mem(son,-1);
    mem(first,-1);
    tot=cnt=0;
}

int main()
{
    int n,u,v,w,m;
    while(scanf("%d%d",&n,&m)&&n+m)
    {
        nn=0;
        init(n);
        for(int i=2; i<=n; i++)
        {
            scanf("%d%d",&u,&v);
            add(u,v);
            add(v,u);
        }
        dfs1(1,1,1);
        dfs2(1,1);

        while(m--)
        {
            scanf("%d%d%d",&u,&v,&w);
            nn=max(nn,w);
            update_path(u,v,w);
        }
        build(1,100000,1);
        for(int i=1; i<=n; i++)
        {
            for(int j=0; j<g[i].size(); j++)
            {
                update(g[i][j].first,1,g[i][j].second);
            }
            if(t[1].maxx)
            ans[rnk[i]]=t[1].id;
            else
                ans[rnk[i]]=0;
        }
        for(int i=1;i<=n;i++)
            printf("%d\n",ans[i]);
    }
    return 0;
}


深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值