結果

問題 No.1254 補強への架け橋
ユーザー kakel-san
提出日時 2025-03-09 00:03:41
言語 C#
(.NET 8.0.404)
結果
WA  
実行時間 -
コード長 2,145 bytes
コンパイル時間 9,803 ms
コンパイル使用メモリ 172,024 KB
実行使用メモリ 223,824 KB
最終ジャッジ日時 2025-03-09 00:04:21
合計ジャッジ時間 32,282 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29 WA * 94
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (99 ミリ秒)。
  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[] 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 n = NN;
        var map = NMap(n);
        var tree = new List<(int id, int to)>[n];
        for (var i = 0; i < n; ++i) tree[i] = new List<(int id, int to)>();
        for (var i = 0; i < n; ++i)
        {
            tree[map[i][0]].Add((i, map[i][1]));
            tree[map[i][1]].Add((i, map[i][0]));
        }
        var threes = -1;
        for (var i = 0; i < n; ++i) if (tree[i].Count > 2)
        {
            threes = i;
            break;
        }
        var ans = new List<int>();
        if (threes < 0)
        {
            for (var i = 0; i < n; ++i) ans.Add(i + 1);
            WriteLine(n);
            WriteLine(string.Join(" ", ans));
            return;
        }
        var visited = new bool[n];
        var isloop = new bool[n];
        DFS(threes, -1, tree, visited, isloop);
        for (var i = 0; i < n; ++i) if (isloop[i]) ans.Add(i + 1);
        WriteLine(ans.Count);
        WriteLine(string.Join(" ", ans));
    }
    static int DFS(int cur, int prev, List<(int id, int to)>[] tree, bool[] visited, bool[] isloop)
    {
        visited[cur] = true;
        var ans = -1;
        foreach (var next in tree[cur])
        {
            if (prev == next.to) continue;
            if (visited[next.to])
            {
                isloop[next.id] = true;
                ans = next.to;
            }
            else
            {
                var bint = DFS(next.to, cur, tree, visited, isloop);
                if (bint >= 0)
                {
                    isloop[next.id] = true;
                    if (bint != cur) ans = bint;
                }
            }
        }
        return ans;
    }
}
0