QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#290390#7206. TripleCharlieVinnieWA 0ms34684kbC++173.6kb2023-12-24 22:30:052023-12-24 22:30:06

Judging History

你现在查看的是最新测评结果

  • [2023-12-24 22:30:06]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:34684kb
  • [2023-12-24 22:30:05]
  • 提交

answer

#include "bits/stdc++.h"
#undef DEBUG
#ifdef DEBUG
#include "PrettyDebug.hpp"
#else
#define debug(...) [](auto...){}(__VA_ARGS__)
#define debuga(...) [](auto...){}(__VA_ARGS__)
#endif
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Rev(i,a,b) for(int i=a;i>=b;i--)
#define Fin(file) freopen(file,"r",stdin)
#define Fout(file) freopen(file,"w",stdout)
using namespace std; typedef long long ll;
constexpr int N=5e5+5;
int n,D,ban[N],cursiz,siz[N],dep[N],son[N],pool[N],*ptr,*f[N]; ll ans,tmp[N],c1[N],c2[N],s1[N],s2[N]; vector<int> to[N];
inline bool ckmax(int& x,int y) { return x<y?x=y,true:false; }
pair<int,int> dfs1(int u,int pa){
    pair<int,int> res(1e9,0);
    int mx=0; siz[u]=1; for(int v:to[u]) if(v!=pa&&!ban[v]) res=min(res,dfs1(v,u)),siz[u]+=siz[v],mx=max(mx,siz[v]);
    return min(res,make_pair(max(mx,cursiz-siz[u]),u));
}
int dfs2(int u,int pa){
    int mx=0; siz[u]=1; for(int v:to[u]) if(v!=pa&&!ban[v]) mx=max(mx,dfs2(v,u)+1),siz[u]+=siz[v];
    return mx;
}
int dfs3(int u,int pa,int d){
    int mx=d; tmp[d]++; for(int v:to[u]) if(v!=pa&&!ban[v]) mx=max(mx,dfs3(v,u,d+1));
    return mx;
}
void dfs4(int u,int pa){
    dep[u]=1; son[u]=0; for(int v:to[u]) if(v!=pa&&!ban[v]) { dfs4(v,u); if(ckmax(dep[u],dep[v]+1)) son[u]=v; }
}
void dfs5(int u,int pa,int d){
    if(son[u]){
        f[son[u]]=f[u]+1; dfs5(son[u],u,d+1);
        ans+=2ll*siz[son[u]]*s1[0];
    }
    f[u][0]=1; int ss=siz[son[u]];
    for(int v:to[u]) if(v!=pa&&!ban[v]&&v!=son[u]){
        f[v]=ptr; ptr+=dep[v]; dfs5(v,u,d+1);
        int tt=ss; For(i,1,dep[v]) { ans+=2ll*f[v][i-1]*tt*(s1[0]-s1[max(0,i-d)]); tt-=f[u][i]; ans+=2ll*f[v][i-1]*tt*s1[max(0,i-d+1)]; }
        tt=0; Rev(i,dep[v]-1,0) { tt+=f[v][i]; ans+=2ll*f[u][i]*tt*(s1[0]-s1[max(0,i-d)]); ans+=2ll*f[u][i]*tt*s1[max(0,i-d+1)]; }
        ss+=siz[v]; For(i,1,dep[v]) f[u][i]+=f[v][i-1];
    }
}
void solve(int A){
    A=dfs1(A,0).second; D=dfs2(A,0);
    debug(A,D);
    For(i,0,D) c1[i]=c2[i]=s1[i]=s2[i]=0;
    c1[0]=1;
    for(int u:to[A]) if(!ban[u]){
        For(i,0,D) tmp[i]=0;; int d=dfs3(u,A,1);
        int tt=0; Rev(i,d,1) { c1[i]+=tmp[i]; c2[i]+=2ll*tt*tmp[i]+1ll*tmp[i]*(tmp[i]-1); tt+=tmp[i]; }
    }
    s1[D+1]=s2[D+1]=0; Rev(i,D,0) s1[i]=s1[i+1]+c1[i],s2[i]=s2[i+1]+c2[i];
    ans+=1ll*s1[1]*(s1[1]-1)-s2[1];
    for(int u:to[A]) if(!ban[u]){
        For(i,0,D) tmp[i]=0;; int d=dfs3(u,A,1);
        int tt=0; Rev(i,d,1) { c1[i]-=tmp[i]; c2[i]-=2ll*tt*tmp[i]+1ll*tmp[i]*(tmp[i]-1); tt+=tmp[i]; }
        Rev(i,d,0) s1[i]=s1[i+1]+c1[i],s2[i]=s2[i+1]+c2[i];
        For(i,1,d) ans+=tmp[i]*(1ll*s1[i+1]*(s1[i+1]-1)-s2[i+1]);
        dfs4(u,A); ptr=pool; f[u]=ptr; ptr+=dep[u]; dfs5(u,A,1);
            tt=0; Rev(i,d,1) { c1[i]+=tmp[i]; c2[i]+=2ll*tt*tmp[i]+1ll*tmp[i]*(tmp[i]-1); tt+=tmp[i]; }
        Rev(i,d,0) s1[i]=s1[i+1]+c1[i],s2[i]=s2[i+1]+c2[i];
    }
    debug(A,ans);
    ban[A]=1; for(int u:to[A]) if(!ban[u]) cursiz=siz[u],solve(u);
}
void solve(){
    cin>>n; ans=0; For(i,1,n) to[i].clear(),ban[i]=0;
    For(i,1,n-1) { int x,y; cin>>x>>y; to[x].push_back(y); to[y].push_back(x); }
    cursiz=n; solve(1); debug(ans); cout<<1ll*n*(n-1)*(n-2)-ans<<'\n';
}
signed main(){
    // Fin("lct.in"); Fout("lct.out");
    atexit([](){cerr<<"Time = "<<clock()<<" ms"<<endl;}); ios::sync_with_stdio(0); cin.tie(0);
    while(cin) solve();
    // int T; cin>>T; while(T--) solve();
    return 0;
}

// START TYPING IF YOU DON'T KNOW WHAT TO DO
// STOP TYPING IF YOU DON'T KNOW WHAT YOU'RE DOING
// CONTINUE, NON-STOPPING, FOR CHARLIEVINNIE

// Started Coding On: December 24 Sun, 20 : 39 : 04

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 34684kb

input:

3
1 2
2 3
4
1 2
2 3
2 4

output:

4
18
24

result:

wrong answer Participant output contains extra tokens