結果
問題 | No.416 旅行会社 |
ユーザー | AreTrash |
提出日時 | 2016-09-01 07:53:40 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 477 ms / 4,000 ms |
コード長 | 4,227 bytes |
コンパイル時間 | 1,077 ms |
コンパイル使用メモリ | 117,084 KB |
実行使用メモリ | 64,500 KB |
最終ジャッジ日時 | 2024-05-08 15:20:53 |
合計ジャッジ時間 | 6,978 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge5 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 250 ms
49,568 KB |
testcase_01 | AC | 29 ms
20,480 KB |
testcase_02 | AC | 29 ms
20,608 KB |
testcase_03 | AC | 29 ms
20,608 KB |
testcase_04 | AC | 30 ms
20,608 KB |
testcase_05 | AC | 29 ms
20,480 KB |
testcase_06 | AC | 29 ms
20,480 KB |
testcase_07 | AC | 32 ms
20,992 KB |
testcase_08 | AC | 34 ms
21,992 KB |
testcase_09 | AC | 54 ms
25,728 KB |
testcase_10 | AC | 263 ms
49,836 KB |
testcase_11 | AC | 242 ms
48,580 KB |
testcase_12 | AC | 239 ms
48,588 KB |
testcase_13 | AC | 249 ms
48,684 KB |
testcase_14 | AC | 473 ms
64,116 KB |
testcase_15 | AC | 457 ms
63,988 KB |
testcase_16 | AC | 456 ms
64,372 KB |
testcase_17 | AC | 472 ms
64,500 KB |
testcase_18 | AC | 477 ms
64,128 KB |
testcase_19 | AC | 319 ms
53,700 KB |
testcase_20 | AC | 312 ms
53,432 KB |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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; } } }