結果

問題 No.1420 国勢調査 (Easy)
ユーザー MisterMister
提出日時 2021-03-05 21:49:30
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 93 ms / 2,000 ms
コード長 1,723 bytes
コンパイル時間 818 ms
コンパイル使用メモリ 81,708 KB
実行使用メモリ 10,752 KB
最終ジャッジ日時 2024-04-16 09:15:52
合計ジャッジ時間 4,639 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 48 ms
7,808 KB
testcase_03 AC 47 ms
7,936 KB
testcase_04 AC 46 ms
7,936 KB
testcase_05 AC 48 ms
7,936 KB
testcase_06 AC 48 ms
7,936 KB
testcase_07 AC 44 ms
7,936 KB
testcase_08 AC 44 ms
7,936 KB
testcase_09 AC 46 ms
8,064 KB
testcase_10 AC 46 ms
7,936 KB
testcase_11 AC 45 ms
7,936 KB
testcase_12 AC 10 ms
6,784 KB
testcase_13 AC 17 ms
6,528 KB
testcase_14 AC 9 ms
6,784 KB
testcase_15 AC 18 ms
6,656 KB
testcase_16 AC 18 ms
6,528 KB
testcase_17 AC 18 ms
6,528 KB
testcase_18 AC 19 ms
6,528 KB
testcase_19 AC 19 ms
6,400 KB
testcase_20 AC 19 ms
6,528 KB
testcase_21 AC 19 ms
6,528 KB
testcase_22 AC 89 ms
10,624 KB
testcase_23 AC 88 ms
10,752 KB
testcase_24 AC 89 ms
10,752 KB
testcase_25 AC 93 ms
10,752 KB
testcase_26 AC 83 ms
10,752 KB
testcase_27 AC 65 ms
10,368 KB
testcase_28 AC 62 ms
10,240 KB
testcase_29 AC 60 ms
10,240 KB
testcase_30 AC 59 ms
10,368 KB
testcase_31 AC 62 ms
10,368 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#line 2 "/Users/tiramister/CompetitiveProgramming/Cpp/CppLibrary/Graph/graph.hpp"

#include <vector>

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;

    Edge() = default;
    Edge(int src, int dst, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return cost > e.cost; }
};

template <class Cost = int>
struct Graph : public std::vector<std::vector<Edge<Cost>>> {
    using std::vector<std::vector<Edge<Cost>>>::vector;

    void span(bool direct, int src, int dst, Cost cost = 1) {
        (*this)[src].emplace_back(src, dst, cost);
        if (!direct) (*this)[dst].emplace_back(dst, src, cost);
    }
};
#line 2 "main.cpp"
#include <iostream>
#line 4 "main.cpp"

using namespace std;

void solve() {
    int n, m;
    cin >> n >> m;

    Graph<int> graph(n);
    while (m--) {
        int u, v, c;
        cin >> u >> v >> c;
        graph.span(false, --u, --v, c);
    }

    vector<int> ans(n, -1);
    auto dfs = [&](auto&& f, int v) -> void {
        for (auto e : graph[v]) {
            int u = e.dst, c = e.cost;

            if (ans[u] != -1) {
                if ((ans[v] ^ ans[u]) != c) {
                    cout << "-1\n";
                    exit(0);
                }
            } else {
                ans[u] = (ans[v] ^ c);
                f(f, u);
            }
        }
    };
    for (int v = 0; v < n; ++v) {
        if (ans[v] != -1) continue;
        ans[v] = 0;
        dfs(dfs, v);
    }

    for (auto x : ans) cout << x << "\n";
}

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

    solve();

    return 0;
}
0