結果

問題 No.1911 Alternately Coloring
ユーザー 👑 colognecologne
提出日時 2022-04-22 22:09:20
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,925 bytes
コンパイル時間 1,679 ms
コンパイル使用メモリ 97,456 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-06 08:40:10
合計ジャッジ時間 6,755 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 188 ms
4,376 KB
testcase_07 AC 145 ms
4,376 KB
testcase_08 AC 135 ms
4,376 KB
testcase_09 AC 176 ms
4,380 KB
testcase_10 AC 178 ms
4,376 KB
testcase_11 AC 213 ms
4,376 KB
testcase_12 AC 159 ms
4,376 KB
testcase_13 AC 152 ms
4,380 KB
testcase_14 AC 115 ms
4,376 KB
testcase_15 AC 185 ms
4,376 KB
testcase_16 AC 162 ms
4,380 KB
testcase_17 AC 178 ms
4,380 KB
testcase_18 AC 183 ms
4,376 KB
testcase_19 AC 213 ms
4,376 KB
testcase_20 AC 240 ms
4,380 KB
testcase_21 AC 125 ms
4,376 KB
testcase_22 AC 176 ms
4,376 KB
testcase_23 AC 194 ms
4,376 KB
testcase_24 AC 145 ms
4,376 KB
testcase_25 AC 153 ms
4,380 KB
testcase_26 WA -
testcase_27 AC 79 ms
4,376 KB
testcase_28 AC 76 ms
4,380 KB
testcase_29 AC 193 ms
4,376 KB
testcase_30 AC 1 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, nat = 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]);
            nat += b ? R[a] : B[a];
            for (auto x : adj[a])
                if (!vis[x])
                    dfs(x, !b);
        };
        dfs(a, true);

        long sav = min(nat, ans - nat);

        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