几乎一模一样的两个代码了,为什么一个永远WA一个永远AC?

P3806 【模板】点分治 1

NF_水饺 @ 2018-08-20 15:39:24

#define MAXN 10001
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
struct edge
{
    int x,c;
    edge *nex;
    edge(int x1=0,int c1=0,edge *n1=0):x(x1),c(c1),nex(n1){}
}*e[10000+10],es[20000+10];
int siz[10000+10],dep[10000+10],maxs[10000+10];
bool vis[10000+10];
int cnt;
int q[10000+10],head,tail;
int n,k;
int sum[10000+10],py;
using namespace std;
bool cmp(int a,int b)
{
    return dep[a]<dep[b];
}
void insert(int x,int y,int z)
{
    es[++cnt]=edge(y,z,e[x]);
    e[x]=&es[cnt];
}
void dfs(int x)
{
    siz[x]=1;
    vis[x]=1;
    maxs[x]=0;
    q[tail++]=x;
    for(edge* i=e[x];i;i=i->nex)
    {
        int y=i->x;
        if(vis[y]) continue;
        dep[y]=dep[x]+i->c;
        dfs(y);
        siz[x]+=siz[y];
        maxs[x]=max(maxs[x],siz[y]);
    }
    vis[x]=0;
    return;
}
int calc(int l,int r,int jkl)
{
    sort(q+l,q+r,cmp);
    int ans=0;
    for(int i=l,j=r-1;i<j;++i)
    {
        while(j>i&&dep[q[j]]+dep[q[i]]>k) --j;
        ans+=(j-i);
    }
    return ans;
}
int work(int x)
{
    head=tail=0;
    dfs(x);
    int tot=siz[x],minn=maxs[x];
    for(register int i=0;i<tail;i++)
    {
        int y=q[i];
        int m=max(maxs[y],tot-siz[y]);
        if(m<minn)
        {
            minn=m;
            x=y;
        }
    }
    vis[x]=1;
    head=tail=0;
    int ans=0;
    for(edge *i=e[x];i;i=i->nex)
    {
        int y=i->x;
        if(vis[y]) continue;
        dep[y]=i->c;
        dfs(y);
        for(int j=1;j<=py;j++)
        {
            sum[j]-=calc(head,tail,sum[j]);
            sum[j]+=calc(head,tail,sum[j]-1);
        }
        head=tail;
    }
    dep[x]=0;
    q[tail++]=x;
    for(int i=1;i<=py;i++)
    {
        sum[i]+=calc(0,tail,sum[i]);
        sum[i]-=calc(0,tail,sum[i]-1);
    }
    for(edge *i=e[x];i;i=i->nex)
    {
        int y=i->x;
        if(!vis[y]) ans+=work(y);
    }
    return ans;
}
int main()
{
    memset(e,0,sizeof(e));
    int mm,p;
    int g,q,z;
    cin>>mm>>p;
    cnt=0;
    for(int i=1;i<mm;i++)
    {
        cin>>g>>q>>z;
        insert(g,q,z);
        insert(q,g,z);
    }
    while(p--)
    {
        cin>>k;
        py++;
        sum[py]=k;
    }
    memset(vis,0,sizeof(vis));
    work(1);
    for(int i=1;i<=py;i++)
    {
        if(sum[i]) cout<<"Yes"<<endl;
        else cout<<"No"<<endl;
    }
    return 0;
}

上面是WA代码


#define MAXN 10001
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<ctime>
using namespace std;
struct edgedata
{
    int x,c;
    edgedata *Next;
    edgedata(int x1=0,int c1=0,edgedata *ne=0):x(x1),c(c1),Next(ne){}
}*E[MAXN],Es[MAXN<<1];
int Size[MAXN],Maxs[MAXN],vis[MAXN],queue[MAXN],Dep[MAXN];
int n,k,cnt,head,tail,kk,vv;
int anss[MAXN],kp[MAXN];
void read(int &x)
{
    int f=1;x=0;char s=getchar();
    while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
    while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
    x*=f;
}
bool cmp(int a,int b)
{
    return Dep[a]<Dep[b];
}
void Insert(int u,int v,int l)
{
    Es[++cnt]=edgedata(v,l,E[u]);
    E[u]=&Es[cnt];
}
void DFS(int x)
{
    Size[x]=1;vis[x]=1;
    Maxs[x]=0;
    queue[tail++]=x;
    for(edgedata* i=E[x];i;i=i->Next)
    {
        int y=i->x;
        if(vis[y]) continue;
        Dep[y]=Dep[x]+i->c;
        DFS(y);
        Size[x]+=Size[y];
        Maxs[x]=max(Maxs[x],Size[y]);
    }
    vis[x]=0;
    return;
}
int sumup(int l,int r,int k)
{
    sort(queue+l,queue+r,cmp);
    int ans=0;
    for(int i=l,j=r-1;i<j;++i)
    {
        while(j>i&&Dep[queue[j]]+Dep[queue[i]]>k) --j;
        ans+=(j-i);
    }
    return ans;
}
int Do(int x)
{
    head=tail=0;
    DFS(x);
    int tot=Size[x],minmax=Maxs[x];
    for(register int i=0;i<tail;i++)
    {
        int y=queue[i];
        int minpos=max(Maxs[y],tot-Size[y]);
        if(minpos<minmax)
        {
            minmax=minpos;
            x=y;
        }
    }
    vis[x]=1;
    head=tail=0;
    int ans=0;
    for(edgedata *i=E[x];i;i=i->Next)
    {
        int y=i->x;
        if(vis[y]) continue;
        Dep[y]=i->c;
        DFS(y);
        for(int j=1;j<=vv;j++)
        {
            anss[j]-=sumup(head,tail,kp[j]);
            anss[j]+=sumup(head,tail,kp[j]-1);
        }
        head=tail;
    }
    Dep[x]=0;
    queue[tail++]=x;
    for(int j=1;j<=vv;j++)
    {
        anss[j]+=sumup(0,tail,kp[j]);
        anss[j]-=sumup(0,tail,kp[j]-1);
    }
    for(edgedata *i=E[x];i;i=i->Next)
    {
        int y=i->x;
        if(!vis[y]) ans+=Do(y);
    }
    return ans;
}
int main()
{
    int u,v,l;
    read(n);
    read(kk);
    memset(E,0,sizeof(E));
    cnt=0;
    int summ=0;
    for(register int i=1;i<n;i++)
    {
        read(u);read(v);read(l);
        Insert(u,v,l);
        Insert(v,u,l);
    }
    int dd;
    vv=0;
    while(kk--)
    {   
        cin>>kp[++vv];
    }
    memset(vis,0,sizeof(vis));
    Do(1);
    for(int i=1;i<=vv;i++)
    {
        if(anss[i])
            puts("AYE");
        else
            put…

by Sirius_X @ 2018-08-20 15:45:05

那是因为几乎一样,不是完全一样


by 引领天下 @ 2018-08-20 15:46:22

@NF_水饺 您用一下Windows的FC命令就可以了


by 不争不闹 @ 2018-08-20 15:49:24

Vim的diff命令了解一下


by NF_水饺 @ 2018-08-20 15:54:35

已解决,谢谢


by hyfhaha @ 2018-08-20 16:01:08

哇,淀粉质


by matsuk @ 2018-09-26 09:45:23

完全一样的我还交过一个wa一个ac


|