結果
問題 | No.1266 7 Colors |
ユーザー | keymoon |
提出日時 | 2020-10-23 22:19:00 |
言語 | C#(csc) (csc 3.9.0) |
結果 |
AC
|
実行時間 | 928 ms / 3,000 ms |
コード長 | 3,440 bytes |
コンパイル時間 | 1,472 ms |
コンパイル使用メモリ | 110,336 KB |
実行使用メモリ | 44,276 KB |
最終ジャッジ日時 | 2024-07-21 10:56:00 |
合計ジャッジ時間 | 19,164 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge2 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 35 ms
18,944 KB |
testcase_01 | AC | 35 ms
18,944 KB |
testcase_02 | AC | 34 ms
19,072 KB |
testcase_03 | AC | 732 ms
27,904 KB |
testcase_04 | AC | 779 ms
37,112 KB |
testcase_05 | AC | 737 ms
28,672 KB |
testcase_06 | AC | 799 ms
39,800 KB |
testcase_07 | AC | 875 ms
41,452 KB |
testcase_08 | AC | 849 ms
37,504 KB |
testcase_09 | AC | 760 ms
34,420 KB |
testcase_10 | AC | 746 ms
33,280 KB |
testcase_11 | AC | 692 ms
30,208 KB |
testcase_12 | AC | 799 ms
31,104 KB |
testcase_13 | AC | 728 ms
32,128 KB |
testcase_14 | AC | 758 ms
28,800 KB |
testcase_15 | AC | 887 ms
41,712 KB |
testcase_16 | AC | 734 ms
31,360 KB |
testcase_17 | AC | 832 ms
40,812 KB |
testcase_18 | AC | 928 ms
44,276 KB |
testcase_19 | AC | 437 ms
43,124 KB |
testcase_20 | AC | 424 ms
43,120 KB |
testcase_21 | AC | 675 ms
23,936 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; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Numerics; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using static System.Math; using MethodImplAttribute = System.Runtime.CompilerServices.MethodImplAttribute; using MethodImplOptions = System.Runtime.CompilerServices.MethodImplOptions; public static class P { public static void Main() { var nmq = Console.ReadLine().Split().Select(int.Parse).ToArray(); var n = nmq[0]; var m = nmq[1]; var q = nmq[2]; UnionFind uf = new UnionFind(n * 7); bool[][] colors = new bool[n][]; for (int i = 0; i < n; i++) { var s = Console.ReadLine(); colors[i] = s.Select(x => x == '1').ToArray(); for (int j = 0; j < 7; j++) { var next = (j + 1) % 7; if (colors[i][j] && colors[i][next]) uf.TryUnite(j * n + i, next * n + i); } } List<int>[] edges = Enumerable.Repeat(0, n).Select(_ => new List<int>()).ToArray(); for (int i = 0; i < m; i++) { var uv = Console.ReadLine().Split().Select(int.Parse).ToArray(); int u = uv[0] - 1, v = uv[1] - 1; edges[u].Add(v); edges[v].Add(u); for (int j = 0; j < 7; j++) { if (colors[u][j] && colors[v][j]) uf.TryUnite(j * n + v, j * n + u); } } for (int i = 0; i < q; i++) { var txy = Console.ReadLine().Split().Select(int.Parse).ToArray(); var t = txy[0]; var x = txy[1] - 1; var y = txy[2] - 1; if (t == 1) { colors[x][y] = true; var prev = (y + 6) % 7; var next = (y + 1) % 7; if (colors[x][prev] && colors[x][y]) uf.TryUnite(prev * n + x, y * n + x); if (colors[x][y] && colors[x][next]) uf.TryUnite(y * n + x, next * n + x); foreach (var adj in edges[x]) { if (colors[adj][y]) uf.TryUnite(y * n + x, y * n + adj); } } else { Console.WriteLine(uf.GetSize(x)); } } } } class UnionFind { public int Size { get; private set; } public int GroupCount { get; private set; } public IEnumerable<int> AllRepresents => Parent.Where((x, y) => x == y); int[] Sizes; int[] Parent; public UnionFind(int count) { Size = count; GroupCount = count; Parent = new int[count]; Sizes = new int[count]; for (int i = 0; i < count; i++) Sizes[Parent[i] = i] = 1; } public bool TryUnite(int x, int y) { int xp = Find(x); int yp = Find(y); if (yp == xp) return false; if (Sizes[xp] < Sizes[yp]) { var tmp = xp; xp = yp; yp = tmp; } GroupCount--; Parent[yp] = xp; Sizes[xp] += Sizes[yp]; return true; } [MethodImpl(MethodImplOptions.AggressiveInlining)] public int GetSize(int x) => Sizes[Find(x)]; [MethodImpl(MethodImplOptions.AggressiveInlining)] public int Find(int x) { while (x != Parent[x]) x = (Parent[x] = Parent[Parent[x]]); return x; } }