結果

問題 No.483 マッチ並べ
コンテスト
ユーザー 👑 kakel-san
提出日時 2026-11-19 00:10:35
言語 C#
(.NET 10.0.403 + ACL)
コンパイル:
dotnet_c
実行:
/usr/bin/dotnet_wrap
結果
AC  
実行時間 43 ms / 2,000 ms
+ 205µs
コード長 2,233 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,513 ms
コンパイル使用メモリ 168,940 KB
実行使用メモリ 32,768 KB
最終ジャッジ日時 2026-11-19 00:10:48
合計ジャッジ時間 11,345 ms
ジャッジサーバーID
(参考情報)
judge5_0 / judge4_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 59
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (178 ミリ秒)。
  main -> /home/judge/data/code/bin/Release/net10.0/main.dll
  main -> /home/judge/data/code/bin/Release/net10.0/publish/

ソースコード

diff #
raw source code

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 n = NN;
        var map = NArr(n);
        var dic = new Dictionary<int, int>();
        var count = 0;
        foreach (var edge in map)
        {
            var key1 = GetKey(edge[0], edge[1]);
            if (!dic.ContainsKey(key1)) dic[key1] = count++;
            var key2 = GetKey(edge[2], edge[3]);
            if (!dic.ContainsKey(key2)) dic[key2] = count++;
        }
        var tree = new List<(int id, int to)>[count];
        for (var i = 0; i < count; ++i) tree[i] = new List<(int id, int to)>();
        for (var i = 0; i < n; ++i)
        {
            var edge = map[i];
            var key1 = GetKey(edge[0], edge[1]);
            var key2 = GetKey(edge[2], edge[3]);
            tree[dic[key1]].Add((i, dic[key2]));
            tree[dic[key2]].Add((i, dic[key1]));
        }
        var visited = new bool[count];
        var used = new bool[n];
        for (var i = 0; i < count; ++i) if (!visited[i])
        {
            var loopcount = 0;
            OrderGraph(i, -1, tree, visited, used, ref loopcount);
            if (loopcount > 1)
            {
                WriteLine("NO");
                return;
            }
        }
        WriteLine("YES");
    }
    static void OrderGraph(int cur, int prev, List<(int id, int to)>[] tree, bool[] visited, bool[] used, ref int loopcount)
    {
        visited[cur] = true;
        foreach (var next in tree[cur])
        {
            if (prev == next.to) continue;
            if (used[next.id]) continue;
            used[next.id] = true;
            if (!visited[next.to]) OrderGraph(next.to, cur, tree, visited, used, ref loopcount);
            else ++loopcount;
        }
    }
    static int GetKey(int x, int y)
    {
        return x * 1000 + y;
    }
}
0