結果

問題 No.922 東北きりきざむたん
ユーザー ひばちひばち
提出日時 2019-11-08 23:03:34
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 10,154 bytes
コンパイル時間 1,334 ms
コンパイル使用メモリ 121,024 KB
実行使用メモリ 66,776 KB
最終ジャッジ日時 2024-09-15 02:10:00
合計ジャッジ時間 6,241 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 45 ms
25,344 KB
testcase_01 AC 46 ms
19,712 KB
testcase_02 AC 42 ms
19,584 KB
testcase_03 AC 44 ms
19,712 KB
testcase_04 AC 47 ms
20,224 KB
testcase_05 WA -
testcase_06 WA -
testcase_07 AC 47 ms
20,864 KB
testcase_08 WA -
testcase_09 WA -
testcase_10 TLE -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 System.Linq;
using System.IO;
using System.Runtime.CompilerServices;
using System.Text;
using static Template;
using static System.Console;
using static System.Convert;
using static System.Math;
using Pi = Pair<int, int>;
using Number = System.Int64;

class Solver
{
    long res;
    long cost,calc;
    List<int>[] edge;
    UnionFind uf;
    int[] w;
    Dictionary<int, int> dic;
    Dictionary<int ,long>[] dist;
    public void Solve()
    {
        int num, m, q;
        Input.Make(out num, out m, out q);
        res = 0L;w = new int[num];
        uf = new UnionFind(num);
        edge = Input.edge<int>(num);
        for(var i = 0; i < m; i++)
        {
            int u, v;
            Input.Make(out u, out v);
            u--;v--;uf.Union(u, v);
            edge[u].Add(v);edge[v].Add(u);
        }
        dic = new Dictionary<int, int>();var d = 0;
        var cn = new int[num];
        var ct = new int[num];var p = new int[num];
        for (var i = 0; i < num; i++)
            if (!dic.ContainsKey(uf[i]))
            { p[d] = uf[i]; dic[uf[i]] = d++; cn[i] = ct[uf[i]]++; }
            else cn[i] = ct[uf[i]]++;
        dist = Create(num,() => new Dictionary<int, Number>());
        var ab = new int[q][];
        for (var i = 0; i < q; i++)
            ab[i] = new[] { Input.Next<int>() - 1, Input.Next<int>() - 1 };
        foreach (var ar in ab)
            if(uf[ar[0]]!=uf[ar[1]])
            { w[ar[0]]++;w[ar[1]]++; }
        var use = new bool[d];
        for(var i=0;i<num;i++)
            if (!use[dic[uf[i]]])
            {
                if (edge[i].Count > 1) continue;
                use[dic[uf[i]]] = true;
                calc = 0;
                dfs1(i, -1, 0);
                cost = calc;
                dfs2(i, -1, calc);
                res += cost;
            }
        var lcs = Enumerable.Range(0, d).Select(v => new LowestCommonAncestor(uf.Size(p[v]))).ToArray();
        for (var i = 0; i < num; i++)
        {
            var u = dic[uf[i]];
            foreach (var e in edge[i])
                lcs[u].AddEdge(cn[i], cn[e]);
        }
        foreach (var lc in lcs)
            lc.Build();
        foreach (var ar in ab)
            if (uf[ar[0]] == uf[ar[1]])
                res += lcs[dic[uf[ar[0]]]].dist(cn[ar[0]], cn[ar[1]]);
        WriteLine(res);
    }
    long dfs1(int idx, int p, long wei)
    {
        if (p != -1)
            dist[p][idx] = wei;
        var rt = 0L;
        foreach (var e in edge[idx])
            if (e != p)
            {
                var v = dfs1(e, idx, wei + w[idx]);
                dist[e][idx] = v;
                rt += v;
                calc += v;
            }
        return rt + w[idx];
    }
    void dfs2(int idx, int p, long c)
    {
        foreach (var e in edge[idx])
            if (e != p)
            {
                var v = c + dist[idx][e] - dist[e][idx];
                chmin(ref cost, v);
                dfs2(e, idx, v);
            }
    }
}

class LowestCommonAncestor
{
    private List<int>[] edge;
    private int num, lb;
    private int[][] parent;
    private int[] depth;
    public LowestCommonAncestor(int num)
    { this.num = num; edge = Create(num, () => new List<int>()); }
    public void AddEdge(int u, int v)
    {
        edge[u].Add(v); edge[v].Add(u);
    }
    public void Build(int root = 0)
    {
        for (lb = 31; lb >= 0; lb--)
            if ((1 & num >> lb) == 1)
                break;
        parent = Create(lb + 1, () => Create(num, () => -1));
        depth = new int[num];
        PreDFS(root, -1);
        for (var i = 1; i <= lb; i++)
            for (var j = 0; j < num; j++)
                if (parent[i - 1][j] != -1)
                {
                    parent[i][j] = parent[i - 1][parent[i - 1][j]];
                }
    }
    public int dist(int u, int v)
        => depth[u] + depth[v] - 2 * depth[LCA(u, v)];
    private void PreDFS(int index, int p)
    {
        parent[0][index] = p;
        foreach (var e in edge[index])
            if (e != p)
            {
                depth[e] = depth[index] + 1;
                PreDFS(e, index);
            }
    }
    public int LCA(int u, int v)
    {
        if (depth[u] > depth[v])
            swap(ref u, ref v);
        for (var i = lb; i >= 0; i--)
            if ((1 & (depth[v] - depth[u]) >> i) == 1)
                v = parent[i][v];
        if (u == v) return u;
        for (var i = lb; i >= 0; i--)
            if (parent[i][u] != parent[i][v])
            { u = parent[i][u]; v = parent[i][v]; }
        return parent[0][u];
    }
}
public class UnionFind
{
    protected int[] parent, size;
    public virtual int this[int i] { get { return Find(i); } }
    public UnionFind(int num)
    {
        parent = new int[num]; size = new int[num];
        for (var i = 0; i < num; i++) { parent[i] = i; size[i] = 1; }
    }
    protected int Find(int i)
        => i == parent[i] ? i : (parent[i] = Find(parent[i]));
    public int Size(int i)
        => size[Find(i)];
    public virtual bool Union(int v1, int v2)
    {
        v1 = Find(v1); v2 = Find(v2);
        if (v1 == v2) return false;
        if (size[v1] < size[v2])
            swap(ref v1, ref v2);
        parent[v2] = v1;
        size[v1] += size[v2];
        return true;
    }
}


#region Template
public class Template
{
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static bool chmin<T>(ref T num, T val) where T : IComparable<T>
    { if (num.CompareTo(val) == 1) { num = val; return true; } return false; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static bool chmax<T>(ref T num, T val) where T : IComparable<T>
    { if (num.CompareTo(val) == -1) { num = val; return true; } return false; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static void swap<T>(ref T v1, ref T v2)
    { var t = v2; v2 = v1; v1 = t; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static T[] Create<T>(int n, Func<T> f)
        => Enumerable.Repeat(0, n).Select(_ => f()).ToArray();
    public static void Fail() => Fail("No");
    public static void Fail<T>(T s) { Console.WriteLine(s); Console.Out.Close(); Environment.Exit(0); }
    static void Main(string[] args)
    {
        var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
        Console.SetOut(sw);
        var p = new Solver();
        for (var i = 1; i > 0; --i)
            p.Solve();
        Console.Out.Flush();
    }
}

public class Input
{
    public static string read => Console.ReadLine().Trim();
    public static int[] ar => read.Split(' ').Select(int.Parse).ToArray();
    public static int num => Convert.ToInt32(read);
    public static long[] arL => read.Split(' ').Select(long.Parse).ToArray();
    public static long numL => Convert.ToInt64(read);
    public static char[][] grid(int h)
        => Create(h, () => read.ToCharArray());
    public static int[] ar1D(int n)
        => Create(n, () => num);
    public static long[] arL1D(int n)
        => Create(n, () => numL);
    public static string[] strs(int n)
        => Create(n, () => read);
    public static int[][] ar2D(int n)
        => Create(n, () => ar);
    public static long[][] arL2D(int n)
        => Create(n, () => arL);
    public static List<T>[] edge<T>(int n)
        => Create(n, () => new List<T>());
    public static void Make<T1, T2>(out T1 v1, out T2 v2)
    {
        v1 = Next<T1>();
        v2 = Next<T2>();
    }
    public static void Make<T1, T2, T3>(out T1 v1, out T2 v2, out T3 v3)
    {
        Make(out v1, out v2);
        v3 = Next<T3>();
    }
    public static void Make<T1, T2, T3, T4>(out T1 v1, out T2 v2, out T3 v3, out T4 v4)
    {
        Make(out v1, out v2, out v3);
        v4 = Next<T4>();
    }
    public static void Make<T1, T2, T3, T4, T5>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5)
    {
        Make(out v1, out v2, out v3, out v4);
        v5 = Next<T5>();
    }
    public static void Make<T1, T2, T3, T4, T5, T6>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5, out T6 v6)
    {
        Make(out v1, out v2, out v3, out v4, out v5);
        v6 = Next<T6>();
    }
    static Input()
    {
        sc = new Queue<string>();
        dic = new Dictionary<Type, Func<string, object>>();
        dic[typeof(int)] = s => int.Parse(s);
        dic[typeof(long)] = s => long.Parse(s);
        dic[typeof(char)] = s => char.Parse(s);
        dic[typeof(double)] = s => double.Parse(s);
        dic[typeof(uint)] = s => uint.Parse(s);
        dic[typeof(ulong)] = s => ulong.Parse(s);
        dic[typeof(string)] = s => s;
    }
    private static Dictionary<Type, Func<string, object>> dic;
    private static Queue<string> sc;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static T Next<T>() { if (sc.Count == 0) foreach (var item in read.Split(' ')) sc.Enqueue(item); return (T)dic[typeof(T)](sc.Dequeue()); }
    public const int MOD = 1000000007;
}

public class Pair<T1, T2> : IComparable<Pair<T1, T2>>
{
    public T1 v1;
    public T2 v2;
    public Pair() { v1 = default(T1);v2 = default(T2); }
    public Pair(T1 v1, T2 v2)
    { this.v1 = v1; this.v2 = v2; }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public int CompareTo(Pair<T1, T2> p)
    {
        var c = Comparer<T1>.Default.Compare(v1, p.v1);
        if (c == 0)
            c = Comparer<T2>.Default.Compare(v2, p.v2);
        return c;
    }
    public override string ToString()
        => $"{v1.ToString()} {v2.ToString()}";
}

public class Pair<T1, T2, T3> : Pair<T1, T2>, IComparable<Pair<T1, T2, T3>>
{
    public T3 v3;
    public Pair() : base() { v3 = default(T3); }
    public Pair(T1 v1, T2 v2, T3 v3) : base(v1, v2)
    { this.v3 = v3; }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public int CompareTo(Pair<T1, T2, T3> p)
    {
        var c = base.CompareTo(p);
        if (c == 0)
            c = Comparer<T3>.Default.Compare(v3, p.v3);
        return c;
    }
    public override string ToString()
        => $"{base.ToString()} {v3.ToString()}";
}
#endregion
0