結果

問題 No.1420 国勢調査 (Easy)
ユーザー e869120e869120
提出日時 2021-03-05 21:48:26
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,381 bytes
コンパイル時間 810 ms
コンパイル使用メモリ 88,564 KB
実行使用メモリ 18,232 KB
最終ジャッジ日時 2024-04-16 09:14:13
合計ジャッジ時間 10,023 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
13,208 KB
testcase_01 AC 5 ms
14,104 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 149 ms
16,576 KB
testcase_08 AC 147 ms
17,080 KB
testcase_09 AC 145 ms
17,268 KB
testcase_10 AC 144 ms
16,516 KB
testcase_11 AC 145 ms
16,604 KB
testcase_12 AC 61 ms
15,176 KB
testcase_13 WA -
testcase_14 AC 65 ms
13,608 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 AC 300 ms
17,180 KB
testcase_28 AC 305 ms
17,408 KB
testcase_29 AC 294 ms
17,396 KB
testcase_30 AC 312 ms
17,720 KB
testcase_31 AC 303 ms
17,852 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
using namespace std;

int N, M;
int A[1 << 18], B[1 << 18], Y[1 << 18];
int Answer[1 << 18];
int col[1 << 18];
bool used[1 << 18], flag = false;
vector<pair<int, int>> G[1 << 18];

void dfs(int pos, int dep) {
	used[pos] = true;
	col[pos] = dep;
	for (int i = 0; i < G[pos].size(); i++) {
		int to = G[pos][i].first;
		int cost = G[pos][i].second;
		if (used[to] == true) {
			if (col[to] != (cost ^ dep)) flag = true;
			return;
		}
		else {
			dfs(to, (dep ^ cost));
		}
	}
}

int main() {
	// Step 1. 入力
	cin >> N >> M;
	for (int i = 1; i <= M; i++) {
		cin >> A[i] >> B[i] >> Y[i];
	}

	// Step #2. ビットごとに計算
	for (int i = 0; i < 30; i++) {
		for (int j = 1; j <= N; j++) G[j].clear();
		for (int j = 1; j <= N; j++) used[j] = false;
		for (int j = 1; j <= M; j++) {
			int t = (Y[j] / (1 << i)) % 2;
			G[A[j]].push_back(make_pair(B[j], t));
			G[B[j]].push_back(make_pair(A[j], t));
		}
		for (int j = 1; j <= N; j++) {
			if (used[j] == false) dfs(j, 0);
		}
		for (int j = 1; j <= N; j++) {
			if (col[j] == 1) Answer[j] += (1 << i);
		}
	}

	// Step #3. 出力
	if (flag == true) {
		cout << "-1" << endl;
	}
	else {
		for (int i = 1; i <= N; i++) cout << Answer[i] << endl;
	}
	return 0;
}
0