BZOJ3252 strategy [tree chain dissection] [Do not use line-segment tree]__bzoj

Source: Internet
Author: User

I don't really know what the people in this line of the tree are thinking, online incredibly no long chain dissection of the problem, hey, this question is a standard long chain, without merging, deep right, sort take before k big on line, SCOI2016 day1 T1 original, I can't spit slot.

#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace
Std
    Template<class t>inline void Read (T &res) {static char ch;register int flag=1; while ((Ch=getchar ()) < ' 0 ' | |
    Ch> ' 9 ') if (ch== '-') flag=-1;res=ch-48;
while ((Ch=getchar ()) >= ' 0 ' &&ch<= ' 9 ') Res=res*10+ch-48;res*=flag;
const int N = 200005;
int n,k,tot,fa[n],head[n],son[n];
Long Long ans,sz[n],g[n],num[n],dep[n]; struct data{int to,nxt;}
e[n<<1]; inline void Addedge (int x,int y) {e[++tot].nxt=head[x],head[x]=tot,e[tot].to=y;} void dfsf (int x,int f) {fa[x]=f;
    SZ[X]=DEP[X];
            for (register int v,i=head[x];i;i=e[i].nxt) {if (v=e[i].to,v!=f) {DEP[V]=DEP[X]+NUM[V],DFSF (v,x);
        if (Sz[v]>sz[son[x]]) son[x]=v,sz[x]=sz[v];
    }} void DFSs (int x,long long sum) {if (son[x]) DFSS (son[x],sum+num[son[x]);
    else g[x]=sum; for (register int v,i=head[x];i;i=e[i].nxt) {if (V=e[i]. To,v!=fa[x]&&v!=son[x]) DFSS (V,num[v]);
    int CMP (long long A,long long b) {return a>b;} int main () {read (n), read (k);
    for (register int i=1;i<=n;i++) read (Num[i]);
    for (register int x,y,i=1;i<n;i++) read (x), read (y), Addedge (X,y), Addedge (y,x);
    DFSF (1,0), DFSS (1,num[1]);
    Sort (g+1,g+1+n,cmp);
    for (register int i=1;i<=k;i++) ans+=g[i];
    printf ("%lld", ans);
return 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.