結果

問題 No.317 辺の追加
ユーザー eitahoeitaho
提出日時 2015-12-10 22:12:46
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 348 ms / 2,000 ms
コード長 4,333 bytes
コンパイル時間 4,847 ms
コンパイル使用メモリ 111,564 KB
実行使用メモリ 47,072 KB
最終ジャッジ日時 2023-10-13 10:40:00
合計ジャッジ時間 14,968 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
21,696 KB
testcase_01 AC 68 ms
23,704 KB
testcase_02 AC 192 ms
32,472 KB
testcase_03 AC 203 ms
28,124 KB
testcase_04 AC 221 ms
36,284 KB
testcase_05 AC 205 ms
27,420 KB
testcase_06 AC 235 ms
28,716 KB
testcase_07 AC 123 ms
30,448 KB
testcase_08 AC 299 ms
36,952 KB
testcase_09 AC 225 ms
36,180 KB
testcase_10 AC 262 ms
30,224 KB
testcase_11 AC 174 ms
47,072 KB
testcase_12 AC 262 ms
32,172 KB
testcase_13 AC 200 ms
44,268 KB
testcase_14 AC 238 ms
33,132 KB
testcase_15 AC 254 ms
32,264 KB
testcase_16 AC 229 ms
34,672 KB
testcase_17 AC 243 ms
34,528 KB
testcase_18 AC 261 ms
32,220 KB
testcase_19 AC 269 ms
32,252 KB
testcase_20 AC 250 ms
35,624 KB
testcase_21 AC 124 ms
27,628 KB
testcase_22 AC 99 ms
28,692 KB
testcase_23 AC 98 ms
26,536 KB
testcase_24 AC 164 ms
31,436 KB
testcase_25 AC 136 ms
28,072 KB
testcase_26 AC 142 ms
30,412 KB
testcase_27 AC 126 ms
29,844 KB
testcase_28 AC 99 ms
26,584 KB
testcase_29 AC 77 ms
23,680 KB
testcase_30 AC 347 ms
29,020 KB
testcase_31 AC 323 ms
30,972 KB
testcase_32 AC 322 ms
28,996 KB
testcase_33 AC 324 ms
31,040 KB
testcase_34 AC 321 ms
27,084 KB
testcase_35 AC 327 ms
28,928 KB
testcase_36 AC 327 ms
28,988 KB
testcase_37 AC 327 ms
30,992 KB
testcase_38 AC 348 ms
31,048 KB
testcase_39 AC 325 ms
28,968 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 System.Numerics;
using Enu = System.Linq.Enumerable;

class Program
{
    static readonly int INF = (int)1e9;

    public void Solve()
    {
        int N = Reader.Int(), M = Reader.Int();
        var uf = new UnionFind(N);
        for (int i = 0; i < M; i++)
            uf.Unite(Reader.Int() - 1, Reader.Int() - 1);
        int[] num = new int[N + 1];
        foreach (var c in uf.Components())
            num[c.Count]++;
        var dp = new int[N + 1];
        for (int i = 1; i <= N; i++) dp[i] = INF;

        for (int size = 1; size <= N; size++)
        {
            int rem = num[size];
            for (int bit = 1; rem > 0; bit <<= 1)
            {
                int add = Math.Min(rem, bit);
                for (int x = N; x >= size * add; x--)
                    dp[x] = Math.Min(dp[x], dp[x - size * add] + add);
                rem -= add;
            }
        }

        Console.WriteLine(string.Join("\n", dp.Skip(1).Select(v => v == INF ? -1 : v - 1)));
    }


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

        public UnionFind(int N)
        {
            parent = new int[N];
            rank = new int[N];
            numElements = new int[N];
            for (int i = 0; i < N; i++)
            {
                parent[i] = i;
                numElements[i] = 1;
            }
        }
        public int Root(int x)
        {
            if (parent[x] == x) return x;
            return parent[x] = Root(parent[x]);
        }
        public bool Same(int x, int y)
        {
            return Root(x) == Root(y);
        }
        public int NumElements(int x)
        {
            return numElements[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[x] = y;
            numElements[y] += numElements[x];
            return true;
        }
        public 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.Values.ToArray();
        }
    }

}


class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    private static TextReader reader = Console.In;
    private static readonly char[] separator = { ' ' };
    private static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    private static string[] A = new string[0];
    private static int i;
    private static void Init() { A = new string[0]; }
    public static void Set(TextReader r) { reader = r; Init(); }
    public static void Set(string file) { reader = new StreamReader(file); Init(); }
    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 Enu.Range(0, N).Select(i => Int()).ToArray(); }
    public static int[][] IntTable(int H) { return Enu.Range(0, H).Select(i => IntLine()).ToArray(); }
    public static string[] StringArray(int N) { return Enu.Range(0, N).Select(i => Next()).ToArray(); }
    public static string Line() { return reader.ReadLine().Trim(); }
    private static string[] Split(string s) { return s.Split(separator, op); }
    private static string Next() { CheckNext(); return A[i++]; }
    private 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