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