Submission #826143


Source Code Expand

using System;
using System.Linq;
using System.Linq.Expressions;
using System.Collections.Generic;
using Debug = System.Diagnostics.Debug;
using StringBuilder = System.Text.StringBuilder;
using System.Numerics;
using Number = System.Int64;
namespace Program
{
    public class Solver
    {
        public void Solve()
        {

            var n = sc.Integer();
            var m = sc.Integer();
            const int SZ = 320;

            var his = new int[SZ + 1][];
            var set = new DisjointSet(n);
            his[0] = set.par.Clone() as int[];
            var a = new int[m];
            var b = new int[m];
            for (int i = 1; i <= m; i++)
            {
                a[i - 1] = sc.Integer() - 1;
                b[i - 1] = sc.Integer() - 1;
                if (set.Unite(a[i - 1], b[i - 1]))
                    Debug.WriteLine("unite");
                else Debug.WriteLine("united");
                if (i % SZ == 0)
                    his[i / SZ] = set.par.Clone() as int[];
                else if (i == m)
                    his[(i / SZ) + 1] = set.par.Clone() as int[];
            }
            var q = sc.Integer();
            for (int i = 0; i < q; i++)
            {
                var f = sc.Integer() - 1;
                var t = sc.Integer() - 1;
                var c = sc.Integer();
                var p = -1;
                for (int j = 0; j < SZ && p == -1; j++)
                {
                    Array.Copy(his[j], set.par, n);
                    if (set.IsUnited(f, t))
                    {
                        if (set.Size(f) >= c) p = j - 1;
                    }
                    else
                    {
                        if (set.Size(f) + set.Size(t) >= c) p = j - 1;

                    }
                }
                Array.Copy(his[p], set.par, n);
                for (int j = p * SZ; j < (p + 1) * SZ; j++)
                {
                    set.Unite(a[j], b[j]);
                    if (set.IsUnited(f, t))
                    {
                        if (set.Size(f) >= c)
                        {
                            IO.Printer.Out.WriteLine(j + 1);
                            break;
                        }
                    }
                    else
                    {
                        if (set.Size(f) + set.Size(t) >= c)
                        {
                            IO.Printer.Out.WriteLine(j + 1);
                            break;
                        }

                    }
                }
            }


        }
        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 DisjointSet
public class DisjointSet
{
    public int[] par;
    public DisjointSet(int n)
    {
        par = new int[n];
        for (int i = 0; i < n; i++)
            par[i] = -1;
    }
    public int this[int id] { get { return (par[id] < 0) ? id : par[id] = this[par[id]]; } }
    public bool Unite(int x, int y)
    {
        x = this[x]; y = this[y];
        if (x == y) return false;
        par[x] += par[y];
        par[y] = 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

Submission Info

Submission Time
Task D - Stamp Rally
User camypaper
Language C# (Mono 4.6.2.0)
Score 0
Code Size 7388 Byte
Status TLE
Exec Time 2121 ms
Memory 128304 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 1000
Status
AC × 1
AC × 1
TLE × 32
Set Name Test Cases
Sample 0_00.txt
All 0_00.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 1_30.txt, 1_31.txt
Case Name Status Exec Time Memory
0_00.txt AC 36 ms 2776 KB
1_00.txt TLE 2119 ms 128304 KB
1_01.txt TLE 2118 ms 128304 KB
1_02.txt TLE 2120 ms 128304 KB
1_03.txt TLE 2118 ms 128304 KB
1_04.txt TLE 2118 ms 128304 KB
1_05.txt TLE 2119 ms 128304 KB
1_06.txt TLE 2118 ms 128304 KB
1_07.txt TLE 2118 ms 128304 KB
1_08.txt TLE 2118 ms 128304 KB
1_09.txt TLE 2118 ms 128304 KB
1_10.txt TLE 2119 ms 128304 KB
1_11.txt TLE 2119 ms 128304 KB
1_12.txt TLE 2120 ms 128304 KB
1_13.txt TLE 2118 ms 128304 KB
1_14.txt TLE 2118 ms 128304 KB
1_15.txt TLE 2121 ms 128304 KB
1_16.txt TLE 2117 ms 122680 KB
1_17.txt TLE 2114 ms 125744 KB
1_18.txt TLE 2117 ms 122936 KB
1_19.txt TLE 2118 ms 128304 KB
1_20.txt TLE 2116 ms 109752 KB
1_21.txt TLE 2117 ms 126256 KB
1_22.txt TLE 2118 ms 121016 KB
1_23.txt TLE 2114 ms 122552 KB
1_24.txt TLE 2116 ms 115896 KB
1_25.txt TLE 2118 ms 122552 KB
1_26.txt TLE 2116 ms 112440 KB
1_27.txt TLE 2118 ms 122936 KB
1_28.txt TLE 2117 ms 121400 KB
1_29.txt TLE 2117 ms 114744 KB
1_30.txt TLE 2118 ms 121784 KB
1_31.txt TLE 2116 ms 109752 KB