cf1281 F. Miss Punyverse

Link

Link

Solution

这种以 p a i r pair 为状态的 d p dp 还是第一次见到

f [ i ] [ j ] f[i][j] 表示以 i i 为的子树,划分成 j j 个连通块时,最大的二元组。二元组 ( a , b ) (a,b) 的意义是当前已经有 a a 个连通块是有收益的(不包含最后一个),最后一个连通块的和为 b b

然后树形 d p dp 即可

时间复杂度的计算:

每个节点上的复杂度为

O ( s i z e s o n 1 + ( 1 + s i z e s o n 1 ) s i z e s o n 2 + ( 1 + s i z e s o n 1 + s i z e s o n 2 ) s i z e s o n 3 + ) = O ( s i z e u 2 s i z e s o n i 2 ) O(size_{son1} + (1+size_{son1})size_{son2} + (1+size_{son1}+size_{son2})size_{son3} + \dots)\\ = O(size_u^2 - \sum size_{soni}^2)

对整棵树求和,复杂度是 O ( n 2 ) O(n^2)

Code

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 3010
#define maxe 6010
#define cl(x) memset(x,0,sizeof(x))
#define rep(_,__) for(_=1;_<=(__);_++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
struct Graph
{
    int etot, head[maxn], to[maxe], next[maxe], w[maxe];
    void clear(int N)
    {
        for(int i=1;i<=N;i++)head[i]=0;
        etot=0;
    }
    void adde(int a, int b, int c=0){to[++etot]=b;w[etot]=c;next[etot]=head[a];head[a]=etot;}
    #define forp(pos,G) for(auto p=G.head[pos];p;p=G.next[p])
}G;
ll n, m, a[maxn], b[maxn], w[maxn], sz[maxn];
pll f[maxn][maxn];
pll operator+(pll p1, pll p2)
{
    return pll(p1.first+p2.first,p1.second+p2.second);
}
pll operator!(pll p)
{
    return pll(p.first+(p.second>0),0);
}
void dfs(ll u, ll fa)
{
    ll i, j;
    sz[u]=1;
    f[u][1]=pll(0,w[u]);
    forp(u,G)if(G.to[p]!=fa)
    {
        auto v=G.to[p];
        dfs(v,u);
        pll ff[maxn];
        rep(i,m)ff[i]=pll(-1,0);
        for(i=1;i<=sz[u];i++)for(j=1;j<=sz[v];j++)
        {
            ff[i+j]=max(ff[i+j],f[u][i]+!f[v][j]);
            ff[i+j-1]=max(ff[i+j-1],f[u][i]+f[v][j]);
        }
        rep(i,m)f[u][i]=ff[i];
        sz[u]+=sz[v];
    }
}
int main()
{
    ll T=read(), u, v, i, j;
    while(T--)
    {
        n=read(), m=read();
        G.clear(n);
        rep(i,n)a[i]=read();
        rep(i,n)b[i]=read();
        rep(i,n)w[i]=b[i]-a[i];
        rep(i,n-1)u=read(), v=read(), G.adde(u,v), G.adde(v,u);
        dfs(1,0);
        printf("%lld\n",(!f[1][m]).first);
    }
    return 0;
}
发布了863 篇原创文章 · 获赞 72 · 访问量 19万+

猜你喜欢

转载自blog.csdn.net/FSAHFGSADHSAKNDAS/article/details/103980222
今日推荐