Submission #34578204


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()
        {
            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 A = Scanner.ScanEnumerable<long>().ToArray();
            var G = new List<int>[N].Select(x => new List<int>()).ToArray();
            var costs = new long[N];
            for (var i = 0; i < M; i++)
            {
                var (u, v) = Scanner.Scan<int, int>();
                u--; v--;
                costs[u] += A[v];
                costs[v] += A[u];
                G[u].Add(v);
                G[v].Add(u);
            }

            var answer = 0L;
            var queue = new PriorityQueue<(int U, long C)>((x, y) => x.C.CompareTo(y.C));
            for (var i = 0; i < N; i++)
            {
                queue.Enqueue((i, costs[i]));
            }

            var used = new bool[N];
            while (queue.Count > 0)
            {
                var (u, c) = queue.Dequeue();
                if (used[u]) continue;
                used[u] = true;
                answer = Math.Max(answer, c);
                foreach (var v in G[u])
                {
                    if (used[v]) continue;
                    costs[v] -= A[u];
                    queue.Enqueue((v, costs[v]));
                }
            }

            Console.WriteLine(answer);
        }

        public static long BinarySearch(long ng, long ok, Func<long, bool> func)
        {
            while (Math.Abs(ok - ng) > 1)
            {
                var m = (ok + ng) / 2;
                if (func(m)) ok = m;
                else ng = m;
            }
            return ok;
        }

        public class PriorityQueue<T> : IReadOnlyCollection<T>
        {
            private readonly Comparison<T> _comparison;
            private readonly List<T> _heap;
            public PriorityQueue(IEnumerable<T> items, IComparer<T> comparer = null) : this(comparer)
            {
                foreach (var item in items) Enqueue(item);
            }
            public PriorityQueue(IEnumerable<T> items, Comparison<T> comparison) : this(comparison)
            {
                foreach (var item in items) Enqueue(item);
            }
            public PriorityQueue(IComparer<T> comparer = null) : this((comparer ?? Comparer<T>.Default).Compare) { }
            public PriorityQueue(Comparison<T> comparison)
            {
                _heap = new List<T>();
                _comparison = comparison;
            }
            public int Count => _heap.Count;
            public IEnumerator<T> GetEnumerator() => _heap.GetEnumerator();
            IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
            public void Enqueue(T item)
            {
                var child = Count;
                _heap.Add(item);
                while (child > 0)
                {
                    var parent = (child - 1) / 2;
                    if (_comparison(_heap[parent], _heap[child]) <= 0) break;
                    (_heap[parent], _heap[child]) = (_heap[child], _heap[parent]);
                    child = parent;
                }
            }
            public T Dequeue()
            {
                if (Count == 0) throw new InvalidOperationException();
                var result = _heap[0];
                _heap[0] = _heap[Count - 1];
                _heap.RemoveAt(Count - 1);
                var parent = 0;
                while (parent * 2 + 1 < Count)
                {
                    var left = parent * 2 + 1;
                    var right = parent * 2 + 2;
                    if (right < Count && _comparison(_heap[left], _heap[right]) > 0)
                        left = right;
                    if (_comparison(_heap[parent], _heap[left]) <= 0) break;
                    (_heap[parent], _heap[left]) = (_heap[left], _heap[parent]);
                    parent = left;
                }
                return result;
            }
            public T Peek()
            {
                if (Count == 0) throw new InvalidOperationException();
                return _heap[0];
            }
            public bool TryDequeue(out T result)
            {
                if (Count > 0)
                {
                    result = Dequeue();
                    return true;
                }
                result = default;
                return false;
            }
            public bool TryPeek(out T result)
            {
                if (Count > 0)
                {
                    result = Peek();
                    return true;
                }
                result = default;
                return false;
            }
            public void Clear() => _heap.Clear();
            public bool Contains(T item) => _heap.Contains(item);
        }

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

Submission Info

Submission Time
Task E - Erasing Vertices 2
User AconCavy
Language C# (.NET Core 3.1.201)
Score 500
Code Size 7578 Byte
Status AC
Exec Time 1323 ms
Memory 79364 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 500 / 500
Status
AC × 2
AC × 32
Set Name Test Cases
Sample example_00.txt, example_01.txt
All example_00.txt, example_01.txt, test_00.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt, test_29.txt
Case Name Status Exec Time Memory
example_00.txt AC 126 ms 28024 KiB
example_01.txt AC 86 ms 28404 KiB
test_00.txt AC 1022 ms 78360 KiB
test_01.txt AC 420 ms 54044 KiB
test_02.txt AC 497 ms 52952 KiB
test_03.txt AC 1132 ms 78724 KiB
test_04.txt AC 717 ms 57300 KiB
test_05.txt AC 430 ms 46808 KiB
test_06.txt AC 1093 ms 77312 KiB
test_07.txt AC 198 ms 41516 KiB
test_08.txt AC 252 ms 40448 KiB
test_09.txt AC 404 ms 62284 KiB
test_10.txt AC 85 ms 29020 KiB
test_11.txt AC 138 ms 45120 KiB
test_12.txt AC 160 ms 38672 KiB
test_13.txt AC 89 ms 30952 KiB
test_14.txt AC 1137 ms 78908 KiB
test_15.txt AC 1292 ms 78844 KiB
test_16.txt AC 1257 ms 78744 KiB
test_17.txt AC 1315 ms 78844 KiB
test_18.txt AC 1309 ms 79096 KiB
test_19.txt AC 1279 ms 78928 KiB
test_20.txt AC 1323 ms 79364 KiB
test_21.txt AC 1252 ms 79172 KiB
test_22.txt AC 1247 ms 78764 KiB
test_23.txt AC 1049 ms 78896 KiB
test_24.txt AC 404 ms 49600 KiB
test_25.txt AC 402 ms 49588 KiB
test_26.txt AC 402 ms 49568 KiB
test_27.txt AC 392 ms 49276 KiB
test_28.txt AC 422 ms 49424 KiB
test_29.txt AC 376 ms 49536 KiB