結果

問題 No.583 鉄道同好会
ユーザー eitahoeitaho
提出日時 2017-10-28 16:22:01
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 172 ms / 2,000 ms
コード長 3,652 bytes
コンパイル時間 2,389 ms
コンパイル使用メモリ 111,872 KB
実行使用メモリ 32,892 KB
最終ジャッジ日時 2023-08-14 08:27:08
合計ジャッジ時間 4,982 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
21,528 KB
testcase_01 AC 58 ms
21,572 KB
testcase_02 AC 57 ms
23,548 KB
testcase_03 AC 58 ms
21,684 KB
testcase_04 AC 58 ms
21,456 KB
testcase_05 AC 60 ms
23,532 KB
testcase_06 AC 59 ms
21,536 KB
testcase_07 AC 59 ms
21,432 KB
testcase_08 AC 58 ms
19,560 KB
testcase_09 AC 59 ms
21,492 KB
testcase_10 AC 60 ms
23,596 KB
testcase_11 AC 88 ms
26,412 KB
testcase_12 AC 95 ms
27,012 KB
testcase_13 AC 97 ms
27,104 KB
testcase_14 AC 98 ms
29,152 KB
testcase_15 AC 110 ms
29,680 KB
testcase_16 AC 144 ms
31,692 KB
testcase_17 AC 165 ms
32,772 KB
testcase_18 AC 172 ms
32,892 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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);
        int count = deg.Count(d => d > 0);
        int v = Array.FindIndex(deg, d => d > 0);
        Console.WriteLine(uf.Count(v) == count && 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