Language: C

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn=210000;
struct node
{
    int u,next,d;
}a[maxn];
int head[maxn],b[maxn],f[maxn],rec1[maxn],rec2[maxn],max1[maxn],max2[maxn];
bool v[maxn];
int n;
void dp(int x)
{
    if(v[x])return;
    v[x]=true;
    bool flag=false;
    //max1
    for(int i=head[x];i!=-1;i=a[i].next)
    if(!v[a[i].u])
    {
        flag=true;
        dp(a[i].u);
        if(max1[a[i].u]+a[i].d>=max1[x])
        {
            max2[x]=max1[x];
            rec2[x]=rec1[x];
            max1[x]=max1[a[i].u]+a[i].d;
            rec1[x]=a[i].u;
        }
        else if(max1[a[i].u]+a[i].d>max2[x])
        {
            max2[x]=max1[a[i].u]+a[i].d;
            rec2[x]=a[i].u;
        }
    }
    if(!flag)max1[x]=0;
}
void dfs(int x,int father,int d)
{
    v[x]=true;
    if(x==1)f[x]=0;
    else
    {
        if(rec1[father]!=x && max1[father]!=-1)
        f[x]=max1[father]+d;
        else if(rec2[father]!=x && max2[father]!=-1)
        f[x]=max2[father]+d;

        f[x]=max(f[x],f[father]+d);
    }
    for(int i=head[x];i!=-1;i=a[i].next)
    if(!v[a[i].u])
    {
        dfs(a[i].u,x,a[i].d);
    }
}
int bmax[maxn][20], bmin[maxn][20];
void RMQ_init ()
{
    for(int i=1; i<=n; i++) bmax[i][0]=bmin[i][0]=i;
    for(int j=1; (1<<j)<=n; j++)
        for(int i=1; i+(1<<j)-1<=n; i++){
            if(b[bmax[i][j-1]] > b[bmax[i+(1<<(j-1))][j-1]]) bmax[i][j]=bmax[i][j-1];
            else bmax[i][j]=bmax[i+(1<<(j-1))][j-1];

            if(b[bmin[i][j-1]] < b[bmin[i+(1<<(j-1))][j-1]]) bmin[i][j]=bmin[i][j-1];
            else bmin[i][j]=bmin[i+(1<<(j-1))][j-1];
        }
}
int RMQ_max (int L, int R)
{
    int k=0;
    while((1<<(k+1))<= R-L+1) k++;
    if(b[bmax[L][k]] > b[bmax[R-(1<<k)+1][k]]) return bmax[L][k];
    else return bmax[R-(1<<k)+1][k];
}
int RMQ_min (int L, int R)
{
    int k=0;
    while((1<<(k+1))<= R-L+1) k++;
    if(b[bmin[L][k]] < b[bmin[R-(1<<k)+1][k]]) return bmin[L][k];
    else return bmin[R-(1<<k)+1][k];
}
int query (int q)
{
    int ret, minx, maxx, minloc, maxloc, r, l;

    ret=1;
    minx=maxx=b[1];
    maxloc=minloc=1;
    l=r=1;

    while(r<=n){
        if(b[r]>=minx && b[r]<=maxx) r++;
        else{
            if(b[r]<minx){
                if(maxx-b[r]<=q){
                    minx=b[r]; minloc=r;
                    r++;
                }
                else{
                    ret=max(ret, r-l);
                    l=maxloc+1;
                    maxloc=RMQ_max(l, r);
                    maxx=b[maxloc];
                    r++;
                }
            }
            else{
                if(b[r]-minx<=q){
                    maxx=b[r]; maxloc=r;
                    r++;
                }
                else{
                    ret=max(ret, r-l);
                    l=minloc+1;
                    minloc=RMQ_min(l, r);
                    minx=b[minloc];
                    r++;
                }
            }
        }
    }

    return ret;
}
int main()
{
    freopen("data","r",stdin);
    int m;
    while(scanf("%d%d",&n,&m),n|m)
    {
        int num=0;
        for(int i=1;i<=n-1;i++)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            num++;a[num].u=y;a[num].d=z;a[num].next=head[x];head[x]=num;
            num++;a[num].u=x;a[num].d=z;a[num].next=head[y];head[y]=num;
        }
        //pre
        memset(rec1,0,sizeof(rec1));
        memset(rec2,0,sizeof(rec2));
        memset(max1,-1,sizeof(max1));
        memset(max2,-1,sizeof(max2));
        memset(v,false,sizeof(v));
        dp(1);
        memset(f,-1,sizeof(f));
        memset(v,false,sizeof(v));
        dfs(1,0,0);

        for(int i=1;i<=n;i++)
        {
            b[i]=max(max1[i],f[i]);
            cout<<b[i]<<endl;
        }

        //solve b
        RMQ_init();
        int ans=0;
        for(int i=1;i<=m;i++)
        {
            int q;
            scanf("%d",&q);
            printf("%d\n", query(q));
        }
    }
    return 0;
}

Compiler output:
main.c:1:9: fatal error: iostream: No such file or directory
    1 | #include<iostream>
      |         ^~~~~~~~~~
compilation terminated.