Submission #40870875


Source Code Expand

using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;

namespace Tasks
{
    public class E
    {
        public static void Main()
        {
            using var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
            Console.SetOut(sw);
            Solve();
            Console.Out.Flush();
        }

        public static void Solve()
        {
            var (N, M) = Scanner.Scan<int, int>();

            var G = new List<int>[N].Select(x => new List<int>()).ToArray();
            for (var i = 0; i < M; i++)
            {
                var (u, v) = Scanner.Scan<int, int>();
                u--; v--;
                G[u].Add(v);
                G[v].Add(u);
            }

            var K = Scanner.Scan<int>();
            if (K == 0)
            {
                Console.WriteLine("Yes");
                Console.WriteLine(new string('1', N));
                return;
            }

            var D = new int[N][];
            var queue = new Queue<int>();
            for (var i = 0; i < N; i++)
            {
                var dist = new int[N];
                Array.Fill(dist, -1);
                queue.Enqueue(i);
                dist[i] = 0;
                while (queue.Count > 0)
                {
                    var u = queue.Dequeue();
                    foreach (var v in G[u])
                    {
                        if (dist[v] != -1) continue;
                        dist[v] = dist[u] + 1;
                        queue.Enqueue(v);
                    }
                }

                D[i] = dist;
            }

            var minD = new int[N];
            const int Inf = (int)1e9;
            Array.Fill(minD, Inf);
            var PD = new (int P, int D)[K];
            for (var i = 0; i < K; i++)
            {
                var (p, d) = Scanner.Scan<int, int>();
                p--;
                PD[i] = (p, d);
                minD[p] = d;
            }

            var DD = new Dictionary<int, List<int>>[N];
            for (var i = 0; i < N; i++)
            {
                DD[i] = new Dictionary<int, List<int>>();
                for (var j = 0; j < N; j++)
                {
                    var d = D[i][j];
                    if (!DD[i].ContainsKey(d)) DD[i][d] = new List<int>();
                    DD[i][d].Add(j);
                }
            }

            var C = new int[N];
            // foreach (var (p, d) in PD)
            // {
            //     if (!DD[p].ContainsKey(d))
            //     {
            //         Console.WriteLine("No");
            //         return;
            //     }

            //     foreach (var v in DD[p][d])
            //     {
            //         C[v] = 1;
            //     }
            // }

            Array.Fill(C, 1);
            for (var i = 0; i < N; i++)
            {
                if (minD[i] == Inf) continue;
                for (var j = 0; j < N; j++)
                {
                    if (minD[i] > D[i][j])
                    {
                        C[j] = 0;
                    }
                }
            }

            // for (var i = 0; i < N; i++)
            // {
            //     if (minD[i] == Inf) continue;
            //     for (var j = 0; j < N; j++)
            //     {
            //         if (C[j] == 1 && D[i][j] != -1 && minD[i] > D[i][j])
            //         {
            //             C[j] = 0;
            //         }
            //     }
            // }

            for (var i = 0; i < N; i++)
            {
                if (minD[i] == Inf) continue;
                var ok = false;
                for (var j = 0; j < N; j++)
                {
                    if (C[j] == 1 && minD[i] == D[i][j])
                    {
                        ok = true;
                    }
                }

                if (!ok)
                {
                    Console.WriteLine("No");
                    return;
                }
            }

            if (!C.Contains(1))
            {
                Console.WriteLine("No");
                return;
            }

            Console.WriteLine("Yes");
            Console.WriteLine(string.Join("", C));
        }

        public static class Scanner
        {
            public static T Scan<T>() where T : IConvertible => Convert<T>(Scan()[0]);
            public static (T1, T2) Scan<T1, T2>() where T1 : IConvertible where T2 : IConvertible
            {
                var buffer = Scan();
                return (Convert<T1>(buffer[0]), Convert<T2>(buffer[1]));
            }
            public static (T1, T2, T3) Scan<T1, T2, T3>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible
            {
                var buffer = Scan();
                return (Convert<T1>(buffer[0]), Convert<T2>(buffer[1]), Convert<T3>(buffer[2]));
            }
            public static (T1, T2, T3, T4) Scan<T1, T2, T3, T4>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible
            {
                var buffer = Scan();
                return (Convert<T1>(buffer[0]), Convert<T2>(buffer[1]), Convert<T3>(buffer[2]), Convert<T4>(buffer[3]));
            }
            public static (T1, T2, T3, T4, T5) Scan<T1, T2, T3, T4, T5>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible where T5 : IConvertible
            {
                var buffer = Scan();
                return (Convert<T1>(buffer[0]), Convert<T2>(buffer[1]), Convert<T3>(buffer[2]), Convert<T4>(buffer[3]), Convert<T5>(buffer[4]));
            }
            public static (T1, T2, T3, T4, T5, T6) Scan<T1, T2, T3, T4, T5, T6>() where T1 : IConvertible where T2 : IConvertible where T3 : IConvertible where T4 : IConvertible where T5 : IConvertible where T6 : IConvertible
            {
                var buffer = Scan();
                return (Convert<T1>(buffer[0]), Convert<T2>(buffer[1]), Convert<T3>(buffer[2]), Convert<T4>(buffer[3]), Convert<T5>(buffer[4]), Convert<T6>(buffer[5]));
            }
            public static IEnumerable<T> ScanEnumerable<T>() where T : IConvertible => Scan().Select(Convert<T>);
            private static string[] Scan()
            {
                var line = Console.ReadLine()?.Trim() ?? string.Empty;
                return string.IsNullOrEmpty(line) ? Array.Empty<string>() : line.Split(' ');
            }
            private static T Convert<T>(string value) where T : IConvertible => (T)System.Convert.ChangeType(value, typeof(T));
        }
    }
}

Submission Info

Submission Time
Task E - Nearest Black Vertex
User AconCavy
Language C# (.NET Core 3.1.201)
Score 500
Code Size 6857 Byte
Status AC
Exec Time 1266 ms
Memory 387488 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 3
AC × 47
Set Name Test Cases
Sample example0.txt, example1.txt, example2.txt
All 000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, 043.txt, example0.txt, example1.txt, example2.txt
Case Name Status Exec Time Memory
000.txt AC 81 ms 28184 KiB
001.txt AC 82 ms 28348 KiB
002.txt AC 85 ms 28444 KiB
003.txt AC 78 ms 28764 KiB
004.txt AC 348 ms 78372 KiB
005.txt AC 1266 ms 386560 KiB
006.txt AC 1205 ms 387488 KiB
007.txt AC 928 ms 326960 KiB
008.txt AC 926 ms 326772 KiB
009.txt AC 85 ms 28848 KiB
010.txt AC 157 ms 40188 KiB
011.txt AC 123 ms 34992 KiB
012.txt AC 160 ms 41160 KiB
013.txt AC 136 ms 36416 KiB
014.txt AC 115 ms 34052 KiB
015.txt AC 165 ms 42900 KiB
016.txt AC 85 ms 29060 KiB
017.txt AC 117 ms 33356 KiB
018.txt AC 137 ms 37312 KiB
019.txt AC 87 ms 29784 KiB
020.txt AC 111 ms 32932 KiB
021.txt AC 97 ms 30692 KiB
022.txt AC 132 ms 36868 KiB
023.txt AC 173 ms 44296 KiB
024.txt AC 83 ms 29204 KiB
025.txt AC 111 ms 32152 KiB
026.txt AC 124 ms 35608 KiB
027.txt AC 168 ms 40256 KiB
028.txt AC 164 ms 42796 KiB
029.txt AC 162 ms 40200 KiB
030.txt AC 111 ms 32692 KiB
031.txt AC 105 ms 31276 KiB
032.txt AC 115 ms 33200 KiB
033.txt AC 133 ms 37368 KiB
034.txt AC 90 ms 29960 KiB
035.txt AC 114 ms 32656 KiB
036.txt AC 87 ms 29880 KiB
037.txt AC 110 ms 31392 KiB
038.txt AC 99 ms 30464 KiB
039.txt AC 83 ms 28852 KiB
040.txt AC 98 ms 28940 KiB
041.txt AC 91 ms 29608 KiB
042.txt AC 133 ms 37312 KiB
043.txt AC 166 ms 42252 KiB
example0.txt AC 86 ms 28296 KiB
example1.txt AC 89 ms 27920 KiB
example2.txt AC 80 ms 28092 KiB