結果

問題 No.2319 Friends+
ユーザー 👑 kakel-sankakel-san
提出日時 2023-08-03 23:35:07
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 2,969 bytes
コンパイル時間 1,374 ms
コンパイル使用メモリ 115,676 KB
実行使用メモリ 77,860 KB
最終ジャッジ日時 2024-04-21 22:16:08
合計ジャッジ時間 31,062 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 35 ms
19,328 KB
testcase_01 AC 34 ms
19,328 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 581 ms
67,560 KB
testcase_05 WA -
testcase_06 AC 591 ms
67,572 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 34 ms
19,328 KB
testcase_18 AC 571 ms
66,916 KB
testcase_19 AC 595 ms
69,224 KB
testcase_20 AC 596 ms
68,064 KB
testcase_21 AC 623 ms
67,568 KB
testcase_22 AC 595 ms
67,680 KB
testcase_23 AC 618 ms
68,320 KB
testcase_24 AC 586 ms
68,460 KB
testcase_25 AC 599 ms
67,692 KB
testcase_26 AC 614 ms
67,940 KB
testcase_27 AC 601 ms
67,684 KB
testcase_28 AC 598 ms
67,828 KB
testcase_29 AC 613 ms
67,948 KB
testcase_30 AC 604 ms
67,824 KB
testcase_31 AC 594 ms
68,080 KB
testcase_32 AC 596 ms
68,080 KB
testcase_33 AC 597 ms
67,816 KB
testcase_34 AC 592 ms
67,940 KB
testcase_35 AC 600 ms
67,944 KB
testcase_36 AC 598 ms
69,104 KB
testcase_37 AC 572 ms
67,796 KB
testcase_38 AC 600 ms
69,100 KB
testcase_39 AC 604 ms
69,088 KB
testcase_40 AC 612 ms
69,352 KB
testcase_41 AC 598 ms
69,352 KB
testcase_42 AC 602 ms
69,220 KB
testcase_43 AC 616 ms
69,220 KB
testcase_44 AC 602 ms
69,488 KB
testcase_45 AC 598 ms
69,468 KB
testcase_46 AC 590 ms
67,940 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 static System.Console;
using System.Linq;
using System.Collections.Generic;
class Program
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (n, m) = (c[0], c[1]);
        var p = NList;
        var map = NArr(m);
        var q = NN;
        var query = NArr(q);

        var fr = new UnionFindTree(n + q);
        foreach (var edge in map) fr.Unite(edge[0] - 1, edge[1] - 1);

        var gw = new Dictionary<int, int>[n + q];
        for (var i = 0; i < gw.Length; ++i) gw[i] = new Dictionary<int, int>();

        var pos = Enumerable.Repeat(-1, n + q).ToArray();
        for (var i = 0; i < n; ++i)
        {
            pos[i] = p[i];
            if (gw[fr.GetRoot(i)].ContainsKey(p[i])) ++gw[fr.GetRoot(i)][p[i]];
            else gw[fr.GetRoot(i)][p[i]] = 1;
        }

        var id = new int[n];
        for (var i = 0; i < n; ++i) id[i] = i;

        var ans = new bool[q];
        for (var i = 0; i < q; ++i)
        {
            var x = query[i][0] - 1;
            var y = query[i][1] - 1;
            var xpos = pos[id[x]];
            var ypos = pos[id[y]];
            var gid = fr.GetRoot(id[x]);
            var xgroup = gw[gid];
            if (xpos != ypos && xgroup.ContainsKey(ypos))
            {
                ans[i] = true;
                --xgroup[xpos];
                if (xgroup[xpos] == 0) xgroup.Remove(xpos);
                fr.Unite(id[x], n + i);
                id[x] = n + i;
                ++xgroup[ypos];
                pos[id[x]] = ypos;
                var newgid = fr.GetRoot(id[x]);
                if (newgid != gid) gw[newgid] = xgroup;
            }
        }
        WriteLine(string.Join("\n", ans.Select(f => f ? "Yes" : "No")));
    }
    class UnionFindTree
    {
        int[] roots;
        public UnionFindTree(int size)
        {
            roots = new int[size];
            for (var i = 0; i < size; ++i) roots[i] = -1;
        }
        public int GetRoot(int a)
        {
            if (roots[a] < 0) return a;
            return roots[a] = GetRoot(roots[a]);
        }
        public bool IsSameTree(int a, int b)
        {
            return GetRoot(a) == GetRoot(b);
        }
        public bool Unite(int a, int b)
        {
            var x = GetRoot(a);
            var y = GetRoot(b);
            if (x == y) return false;
            if (-roots[x] < -roots[y]) { var tmp = x; x = y; y = tmp; }
            roots[x] += roots[y];
            roots[y] = x;
            return true;
        }
        public int GetSize(int a)
        {
            return -roots[GetRoot(a)];
        }
    }
}
0