답안 #469029

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
469029 2021-08-30T12:43:20 Z wdjpng Chase (CEOI17_chase) C++17
0 / 100
4000 ms 385580 KB
#include <bits/stdc++.h>
    
#pragma GCC optimize ("O3")
#pragma GCC optimize ("unroll-loops")
    
#define int long long
#define rep(i,n) for(int i = 0; i < n; i++)
    
using namespace std;
    
int n,v;
vector<int>p;
vector<vector<int>>mem;
vector<vector<int>>E;

vector<vector<signed>>takei;
vector<vector<signed>>notakei;
vector<vector<int>>takemaxx;
vector<vector<int>>notakemaxx;

int dfs(int pos, int bc, int parent)
{
    if(mem[pos][bc]!=-1) return mem[pos][bc];
    if(bc==0) return 0;
    
    int sum = 0;
    for(int w : E[pos])
    {
        if(w==parent) continue;
        sum+=p[w];
    }
    
    int maxx = 0;
    takei[pos][bc] = -1;
    takemaxx[pos][bc] = sum;

    for(int w : E[pos])
    {
        if(w==parent) continue;
        int t = dfs(w, bc-1, pos) + sum;
        int nt = dfs(w, bc, pos);
        if(nt>notakemaxx[pos][bc])
        {
            notakei[pos][bc] = w;
            notakemaxx[pos][bc] = nt;
        }

        if(t>takemaxx[pos][bc])
        {
            takei[pos][bc] = w;
            takemaxx[pos][bc] = t;
        }

        if(parent!=-1) maxx=max(maxx, nt);
        maxx=max(maxx, t);
    }
    
    return mem[pos][bc] = maxx;
}
    
int bruteforce()
{
    int maxx=0;
    rep(i, n) 
    {
        mem.assign(n+1, vector<int>(v+1, -1));
        maxx=max(maxx, dfs(i+1, v, -1));
    }
    return maxx;
}

void reset(int w)
{
    mem[w].assign(v+1, -1);
    takemaxx[w].assign(v+1, -1);
    notakemaxx[w].assign(v+1, -1);
    notakei[w].assign(v+1, -1);
    takei[w].assign(v+1, -1);
}
int mx = 0;
void meta_dfs(int pos, int parent)
{
    vector<int>oldmem = mem[pos];

    mx=max(mx, dfs(pos, v, -1));
    for(int w : E[pos])
    {
        if(w==parent) continue;
       
        //unroot root
        for(int i = 1; i <= v; i++)
        { 
            mem[pos][i] = max(takemaxx[pos][i]-p[w], notakemaxx[pos][i]);
            if(takei[pos][i] == w || notakei[pos][i]==w)
            {
                reset(pos);
                break;
            }
        }

        // child as root
        //reset(pos); // undo
        reset(w);
        meta_dfs(w, pos);
    }

    mem[pos] = oldmem;
}

    
signed main()
{
    cin>>n>>v;
    p.resize(n+1);
    rep(i, n) cin>>p[i+1];
    
    E.resize(n+1);
    
    rep(i,n-1)
    {
        int a,b;
        cin>>a>>b;
        E[a].push_back(b);
        E[b].push_back(a);
    }

    mem.assign(n+1, vector<int>(v+1, -1));
    takei.assign(n+1, vector<signed>(v+1, -1));
    notakei.assign(n+1, vector<signed>(v+1, -1));
    takemaxx.assign(n+1, vector<int>(v+1, -1));
    notakemaxx.assign(n+1, vector<int>(v+1, -1));
    meta_dfs(1,-1);
    cout<<mx<<"\n";
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Incorrect 1 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Incorrect 1 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 4082 ms 385580 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 204 KB Output is correct
2 Correct 0 ms 204 KB Output is correct
3 Correct 0 ms 204 KB Output is correct
4 Incorrect 1 ms 204 KB Output isn't correct
5 Halted 0 ms 0 KB -