結果
| 問題 |
No.2563 色ごとのグループ
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-12-02 14:59:42 |
| 言語 | C# (.NET 8.0.404) |
| 結果 |
AC
|
| 実行時間 | 339 ms / 2,000 ms |
| コード長 | 2,056 bytes |
| コンパイル時間 | 8,838 ms |
| コンパイル使用メモリ | 165,784 KB |
| 実行使用メモリ | 240,928 KB |
| 最終ジャッジ日時 | 2024-09-26 17:52:08 |
| 合計ジャッジ時間 | 15,450 ms |
|
ジャッジサーバーID (参考情報) |
judge4 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 35 |
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /home/judge/data/code/main.csproj を復元しました (95 ms)。 MSBuild のバージョン 17.9.6+a4ecab324 (.NET) 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 int[] NMi => ReadLine().Split().Select(c => int.Parse(c) - 1).ToArray();
static int[][] NMap(int n) => Enumerable.Repeat(0, n).Select(_ => NMi).ToArray();
public static void Main()
{
Solve();
}
static void Solve()
{
var c = NList;
var (n, m) = (c[0], c[1]);
var col = NList;
var map = NMap(m);
var uf = new UnionFindTree(n);
foreach (var edge in map)
{
if (col[edge[0]] == col[edge[1]]) uf.Unite(edge[0], edge[1]);
}
var exists = new bool[n];
var ans = 0;
for (var i = 0; i < n; ++i) if (uf.GetRoot(i) == i)
{
if (exists[col[i] - 1]) ++ans;
else exists[col[i] - 1] = true;
}
WriteLine(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)];
}
}
}