結果

問題 No.1390 Get together
ユーザー bluemeganebluemegane
提出日時 2021-04-20 11:03:43
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 349 ms / 2,000 ms
コード長 2,155 bytes
コンパイル時間 901 ms
コンパイル使用メモリ 63,904 KB
実行使用メモリ 47,164 KB
最終ジャッジ日時 2023-09-17 08:37:56
合計ジャッジ時間 8,690 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
21,432 KB
testcase_01 AC 63 ms
21,444 KB
testcase_02 AC 62 ms
19,476 KB
testcase_03 AC 67 ms
21,544 KB
testcase_04 AC 66 ms
21,440 KB
testcase_05 AC 66 ms
21,464 KB
testcase_06 AC 67 ms
23,668 KB
testcase_07 AC 66 ms
23,604 KB
testcase_08 AC 66 ms
23,544 KB
testcase_09 AC 68 ms
23,636 KB
testcase_10 AC 63 ms
23,296 KB
testcase_11 AC 63 ms
21,428 KB
testcase_12 AC 62 ms
21,416 KB
testcase_13 AC 62 ms
21,384 KB
testcase_14 AC 62 ms
23,476 KB
testcase_15 AC 60 ms
21,300 KB
testcase_16 AC 248 ms
28,740 KB
testcase_17 AC 321 ms
45,340 KB
testcase_18 AC 243 ms
26,644 KB
testcase_19 AC 334 ms
44,080 KB
testcase_20 AC 331 ms
45,804 KB
testcase_21 AC 333 ms
47,164 KB
testcase_22 AC 297 ms
41,644 KB
testcase_23 AC 292 ms
43,712 KB
testcase_24 AC 302 ms
42,576 KB
testcase_25 AC 349 ms
43,984 KB
testcase_26 AC 348 ms
44,164 KB
testcase_27 AC 338 ms
45,192 KB
testcase_28 AC 339 ms
42,984 KB
testcase_29 AC 342 ms
43,164 KB
testcase_30 AC 334 ms
44,112 KB
testcase_31 AC 334 ms
42,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

using System.Linq;
using System.Collections.Generic;
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) => Root(x) == Root(y);
    public int Root(int x) => data[x] < 0 ? x : data[x] = Root(data[x]);
    public int getMem(int x) => -data[Root(x)];
}

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 box = new int[m];
        var color = new Dictionary<int, List<int>>();
        var uf = new UnionFind(n + 1);
        for (int i = 1; i <= n; i++)
        {
            line = Console.ReadLine().Trim().Split(' ');
            var b = int.Parse(line[0]) - 1;
            var c = int.Parse(line[1]);
            if (box[b] == 0) box[b] = i;
            else uf.Unite(box[b], i);
            if (color.ContainsKey(c)) color[c].Add(i);
            else
            {
                color[c] = new List<int>();
                color[c].Add(i);
            }
        }
        getAns(color, uf);
    }
    static void getAns(Dictionary<int, List<int>> d, UnionFind uf)
    {
        var count = 0;
        foreach (var x in d)
        {
            var cv = x.Value.Count();
            if (cv > 1)
            {
                var t = x.Value[0];
                for (int i = 1; i < cv; i++)
                {
                    if (uf.Root(t) != uf.Root(x.Value[i]))
                    {
                        uf.Unite(t, x.Value[i]);
                        count++;
                    }
                }
            }
        }
        Console.WriteLine(count);
    }
}
0