結果

問題 No.583 鉄道同好会
ユーザー eitahoeitaho
提出日時 2017-10-28 16:14:06
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 3,555 bytes
コンパイル時間 2,219 ms
コンパイル使用メモリ 109,772 KB
実行使用メモリ 35,044 KB
最終ジャッジ日時 2023-08-14 08:26:21
合計ジャッジ時間 4,690 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 51 ms
21,548 KB
testcase_01 AC 51 ms
23,540 KB
testcase_02 AC 51 ms
21,612 KB
testcase_03 AC 50 ms
19,456 KB
testcase_04 AC 53 ms
21,456 KB
testcase_05 AC 54 ms
21,492 KB
testcase_06 WA -
testcase_07 AC 50 ms
21,508 KB
testcase_08 AC 53 ms
23,560 KB
testcase_09 AC 53 ms
21,396 KB
testcase_10 AC 55 ms
23,528 KB
testcase_11 AC 79 ms
28,472 KB
testcase_12 AC 87 ms
29,088 KB
testcase_13 AC 83 ms
29,028 KB
testcase_14 AC 83 ms
27,016 KB
testcase_15 AC 93 ms
27,628 KB
testcase_16 AC 129 ms
31,580 KB
testcase_17 AC 149 ms
32,804 KB
testcase_18 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Collections.Generic;
using System.Diagnostics;
using Enu = System.Linq.Enumerable;

public class Program
{
    public void Solve()
    {
        int N = Reader.Int(), M = Reader.Int();
        var E = Reader.IntTable(M);
        var uf = new UnionFind(N);
        var deg = new int[N];
        foreach (var e in E) { deg[e[0]]++; deg[e[1]]++; uf.Unite(e[0], e[1]); }
        int odd = deg.Count(d => d % 2 == 1);
        Console.WriteLine(uf.Count(0) == N && odd == 0 || odd == 2 ? "YES" : "NO");
    }
}

class UnionFind
{
    private int[] parent;
    private byte[] rank;

    public UnionFind(int N)
    {
        parent = new int[N];
        rank = new byte[N];
        for (int i = 0; i < N; i++) parent[i] = -1;
    }
    public int Root(int x)
    {
        if (parent[x] < 0) return x;
        return parent[x] = Root(parent[x]);
    }
    public bool Same(int x, int y)
    {
        return Root(x) == Root(y);
    }
    public int Count(int x)
    {
        return -parent[Root(x)];
    }
    public bool Unite(int x, int y)
    {
        x = Root(x); y = Root(y);
        if (x == y) return false;
        if (rank[x] > rank[y]) { var t = x; x = y; y = t; }
        if (rank[x] == rank[y]) rank[y]++;
        parent[y] += parent[x];
        parent[x] = y;
        return true;
    }
    public Dictionary<int, List<int>> Components()
    {
        var dic = new Dictionary<int, List<int>>();
        for (int i = 0; i < parent.Length; i++)
        {
            int root = Root(i);
            if (!dic.ContainsKey(root)) dic[root] = new List<int>();
            dic[root].Add(i);
        }
        return dic;
    }
}



class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    static TextReader reader = Console.In;
    static readonly char[] separator = { ' ' };
    static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    static string[] A = new string[0];
    static int i;
    static void Init() { Dispose(); A = new string[0]; }
    public static void Set(TextReader r) { Init(); reader = r; }
    public static void Set(string file) { Init(); reader = new StreamReader(file); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Range(N, Int); }
    public static int[][] IntTable(int H) { return Range(H, IntLine); }
    public static string[] StringArray(int N) { return Range(N, Next); }
    public static string[][] StringTable(int N) { return Range(N, () => Split(Line())); }
    public static string Line() { return reader.ReadLine().Trim(); }
    public static T[] Range<T>(int N, Func<T> f) { var r = new T[N]; for (int i = 0; i < N; r[i++] = f()) ; return r; }
    public static void Dispose() { reader.Dispose(); }
    static string[] Split(string s) { return s.Split(separator, op); }
    static string Next() { CheckNext(); return A[i++]; }
    static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}
0