結果

問題 No.416 旅行会社
ユーザー AreTrashAreTrash
提出日時 2016-09-01 07:04:58
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 3,965 bytes
コンパイル時間 1,107 ms
コンパイル使用メモリ 110,080 KB
実行使用メモリ 57,124 KB
最終ジャッジ日時 2024-04-26 22:24:05
合計ジャッジ時間 12,823 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
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;

namespace No416{
    public class Program{
        public static void Main(string[] args){
            var sr = new StreamReader(Console.OpenStandardInput());
            //---------------------------------
            var N = sr.Next<int>();
            var M = sr.Next<int>();
            var Q = sr.Next<int>();
            var AB = sr.Next<int>(M, 2);
            var CD = sr.Next<int>(Q, 2);

            Func<int, int, long> hash = (s, t) => s * 1000000L + t;

            var hs = new HashSet<long>();
            foreach(var e in CD){
                hs.Add(hash(e[0], e[1]));
            }

            var qf = new QuickFind(N + 1);
            foreach(var e in AB){
                if(hs.Contains(hash(e[0], e[1]))) continue;
                qf.Merge(e[0], e[1]);
            }

            var res = new int[N + 1];
            foreach(var sgi in qf.SameGroupItems(1)){
                res[sgi] = -1;
            }

            for(var i = CD.Length - 1; i >= 0; i--){
                var e = CD[i];
                qf.Merge(e[0], e[1]);

                foreach(var sgi in qf.SameGroupItems(1)){
                    if(res[sgi] == 0) res[sgi] = i + 1;
                }
            }

            Console.WriteLine(string.Join("\n", res.Skip(2)));
            //---------------------------------
        }
    }

    public class Ex{
        public static void Swap<T>(ref T left, ref T right){
            var tmp = left;
            left = right;
            right = tmp;
        }
    }

    public class QuickFind{
        //データ構造をマージする一般的なテク
        public readonly int[] Group;
        public readonly List<int>[] Items;

        public QuickFind(int n){
            Group = new int[n];
            Items = new List<int>[n];
            for(var i = 0; i < n; i++){
                Group[i] = i;
                Items[i] = new List<int>{i};
            }
        }

        public void Merge(int x, int y){
            if(Items[Group[x]].Count < Items[Group[y]].Count) Ex.Swap(ref x, ref y);

            var gx = Group[x];
            var gy = Group[y];
            if(gx == gy) return;

            foreach(var iy in Items[gy]){
                Group[iy] = gx;
            }
            Items[gx].AddRange(Items[gy]);
            Items[gy].Clear();
        }

        public List<int> SameGroupItems(int x){
            return Items[Group[x]];
        }

        public bool IsSameGroup(int x, int y){
            return Group[x] == Group[y];
        }
    }

    public class StreamReader{
        private readonly char[] _c = {' '};
        private int _index = -1;
        private string[] _input = new string[0];
        private readonly System.IO.StreamReader _sr;

        public StreamReader(System.IO.Stream st){
            _sr = new System.IO.StreamReader(st);
        }

        public T Next<T>(){
            if(_index == _input.Length - 1){
                _index = -1;
                while(true){
                    string rl = _sr.ReadLine();
                    if(rl == null){
                        if(typeof(T).IsClass) return default(T);
                        return (T)typeof(T).GetField("MinValue").GetValue(null);
                    }
                    if(rl != ""){
                        _input = rl.Split(_c, StringSplitOptions.RemoveEmptyEntries);
                        break;
                    }
                }
            }
            return (T)Convert.ChangeType(_input[++_index], typeof(T), System.Globalization.CultureInfo.InvariantCulture);
        }

        public T[] Next<T>(int x){
            var ret = new T[x];
            for(var i = 0; i < x; ++i) ret[i] = Next<T>();
            return ret;
        }

        public T[][] Next<T>(int y, int x){
            var ret = new T[y][];
            for(var i = 0; i < y; ++i) ret[i] = Next<T>(x);
            return ret;
        }
    }
}
0