結果

問題 No.1420 国勢調査 (Easy)
ユーザー magstamagsta
提出日時 2021-03-05 22:18:19
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 285 ms / 2,000 ms
コード長 1,386 bytes
コンパイル時間 875 ms
コンパイル使用メモリ 78,888 KB
実行使用メモリ 11,520 KB
最終ジャッジ日時 2024-04-16 09:53:42
合計ジャッジ時間 7,751 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 123 ms
7,124 KB
testcase_03 AC 123 ms
7,136 KB
testcase_04 AC 123 ms
7,132 KB
testcase_05 AC 126 ms
7,136 KB
testcase_06 AC 125 ms
7,168 KB
testcase_07 AC 110 ms
7,148 KB
testcase_08 AC 110 ms
7,008 KB
testcase_09 AC 109 ms
7,140 KB
testcase_10 AC 108 ms
7,140 KB
testcase_11 AC 110 ms
7,144 KB
testcase_12 AC 19 ms
6,528 KB
testcase_13 AC 150 ms
6,528 KB
testcase_14 AC 18 ms
6,656 KB
testcase_15 AC 147 ms
6,528 KB
testcase_16 AC 151 ms
6,656 KB
testcase_17 AC 147 ms
6,528 KB
testcase_18 AC 147 ms
6,528 KB
testcase_19 AC 148 ms
6,656 KB
testcase_20 AC 147 ms
6,528 KB
testcase_21 AC 149 ms
6,528 KB
testcase_22 AC 279 ms
11,520 KB
testcase_23 AC 282 ms
11,520 KB
testcase_24 AC 282 ms
11,520 KB
testcase_25 AC 285 ms
11,520 KB
testcase_26 AC 281 ms
11,520 KB
testcase_27 AC 157 ms
11,520 KB
testcase_28 AC 148 ms
11,520 KB
testcase_29 AC 148 ms
11,520 KB
testcase_30 AC 155 ms
11,520 KB
testcase_31 AC 152 ms
11,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
using Graph = vector<vector<pair<int ,int>>>;

// 深さ優先探索
vector<bool> seen;
bool flag = true;
int N;
int xor_[100000];
void dfs(const Graph& G, int v) {
    seen[v] = true;
    for (auto next_v : G[v]) {
        if (seen[next_v.first]) {
            int a = xor_[v] ^ xor_[next_v.first];
            if (a != next_v.second) {
                flag = false;
            }
            continue;
        }
        xor_[next_v.first] = xor_[v] ^ next_v.second;
        dfs(G, next_v.first); // 再帰的に探索
    }
}

int main() {
    // 頂点数と辺数
    int M; cin >> N >> M;

    // グラフ入力受取
    Graph G(N);
    for (int i = 0; i < M; ++i) {
        int a, b, y;
        cin >> a >> b >> y;
        G[a - 1].push_back(make_pair(b - 1, y));
        G[b - 1].push_back(make_pair(a - 1, y));
    }

    // 全頂点が訪問済みになるまで探索
    int count = 0;
    seen.assign(N, false);
    for (int i = 0; i < N; i++) xor_[i] = 0;
    for (int v = 0; v < N; ++v) {
        if (seen[v]) continue; // v が探索済みだったらスルー
        dfs(G, v); // v が未探索なら v を始点とした DFS を行う
        ++count;
    }

    if (flag == false) cout << -1 << endl;
    else {
        for (int i = 0; i < N; i++) {
            cout << xor_[i] << endl;
        }
    }
}
0