제출 #1332705

#제출 시각아이디문제언어결과실행 시간메모리
1332705neptunnsHarmonija (COCI25_harmonija)C++20
68 / 110
2593 ms10136 KiB
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define int long long
#define maxn 1005
#define mi LLONG_MIN
#define ma LLONG_MAX
#define mod 1000000007
#define pb push_back
#define S second
#define F first

int32_t main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int n, q;
    cin >> n >> q;
    vector<int> b(n), r(n);
    for (int i = 0; i < n; i++)
    {
        cin >> b[i];
    }
    for (int i = 0; i < n; i++)
    {
        cin >> r[i];
    }
    vector<vector<int>> g(n);
    for (int i = 0; i < n - 1; i++)
    {
        int u, v;
        cin >> u >> v;
        u--;
        v--;
        g[u].pb(v);
        g[v].pb(u);
    }
    while (q--)
    {
        int x, y;
        cin >> x >> y;
        x--;
        y--;
        vector<int> dist(n, ma);
        dist[x] = 0;
        vector<int> p(n);
        p[x] = -1;
        priority_queue<pair<int, int>> q;
        q.push({0, x});
        vector<bool> used(n);
        while (!q.empty())
        {
            int v = q.top().S, c = -q.top().F;
            q.pop();
            if (dist[v] < c)
            {
                continue;
            }
            for (auto it : g[v])
            {
                if (dist[it] > c + 1)
                {
                    dist[it] = c + 1;
                    p[it] = v;
                    q.push({-c - 1, it});
                }
            }
        }
        vector<int> path;
        int par = y;
        while (par != -1)
        {
            path.pb(par);
            par = p[par];
        }
        reverse(path.begin(), path.end());
        int dp[path.size() + 1][5];
        for (int i = 0; i < path.size() + 1; i++)
        {
            for (int j = 0; j < 5; j++)
            {
                dp[i][j] = -1e18;
            }
        }
        dp[0][2] = 0;
        for (int i = 0; i < path.size(); i++)
        {
            dp[i + 1][0] = dp[i][1] + r[path[i]];
            dp[i + 1][1] = max(dp[i][2] + r[path[i]], dp[i][0] + b[path[i]]);
            dp[i + 1][2] = max(dp[i][1] + b[path[i]], dp[i][3] + r[path[i]]);
            dp[i + 1][3] = max(dp[i][2] + b[path[i]], dp[i][4] + r[path[i]]);
            dp[i + 1][4] = dp[i][3] + b[path[i]];
        }
        int ans = mi;
        for (int i = 0; i < 5; i++)
        {
            ans = max(ans, dp[path.size()][i]);
        }
        cout << ans << endl;
    }
}
#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...