結果

問題 No.2319 Friends+
ユーザー 👑 kakel-sankakel-san
提出日時 2023-08-04 08:21:02
言語 C#
(.NET 8.0.203)
結果
WA  
実行時間 -
コード長 5,156 bytes
コンパイル時間 10,443 ms
コンパイル使用メモリ 166,668 KB
実行使用メモリ 242,396 KB
最終ジャッジ日時 2024-04-22 06:11:08
合計ジャッジ時間 33,152 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 55 ms
30,848 KB
testcase_01 AC 55 ms
30,592 KB
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 409 ms
80,384 KB
testcase_05 WA -
testcase_06 AC 405 ms
80,384 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 55 ms
30,720 KB
testcase_18 AC 385 ms
78,544 KB
testcase_19 AC 391 ms
80,364 KB
testcase_20 AC 416 ms
79,788 KB
testcase_21 AC 434 ms
79,736 KB
testcase_22 AC 430 ms
80,176 KB
testcase_23 AC 409 ms
80,156 KB
testcase_24 AC 408 ms
79,912 KB
testcase_25 AC 409 ms
79,780 KB
testcase_26 AC 442 ms
80,124 KB
testcase_27 AC 396 ms
79,856 KB
testcase_28 AC 402 ms
79,724 KB
testcase_29 AC 401 ms
79,824 KB
testcase_30 AC 407 ms
79,764 KB
testcase_31 AC 403 ms
79,620 KB
testcase_32 AC 396 ms
79,760 KB
testcase_33 AC 487 ms
79,692 KB
testcase_34 AC 444 ms
79,548 KB
testcase_35 AC 412 ms
79,812 KB
testcase_36 AC 398 ms
81,544 KB
testcase_37 AC 412 ms
80,768 KB
testcase_38 AC 411 ms
80,576 KB
testcase_39 AC 415 ms
80,696 KB
testcase_40 AC 394 ms
80,504 KB
testcase_41 AC 392 ms
80,624 KB
testcase_42 AC 405 ms
80,504 KB
testcase_43 AC 402 ms
80,496 KB
testcase_44 AC 414 ms
80,608 KB
testcase_45 AC 411 ms
80,500 KB
testcase_46 AC 397 ms
242,396 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (82 ms)。
MSBuild のバージョン 17.9.6+a4ecab324 (.NET)
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

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();
        // Test();
    }
    static void Test()
    {
        var r = new Random();
        var count = 0;
        while (true)
        {
            var n = 100;
            var q = 500;
            var p = new int[n];
            for (var i = 0; i < n; ++i) p[i] = r.Next(n) + 1;
            var (m, map) = Create(n, r);
            var query = new int[q][];
            for (var i = 0; i < q; ++i) query[i] = new int[] { r.Next(n) + 1, r.Next(n) + 1 };
            // 愚直解
            var s1 = All(n, m, p, map, q, query);
            // 作成解
            var s2 = FriendsPlus(n, m, p, map, q, query);
            if (s1 != s2)
            {
                WriteLine($"{n} {m}");
                WriteLine(string.Join(" ", p));
                WriteLine(string.Join("\n", map.Select(mi => string.Join(" ", mi))));
                WriteLine(q);
                WriteLine(string.Join("\n", query.Select(qi => string.Join(" ", qi))));
                WriteLine(s1);
                WriteLine(s2);
                return;
            }
            ++count;
            if (count % 1000 == 0) WriteLine(count);
        }
    }
    static (int m, int[][] map) Create(int n, Random r)
    {
        var list = new List<int[]>();
        for (var i = 0; i < n; ++i) for (var j = i + 1; j < n; ++j) if (r.Next(3) == 0)
        {
            list.Add(new int[] { i + 1, j + 1 });
        }
        return (list.Count, list.ToArray());
    }
    static string All(int n, int m, int[] p, int[][] map, int q, int[][] query)
    {
        var fr = new UnionFindTree(n);
        foreach (var edge in map) fr.Unite(edge[0] - 1, edge[1] - 1);
        var pos = (int[]) p.Clone();
        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 flg = false;
            for (var f = 0; f < n; ++f) if (fr.IsSameTree(f, x))
            {
                if (pos[f] == pos[y]) flg = true;
            }
            if (flg && pos[x] != pos[y])
            {
                pos[x] = pos[y];
                ans[i] = true;
            }
        }
        return string.Join("\n", ans.Select(f => f ? "Yes" : "No"));
    }
    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);
        WriteLine(FriendsPlus(n, m, p, map, q, query));
    }
    static string FriendsPlus(int n, int m, int[] p, int[][] map, int q, int[][] query)
    {
        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 gid = fr.GetRoot(x);
            var xgroup = gw[gid];
            if (pos[x] != pos[y] && xgroup.ContainsKey(pos[y]))
            {
                ans[i] = true;
                --xgroup[pos[x]];
                if (xgroup[pos[x]] == 0) xgroup.Remove(pos[x]);
                ++xgroup[pos[y]];
                pos[x] = pos[y];
            }
        }
        return 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