結果

問題 No.1420 国勢調査 (Easy)
ユーザー srjywrdnprktsrjywrdnprkt
提出日時 2023-06-19 20:40:50
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 279 ms / 2,000 ms
コード長 862 bytes
コンパイル時間 2,895 ms
コンパイル使用メモリ 210,756 KB
実行使用メモリ 10,560 KB
最終ジャッジ日時 2023-09-09 15:51:24
合計ジャッジ時間 9,543 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 112 ms
6,616 KB
testcase_03 AC 110 ms
6,812 KB
testcase_04 AC 112 ms
6,528 KB
testcase_05 AC 112 ms
6,560 KB
testcase_06 AC 113 ms
6,668 KB
testcase_07 AC 96 ms
7,012 KB
testcase_08 AC 98 ms
6,796 KB
testcase_09 AC 96 ms
6,836 KB
testcase_10 AC 97 ms
6,848 KB
testcase_11 AC 95 ms
6,692 KB
testcase_12 AC 13 ms
6,360 KB
testcase_13 AC 147 ms
6,180 KB
testcase_14 AC 15 ms
6,528 KB
testcase_15 AC 145 ms
6,532 KB
testcase_16 AC 145 ms
6,492 KB
testcase_17 AC 149 ms
6,464 KB
testcase_18 AC 146 ms
6,492 KB
testcase_19 AC 145 ms
6,456 KB
testcase_20 AC 145 ms
6,560 KB
testcase_21 AC 146 ms
6,728 KB
testcase_22 AC 269 ms
10,432 KB
testcase_23 AC 272 ms
10,516 KB
testcase_24 AC 274 ms
10,400 KB
testcase_25 AC 271 ms
10,560 KB
testcase_26 AC 279 ms
10,440 KB
testcase_27 AC 120 ms
9,356 KB
testcase_28 AC 120 ms
9,344 KB
testcase_29 AC 121 ms
9,336 KB
testcase_30 AC 119 ms
9,384 KB
testcase_31 AC 119 ms
9,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

vector<bool> vst;
vector<int> ans;

void dfs(vector<vector<pair<int, int>>> &E, int from){
    vst[from] = 1;
    int alt;

    for(auto [to, C] : E[from]){
        alt = C ^ ans[from];
        if (vst[to]){
            if (alt != ans[to]){
                cout << -1 << endl;
                exit(0);
            }
            continue;
        }
        ans[to] = alt;
        dfs(E, to);
    }
}

int main(){

    int N, M, A, B, Y;
    cin >> N >> M;
    vector<vector<pair<int, int>>> E(N);

    for (int i=0; i<M; i++){
        cin >> A >> B >> Y; A--; B--;
        E[A].push_back({B, Y});
        E[B].push_back({A, Y});
    }
    vst.resize(N);
    ans.resize(N);
    
    for (int i=0; i<N; i++){
        if (!vst[i]) dfs(E, i);
    }

    for (int i=0; i<N; i++) cout << ans[i] << endl;

    return 0;
}
0