結果
| 問題 | 
                            No.2301 Namorientation
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2023-07-27 09:35:12 | 
| 言語 | C#(csc)  (csc 3.9.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 527 ms / 3,000 ms | 
| コード長 | 2,949 bytes | 
| コンパイル時間 | 3,292 ms | 
| コンパイル使用メモリ | 115,268 KB | 
| 実行使用メモリ | 124,668 KB | 
| 最終ジャッジ日時 | 2024-10-04 02:11:56 | 
| 合計ジャッジ時間 | 18,539 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 2 | 
| other | AC * 30 | 
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var n = NN;
        var map = NMap(n);
        var tree = new List<(int to, int id)>[n];
        for (var i = 0; i < tree.Length; ++i) tree[i] = new List<(int to, int id)>();
        for (var i = 0; i < map.Length; ++i)
        {
            tree[map[i][0]].Add((map[i][1], i));
            tree[map[i][1]].Add((map[i][0], i));
        }
        var loop = new List<int>();
        var visited = new bool[n];
        for (var i = 0; i < n; ++i)
        {
            loop = new List<int>();
            if (!visited[i])
            {
                if (DFS(-1, i, tree, visited, loop)) break;
            }
        }
        visited = new bool[n];
        var ans = new bool[n];
        var q = new Queue<int>();
        for (var i = 0; i < loop.Count; ++i)
        {
            var f = loop[i];
            var t = loop[(i + 1) % loop.Count];
            foreach (var edge in tree[f])
            {
                if (edge.to == t)
                {
                    if (map[edge.id][0] == t) ans[edge.id] = true;
                }
            }
            q.Enqueue(f);
            visited[f] = true;
        }
        while (q.Count > 0)
        {
            var cur = q.Dequeue();
            foreach (var edge in tree[cur])
            {
                if (!visited[edge.to])
                {
                    if (map[edge.id][0] == edge.to) ans[edge.id] = true;
                    q.Enqueue(edge.to);
                    visited[edge.to] = true;
                }
            }
        }
        WriteLine(string.Join("\n", ans.Select(f => f ? "->" : "<-")));
    }
    static bool DFS(int prev, int cur, List<(int to, int id)>[] tree, bool[] visited, List<int> loop)
    {
        if (visited[cur])
        {
            var nl = new List<int>();
            for (var i = loop.Count - 1; i >= 0; --i)
            {
                nl.Add(loop[i]);
                if (loop[i] == cur) break;
            }
            loop.Clear();
            foreach (var li in nl) loop.Add(li);
            return true;
        }
        visited[cur] = true;
        loop.Add(cur);
        foreach (var next in tree[cur])
        {
            if (prev == next.to) continue;
            if (DFS(cur, next.to, tree, visited, loop)) return true;
        }
        loop.RemoveAt(loop.Count - 1);
        visited[cur] = false;
        return false;
    }
}