結果

問題 No.1420 国勢調査 (Easy)
ユーザー e869120e869120
提出日時 2021-03-05 21:48:52
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 518 ms / 2,000 ms
コード長 1,383 bytes
コンパイル時間 1,019 ms
コンパイル使用メモリ 86,160 KB
実行使用メモリ 18,960 KB
最終ジャッジ日時 2023-07-29 01:29:43
合計ジャッジ時間 11,859 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
13,632 KB
testcase_01 AC 5 ms
13,540 KB
testcase_02 AC 168 ms
17,132 KB
testcase_03 AC 169 ms
17,168 KB
testcase_04 AC 169 ms
17,084 KB
testcase_05 AC 169 ms
17,144 KB
testcase_06 AC 169 ms
17,136 KB
testcase_07 AC 156 ms
17,092 KB
testcase_08 AC 153 ms
17,164 KB
testcase_09 AC 154 ms
17,164 KB
testcase_10 AC 151 ms
17,172 KB
testcase_11 AC 168 ms
17,092 KB
testcase_12 AC 58 ms
14,156 KB
testcase_13 AC 186 ms
14,448 KB
testcase_14 AC 59 ms
14,236 KB
testcase_15 AC 187 ms
14,320 KB
testcase_16 AC 187 ms
14,188 KB
testcase_17 AC 184 ms
14,232 KB
testcase_18 AC 183 ms
14,156 KB
testcase_19 AC 186 ms
14,260 KB
testcase_20 AC 184 ms
14,236 KB
testcase_21 AC 188 ms
14,440 KB
testcase_22 AC 497 ms
18,716 KB
testcase_23 AC 506 ms
18,760 KB
testcase_24 AC 510 ms
18,704 KB
testcase_25 AC 518 ms
18,660 KB
testcase_26 AC 515 ms
18,776 KB
testcase_27 AC 401 ms
18,656 KB
testcase_28 AC 395 ms
18,960 KB
testcase_29 AC 401 ms
18,664 KB
testcase_30 AC 390 ms
18,704 KB
testcase_31 AC 401 ms
18,692 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;
			continue;
		}
		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