E. Paint the Tree

http://codeforces.com/contest/1241/problem/E

#include 
//#include 
//#include 
//#include 
//#include 

#define lson rt<<1
#define rson rt<<1|1
#define lowbit(x) ((x)&(-x))
#define mem(x,y) memset(x,y,sizeof(x))
#define pb push_back
#define INF 0x3f3f3f3f
#define ll long long
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;

const int mod=1e9+7;
const int N=5e5+9;

vector >g[N];
ll dp[N][2];
int n,k;

void dfs(int u,int fa)
{
    dp[u][1]=dp[u][0]=0;
    ll sum=0;
    vectorvec;
    for(int i=0;i());
    for(int i=0;i0)
            sum+=vec[i];
    }
    dp[u][0]=dp[u][1]=sum;
    if(vec.size()>=k) if(vec[k-1]>0) dp[u][1]-=vec[k-1];
}

int main()
{
    FAST_IO;

    int q;
    cin>>q;
    while(q--)
    {
        cin>>n>>k;
        for(int i=0;i<=n;i++) g[i].clear();
        for(int i=0;i>u>>v>>w;
            g[u].pb({v,w});
            g[v].pb({u,w});

        }

        dfs(1,0);

        cout<

 

你可能感兴趣的:(树形dp)