結果
| 問題 | No.1420 国勢調査 (Easy) | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2021-03-05 21:49:30 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 67 ms / 2,000 ms | 
| コード長 | 1,723 bytes | 
| コンパイル時間 | 725 ms | 
| コンパイル使用メモリ | 77,944 KB | 
| 最終ジャッジ日時 | 2025-01-19 10:52:33 | 
| ジャッジサーバーID (参考情報) | judge4 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 30 | 
ソースコード
#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;
}
            
            
            
        