結果

問題 No.922 東北きりきざむたん
ユーザー 紙ぺーぱー紙ぺーぱー
提出日時 2019-11-08 22:54:07
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 788 ms / 2,000 ms
コード長 14,887 bytes
コンパイル時間 4,434 ms
コンパイル使用メモリ 113,832 KB
実行使用メモリ 92,696 KB
最終ジャッジ日時 2023-10-13 04:30:19
合計ジャッジ時間 16,759 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 56 ms
18,620 KB
testcase_01 AC 55 ms
22,504 KB
testcase_02 AC 57 ms
20,580 KB
testcase_03 AC 56 ms
20,504 KB
testcase_04 AC 56 ms
20,544 KB
testcase_05 AC 56 ms
20,512 KB
testcase_06 AC 58 ms
20,576 KB
testcase_07 AC 58 ms
18,484 KB
testcase_08 AC 58 ms
20,512 KB
testcase_09 AC 262 ms
57,596 KB
testcase_10 AC 207 ms
36,060 KB
testcase_11 AC 248 ms
47,532 KB
testcase_12 AC 225 ms
71,128 KB
testcase_13 AC 117 ms
35,260 KB
testcase_14 AC 432 ms
81,152 KB
testcase_15 AC 213 ms
76,132 KB
testcase_16 AC 665 ms
78,916 KB
testcase_17 AC 669 ms
75,088 KB
testcase_18 AC 684 ms
77,128 KB
testcase_19 AC 677 ms
75,056 KB
testcase_20 AC 655 ms
74,952 KB
testcase_21 AC 788 ms
62,272 KB
testcase_22 AC 760 ms
59,876 KB
testcase_23 AC 492 ms
73,232 KB
testcase_24 AC 437 ms
66,468 KB
testcase_25 AC 473 ms
86,036 KB
testcase_26 AC 463 ms
81,656 KB
testcase_27 AC 454 ms
83,644 KB
testcase_28 AC 403 ms
92,696 KB
testcase_29 AC 264 ms
55,380 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

//author: camypaper
// modified
// https://camypaper.bitbucket.io/2017/01/06/jagsummer2014day2h/
using System;
using System.Linq;
using System.Collections.Generic;
using Debug = System.Diagnostics.Debug;
using StringBuilder = System.Text.StringBuilder;
//using System.Numerics;
//using Point = System.Numerics.Complex;
using Number = System.Int64;
namespace Program {
    public class Solver {
        public void Solve() {
            var n = sc.Integer() + 1;
            var M = sc.Integer();
            var q = sc.Integer();
            var T = new HLTreeGraph(n);
            var s = new DisjointSet(n);
            var dist = new long[n];
            T.dist = dist;
            for (int i = 0; i < M; i++) {
                var u = sc.Integer() - 1;
                var v = sc.Integer() - 1;
                s.Unite(u, v);
                T.AddEdge(u, v, 1);
            }
            for (int i = 0; i < n - 1; i++)
                if (s[i] == i) T.AddEdge(n - 1, i, 1);
            T.Build(n - 1);


            var ans = 0L;
            var ev = Enumerate(n, x => new List<int>());
            for (int i = 0; i < q; i++) {
                var u = sc.Integer() - 1;
                var v = sc.Integer() - 1;
                if (s.IsUnited(u, v)) {
                    ans += dist[u] + dist[v] - 2 * dist[T.GetLCA(u, v)];
                }
                else {
                    ev[s[u]].Add(u);
                    ev[s[v]].Add(v);
                }
            }
            Debug.WriteLine(ans);



            var k = 0L;
            var sum = 0L;
            Func<int, long> f = v =>
            {
                if (v == -1) return 1L << 60;
                var ret = sum + k * dist[v];
                ret += T.Query(v);
                return ret;
            };
            Func<int, int, KeyValuePair<int, long>> g = (v, p) =>
            {
                var l = T.d[p] - 1; var r = T.d[v];
                while (l + 1 < r) {
                    var m = (l + r) / 2;
                    var ll = f(T.Get(v, m)); var rr = f(T.Get(v, m + 1));
                    if (ll < rr) r = m;
                    else l = m;
                }
                v = T.Get(v, r);
                return new KeyValuePair<int, Number>(v, f(v));
            };
            var c = 0;


            for (int r = 0; r < n; r++) {
                if (ev[r].Count == 0) continue;
                k = 0;
                sum = 0;
                long last = 0;
                foreach (var v in ev[r]) {

                    k++;
                    sum += dist[v];
                    T.Update(v, 1);

                    var lca = T.GetLCA(c, v);
                    var x = g(c, lca);
                    var y = g(v, lca);
                    var z = -1L;
                    if (x.Value <= y.Value) { c = x.Key; z = x.Value; }
                    else { c = y.Key; z = y.Value; }
                    last = z;
                }
                foreach (var v in ev[r])
                    T.Update(v, -1);
                ans += last;
            }

            Console.WriteLine(ans);
        }

        public IO.StreamScanner sc = new IO.StreamScanner(Console.OpenStandardInput());
        static T[] Enumerate<T>(int n, Func<int, T> f) { var a = new T[n]; for (int i = 0; i < n; ++i) a[i] = f(i); return a; }
        static public void Swap<T>(ref T a, ref T b) { var tmp = a; a = b; b = tmp; }
    }
}

#region main
static class Ex {
    //static public string AsString(this IEnumerable<char> ie) { return new string(System.Linq.Enumerable.ToArray(ie)); }
    //static public string AsJoinedString<T>(this IEnumerable<T> ie, string st = " ") { return string.Join(st, ie); }
    static public void Main() {
        var solver = new Program.Solver();
        solver.Solve();
        Program.IO.Printer.Out.Flush();
    }
}
#endregion
#region Ex
namespace Program.IO {
    using System.IO;
    using System.Text;
    using System.Globalization;
    public class Printer : StreamWriter {
        static Printer() { Out = new Printer(Console.OpenStandardOutput()) { AutoFlush = false }; }
        public static Printer Out { get; set; }
        public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } }
        public Printer(System.IO.Stream stream) : base(stream, new UTF8Encoding(false, true)) { }
        public Printer(System.IO.Stream stream, Encoding encoding) : base(stream, encoding) { }
        public void Write<T>(string format, T[] source) { base.Write(format, source.OfType<object>().ToArray()); }
        public void WriteLine<T>(string format, T[] source) { base.WriteLine(format, source.OfType<object>().ToArray()); }
    }
    public class StreamScanner {
        public StreamScanner(Stream stream) { str = stream; }
        public readonly Stream str;
        private readonly byte[] buf = new byte[1024];
        private int len, ptr;
        public bool isEof = false;
        public bool IsEndOfStream { get { return isEof; } }
        private byte read() {
            if (isEof) return 0;
            if (ptr >= len) { ptr = 0; if ((len = str.Read(buf, 0, 1024)) <= 0) { isEof = true; return 0; } }
            return buf[ptr++];
        }
        public char Char() { byte b = 0; do b = read(); while ((b < 33 || 126 < b) && !isEof); return (char)b; }

        public string Scan() {
            var sb = new StringBuilder();
            for (var b = Char(); b >= 33 && b <= 126; b = (char)read())
                sb.Append(b);
            return sb.ToString();
        }
        public string ScanLine() {
            var sb = new StringBuilder();
            for (var b = Char(); b != '\n'; b = (char)read())
                if (b == 0) break;
                else if (b != '\r') sb.Append(b);
            return sb.ToString();
        }
        public long Long() {
            if (isEof) return long.MinValue;
            long ret = 0; byte b = 0; var ng = false;
            do b = read();
            while (b != 0 && b != '-' && (b < '0' || '9' < b));
            if (b == 0) return long.MinValue;
            if (b == '-') { ng = true; b = read(); }
            for (; true; b = read()) {
                if (b < '0' || '9' < b)
                    return ng ? -ret : ret;
                else ret = ret * 10 + b - '0';
            }
        }
        public int Integer() { return (isEof) ? int.MinValue : (int)Long(); }
        public double Double() { var s = Scan(); return s != "" ? double.Parse(s, CultureInfo.InvariantCulture) : double.NaN; }
        private T[] enumerate<T>(int n, Func<T> f) {
            var a = new T[n];
            for (int i = 0; i < n; ++i) a[i] = f();
            return a;
        }

        public char[] Char(int n) { return enumerate(n, Char); }
        public string[] Scan(int n) { return enumerate(n, Scan); }
        public double[] Double(int n) { return enumerate(n, Double); }
        public int[] Integer(int n) { return enumerate(n, Integer); }
        public long[] Long(int n) { return enumerate(n, Long); }
    }
}
#endregion

#region Edge
public struct Edge {
    public int from, to, cost;
    public long add;
    public Edge(int f, int t, int x) : this() { from = f; to = t; cost = x; }
}
#endregion
#region HLTreeGraph

public class HLTreeGraph {
    public long[] dist;
    /// <summary>
    /// 縮約前の頂点の数
    /// </summary>
    int N;
    /// <summary>
    /// 縮約前のグラフ
    /// </summary>
    List<Edge>[] G;
    /// <summary>
    /// チェインの集合
    /// </summary>
    List<Chain> H = new List<Chain>();

    int[] subTreeSize;
    int[] par;
    int[] pos;

    /// <summary>
    /// 元の木上の深さ
    /// </summary>
    public int[] d;


    /// <summary>
    ///uが属するチェイン
    /// </summary>
    Chain[] go;
    public HLTreeGraph(int n) {
        N = n;
        G = Enumerate(n, x => new List<Edge>());
        subTreeSize = new int[n];
        pos = new int[n];
        d = new int[n];
        par = new int[n];
        go = new Chain[n];
    }
    public void AddEdge(int f, int t, int x) {
        G[f].Add(new Edge(f, t, x));
        G[t].Add(new Edge(t, f, x));
    }
    #region impl
    public void Build(int root) {
        ComputeSubTreeSize(root);
        Decomposite(new Edge(-1, root, 0), -1, 0);
    }
    public void ComputeSubTreeSize(int root) {
        const long X = 1000000000;
        var stack = new FastStack<long>(N + 1);
        stack.Push(root * X);
        par[root] = -1;
        d[root] = 0;
        dist[root] = 0;
        while (stack.Any()) {
            var val = stack.Peek();
            var u = (int)(val / X);
            var it = (int)(val % X);

            if (it == G[u].Count) {
                stack.Pop();
                subTreeSize[u]++;
                if (par[u] >= 0) subTreeSize[par[u]] += subTreeSize[u];
            }
            else {
                var to = G[u][it].to;
                stack.Last++;
                if (to == par[u]) continue;

                par[to] = u;
                d[to] = d[u] + 1;
                dist[to] = dist[u] + G[u][it].cost;
                stack.Push(to * X);
            }
        }

    }
    public void Decomposite(Edge light, int prevId, int lv) {
        var chain = new Chain() { light = light, id = H.Count, parId = prevId, level = lv };
        H.Add(chain);
        var prev = light.from;
        var cur = light.to;
        while (cur != prev) {
            var next = cur;
            var max = 0;
            go[cur] = chain;
            pos[cur] = chain.heavy.Count;
            foreach (var to in G[cur]) {
                var t = to.to;
                if (t != prev) max = Math.Max(max, subTreeSize[t]);
            }
            foreach (var to in G[cur]) {
                var t = to.to;
                if (t == prev) continue;
                if (max == subTreeSize[t]) {
                    //Debug.WriteLine("{0}->{1}", cur, t);
                    max = 1 << 30;
                    next = t;
                    chain.heavy.Add(to);
                }
                else Decomposite(to, chain.id, lv + 1);
            }
            prev = cur;
            cur = next;
        }
        chain.init(this);

    }
    #endregion

    public void Update(int v, int coef) {
        while (v != -1) {
            go[v].sub.Add(pos[v] + 1, -2 * coef * dist[v]);
            go[v].coef.Add(1, -2 * coef); go[v].coef.Add(pos[v] + 1, 2 * coef);
            if (go[v].light.from != -1)
                go[v].light.add += 2 * coef * dist[go[v].light.from];
            v = go[v].light.from;
        }
    }
    /// <summary>
    /// (u,v)に関するクエリを処理する
    /// </summary>
    public long Query(int v) {
        long ans = 0;
        while (v != -1) {
            ans += go[v].coef[pos[v] + 1] * dist[v];
            ans += go[v].sub[pos[v] + 1];
            if (go[v].light.from != -1)
                ans += go[v].light.add;
            v = go[v].light.from;
        }
        return ans;
    }
    public int Get(int v, int x) {
        if (x < 0) return -1;
        if (d[v] < x) return -1;
        for (; ; )
        {
            var p = go[v].light.to;
            if (d[p] == x) return p;
            else if (d[p] < x)
                return go[v].heavy[x - d[p] - 1].to;
            v = go[v].light.from;
        }
    }
    /// <summary>
    /// LCA(u,v)を返す
    /// </summary>
    public int GetLCA(int u, int v) {
        while (go[u].id != go[v].id) {
            if (go[u].level < go[v].level) v = go[v].light.from;
            else u = go[u].light.from;
        }
        if (d[u] <= d[v]) return u;
        else return v;
    }

    static T[] Enumerate<T>(int n, Func<int, T> f) { var a = new T[n]; for (int i = 0; i < n; ++i) a[i] = f(i); return a; }

}
public class Chain {
    /// <summary>
    /// light edge
    /// </summary>
    public Edge light;
    /// <summary>
    /// heavy edgeの集合
    /// </summary>
    public List<Edge> heavy = new List<Edge>();
    /// <summary>
    /// 親のチェインの番号
    /// </summary>
    public int parId;
    /// <summary>
    /// 縮約後の木での深さ
    /// </summary>
    public int level;
    public int id;
    public FenwickTree coef;
    public FenwickTree sub;
    public void init(HLTreeGraph G) {
        coef = new FenwickTree(heavy.Count + 2);
        sub = new FenwickTree(heavy.Count + 2);

    }

}
#endregion
#region FenwickTree
public class FenwickTree {
    int n;
    Number[] bit;
    int max = 1;
    public FenwickTree(int size) {
        n = size; bit = new Number[n + 1];
        while ((max << 1) <= n) max <<= 1;
    }
    /// <summary>sum[a,b]</summary>
    public Number this[int i, int j] { get { return this[j] - this[i - 1]; } }
    /// <summary>sum[0,i]</summary>
    public Number this[int i] { get { Number s = 0; for (; i > 0; i -= i & -i) s += bit[i]; return s; } }
    public int LowerBound(Number w) {
        if (w <= 0) return 0;
        int x = 0;
        for (int k = max; k > 0; k >>= 1)
            if (x + k <= n && bit[x + k] < w) {
                w -= bit[x + k];
                x += k;
            }
        return x + 1;
    }
    /// <summary>add v to bit[i]</summary>
    public void Add(int i, Number v) {
        if (i == 0) System.Diagnostics.Debug.Fail("BIT is 1 indexed");
        for (; i <= n; i += i & -i) bit[i] += v;
    }
    public Number[] Items {
        get {
            var ret = new Number[n + 1];
            for (int i = 0; i < ret.Length; i++)
                ret[i] = this[i, i];
            return ret;
        }
    }
}
#endregion

#region Stack<T>
public class FastStack<T> {
    T[] data;
    int ptr;
    public FastStack(int size) { data = new T[size]; }
    public void Push(T item) { data[ptr++] = item; }
    public T Pop() { return data[--ptr]; }
    public T Peek() { return data[ptr - 1]; }
    public bool Any() { return ptr != 0; }
    public T Last { get { return data[ptr - 1]; } set { data[ptr - 1] = value; } }
    public int Count { get { return ptr; } }
}
#endregion

#region DisjointSet
public class DisjointSet {
    int[] par;
    byte[] rank;
    public DisjointSet(int n) {
        par = new int[n];
        for (int i = 0; i < n; i++)
            par[i] = -1;
        rank = new byte[n];
    }
    public int this[int id] {
        get {
            if ((par[id] < 0)) return id;
            return par[id] = this[par[id]];
        }
    }
    public bool Unite(int x, int y) {
        x = this[x]; y = this[y];
        if (x == y) return false;
        if (rank[x] < rank[y]) { var z = x; x = y; y = z; }
        par[x] += par[y];
        par[y] = x;
        if (rank[x] == rank[y])
            rank[x]++;
        return true;
    }
    public int Size(int x) { return -par[this[x]]; }
    public bool IsUnited(int x, int y) { return this[x] == this[y]; }

}
#endregion
0