結果
問題 |
No.583 鉄道同好会
|
ユーザー |
![]() |
提出日時 | 2018-09-11 09:28:44 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 113 ms / 2,000 ms |
コード長 | 2,128 bytes |
コンパイル時間 | 1,586 ms |
コンパイル使用メモリ | 107,904 KB |
実行使用メモリ | 24,320 KB |
最終ジャッジ日時 | 2024-06-23 01:55:03 |
合計ジャッジ時間 | 3,614 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 16 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
using System.Collections.Generic; using System.Linq; using System; public class UnionFind { private int[] data; public UnionFind(int size) { data = new int[size]; for (int i = 0; i < size; i++) data[i] = -1; } public bool Unite(int x, int y) { x = Root(x); y = Root(y); if (x != y) { if (data[y] < data[x]) { var tmp = y; y = x; x = tmp; } data[x] += data[y]; data[y] = x; } return x != y; } public bool IsSameGroup(int x, int y) { return Root(x) == Root(y); } private int Root(int x) { return data[x] < 0 ? x : data[x] = Root(data[x]); } } public class Hello { public static void Main() { string[] line = Console.ReadLine().Trim().Split(' '); var n = int.Parse(line[0]); var m = int.Parse(line[1]); var a = new List<int>[n]; for (int i = 0; i < n; i++) a[i] = new List<int>(); var uf = new UnionFind(n); line = Console.ReadLine().Trim().Split(' '); var s1 = int.Parse(line[0]); var s2 = int.Parse(line[1]); a[s1].Add(s2); a[s2].Add(s1); uf.Unite(s1, s2); var start = s1; for (int i = 1; i < m; i++) { line = Console.ReadLine().Trim().Split(' '); s1 = int.Parse(line[0]); s2 = int.Parse(line[1]); a[s1].Add(s2); a[s2].Add(s1); uf.Unite(s1, s2); } for (int i = 0; i < n; i++) if (a[i].Count() > 0 && !uf.IsSameGroup(start, i)) { Console.WriteLine("NO"); goto end; } if (!checkE(a)) { Console.WriteLine("NO"); goto end; } Console.WriteLine("YES"); end:; } public static bool checkE(List<int>[] a) { var n = a.Length; var count = 0; for (int i = 0; i < n; i++) if (a[i].Count() % 2 == 1) count++; if (count == 0 | count == 2) return true; else return false; } }