結果

問題 No.1420 国勢調査 (Easy)
ユーザー merom686merom686
提出日時 2021-03-05 21:42:58
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 61 ms / 2,000 ms
コード長 1,530 bytes
コンパイル時間 1,059 ms
コンパイル使用メモリ 98,488 KB
実行使用メモリ 7,296 KB
最終ジャッジ日時 2024-04-16 09:07:26
合計ジャッジ時間 4,953 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 47 ms
6,940 KB
testcase_03 AC 46 ms
6,940 KB
testcase_04 AC 46 ms
6,940 KB
testcase_05 AC 47 ms
6,944 KB
testcase_06 AC 47 ms
6,944 KB
testcase_07 AC 43 ms
6,940 KB
testcase_08 AC 46 ms
6,940 KB
testcase_09 AC 48 ms
6,940 KB
testcase_10 AC 46 ms
6,944 KB
testcase_11 AC 47 ms
6,940 KB
testcase_12 AC 8 ms
6,944 KB
testcase_13 AC 13 ms
6,940 KB
testcase_14 AC 8 ms
6,940 KB
testcase_15 AC 13 ms
6,940 KB
testcase_16 AC 13 ms
6,944 KB
testcase_17 AC 13 ms
6,940 KB
testcase_18 AC 13 ms
6,940 KB
testcase_19 AC 13 ms
6,944 KB
testcase_20 AC 14 ms
6,940 KB
testcase_21 AC 13 ms
6,944 KB
testcase_22 AC 61 ms
7,168 KB
testcase_23 AC 59 ms
7,076 KB
testcase_24 AC 59 ms
7,184 KB
testcase_25 AC 61 ms
7,168 KB
testcase_26 AC 61 ms
7,168 KB
testcase_27 AC 55 ms
7,296 KB
testcase_28 AC 55 ms
7,168 KB
testcase_29 AC 54 ms
7,168 KB
testcase_30 AC 55 ms
7,296 KB
testcase_31 AC 55 ms
7,168 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

struct Graph {
    struct Vertex { int n, x, f; };
    struct Edge { int i, n, y; };
    Graph(int n, int m) : v(n, { -1, 0, 0 }), e(m), n(n), m(0) {}
    void add_edge(int a, int b, int y) {
        e[m] = { b, v[a].n, y };
        v[a].n = m;
        m++;
    }
    void dfs(int i, int x) {
        if (v[i].f) return;
        v[i].f = 1;
        v[i].x = x;
        for (int j = v[i].n; j >= 0; j = e[j].n) {
            Edge &o = e[j];
            dfs(o.i, x ^ o.y);
        }
    }
    void solve() {
        for (int i = 0; i < n; i++) {
            dfs(i, 0);
        }
        int b = 1;
        for (int i = 0; i < n; i++) {
            for (int j = v[i].n; j >= 0; j = e[j].n) {
                Edge &o = e[j];
                if ((v[i].x ^ v[o.i].x) != o.y) b = 0;
            }
        }
        if (b) {
            for (int i = 0; i < n; i++) {
                cout << v[i].x << '\n';
            }
        } else {
            cout << -1 << endl;
        }
    }
    vector<Vertex> v;
    vector<Edge> e;
    int n, m;
};

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);

    int n, m;
    cin >> n >> m;

    Graph g(n, m * 2);
    for (int i = 0; i < m; i++) {
        int a, b, y;
        cin >> a >> b >> y;
        a--; b--;

        g.add_edge(a, b, y);
        g.add_edge(b, a, y);
    }
    g.solve();

    return 0;
}
0