結果
問題 |
No.1266 7 Colors
|
ユーザー |
|
提出日時 | 2025-02-13 22:52:20 |
言語 | C# (.NET 8.0.404) |
結果 |
AC
|
実行時間 | 440 ms / 3,000 ms |
コード長 | 3,180 bytes |
コンパイル時間 | 7,804 ms |
コンパイル使用メモリ | 169,752 KB |
実行使用メモリ | 225,476 KB |
最終ジャッジ日時 | 2025-02-13 22:52:42 |
合計ジャッジ時間 | 17,387 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 19 |
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /home/judge/data/code/main.csproj を復元しました (123 ミリ秒)。 main -> /home/judge/data/code/bin/Release/net8.0/main.dll main -> /home/judge/data/code/bin/Release/net8.0/publish/
ソースコード
using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); static char[][] SList(long n) => Enumerable.Repeat(0, (int)n).Select(_ => ReadLine().ToCharArray()).ToArray(); public static void Main() { Solve(); } static void Solve() { var c = NList; var (n, m, q) = (c[0], c[1], c[2]); var s = SList(n); var map = NArr(m); var query = NArr(q); var tree = new List<int>[n]; for (var i = 0; i < n; ++i) tree[i] = new List<int>(); foreach (var edge in map) { tree[edge[0] - 1].Add(edge[1] - 1); tree[edge[1] - 1].Add(edge[0] - 1); } var uf = new UnionFindTree(n * 7); for (var i = 0; i < n; ++i) { for (var j = 0; j < 7; ++j) { if (s[i][j] == '1' && s[i][(j + 1) % 7] == '1') { uf.Unite(i * 7 + j, i * 7 + (j + 1) % 7); } } foreach (var next in tree[i]) if (i < next) for (var j = 0; j < 7; ++j) { if (s[i][j] == '1' && s[next][j] == '1') { uf.Unite(i * 7 + j, next * 7 + j); } } } var ans = new List<int>(); for (var i = 0; i < q; ++i) { var x = query[i][1] - 1; var y = query[i][2] - 1; if (query[i][0] == 1) { s[x][y] = '1'; if (s[x][(y + 1) % 7] == '1') uf.Unite(x * 7 + y, x * 7 + (y + 1) % 7); if (s[x][(y + 6) % 7] == '1') uf.Unite(x * 7 + y, x * 7 + (y + 6) % 7); foreach (var next in tree[x]) { if (s[next][y] == '1') uf.Unite(x * 7 + y, next * 7 + y); } } else ans.Add(uf.GetSize(x * 7)); } WriteLine(string.Join("\n", ans)); } class UnionFindTree { int[] roots; public UnionFindTree(int size) { roots = new int[size]; for (var i = 0; i < size; ++i) roots[i] = -1; } public int GetRoot(int a) { if (roots[a] < 0) return a; return roots[a] = GetRoot(roots[a]); } public bool IsSameTree(int a, int b) { return GetRoot(a) == GetRoot(b); } public bool Unite(int a, int b) { var x = GetRoot(a); var y = GetRoot(b); if (x == y) return false; if (-roots[x] < -roots[y]) { var tmp = x; x = y; y = tmp; } roots[x] += roots[y]; roots[y] = x; return true; } public int GetSize(int a) { return -roots[GetRoot(a)]; } } }