結果

問題 No.1553 Lovely City
ユーザー 👑 kakel-sankakel-san
提出日時 2024-03-09 20:43:32
言語 C#
(.NET 8.0.203)
結果
WA  
実行時間 -
コード長 2,924 bytes
コンパイル時間 15,673 ms
コンパイル使用メモリ 158,872 KB
実行使用メモリ 235,084 KB
最終ジャッジ日時 2024-03-09 20:44:05
合計ジャッジ時間 22,705 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 80 ms
33,060 KB
testcase_01 AC 82 ms
33,060 KB
testcase_02 AC 138 ms
60,912 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 AC 80 ms
33,316 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 AC 342 ms
76,848 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 253 ms
70,456 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (107 ms)。
MSBuild のバージョン 17.7.3+8ec440e68 (.NET)
  main -> /home/judge/data/code/bin/Release/net7.0/main.dll
  main -> /home/judge/data/code/bin/Release/net7.0/publish/

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;

class Program
{
    static long NN => long.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray();
    static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NMi).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (n, m) = (c[0], c[1]);
        var map = NMap(m);
        var uf = new UnionFindTree(n);
        var tree = new List<int>[n];
        for (var i = 0; i < tree.Length; ++i) tree[i] = new List<int>();
        var refs = new int[n];
        foreach (var edge in map)
        {
            uf.Unite(edge[0], edge[1]);
            tree[edge[0]].Add(edge[1]);
            ++refs[edge[1]];
        }
        var q = new Queue<int>();
        for (var i = 0; i < n; ++i) if (refs[i] == 0) q.Enqueue(i);
        var alist = new List<int>[n];
        for (var i = 0; i < alist.Length; ++i) alist[i] = new List<int>();
        while (q.Count > 0)
        {
            var cur = q.Dequeue();
            alist[uf.GetRoot(cur)].Add(cur);
            foreach (var next in tree[cur])
            {
                --refs[next];
                if (refs[next] == 0) q.Enqueue(next);
            }
        }
        var hasloop = new bool[n];
        for (var i = 0; i < n; ++i) if (refs[i] > 0) hasloop[uf.GetRoot(i)] = true;
        for (var i = 0; i < n; ++i) if (hasloop[i]) alist[i] = new List<int>();
        for (var i = 0; i < n; ++i) if (hasloop[uf.GetRoot(i)]) alist[uf.GetRoot(i)].Add(i);
        var ans = new List<string>();
        for (var i = 0; i < n; ++i) for (var j = 0; j + 1 < alist[i].Count; ++j) ans.Add($"{alist[i][j] + 1} {alist[i][j + 1] + 1}");
        WriteLine(ans.Count);
        WriteLine(string.Join("\n", ans));
    }
    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