結果

問題 No.529 帰省ラッシュ
ユーザー ひばちひばち
提出日時 2020-05-15 20:57:34
言語 C#(csc)
(csc 3.9.0)
結果
WA  
実行時間 -
コード長 16,781 bytes
コンパイル時間 2,428 ms
コンパイル使用メモリ 116,864 KB
実行使用メモリ 90,384 KB
最終ジャッジ日時 2024-09-19 07:40:21
合計ジャッジ時間 13,509 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
18,304 KB
testcase_01 AC 32 ms
18,304 KB
testcase_02 AC 32 ms
18,304 KB
testcase_03 AC 32 ms
18,176 KB
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 AC 754 ms
90,384 KB
testcase_14 AC 872 ms
59,248 KB
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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 System.Diagnostics;
using System.Numerics;
using static System.Console;
using static System.Convert;
using static System.Math;
using static Template;
using Pi = Pair<int, int>;

class Solver
{
    public void Solve(Scanner sc)
    {
        int N, M, Q;
        sc.Make(out N, out M, out Q);
        var te = new TwoEdgeConnectedComponents(N, M);
        for (int i = 0; i < M; i++)
        {
            int a, b;
            sc.Make(out a, out b);a--;b--;
            te.AddEdge(a, b);
        }
        var tec = te.Execute();
        var hl = new HLDecomposition(tec.Count);
        for (int i = 0; i < M; i++)
        {
            if (!te.bridge[i]) continue;
            int u = te[te.edge[i].fr], v = te[te.edge[i].to];
            hl.AddEdge(u, v);
        }
        hl.Build();
        var seg = new SegmentTree<(int val, int idx)>(tec.Count, (-1, -1), (a, b) => a.val > b.val ? a : b);
        var pqs = Create(tec.Count, () => { var n = new PriorityQueue<int>((a, b) => b - a); n.Push(-1); return n; });
        while (Q-- > 0)
        {
            var q = sc.Next<int>();
            if (q == 1)
            {
                int u, w;
                sc.Make(out u, out w); u--; u = te[u];
                pqs[u].Push(w);
                if (seg[hl[u]].val < w)
                {
                    seg.Update(hl[u], (w, u));
                }
            }
            else
            {
                int s, t;
                sc.Make(out s, out t); s--; t--; s = te[s]; t = te[t];
                var max = (val: -1, idx: -1);
                hl.For_each(s, t, (a, b) => { var k = seg.Query(a, b); if (max.val < k.val) max = k; });
                Console.WriteLine(max.val);
                if (max.idx != -1 && pqs[max.idx].Top != -1)
                {
                    pqs[max.idx].Pop();
                    seg.Update(max.idx, (pqs[max.idx].Top, max.idx));
                }
            }
        }

    }
}

public class SegmentTree<T>
{
    protected readonly T[] dat;
    protected readonly int sz;
    protected readonly Func<T, T, T> merge;
    protected readonly Func<T, T, T> update;
    protected readonly T id;
    private bool finished = true;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    protected int Left(int i)
        => i << 1;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    protected int Right(int i)
        => (i << 1) | 1;
    public T this[int i]
    {
        get { return dat[i + sz]; }
        set { finished = false; dat[i + sz] = value; }
    }

    public SegmentTree(int N, T id, Func<T, T, T> merge, Func<T, T, T> update = null)
    {
        this.sz = 1;
        while (sz < N) sz <<= 1;
        this.id = id;
        this.merge = merge;
        this.update = update ?? ((T val1, T val2) => val2);
        dat = Create(sz << 1, () => id);
    }

    public void Update(int i, T value)
    {
        i += sz;
        dat[i] = update(dat[i], value);
        while (i > 1)
        {
            i >>= 1;
            dat[i] = merge(dat[Left(i)], dat[Right(i)]);
        }
    }

    public void Build()
    {
        for (int i = sz - 1; i > 0; i--)
            dat[i] = merge(dat[Left(i)], dat[Right(i)]);
        finished = true;
    }

    public virtual T Query(int left, int right)
    {
        if (!finished) throw new Exception("You need to Execute \"Build()\"");
        T l = id, r = id;
        for (left += sz, right += sz; left < right; left >>= 1, right >>= 1)
        {
            if ((left & 1) == 1) l = merge(l, dat[left++]);
            if ((right & 1) == 1) r = merge(dat[--right], r);
        }
        return merge(l, r);
    }

    public int Find(int st, Func<T, bool> check)
    {
        var x = id;
        return Find(st, check, ref x, 1, 0, sz);
    }
    private int Find(int st, Func<T, bool> check, ref T x, int k, int l, int r)
    {
        if (l + 1 == r)
        { x = merge(x, dat[k]); return check(x) ? k - sz : -1; }
        var m = (l + r) >> 1;
        if (m <= st) return Find(st, check, ref x, Right(k), m, r);
        if (st <= l && !check(merge(x, dat[k])))
        { x = merge(x, dat[k]); return -1; }
        var xl = Find(st, check, ref x, Left(k), l, m);
        if (xl >= 0) return xl;
        return Find(st, check, ref x, Right(k), m, r);
    }
}
class HLDecomposition
{
    List<int>[] G;
    public int this[int i] => vertex[i];
    private int[] vertex, head, subtreeSize, par, dep, rev, right;
    public HLDecomposition(int sz)
    {
        G = Create(sz, () => new List<int>());
        vertex = Create(sz, () => -1);
        head = new int[sz];
        subtreeSize = new int[sz];
        par = Create(sz, () => -1);
        dep = new int[sz];
        rev = new int[sz];
        right = new int[sz];
    }
    public void AddEdge(int u, int v)
    {
        G[u].Add(v);
        G[v].Add(u);
    }
    public void Build(int root = 0)
    {
        int c = 0, pos = 0;
        head[root] = root;
        CalcSubtreeSize(root);
        Decomposition(root, ref pos);
    }

    void CalcSubtreeSize(int idx)
    {
        for (int i = 0; i < G[idx].Count; i++)
            if (G[idx][i] == par[idx]) { G[idx].swap(i, G[idx].Count - 1); break; }
        if (par[idx] != -1) G[idx].PopBack();
        for (int i = 0; i < G[idx].Count; i++)
        {
            int to = G[idx][i];
            par[to] = idx;
            dep[to] = dep[idx] + 1;
            CalcSubtreeSize(to);
            subtreeSize[idx] += subtreeSize[to];
            if (subtreeSize[to] > subtreeSize[G[idx][0]]) G[idx].swap(i, 0);
        }
        subtreeSize[idx]++;
    }
    void Decomposition(int idx, ref int pos)
    {
        vertex[idx] = pos++;
        rev[vertex[idx]] = idx;
        for (int i = 0; i < G[idx].Count; i++)
        {
            int to = G[idx][i];
            head[to] = (i == 0 ? head[idx] : to);
            Decomposition(to, ref pos);
        }
        right[idx] = pos;
    }

    public int LCA(int u, int v)
    {
        while (true)
        {
            if (vertex[u] > vertex[v]) swap(ref u, ref v);
            if (head[u] == head[v]) return u;
            v = par[head[v]];
        }
    }
    public int Distance(int u, int v) => dep[u] + dep[v] - 2 * dep[LCA(u, v)];

    public void For_each(int u, int v, Action<int, int> f)
    {
        while (true)
        {
            if (vertex[u] > vertex[v]) swap(ref u, ref v);
            f(Max(vertex[head[v]], vertex[u]), vertex[v] + 1);
            if (head[u] != head[v]) v = par[head[v]];
            else break;
        }
    }

    public void For_each_Edge(int u, int v, Action<int, int> f)
    {
        while (true)
        {
            if (vertex[u] > vertex[v]) swap(ref u, ref v);
            if (head[u] != head[v])
            {
                f(vertex[head[v]], vertex[v] + 1);
                v = par[head[v]];
            }
            else
            {
                if (u != v) f(vertex[u] + 1, vertex[v] + 1);
                break;
            }
        }
    }

    public void SubtreeQuery(int v, Action<int, int> f) => f(vertex[v], right[v]);
}
class TwoEdgeConnectedComponents
{
    private int N, M;
    public int GroupCount { get; private set; }
    int _idx;
    List<E>[] g;
    public E[] edge;
    int[] group, imos, dep;
    public bool[] bridge, use, tree;
    List<List<int>> elements;
    public int this[int i] { get { return group[i]; } }
    public TwoEdgeConnectedComponents(int N, int M)
    {
        this.N = N; this.M = M;
        g = Create(N, () => new List<E>());
        edge = new E[M];
    }
    public void AddEdge(int a, int b)
    {
        g[a].Add(new E(-1, b, _idx));
        g[b].Add(new E(-1, a, _idx));
        edge[_idx] = new E(a, b, _idx);
        _idx++;
    }
    public List<List<int>> Execute()
    {
        group = new int[N];
        imos = new int[N];
        dep = new int[N];
        bridge = new bool[M];
        tree = new bool[M];
        use = new bool[N];
        elements = new List<List<int>>();
        for (int j = 0; j < N; j++)
        {
            if (use[j]) continue;
            MakeDfsTree(j);
        }
        for (var i = 0; i < M; i++)
            if (!tree[i])
            {
                if (dep[edge[i].fr] > dep[edge[i].to])
                    swap(ref edge[i].fr, ref edge[i].to);
                imos[edge[i].fr]--;
                imos[edge[i].to]++;
            }

        use = new bool[N];
        for (int j = 0; j < N; j++)
        {
            if (use[j]) continue;
            FindBridge(j, -1);
        }
        use = new bool[N];
        for (int i = 0; i < N; i++)
        {
            if (use[i]) continue;
            elements.Add(new List<int>());
            dfs(i);
            GroupCount++;
        }
        return elements;
    }
    void MakeDfsTree(int i)
    {
        use[i] = true;
        foreach (E e in g[i])
            if (!use[e.to])
            {
                tree[e.idx] = true; dep[e.to] = dep[i] + 1;
                MakeDfsTree(e.to);
            }
    }
    int FindBridge(int i, int p)
    {
        use[i] = true;
        int now = imos[i];
        foreach (E e in g[i]) if (tree[e.idx] && e.to != p)
            {
                int r = FindBridge(e.to, i);
                if (r == 0) bridge[e.idx] = true;
                now += r;
            }
        return now;
    }
    void dfs(int i)
    {
        group[i] = GroupCount;
        elements[GroupCount].Add(i);
        use[i] = true;
        foreach (E e in g[i]) if (!use[e.to] && !bridge[e.idx])
            {
                dfs(e.to);
            }
    }
    public struct E { public int fr, to, idx; public E(int f, int t, int i) { fr = f; to = t; idx = i; } }
}

public class PriorityQueue<T>
{
    private List<T> data = new List<T>();
    private Comparison<T> cmp;
    public int Count { get { return data.Count; } }
    public T Top { get { return data[0]; } }
    public PriorityQueue() { cmp = cmp ?? Comparer<T>.Default.Compare; }

    public PriorityQueue(Comparison<T> comparison) { cmp = comparison; }

    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    private int Parent(int i)
        => (i - 1) >> 1;
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    private int Left(int i)
        => (i << 1) + 1;
    public T Push(T val)
    {
        int i = data.Count;
        data.Add(val);
        while (i > 0)
        {
            int p = Parent(i);
            if (cmp(data[p], val) <= 0)
                break;
            data[i] = data[p];
            i = p;
        }
        data[i] = val;
        return val;
    }
    public T Pop()
    {
        var ret = data[0];
        var p = 0;
        var x = data[data.Count - 1];
        while (Left(p) < data.Count - 1)
        {
            var l = Left(p);
            if (l < data.Count - 2 && cmp(data[l + 1], data[l]) < 0) l++;
            if (cmp(data[l], x) >= 0)
                break;
            data[p] = data[l];
            p = l;
        }
        data[p] = x;
        data.RemoveAt(data.Count - 1);
        return ret;
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public bool Any() => data.Count > 0;
}
#region Template
public static class Template
{
    static void Main(string[] args)
    {
        Console.SetOut(new StreamWriter(Console.OpenStandardOutput()) { AutoFlush = false });
        new Solver().Solve(new Scanner());
        Console.Out.Flush();
    }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static bool chmin<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) > 0) { a = b; return true; } return false; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static bool chmax<T>(ref T a, T b) where T : IComparable<T> { if (a.CompareTo(b) < 0) { a = b; return true; } return false; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static void swap<T>(ref T a, ref T b) { var t = b; b = a; a = t; }
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public static void swap<T>(this IList<T> A, int i, int j) { var t = A[i]; A[i] = A[j]; A[j] = t; }
    public static T[] Create<T>(int n, Func<T> f) { var rt = new T[n]; for (var i = 0; i < rt.Length; ++i) rt[i] = f(); return rt; }
    public static T[] Create<T>(int n, Func<int, T> f) { var rt = new T[n]; for (var i = 0; i < rt.Length; ++i) rt[i] = f(i); return rt; }
    public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> A) => A.OrderBy(v => Guid.NewGuid());
    public static int CompareTo<T>(this T[] A, T[] B, Comparison<T> cmp = null) { cmp = cmp ?? Comparer<T>.Default.Compare; for (var i = 0; i < Min(A.Length, B.Length); i++) { int c = cmp(A[i], B[i]); if (c > 0) return 1; else if (c < 0) return -1; } if (A.Length == B.Length) return 0; if (A.Length > B.Length) return 1; else return -1; }
    public static int MaxElement<T>(this IList<T> A, Comparison<T> cmp = null) { cmp = cmp ?? Comparer<T>.Default.Compare; T max = A[0]; int rt = 0; for (int i = 1; i < A.Count; i++) if (cmp(max, A[i]) < 0) { max = A[i]; rt = i; } return rt; }
    public static T PopBack<T>(this List<T> A) { var v = A[A.Count - 1]; A.RemoveAt(A.Count - 1); return v; }
    public static void Fail<T>(T s) { Console.WriteLine(s); Console.Out.Close(); Environment.Exit(0); }
}
public class Scanner
{
    public string Str => Console.ReadLine().Trim();
    public int Int => int.Parse(Str);
    public long Long => long.Parse(Str);
    public double Double => double.Parse(Str);
    public int[] ArrInt => Str.Split(' ').Select(int.Parse).ToArray();
    public long[] ArrLong => Str.Split(' ').Select(long.Parse).ToArray();
    public char[][] Grid(int n) => Create(n, () => Str.ToCharArray());
    public int[] ArrInt1D(int n) => Create(n, () => Int);
    public long[] ArrLong1D(int n) => Create(n, () => Long);
    public int[][] ArrInt2D(int n) => Create(n, () => ArrInt);
    public long[][] ArrLong2D(int n) => Create(n, () => ArrLong);
    private Queue<string> q = new Queue<string>();
    [MethodImpl(MethodImplOptions.AggressiveInlining)]
    public T Next<T>() { if (q.Count == 0) foreach (var item in Str.Split(' ')) q.Enqueue(item); return (T)Convert.ChangeType(q.Dequeue(), typeof(T)); }
    public void Make<T1>(out T1 v1) => v1 = Next<T1>();
    public void Make<T1, T2>(out T1 v1, out T2 v2) { v1 = Next<T1>(); v2 = Next<T2>(); }
    public void Make<T1, T2, T3>(out T1 v1, out T2 v2, out T3 v3) { Make(out v1, out v2); v3 = Next<T3>(); }
    public 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 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 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>(); }
    public void Make<T1, T2, T3, T4, T5, T6, T7>(out T1 v1, out T2 v2, out T3 v3, out T4 v4, out T5 v5, out T6 v6, out T7 v7) { Make(out v1, out v2, out v3, out v4, out v5, out v6); v7 = Next<T7>(); }
    //public (T1, T2) Make<T1, T2>() { Make(out T1 v1, out T2 v2); return (v1, v2); }
    //public (T1, T2, T3) Make<T1, T2, T3>() { Make(out T1 v1, out T2 v2, out T3 v3); return (v1, v2, v3); }
    //public (T1, T2, T3, T4) Make<T1, T2, T3, T4>() { Make(out T1 v1, out T2 v2, out T3 v3, out T4 v4); return (v1, v2, v3, v4); }
}
public class Pair<T1, T2> : IComparable<Pair<T1, T2>>
{
    public T1 v1;
    public T2 v2;
    public Pair() { }
    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 void Deconstruct(out T1 a, out T2 b) { a = v1; b = v2; }
}

public class Pair<T1, T2, T3> : Pair<T1, T2>, IComparable<Pair<T1, T2, T3>>
{
    public T3 v3;
    public Pair() : base() { }
    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()}";
    public void Deconstruct(out T1 a, out T2 b, out T3 c) { Deconstruct(out a, out b); c = v3; }
}
#endregion
0