Submission #1310514

#TimeUsernameProblemLanguageResultExecution timeMemory
1310514ayuxhkumxr22Travelling Trader (CCO23_day2problem2)C++20
9 / 25
2088 ms44872 KiB
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;

const long long INF = 1e18;
int N, K;
vector<long long> P;
vector<vector<int>> adj;

// --- K=1 Solution: Longest Path from Root ---
long long max_profit_k1 = -1;
vector<int> path_k1;

void dfs_k1(int u, int p, long long current_profit, vector<int>& current_path) {
    current_path.push_back(u);
    current_profit += P[u];
    
    if (current_profit > max_profit_k1) {
        max_profit_k1 = current_profit;
        path_k1 = current_path;
    }
    
    for (int v : adj[u]) {
        if (v != p) {
            dfs_k1(v, u, current_profit, current_path);
        }
    }
    current_path.pop_back();
}

void solve_k1() {
    vector<int> current_path;
    dfs_k1(1, 0, 0, current_path);
    cout << max_profit_k1 << "\n";
    cout << path_k1.size() << "\n";
    for (int i = 0; i < path_k1.size(); i++) cout << path_k1[i] << (i == path_k1.size()-1 ? "" : " ");
    cout << "\n";
}

// --- K=3 Solution: Visit All ---
void print_k3(int u, int p, bool reverse_mode) {
    if (!reverse_mode) {
        cout << u << " ";
        for (int v : adj[u]) {
            if (v != p) {
                print_k3(v, u, true);
            }
        }
    } else {
        vector<int> children;
        for (int v : adj[u]) if (v != p) children.push_back(v);
        for (int i = children.size() - 1; i >= 0; i--) {
            print_k3(children[i], u, false);
        }
        cout << u << " ";
    }
}

void solve_k3() {
    long long total = 0;
    for (int i = 1; i <= N; i++) total += P[i];
    cout << total << "\n";
    cout << N << "\n";
    print_k3(1, 0, false);
    cout << "\n";
}

// --- K=2 Solution: Tree DP ---
struct DPState {
    long long val;
    int choice_a, choice_b, choice_c; 
    // a: D2 child (loop start), b: D2 child (loop end), c: D1 child (tail)
    // special flag for dp1 jump: choice_a = -2 implies "Jump to dp3[choice_c]"
};

DPState dp[200005][3];

struct Cand {
    long long gain;
    int id;
};

// Optimization: Find top 3 candidates in O(Deg) instead of O(Deg log Deg)
void update_top3(vector<Cand>& top3, const Cand& c) {
    top3.push_back(c);
    int i = top3.size() - 1;
    while(i > 0) {
        if(top3[i].gain > top3[i-1].gain) {
            swap(top3[i], top3[i-1]);
            i--;
        } else break;
    }
    if(top3.size() > 3) top3.pop_back();
}

void dfs_k2(int u, int p) {
    vector<int> children;
    long long sum_p = 0;
    
    // Collect children and base sum
    for (int v : adj[u]) {
        if (v != p) {
            dfs_k2(v, u);
            children.push_back(v);
            sum_p += P[v];
        }
    }

    long long base = sum_p + P[u];

    // Collect candidates for transitions
    vector<Cand> cands_d1, cands_d2;
    long long max_dp3_jump = -INF;
    int dp3_jump_id = -1;

    for (int v : children) {
        // Gain if we replace single P[v] with a chain
        update_top3(cands_d1, {dp[v][0].val - P[v], v});
        update_top3(cands_d2, {dp[v][1].val - P[v], v});
        
        // Track best pure jump (u -> dp3[v])
        // This is exclusive: P[u] + dp3[v]
        if (P[u] + dp[v][2].val > max_dp3_jump) {
            max_dp3_jump = P[u] + dp[v][2].val;
            dp3_jump_id = v;
        }
    }

    // --- DP1: Start u, stay in subtree ---
    dp[u][0] = {base, -1, -1, -1};
    
    // Standard Mix: base + D2[a] (optional) + D1[b] (optional)
    // Try all pairs from top 3
    // Case 1: No specials (base) - set initially
    
    // Case 2: Just D1[b]
    for(auto& b : cands_d1) {
        if(base + b.gain > dp[u][0].val) dp[u][0] = {base + b.gain, -1, -1, b.id};
    }
    // Case 3: Just D2[a]
    for(auto& a : cands_d2) {
        if(base + a.gain > dp[u][0].val) dp[u][0] = {base + a.gain, a.id, -1, -1};
    }
    // Case 4: Both D2[a] and D1[b]
    for(auto& a : cands_d2) {
        for(auto& b : cands_d1) {
            if(a.id != b.id) {
                if(base + a.gain + b.gain > dp[u][0].val) {
                    dp[u][0] = {base + a.gain + b.gain, a.id, -1, b.id};
                }
            }
        }
    }
    
    // Case 5: Jump to Grandchild (P[u] + dp3[v]) [Cite: 150]
    if (max_dp3_jump > dp[u][0].val) {
        // We use special marker -2 to indicate this jump
        dp[u][0] = {max_dp3_jump, -2, -1, dp3_jump_id};
    }

    // --- DP2: Start u, End shallow (at child or u) ---
    // Max: base + D2[a] (optional)
    dp[u][1] = {base, -1, -1, -1};
    for(auto& a : cands_d2) {
        if(base + a.gain > dp[u][1].val) dp[u][1] = {base + a.gain, a.id, -1, -1};
    }

    // --- DP3: Start at child of u, stay in subtree ---
    // Structure: D2[a] (start) -> u -> D2[b] (rev) -> singles -> D1[c] (end)
    // a is mandatory (must start at child)
    dp[u][2] = {-INF, -1, -1, -1};
    
    for(auto& a : cands_d2) {
        long long current = base + a.gain;
        // Just a
        if(current > dp[u][2].val) dp[u][2] = {current, a.id, -1, -1};

        // a + b
        for(auto& b : cands_d2) {
            if(a.id != b.id) {
                if(current + b.gain > dp[u][2].val) dp[u][2] = {current + b.gain, a.id, b.id, -1};
            }
        }
        // a + c
        for(auto& c : cands_d1) {
            if(a.id != c.id) {
                if(current + c.gain > dp[u][2].val) dp[u][2] = {current + c.gain, a.id, -1, c.id};
            }
        }
        // a + b + c
        for(auto& b : cands_d2) {
            if(a.id == b.id) continue;
            for(auto& c : cands_d1) {
                if(a.id != c.id && b.id != c.id) {
                    if(current + b.gain + c.gain > dp[u][2].val) {
                        dp[u][2] = {current + b.gain + c.gain, a.id, b.id, c.id};
                    }
                }
            }
        }
    }
}

// Reconstruction Logic
void build_k2(int u, int p, int type, vector<int>& path);

// Helper: Add path of a child recursively
void add_child_path(int v, int u, int type, vector<int>& path, bool reverse_output) {
    vector<int> sub;
    build_k2(v, u, type, sub);
    if(reverse_output) {
        path.insert(path.end(), sub.rbegin(), sub.rend());
    } else {
        path.insert(path.end(), sub.begin(), sub.end());
    }
}

void build_k2(int u, int p, int type, vector<int>& path) {
    DPState& s = dp[u][type];
    
    // Check for special Jump transition
    if (type == 0 && s.choice_a == -2) {
        // Path: u -> dp3[child]
        path.push_back(u);
        add_child_path(s.choice_c, u, 2, path, false);
        return;
    }

    int va = s.choice_a;
    int vb = s.choice_b;
    int vc = s.choice_c;

    auto add_singles = [&]() {
        for (int v : adj[u]) {
            if (v != p && v != va && v != vb && v != vc) {
                path.push_back(v);
            }
        }
    };

    if (type == 0) { // DP1: u -> Rev(va) -> singles -> vc
        path.push_back(u);
        if (va != -1) add_child_path(va, u, 1, path, true); // Rev(D2)
        add_singles();
        if (vc != -1) add_child_path(vc, u, 0, path, false); // D1
    } 
    else if (type == 1) { // DP2: u -> Rev(va) -> singles
        path.push_back(u);
        if (va != -1) add_child_path(va, u, 1, path, true); // Rev(D2)
        add_singles();
    } 
    else if (type == 2) { // DP3: va -> u -> Rev(vb) -> singles -> vc
        if (va != -1) add_child_path(va, u, 1, path, false); // D2 (Forward)
        path.push_back(u);
        if (vb != -1) add_child_path(vb, u, 1, path, true); // Rev(D2)
        add_singles();
        if (vc != -1) add_child_path(vc, u, 0, path, false); // D1
    }
}

void solve_k2() {
    dfs_k2(1, 0);
    cout << dp[1][0].val << "\n";
    vector<int> path;
    build_k2(1, 0, 0, path);
    cout << path.size() << "\n";
    for(size_t i=0; i<path.size(); ++i) cout << path[i] << (i==path.size()-1?"":" ");
    cout << "\n";
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    if (!(cin >> N >> K)) return 0;
    P.resize(N + 1);
    adj.resize(N + 1);
    for (int i = 0; i < N - 1; i++) {
        int u, v;
        cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    for (int i = 1; i <= N; i++) cin >> P[i];

    if (K == 1) solve_k1();
    else if (K == 2) solve_k2();
    else solve_k3();

    return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...