結果

問題 No.3425 Mod K Graph Increments (Easy)
コンテスト
ユーザー まみめ
提出日時 2025-12-30 00:38:45
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 145 ms / 2,000 ms
コード長 2,285 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,497 ms
コンパイル使用メモリ 346,096 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2026-01-11 13:05:48
合計ジャッジ時間 4,809 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 7
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

void solve() {
    int N, M, K;
    cin >> N >> M >> K;
    vector<int> U(M), V(M);
    vector<vector<int>> G(N);
    for (int i = 0; i < M; i++) {
        cin >> U[i] >> V[i];
        U[i]--, V[i]--;
        G[U[i]].push_back(V[i]);
        G[V[i]].push_back(U[i]);
    }
    vector<int> B(N);
    for (int i = 0; i < N; i++) {
        cin >> B[i];
    }
    vector<int> color(N, -1); // 未着色:-1, 白色:0, 黒色:1
    for (int i = 0; i < N; i++) {
        if (color[i] == -1) {
            long long sum = 0; // 連結成分内の頂点に書かれている数字の和
            long long white_sum =
                0; // 連結成分内の白く塗られている頂点に書かれている数字の和
            long long black_sum =
                0; // 連結成分内の黒く塗られている頂点に書かれている数字の和
            deque<pair<int, int>> que;
            color[i] = 0;
            que.push_back({i, 0});
            bool is_bipartite = true;
            while (que.size()) {
                int v = que.front().first;
                int nowc = que.front().second;
                que.pop_front();
                sum += B[v];
                if (nowc == 0) {
                    white_sum += B[v];
                } else {
                    black_sum += B[v];
                }
                for (int to : G[v]) {
                    if (color[to] == color[v]) {
                        is_bipartite = false;
                    }
                    if (color[to] == -1) {
                        int nxtc = nowc ^ 1;
                        color[to] = nxtc;
                        que.push_back({to, nxtc});
                    }
                }
            }
            if (is_bipartite == true) {
                if (white_sum % K != black_sum % K) {
                    cout << "No" << endl;
                    return;
                }
            } else {
                if (K % 2 == 0 && sum % 2 != 0) {
                    cout << "No" << endl;
                    return;
                }
            }
        }
    }
    cout << "Yes" << endl;
}

int main() {
    int T;
    cin >> T;
    for (int i = 0; i < T; i++) {
        solve();
    }
    return 0;
}
0