結果

問題 No.1911 Alternately Coloring
ユーザー 👑 NachiaNachia
提出日時 2022-04-22 21:33:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,746 bytes
コンパイル時間 1,218 ms
コンパイル使用メモリ 97,540 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-06 07:52:35
合計ジャッジ時間 7,812 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 332 ms
4,376 KB
testcase_07 AC 259 ms
4,380 KB
testcase_08 AC 257 ms
4,380 KB
testcase_09 AC 327 ms
4,380 KB
testcase_10 AC 247 ms
4,376 KB
testcase_11 AC 288 ms
4,376 KB
testcase_12 AC 324 ms
4,376 KB
testcase_13 AC 280 ms
4,380 KB
testcase_14 AC 204 ms
4,380 KB
testcase_15 AC 270 ms
4,380 KB
testcase_16 AC 265 ms
4,376 KB
testcase_17 AC 275 ms
4,380 KB
testcase_18 AC 182 ms
4,380 KB
testcase_19 AC 208 ms
4,376 KB
testcase_20 AC 237 ms
4,380 KB
testcase_21 AC 138 ms
4,380 KB
testcase_22 AC 322 ms
4,376 KB
testcase_23 AC 399 ms
4,376 KB
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 257 ms
4,380 KB
testcase_28 AC 75 ms
4,380 KB
testcase_29 AC 198 ms
4,376 KB
testcase_30 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
using namespace std;
using i32 = int;
using u32 = unsigned int;
using i64 = long long;
using u64 = unsigned long long;
#define rep(i,n) for(int i=0; i<(int)(n); i++)

template<class T> using nega_queue = priority_queue<T, vector<T>, greater<T>>;

const i64 INF = 1001001001001001001;

int main(){
    int N, M; cin >> N >> M;
    vector<vector<int>> E(N);
    rep(i,M){
        int u,v; cin >> u >> v; u--; v--;
        E[u].push_back(v);
        E[v].push_back(u);
    }

    vector<int> A[2];
    rep(t,2) A[t].resize(N);
    rep(t,2) rep(i,N) cin >> A[t][i];

    bool is_bipartite = true;
    {
        vector<int> C(N, -1);
        C[0] = 0;
        vector<int> bfs = {0};
        rep(i,bfs.size()){
            int p = bfs[i];
            for(int e : E[p]) if(C[e] == -1){
                bfs.push_back(e);
                C[e] = C[p] ^ 1;
            }
            for(int e : E[p]) if(C[e] == C[p]){
                is_bipartite = false;
            }
        }
    }
    
    i64 ans = 0;

    {
        vector<int> C(N, -1);
        C[0] = 0;
        vector<int> bfs = {0};
        rep(i,bfs.size()){
            int p = bfs[i];
            for(int e : E[p]) if(C[e] == -1){
                bfs.push_back(e);
                C[e] = C[p] ^ 1;
            }
        }
        i64 sc[2] = {};
        rep(i,N) rep(t,2) sc[t] = A[C[i]^t][i];
        ans = max(sc[0], sc[1]);
    }

    if(!is_bipartite){
        vector<int> maxA(N);
        rep(i,N) maxA[i] = max(A[0][i], A[1][i]);

        i64 full = 0;
        rep(i,N) full += maxA[i];

        vector<vector<pair<int, int>>> adj(N*2);
        vector<int> cost(N*2);
        rep(t,2) rep(u,N) cost[N*t+u] = maxA[u] - A[t][u];
        rep(u,N) for(int v : E[u]){
            adj[u].push_back(make_pair(N+v, cost[N+v]));
            adj[N+u].push_back(make_pair(v, cost[v]));
        }

        rep(s,N*2){
            int t = (s+N) % (N*2);
            vector<i64> D(N*2, INF);
            nega_queue<pair<i64, int>> Que;
            D[s] = 0;
            Que.push(make_pair(D[s], s));
            while(Que.size()){
                auto [d,p] = Que.top(); Que.pop();
                if(D[p] != d) continue;
                for(auto [to, dd] : adj[p]){
                    i64 nxd = d + dd;
                    if(D[to] <= nxd) continue;
                    D[to] = nxd;
                    Que.push(make_pair(D[to], to));
                }
            }
            ans = max(ans, full - D[t]);
        }
    }

    cout << ans << '\n';
    return 0;
}

struct ios_do_not_sync{
    ios_do_not_sync(){
        std::ios::sync_with_stdio(false);
        std::cin.tie(nullptr);
    }
} ios_do_not_sync_instance;
0