結果

問題 No.1911 Alternately Coloring
ユーザー 👑 colognecologne
提出日時 2022-04-22 22:12:01
言語 C++23(draft)
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 247 ms / 4,000 ms
コード長 1,964 bytes
コンパイル時間 1,135 ms
コンパイル使用メモリ 97,156 KB
実行使用メモリ 4,388 KB
最終ジャッジ日時 2023-09-06 08:43:19
合計ジャッジ時間 6,580 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 193 ms
4,380 KB
testcase_07 AC 148 ms
4,384 KB
testcase_08 AC 137 ms
4,380 KB
testcase_09 AC 183 ms
4,380 KB
testcase_10 AC 186 ms
4,380 KB
testcase_11 AC 215 ms
4,380 KB
testcase_12 AC 163 ms
4,380 KB
testcase_13 AC 158 ms
4,376 KB
testcase_14 AC 117 ms
4,384 KB
testcase_15 AC 188 ms
4,384 KB
testcase_16 AC 171 ms
4,376 KB
testcase_17 AC 193 ms
4,380 KB
testcase_18 AC 195 ms
4,376 KB
testcase_19 AC 223 ms
4,376 KB
testcase_20 AC 247 ms
4,384 KB
testcase_21 AC 135 ms
4,380 KB
testcase_22 AC 175 ms
4,376 KB
testcase_23 AC 200 ms
4,380 KB
testcase_24 AC 150 ms
4,380 KB
testcase_25 AC 160 ms
4,388 KB
testcase_26 AC 202 ms
4,380 KB
testcase_27 AC 85 ms
4,380 KB
testcase_28 AC 83 ms
4,376 KB
testcase_29 AC 209 ms
4,376 KB
testcase_30 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <functional>
#include <queue>
#include <vector>
using namespace std;

int main()
{
    int N, M;
    scanf("%d%d", &N, &M);
    vector<vector<int>> adj(N);
    for (int i = 0; i < M; i++)
    {
        int u, v;
        scanf("%d%d", &u, &v);
        adj[u - 1].push_back(v - 1);
        adj[v - 1].push_back(u - 1);
    }
    vector<int> R(N), B(N);
    for (int &r : R)
        scanf("%d", &r);
    for (int &b : B)
        scanf("%d", &b);

    vector<bool> vis(N);

    long ans = 0;

    auto solve = [&](int a)
    {
        long ans = 0, n1 = 0, n2 = 0;
        vector<int> target;
        function<void(int, bool)> dfs = [&](int a, bool b)
        {
            vis[a] = true;
            target.push_back(a);
            ans += max(R[a], B[a]);
            n1 += b ? R[a] : B[a];
            n2 += b ? B[a] : R[a];
            for (auto x : adj[a])
                if (!vis[x])
                    dfs(x, !b);
        };
        dfs(a, true);

        long sav = ans - max(n1, n2);

        for (int u : target)
        {
            using T = tuple<long, int, bool>;
            priority_queue<T, vector<T>, greater<T>> Q;
            Q.emplace(max(R[u], B[u]) - R[u], u, true);
            vector<bool> vis[2] = {vector<bool>(N), vector<bool>(N)};
            while (!Q.empty())
            {
                auto [d, v, t] = Q.top();
                Q.pop();
                if (vis[t][v])
                    continue;
                vis[t][v] = true;
                if (!t && v == u)
                {
                    sav = min(sav, d - (max(R[u], B[u]) - min(R[u], B[u])));
                    break;
                }

                for (auto x : adj[v])
                    Q.emplace(d + max(R[x], B[x]) - (!t ? R[x] : B[x]), x, !t);
            }
        }

        return ans - sav;
    };

    for (int i = 0; i < N; i++)
        if (!vis[i])
            ans = max(ans, solve(i));

    printf("%ld\n", ans);
}
0