結果
問題 | No.1420 国勢調査 (Easy) |
ユーザー |
![]() |
提出日時 | 2021-05-13 20:21:22 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 396 ms / 2,000 ms |
コード長 | 1,983 bytes |
コンパイル時間 | 4,321 ms |
コンパイル使用メモリ | 111,516 KB |
実行使用メモリ | 47,188 KB |
最終ジャッジ日時 | 2024-09-25 08:30:10 |
合計ジャッジ時間 | 15,692 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 30 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System.Linq;using System.Collections.Generic;using System;public class P{public int xor { get; set; }public int to { get; set; }}public class Hello{static void Main(){string[] line = Console.ReadLine().Trim().Split(' ');var n = int.Parse(line[0]);var m = int.Parse(line[1]);var aa = new List<P>[n];for (int i = 0; i < n; i++) aa[i] = new List<P>();var check = new int[m, 3];for (int i = 0; i < m; i++){line = Console.ReadLine().Trim().Split(' ');var t0 = int.Parse(line[0]) - 1;var t1 = int.Parse(line[1]) - 1;var t2 = int.Parse(Console.ReadLine().Trim());aa[t0].Add(new P { to = t1, xor = t2 });aa[t1].Add(new P { to = t0, xor = t2 });check[i, 0] = t0;check[i, 1] = t1;check[i, 2] = t2;}getAns(n, m, aa, check);}static void getAns(int n, int m, List<P>[] aa, int[,] check){var ans = new int[n];for (int i = 0; i < n; i++) ans[i] = -1;var q = new Queue<int>();for (int i = 0; i < n; i++){if (ans[i] == -1){ans[i] = 0;q.Enqueue(i);while (q.Count() > 0){var w = q.Dequeue();foreach (var x in aa[w]){var t = ans[w] ^ x.xor;if (ans[x.to] == -1) { ans[x.to] = t; q.Enqueue(x.to); }}}}}var ok = true;for (int i = 0; i < m; i++){if ((ans[check[i, 0]] ^ ans[check[i, 1]]) != check[i, 2]){ok = false;break;}}if (ok) Console.WriteLine(string.Join("\n", ans));else Console.WriteLine(-1);}}