結果

問題 No.1640 簡単な色塗り
ユーザー kakel-san
提出日時 2021-08-06 23:01:03
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 241 ms / 2,000 ms
コード長 2,947 bytes
コンパイル時間 2,327 ms
コンパイル使用メモリ 114,308 KB
実行使用メモリ 44,280 KB
最終ジャッジ日時 2024-06-29 16:08:54
合計ジャッジ時間 17,953 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 53
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

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

class yuki308
{
    static int NN => int.Parse(ReadLine());
    static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
    static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NList).ToArray();
    static void Main()
    {
        var n = NN;
        var map = NMap(n);
        for (var i = 0; i < n; ++i)
        {
            --map[i][0];
            --map[i][1];
            Array.Sort(map[i]);
        }

        var p = new Pair[n];
        for (var i = 0; i < p.Length; ++i) p[i] = new Pair(i);
        for (var i = 0; i < map.Length; ++i)
        {
            p[map[i][0]].Edges.Add(i);
            p[map[i][1]].Edges.Add(i);
        }

        var used = new bool[n];
        var res = new int[n];
        for (var i = 0; i < p.Length; ++i)
        {
            var cur = i;
            while (p[cur].Edges.Count == 1)
            {
                var paint = p[cur].Edges[0];
                res[paint] = cur + 1;
                used[cur] = true;
                p[map[paint][0]].Edges.Remove(paint);
                p[map[paint][1]].Edges.Remove(paint);
                cur = cur ^ (map[paint][0]) ^ (map[paint][1]);
            }
        }
        for (var i = 0; i < p.Length; ++i)
        {
            var cur = i;
            while (p[cur].Edges.Count > 0)
            {
                var paint = p[cur].Edges[0];
                res[paint] = cur + 1;
                used[cur] = true;
                p[map[paint][0]].Edges.Remove(paint);
                p[map[paint][1]].Edges.Remove(paint);
                cur = cur ^ (map[paint][0]) ^ (map[paint][1]);
            }
        }
        if (used.Count(f => !f) == 0)
        {
            WriteLine("Yes");
            WriteLine(string.Join("\n", res));
        }
        else WriteLine("No");
    }
    class Pair
    {
        public List<int> Edges = new List<int>();
        public int Num;
        public Pair(int num)
        {
            Num = num;
        }
    }
    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