結果

問題 No.416 旅行会社
ユーザー AreTrashAreTrash
提出日時 2016-09-01 07:53:40
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 505 ms / 4,000 ms
コード長 4,227 bytes
コンパイル時間 4,296 ms
コンパイル使用メモリ 108,924 KB
実行使用メモリ 70,016 KB
最終ジャッジ日時 2023-08-21 10:03:28
合計ジャッジ時間 10,997 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 286 ms
52,908 KB
testcase_01 AC 66 ms
22,692 KB
testcase_02 AC 69 ms
24,820 KB
testcase_03 AC 68 ms
22,808 KB
testcase_04 AC 69 ms
26,972 KB
testcase_05 AC 68 ms
22,692 KB
testcase_06 AC 67 ms
24,840 KB
testcase_07 AC 69 ms
22,664 KB
testcase_08 AC 70 ms
20,696 KB
testcase_09 AC 89 ms
28,440 KB
testcase_10 AC 285 ms
52,412 KB
testcase_11 AC 278 ms
51,160 KB
testcase_12 AC 277 ms
54,168 KB
testcase_13 AC 278 ms
53,908 KB
testcase_14 AC 504 ms
67,988 KB
testcase_15 AC 500 ms
70,016 KB
testcase_16 AC 497 ms
68,052 KB
testcase_17 AC 505 ms
68,960 KB
testcase_18 AC 504 ms
69,064 KB
testcase_19 AC 356 ms
60,348 KB
testcase_20 AC 358 ms
55,976 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;

namespace No416{
    public class Program{
        public static void Main(string[] args){
            var sr = new StreamReader();
            //---------------------------------
            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];

                var items = new List<int>();
                if(qf.Group[e[0]] == qf.Group[1]){
                    items = qf.Items[qf.Group[e[1]]];
                }
                if(qf.Group[e[1]] == qf.Group[1]){
                    items = qf.Items[qf.Group[e[0]]];
                }

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

                qf.Merge(e[0], e[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].Add(iy);
            }
        }

        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 =
            new System.IO.StreamReader(/*@"test_in\line1.txt"*/Console.OpenStandardInput());

        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