测数过了但wa声一片,求大佬帮助

P1600 [NOIP2016 提高组] 天天爱跑步

hxy147 @ 2017-11-12 15:30:15

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int m,n,head[299999],size=0,w[1000000],ans[1000000],lj[1000000],q[1000000],f[1000000],cd[1000000];
struct E
{
    int next,to;
}e[100000];
void addline(int u,int v)
{
    size++;
    e[size].next=head[u];
    e[size].to=v;
    head[u]=size;
    return;
}
int main()
{
    freopen("in.txt","r",stdin);
    int i,j,x,y,s,t,h,tail,z,step,step2;
    cin>>n>>m;
    for(i=1;i<=n-1;i++)
    {
        cin>>x>>y;
        addline(x,y);
        addline(y,x);
    }
    for(i=1;i<=n;i++)
    cin>>w[i];
    for(i=1;i<=m;i++)
    {
        cin>>s>>t;
        memset(f,0,sizeof(f));
        memset(cd,0,sizeof(cd));
        memset(q,0,sizeof(q));
        h=0;
        tail=1;
        q[1]=s;
        while(h<tail)
        {
            h++;
            for(j=head[q[h]];j!=0;j=e[j].next)
            {
                tail++;
                q[tail]=e[j].to;
                f[tail]=h;
                cd[tail]=cd[h]+1;
                if(q[tail]==t)
                {
                    step=cd[tail];
                    step2=step;
                    for(z=tail;z!=0;z=f[z])
                    {
                        lj[step]=q[z];
                        step--;
                    }
                    h=tail;
                    break;
                }
            }
        }
        for(j=1;j<=n;j++)
        {
            if(lj[w[j]]==j&&w[j]<=step2)
            ans[j]++;
        }
    }
    for(i=1;i<=n;i++)
    cout<<ans[i]<<" ";
    return 0;
}

by Peter_Z @ 2017-11-12 17:44:24

不应该是TLE吗?把freopen注释掉试一下


by Peter_Z @ 2017-11-12 17:46:39

等等为什么加了freopen会WA,我以前加freopen只会TLE


by hxy147 @ 2017-11-14 12:46:45

去掉也是wa啊,绝望


|