結果

問題 No.1212 Second Path
ユーザー りあんりあん
提出日時 2020-08-30 17:55:59
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 12,650 bytes
コンパイル時間 3,658 ms
コンパイル使用メモリ 119,632 KB
実行使用メモリ 278,004 KB
最終ジャッジ日時 2024-04-27 12:47:21
合計ジャッジ時間 15,134 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2,115 ms
271,696 KB
testcase_01 TLE -
testcase_02 TLE -
testcase_03 TLE -
testcase_04 TLE -
testcase_05 TLE -
testcase_06 AC 1,605 ms
216,852 KB
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 AC 2,074 ms
273,752 KB
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 AC 155 ms
31,064 KB
testcase_24 WA -
testcase_25 AC 166 ms
30,932 KB
testcase_26 WA -
testcase_27 AC 162 ms
26,848 KB
testcase_28 AC 165 ms
30,568 KB
testcase_29 AC 2,842 ms
273,864 KB
testcase_30 AC 2,785 ms
273,608 KB
testcase_31 AC 2,808 ms
278,004 KB
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
testcase_36 WA -
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 AC 35 ms
26,700 KB
testcase_43 AC 34 ms
26,696 KB
testcase_44 AC 33 ms
26,816 KB
testcase_45 AC 1,535 ms
214,332 KB
testcase_46 AC 1,495 ms
214,316 KB
testcase_47 AC 1,558 ms
214,908 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
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.Threading;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics;
using static util;
using P = pair<int, int>;

class Program {
    static void Main(string[] args) {
        var sw = new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false };
        var solver = new Solver(sw);
        // var t = new Thread(solver.solve, 1 << 28); // 256 MB
        // t.Start();
        // t.Join();
        solver.solve();
        sw.Flush();
    }
}

class Solver {
    StreamWriter sw;
    Scan sc;
    void Prt(string a) => sw.WriteLine(a);
    void Prt<T>(IEnumerable<T> a) => Prt(string.Join(" ", a));
    void Prt(params object[] a) => Prt(string.Join(" ", a));
    public Solver(StreamWriter sw) {
        this.sw = sw;
        this.sc = new Scan();
    }

    public void solve() {
        int n = sc.Int;
        var edge = new List<P>[n];
        for (int i = 0; i < n; i++)
        {
            edge[i] = new List<P>();
        }
        for (int i = 0; i < n - 1; i++)
        {
            int u, v, w;
            sc.Multi(out u, out v, out w);
            --u;
            --v;
            edge[u].Add(new P(w, v));
            edge[v].Add(new P(w, u));
        }
        var lca = new LCATree(edge);
        int q = sc.Int;
        for (int i = 0; i < q; i++)
        {
            int u, v;
            sc.Multi(out u, out v);
            --u;
            --v;
            Prt(lca.run(u, v));
        }



    }
}

class pair<T, U> : IComparable<pair<T, U>> {
    public T v1;
    public U v2;
    public pair() : this(default(T), default(U)) {}
    public pair(T v1, U v2) { this.v1 = v1; this.v2 = v2; }
    public int CompareTo(pair<T, U> a) {
        int c = Comparer<T>.Default.Compare(v1, a.v1);
        return c != 0 ? c : Comparer<U>.Default.Compare(v2, a.v2);
    }
    public override string ToString() => v1 + " " + v2;
    public void Deconstruct(out T a, out U b) { a = v1; b = v2; }
}
static class util {
    public static readonly int M = 1000000007;
    // public static readonly int M = 998244353;
    public static readonly long LM = 1L << 60;
    public static readonly double eps = 1e-11;
    public static void DBG(string a) => Console.Error.WriteLine(a);
    public static void DBG<T>(IEnumerable<T> a) => DBG(string.Join(" ", a));
    public static void DBG(params object[] a) => DBG(string.Join(" ", a));
    public static void Assert(params bool[] conds) {
        if (conds.Any(x => !x)) throw new Exception();
    }
    public static pair<T, U> make_pair<T, U>(T v1, U v2) => new pair<T, U>(v1, v2);
    public static int CompareList<T>(IList<T> a, IList<T> b) where T : IComparable<T> {
        for (int i = 0; i < a.Count && i < b.Count; i++)
            if (a[i].CompareTo(b[i]) != 0) return a[i].CompareTo(b[i]);
        return a.Count.CompareTo(b.Count);
    }
    public static bool inside(int i, int j, int h, int w) => i >= 0 && i < h && j >= 0 && j < w;
    public static readonly int[] dd = { 0, 1, 0, -1 };
    // static readonly string dstring = "RDLU";
    public static IEnumerable<P> adjacents(int i, int j)
        => Enumerable.Range(0, dd.Length).Select(k => new P(i + dd[k], j + dd[k ^ 1]));
    public static IEnumerable<P> adjacents(int i, int j, int h, int w)
        => adjacents(i, j).Where(p => inside(p.v1, p.v2, h, w));
    public static IEnumerable<P> adjacents(this P p) => adjacents(p.v1, p.v2);
    public static IEnumerable<P> adjacents(this P p, int h, int w) => adjacents(p.v1, p.v2, h, w);
    public static IEnumerable<int> all_subset(this int p) {
        for (int i = 0; ; i = i - p & p) {
            yield return i;
            if (i == p) break;
        }
    }
    public static Dictionary<T, int> compress<T>(this IEnumerable<T> a)
        => a.Distinct().OrderBy(v => v).Select((v, i) => new { v, i }).ToDictionary(p => p.v, p => p.i);
    public static Dictionary<T, int> compress<T>(params IEnumerable<T>[] a) => compress(a.SelectMany(x => x));
    public static T[] inv<T>(this Dictionary<T, int> dic) {
        var res = new T[dic.Count];
        foreach (var item in dic) res[item.Value] = item.Key;
        return res;
    }
    public static void swap<T>(ref T a, ref T b) where T : struct { var t = a; a = b; b = t; }
    public static void swap<T>(this IList<T> a, int i, int j) where T : struct { var t = a[i]; a[i] = a[j]; a[j] = t; }
    public static T[] copy<T>(this IList<T> a) {
        var ret = new T[a.Count];
        for (int i = 0; i < a.Count; i++) ret[i] = a[i];
        return ret;
    }
}

class Scan {
    StreamReader sr;
    public Scan() { sr = new StreamReader(Console.OpenStandardInput()); }
    public Scan(string path) { sr = new StreamReader(path); }
    public int Int => int.Parse(Str);
    public long Long => long.Parse(Str);
    public double Double => double.Parse(Str);
    public string Str => ReadLine.Trim();
    public string ReadLine => sr.ReadLine();
    public pair<T, U> Pair<T, U>() {
        T a; U b;
        Multi(out a, out b);
        return new pair<T, U>(a, b);
    }
    public P P => Pair<int, int>();
    public int[] IntArr => StrArr.Select(int.Parse).ToArray();
    public long[] LongArr => StrArr.Select(long.Parse).ToArray();
    public double[] DoubleArr => StrArr.Select(double.Parse).ToArray();
    public string[] StrArr => Str.Split(new[]{' '}, StringSplitOptions.RemoveEmptyEntries);
    bool eq<T, U>() => typeof(T).Equals(typeof(U));
    T ct<T, U>(U a) => (T)Convert.ChangeType(a, typeof(T));
    T cv<T>(string s) => eq<T, int>()    ? ct<T, int>(int.Parse(s))
                       : eq<T, long>()   ? ct<T, long>(long.Parse(s))
                       : eq<T, double>() ? ct<T, double>(double.Parse(s))
                       : eq<T, char>()   ? ct<T, char>(s[0])
                                         : ct<T, string>(s);
    public void Multi<T>(out T a) => a = cv<T>(Str);
    public void Multi<T, U>(out T a, out U b) {
        var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]);
    }
    public void Multi<T, U, V>(out T a, out U b, out V c) {
        var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]);
    }
    public void Multi<T, U, V, W>(out T a, out U b, out V c, out W d) {
        var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]);
    }
    public void Multi<T, U, V, W, X>(out T a, out U b, out V c, out W d, out X e) {
        var ar = StrArr; a = cv<T>(ar[0]); b = cv<U>(ar[1]); c = cv<V>(ar[2]); d = cv<W>(ar[3]); e = cv<X>(ar[4]);
    }
}

class LCATree {
    int m;
    int[][] parents;
    long[][] dist;
    int[][] last;
    P[][][] mins;
    P INF = new P(M, -1);
    int[] depth;

    public LCATree(List<pair<int, int>>[] g, int root = 0) {
        int n = g.Length;
        parents = new int[n][];
        dist = new long[n][];
        depth = new int[n];
        last = new int[n][];
        mins = new P[n][][];

        m = 1;
        while ((1 << m - 1) < n) ++m;
        for (int i = 0; i < n; i++) {
            parents[i] = new int[m];
            dist[i] = new long[m];
            last[i] = new int[m];
            mins[i] = new P[m][];
            depth[i] = -1;
            for (int j = 0; j < m; j++) {
                parents[i][j] = -1;
                dist[i][j] = 0;
                last[i][j] = -1;
                mins[i][j] = new P[]{ INF, INF };
            }
        }
        for (int i = 0; i < n; i++)
        {
            P mi1 = INF;
            P mi2 = INF;
            foreach (var item in g[i])
            {
                if (mi1.v1 > item.v1) {
                    mi2 = mi1;
                    mi1 = item;
                }
                else if (mi2.v1 > item.v1) {
                    mi2 = item;
                }
            }
            mins[i][0] = new P[]{ mi1, mi2 };
        }
        depth[root] = 0;
        var q = new Queue<int>();
        q.Enqueue(root);
        while (q.Count > 0) {
            var p = q.Dequeue();
            foreach (var item in g[p]) {
                if (depth[item.v2] == -1) {
                    depth[item.v2] = depth[p] + 1;
                    parents[item.v2][0] = p;
                    last[item.v2][0] = item.v2;
                    dist[item.v2][0] = item.v1;
                    for (int i = 1; i < m && parents[item.v2][i - 1] != -1; i++) {
                        parents[item.v2][i] = parents[parents[item.v2][i - 1]][i - 1];
                        dist[item.v2][i] = dist[item.v2][i - 1] + dist[parents[item.v2][i - 1]][i - 1];
                        last[item.v2][i] = last[parents[item.v2][i - 1]][i - 1];
                        P mi1 = INF;
                        P mi2 = INF;
                        foreach (var mi in mins[item.v2][i - 1])
                        {
                            if (mi.v2 == parents[item.v2][i - 1]) continue;
                            if (mi1.v1 > mi.v1) {
                                mi2 = mi1;
                                mi1 = mi;
                            }
                            else if (mi2.v1 > mi.v1) {
                                mi2 = mi;
                            }
                        }
                        foreach (var mi in mins[parents[item.v2][i - 1]][i - 1])
                        {
                            if (mi.v2 == last[item.v2][i - 1]) continue;
                            if (mi1.v1 > mi.v1) {
                                mi2 = mi1;
                                mi1 = mi;
                            }
                            else if (mi2.v1 > mi.v1) {
                                mi2 = mi;
                            }
                        }
                        mins[item.v2][i] = new P[]{ mi1, mi2 };
                    }
                    q.Enqueue(item.v2);
                }
            }
        }
    }
    void climb(ref int p, int cnt) {
        for (int i = m - 1; i >= 0 ; i--) {
            if (((cnt >> i) & 1) == 1) {
                p = parents[p][i];
            }
        }
    }
    long climb(ref int p, ref P[] mis, ref int las, int cnt) {
        long dis = 0;
        for (int i = m - 1; i >= 0 ; i--) {
            if (((cnt >> i) & 1) == 1) {
                dis += dist[p][i];
                P mi1 = INF;
                P mi2 = INF;
                foreach (var mi in mis)
                {
                    if (mi.v2 == p) continue;
                    if (mi1.v1 > mi.v1) {
                        mi2 = mi1;
                        mi1 = mi;
                    }
                    else if (mi2.v1 > mi.v1) {
                        mi2 = mi;
                    }
                }
                foreach (var mi in mins[p][i])
                {
                    if (mi.v2 == las) continue;
                    if (mi1.v1 > mi.v1) {
                        mi2 = mi1;
                        mi1 = mi;
                    }
                    else if (mi2.v1 > mi.v1) {
                        mi2 = mi;
                    }
                }
                mis = new P[]{ mi1, mi2 };
                las = last[p][i];
                p = parents[p][i];
            }
        }
        return dis;
    }
    public long run(int p, int q) {
        int lca = getlca(p, q);
        var mip = new P[]{ INF, INF };
        int lasp = -1;
        var miq = new P[]{ INF, INF };
        int lasq = -1;

        long dist = 0;
        dist += climb(ref p, ref mip, ref lasp, depth[p] - depth[lca]);
        dist += climb(ref q, ref miq, ref lasq, depth[q] - depth[lca]);
        int min = M;
        foreach (var mi in mip)
        {
            if (mi.v2 == lca || mi.v2 == lasq) continue;
            min = Math.Min(min, mi.v1);
        }
        foreach (var mi in miq)
        {
            if (mi.v2 == lca || mi.v2 == lasp) continue;
            min = Math.Min(min, mi.v1);
        }
        foreach (var mi in mins[lca][0])
        {
            if (mi.v2 == lasq || mi.v2 == lasp) continue;
            min = Math.Min(min, mi.v1);
        }
        if (min == M) return -1;
        return dist + min * 2;
    }
    int getlca(int p, int q) {
        if (depth[p] > depth[q]) climb(ref p, depth[p] - depth[q]);
        if (depth[p] < depth[q]) climb(ref q, depth[q] - depth[p]);
        if (p == q) return p;
        for (int i = m - 1; i >= 0 ; i--) {
            if (parents[p][i] != parents[q][i]) {
                p = parents[p][i];
                q = parents[q][i];
            }
        }
        {
            p = parents[p][0];
            q = parents[q][0];
        }
        return p;
    }

}
0