結果

問題 No.1420 国勢調査 (Easy)
ユーザー chocono2230chocono2230
提出日時 2021-03-05 22:42:53
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 383 ms / 2,000 ms
コード長 1,523 bytes
コンパイル時間 2,517 ms
コンパイル使用メモリ 208,508 KB
実行使用メモリ 17,920 KB
最終ジャッジ日時 2024-04-16 10:28:59
合計ジャッジ時間 10,793 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 203 ms
13,416 KB
testcase_03 AC 223 ms
13,412 KB
testcase_04 AC 203 ms
13,544 KB
testcase_05 AC 205 ms
13,424 KB
testcase_06 AC 208 ms
13,416 KB
testcase_07 AC 192 ms
13,416 KB
testcase_08 AC 194 ms
13,416 KB
testcase_09 AC 200 ms
13,420 KB
testcase_10 AC 193 ms
13,416 KB
testcase_11 AC 188 ms
13,296 KB
testcase_12 AC 14 ms
6,272 KB
testcase_13 AC 152 ms
7,168 KB
testcase_14 AC 18 ms
6,528 KB
testcase_15 AC 156 ms
7,168 KB
testcase_16 AC 151 ms
7,296 KB
testcase_17 AC 152 ms
7,168 KB
testcase_18 AC 154 ms
7,296 KB
testcase_19 AC 153 ms
7,296 KB
testcase_20 AC 152 ms
7,168 KB
testcase_21 AC 153 ms
7,168 KB
testcase_22 AC 380 ms
17,792 KB
testcase_23 AC 383 ms
17,920 KB
testcase_24 AC 379 ms
17,920 KB
testcase_25 AC 379 ms
17,792 KB
testcase_26 AC 375 ms
17,792 KB
testcase_27 AC 97 ms
10,624 KB
testcase_28 AC 247 ms
17,408 KB
testcase_29 AC 50 ms
8,320 KB
testcase_30 AC 149 ms
12,544 KB
testcase_31 AC 145 ms
12,416 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (int)(n); i++)
#define rrep(ri,n) for(int ri = (int)(n-1); ri >= 0; ri--)
#define rep2(i,x,n) for(int i = (int)(x); i < (int)(n); i++)
#define rrep2(ri,x,n) for(int ri = (int)(n-1); ri >= (int)(x); ri--)
#define repit(itr,x) for(auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr,x) for(auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

bool dfs(vector<vector<pair<int, int>>> &tree, vector<int> &ans, int now) {
  bool res = false;
  for(auto [nx, y] : tree.at(now)) {
    if(ans.at(nx) != -1) {
      if((ans.at(nx) ^ ans.at(now)) != y) return true;
      continue;
    }
    ans.at(nx) = ans.at(now) ^ y;
    res |= dfs(tree, ans, nx);
  }
  return res;
}

int main() {
  int n, m;
  cin >> n >> m;
  vector tree(n, vector<pair<int, int>>());
  map<pair<int, int>, int> mp;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--; b--;
    int y;
    cin >> y;
    if(mp.find({a, b}) != mp.end()) {
      if(mp[{a, b}] != y) {
        cout << -1 << endl;
        return 0;
      }
    } else {
      tree.at(a).push_back({b, y});
      tree.at(b).push_back({a, y});
      mp[{a, b}] = y;
    }
  }

  vector<int> ans(n, -1);
  rep(i, n) {
    if(ans.at(i) == -1) {
      ans.at(i) = 0;
      bool r = dfs(tree, ans, i);
      if(r == true) {
        cout << -1 << endl;
        return 0;
      }
    }
  }
  rep(i, n) {
    cout << ans.at(i) << endl;
  }
  return 0;
}
0