結果
問題 | No.416 旅行会社 |
ユーザー | AreTrash |
提出日時 | 2016-09-01 07:04:58 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,965 bytes |
コンパイル時間 | 1,176 ms |
コンパイル使用メモリ | 116,656 KB |
実行使用メモリ | 128,240 KB |
最終ジャッジ日時 | 2024-11-14 14:06:59 |
合計ジャッジ時間 | 55,003 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge4 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | TLE | - |
testcase_01 | AC | 33 ms
35,428 KB |
testcase_02 | AC | 35 ms
80,244 KB |
testcase_03 | AC | 34 ms
35,696 KB |
testcase_04 | AC | 34 ms
35,388 KB |
testcase_05 | AC | 34 ms
33,596 KB |
testcase_06 | AC | 34 ms
35,004 KB |
testcase_07 | AC | 36 ms
101,028 KB |
testcase_08 | AC | 52 ms
33,632 KB |
testcase_09 | AC | 306 ms
103,956 KB |
testcase_10 | TLE | - |
testcase_11 | AC | 285 ms
128,240 KB |
testcase_12 | AC | 274 ms
61,348 KB |
testcase_13 | AC | 277 ms
112,364 KB |
testcase_14 | TLE | - |
testcase_15 | TLE | - |
testcase_16 | TLE | - |
testcase_17 | TLE | - |
testcase_18 | TLE | - |
testcase_19 | TLE | - |
testcase_20 | TLE | - |
コンパイルメッセージ
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(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; } } }