結果

問題 No.1420 国勢調査 (Easy)
ユーザー milanis48663220
提出日時 2021-03-05 22:36:31
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 205 ms / 2,000 ms
コード長 1,543 bytes
コンパイル時間 864 ms
コンパイル使用メモリ 93,028 KB
最終ジャッジ日時 2025-01-19 11:33:00
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <iomanip>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <cassert>

#define debug_value(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << #x << "=" << x << endl;
#define debug(x) cerr << "line" << __LINE__ << ":<" << __func__ << ">:" << x << endl;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

using namespace std;
typedef long long ll;
bool used[100000];

struct edge{
    int to, y;
};

vector<edge> g[100000];
int ans[100000];

bool dfs(int v, int x){
    used[v] = true;
    ans[v] = x;
    for(edge e : g[v]){
        if(!used[e.to]){
            if(!dfs(e.to, x^e.y)) return false;
        }else{
            if(ans[e.to] != (x^e.y)) return false;
        }
    }
    return true;
}

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << setprecision(10) << fixed;
    int n, m; cin >> n >> m;
    for(int i = 0; i < m; i++){
        int a, b, y; cin >> a >> b >> y; a--; b--;
        g[a].push_back(edge{b, y});
        g[b].push_back(edge{a, y});
    }
    for(int i = 0; i < n; i++){
        if(!used[i]){
            if(!dfs(i, 0)){
                cout << -1 << endl;
                return 0;
            }
        }
    }
    
    for(int v = 0; v < n; v++){
        for(edge e : g[v]) assert(ans[v]^ans[e.to] == e.y);
    }
    for(int i = 0; i < n; i++) cout << ans[i] << endl;
}
0