結果
問題 | No.416 旅行会社 |
ユーザー | AreTrash |
提出日時 | 2016-09-01 07:37:15 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 4,016 bytes |
コンパイル時間 | 1,262 ms |
コンパイル使用メモリ | 114,844 KB |
実行使用メモリ | 74,352 KB |
最終ジャッジ日時 | 2024-11-14 14:07:08 |
合計ジャッジ時間 | 8,073 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 277 ms
57,564 KB |
testcase_01 | WA | - |
testcase_02 | AC | 35 ms
26,508 KB |
testcase_03 | AC | 35 ms
26,516 KB |
testcase_04 | AC | 35 ms
26,164 KB |
testcase_05 | WA | - |
testcase_06 | WA | - |
testcase_07 | WA | - |
testcase_08 | WA | - |
testcase_09 | WA | - |
testcase_10 | AC | 278 ms
56,024 KB |
testcase_11 | WA | - |
testcase_12 | WA | - |
testcase_13 | WA | - |
testcase_14 | WA | - |
testcase_15 | WA | - |
testcase_16 | WA | - |
testcase_17 | WA | - |
testcase_18 | WA | - |
testcase_19 | WA | - |
testcase_20 | AC | 383 ms
59,408 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 update = qf.Merge(e[0], e[1]); if(qf.Group[e[0]] == qf.Group[1] || qf.Group[e[1]] == qf.Group[1]){ foreach(var sgi in update){ 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 List<int> 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 new List<int>(); foreach(var iy in Items[gy]){ Group[iy] = gx; Items[gx].Add(iy); } return Items[gy]; } 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; } } }