萌新刚学OI一普朗克时求助

P3806 【模板】点分治 1

zhoukangyang @ 2020-03-04 22:14:24

#include<bits/stdc++.h>
using namespace std;
int kn,N,n,k[23333],u,v,q,siz[41111],rot,mas,fa[41111],mi[41111],paint[41111],deep[41111],color_id;
int head[41111],last[41111],ans[23333],stackk[41111],dfn;
struct node {
    int to,next,val;
} s[233333];
void find_root(int x) {
    siz[x]=1,mi[x]=0;
    for(int i = head[x]; i != 0; i = s[i].next) {
        if(s[i].to==fa[x]) continue;
        if(paint[s[i].to]!=paint[x]) continue;
        fa[s[i].to]=x;
        find_root(s[i].to);
        mi[x]=max(mi[x],siz[s[i].to]),siz[x]+=siz[s[i].to];
    }
    mi[x]=max(mi[x],N-siz[x]);
    if(mas>mi[x]) mas=mi[x],rot=x;
}
void cz(int x) {
    siz[x]=1;
    for(int i = head[x]; i != 0; i = s[i].next) {
        if(s[i].to==fa[x]) continue;
        if(paint[s[i].to]!=paint[x]) continue;
        fa[s[i].to]=x,cz(s[i].to),siz[x]+=siz[s[i].to];
    }
}
void low(int x) {
    for(int i = head[x]; i != 0; i = s[i].next) {
        if(s[i].to==fa[x]) continue;
        if(paint[s[i].to]!=paint[x]) continue;
        deep[s[i].to]=deep[x]+s[i].val,fa[s[i].to]=x,low(s[i].to),siz[x]+=siz[s[i].to];
    }
    ++dfn,stackk[dfn]=x,paint[x]=color_id;
}
void dfs(int x) {
    int ma[11111111];
    if(N==1) return;
    int yousb[41111],sz[41111],lowbit=0;
    fa[x]=rot=0,mas=1e9,find_root(x);
    fa[rot]=0,cz(rot),deep[rot]=0;
    ma[0]=1;
    for(int i = head[rot]; i != 0; i = s[i].next) {
        if(paint[s[i].to]!=paint[rot]) continue;
        ++lowbit,yousb[lowbit]=s[i].to,sz[lowbit]=siz[s[i].to];
        dfn=0,++color_id,deep[s[i].to]=s[i].val,low(s[i].to);
        for(int j = 1; j <= dfn; j++) for(int kk = 1; kk <= kn; kk++)
                if(k[kk]-deep[stackk[j]]>=0) ans[kk]+=ma[k[kk]-deep[stackk[j]]];
        for(int j = 1; j <= dfn; j++) if(deep[stackk[j]]<=1e7) ma[deep[stackk[j]]]++;
    }
    for(int i = 1; i <= lowbit; i++) N=sz[i],dfs(yousb[i]);
}
void add_edge(int id,int U,int V,int VAL) {
    if(head[U]==0) head[U]=id;
    else s[last[U]].next=id;
    last[U]=id,s[id].to=V,s[id].val=VAL;
}
int main() {
    scanf("%d%d",&n,&kn);
    for(int i = 1; i < n; i++) {
        scanf("%d%d%d",&u,&v,&q);
        add_edge(i*2-1,u,v,q);
        add_edge(i*2,v,u,q);
    }
    for(int i = 1; i <= kn; i++) scanf("%d",&k[i]);
    N=n;
    dfs(1);
    for(int i = 1; i <= kn; i++) printf("%s\n",ans[i]!=0?"AYE":"NAY");
    return 0;
}

map

#include<bits/stdc++.h>
using namespace std;
int kn,N,n,k[23333],u,v,q,siz[41111],rot,mas,fa[41111],mi[41111],paint[41111],deep[41111],color_id;
int head[41111],last[41111],ans[23333],stackk[41111],dfn;
struct node{
    int to,next,val;
} s[233333];
void find_root(int x){
    siz[x]=1,mi[x]=0;
    for(int i = head[x]; i != 0; i = s[i].next){
        if(s[i].to==fa[x]) continue;
        if(paint[s[i].to]!=paint[x]) continue;
        fa[s[i].to]=x;
        find_root(s[i].to);
        mi[x]=max(mi[x],siz[s[i].to]),siz[x]+=siz[s[i].to];
    }
    mi[x]=max(mi[x],N-siz[x]);
    if(mas>mi[x]) mas=mi[x],rot=x;
}
void cz(int x){
    siz[x]=1;
    for(int i = head[x]; i != 0; i = s[i].next){
        if(s[i].to==fa[x]) continue;
        if(paint[s[i].to]!=paint[x]) continue;
        fa[s[i].to]=x,cz(s[i].to),siz[x]+=siz[s[i].to];
    }
}
void low(int x){
    for(int i = head[x]; i != 0; i = s[i].next){
        if(s[i].to==fa[x]) continue;
        if(paint[s[i].to]!=paint[x]) continue;
        deep[s[i].to]=deep[x]+s[i].val,fa[s[i].to]=x,low(s[i].to),siz[x]+=siz[s[i].to];
    }
    ++dfn,stackk[dfn]=x,paint[x]=color_id;
}
void dfs(int x){
    map<int,int> ma;
    if(N==1) return;
    int yousb[41111],sz[41111],lowbit=0;
    fa[x]=rot=0,mas=1e9,find_root(x);
    fa[rot]=0,cz(rot),deep[rot]=0;
    ma[0]=1;
    for(int i = head[rot]; i != 0; i = s[i].next) {
        if(paint[s[i].to]!=paint[rot]) continue;
        ++lowbit,yousb[lowbit]=s[i].to,sz[lowbit]=siz[s[i].to];
        dfn=0,++color_id,deep[s[i].to]=s[i].val,low(s[i].to);
        for(int j = 1; j <= dfn; j++) for(int kk = 1; kk <= kn; kk++) ans[kk]+=ma[k[kk]-deep[stackk[j]]];
        for(int j = 1; j <= dfn; j++) ma[deep[stackk[j]]]++;
    }
    for(int i = 1; i <= lowbit; i++) N=sz[i],dfs(yousb[i]);
}
void add_edge(int id,int U,int V,int VAL){
    if(head[U]==0) head[U]=id;
    else s[last[U]].next=id;
    last[U]=id,s[id].to=V,s[id].val=VAL;
}
int main(){
    scanf("%d%d",&n,&kn);
    for(int i = 1; i < n; i++){
        scanf("%d%d%d",&u,&v,&q);
        add_edge(i*2-1,u,v,q);
        add_edge(i*2,v,u,q);
    }
    for(int i = 1; i <= kn; i++) scanf("%d",&k[i]);
    N=n;
    dfs(1);
    for(int i = 1; i <= kn; i++) printf("%s\n",ans[i]!=0?"AYE":"NAY");
    return 0;
}

求助求助求助求助求助


by dead_X @ 2020-03-04 22:16:33

ntql

同为一个学校的我怎么能这么菜

关键是人品还这么差


by 麻省理工学院 @ 2020-03-04 22:16:35

@zhoukangyang %%% 这么短的时间就会了淀粉质,将来肯定AKIOI


by Keith_2006 @ 2020-03-04 22:18:00

qndmx


by zhoukangyang @ 2020-03-04 22:18:15

@麻省理工学院 您已经AK了说什么说


by zhoukangyang @ 2020-03-04 22:18:40

@dead_X 您不是天天吊打我吗


by chenyuhe @ 2020-03-04 22:19:15

@zhoukangyang 您天天吊打我


by 麻省理工学院 @ 2020-03-04 22:20:30

@zhoukangyang 哪能啊,我现在仍是一名进不了省队的蒟蒻


by Immortal_Bird @ 2020-03-04 22:21:26

Orz

by zhoukangyang @ 2020-03-04 22:21:45

@麻省理工学院 我CSPJ爆0(真的


by 麻省理工学院 @ 2020-03-04 22:22:44

@zhoukangyang %%% 我连CSPJ初赛都没过


| 下一页