結果

問題 No.1390 Get together
ユーザー bluemeganebluemegane
提出日時 2021-04-20 11:01:34
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 2,146 bytes
コンパイル時間 2,051 ms
コンパイル使用メモリ 67,532 KB
実行使用メモリ 46,080 KB
最終ジャッジ日時 2023-09-17 08:37:38
合計ジャッジ時間 9,947 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 61 ms
21,472 KB
testcase_01 AC 61 ms
21,436 KB
testcase_02 AC 62 ms
21,420 KB
testcase_03 AC 67 ms
23,584 KB
testcase_04 AC 66 ms
19,512 KB
testcase_05 WA -
testcase_06 AC 66 ms
21,608 KB
testcase_07 WA -
testcase_08 AC 66 ms
23,604 KB
testcase_09 AC 68 ms
21,432 KB
testcase_10 AC 63 ms
21,388 KB
testcase_11 WA -
testcase_12 AC 62 ms
21,256 KB
testcase_13 WA -
testcase_14 AC 63 ms
23,500 KB
testcase_15 AC 61 ms
21,392 KB
testcase_16 WA -
testcase_17 WA -
testcase_18 AC 244 ms
28,640 KB
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 AC 344 ms
42,148 KB
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
権限があれば一括ダウンロードができます

ソースコード

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) != x.Value[i])
                    {
                        uf.Unite(t, x.Value[i]);
                        count++;
                    }
                }
            }
        }
        Console.WriteLine(count);
    }
}
0