結果

問題 No.1865 Make Cycle
ユーザー 👑 kakel-sankakel-san
提出日時 2022-03-04 23:33:46
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 659 ms / 3,000 ms
コード長 1,896 bytes
コンパイル時間 1,119 ms
コンパイル使用メモリ 68,188 KB
実行使用メモリ 49,556 KB
最終ジャッジ日時 2023-09-26 02:40:02
合計ジャッジ時間 10,806 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 388 ms
40,336 KB
testcase_01 AC 306 ms
39,668 KB
testcase_02 AC 468 ms
48,408 KB
testcase_03 AC 336 ms
47,708 KB
testcase_04 AC 466 ms
46,272 KB
testcase_05 AC 155 ms
37,100 KB
testcase_06 AC 376 ms
45,432 KB
testcase_07 AC 426 ms
46,716 KB
testcase_08 AC 525 ms
45,852 KB
testcase_09 AC 153 ms
35,740 KB
testcase_10 AC 474 ms
48,856 KB
testcase_11 AC 428 ms
47,972 KB
testcase_12 AC 415 ms
47,540 KB
testcase_13 AC 394 ms
43,912 KB
testcase_14 AC 330 ms
44,428 KB
testcase_15 AC 455 ms
44,772 KB
testcase_16 AC 472 ms
46,900 KB
testcase_17 AC 150 ms
29,816 KB
testcase_18 AC 157 ms
31,528 KB
testcase_19 AC 659 ms
49,556 KB
testcase_20 AC 65 ms
21,876 KB
testcase_21 AC 67 ms
21,956 KB
testcase_22 AC 67 ms
21,936 KB
testcase_23 AC 67 ms
19,884 KB
権限があれば一括ダウンロードができます

ソースコード

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[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray();
    static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NMi).ToArray();
    static void Main()
    {
        var c = NList;
        var (n, q) = (c[0], c[1]);
        var map = NMap(q);

        var tree = new List<(int id, int to)>[n];
        for (var i = 0; i < tree.Length; ++i) tree[i] = new List<(int id, int to)>();
        for (var i = 0; i < map.Length; ++i)
        {
            tree[map[i][0]].Add((i, map[i][1]));
        }
        if (!HasLoop(tree, q))
        {
            WriteLine(-1);
            return;
        }
        var ok = q;
        var ng = 0;
        while (ok - ng > 1)
        {
            var center = (ng + ok) / 2;
            if (HasLoop(tree, center)) ok = center;
            else ng = center;
        }
        WriteLine(ok);
    }
    static bool HasLoop(List<(int id, int to)>[] tree, int time)
    {
        var refcount = new int[tree.Length];
        foreach (var from in tree)
        {
            foreach (var edge in from)
            {
                if (edge.id < time) ++refcount[edge.to];
            }
        }
        var q = new Queue<int>();
        for (var i = 0; i < refcount.Length; ++i) if (refcount[i] == 0) q.Enqueue(i);
        while (q.Count > 0)
        {
            var cur = q.Dequeue();
            foreach (var edge in tree[cur])
            {
                if (edge.id < time)
                {
                    --refcount[edge.to];
                    if (refcount[edge.to] == 0) q.Enqueue(edge.to);
                }
            }
        }
        return refcount.Max() > 0;
    }
}
0