結果
| 問題 |
No.2319 Friends+
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2023-08-03 23:35:07 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 2,969 bytes |
| コンパイル時間 | 951 ms |
| コンパイル使用メモリ | 109,568 KB |
| 実行使用メモリ | 69,352 KB |
| 最終ジャッジ日時 | 2024-10-13 20:40:30 |
| 合計ジャッジ時間 | 29,988 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 32 WA * 13 |
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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();
public static void Main()
{
Solve();
}
static void Solve()
{
var c = NList;
var (n, m) = (c[0], c[1]);
var p = NList;
var map = NArr(m);
var q = NN;
var query = NArr(q);
var fr = new UnionFindTree(n + q);
foreach (var edge in map) fr.Unite(edge[0] - 1, edge[1] - 1);
var gw = new Dictionary<int, int>[n + q];
for (var i = 0; i < gw.Length; ++i) gw[i] = new Dictionary<int, int>();
var pos = Enumerable.Repeat(-1, n + q).ToArray();
for (var i = 0; i < n; ++i)
{
pos[i] = p[i];
if (gw[fr.GetRoot(i)].ContainsKey(p[i])) ++gw[fr.GetRoot(i)][p[i]];
else gw[fr.GetRoot(i)][p[i]] = 1;
}
var id = new int[n];
for (var i = 0; i < n; ++i) id[i] = i;
var ans = new bool[q];
for (var i = 0; i < q; ++i)
{
var x = query[i][0] - 1;
var y = query[i][1] - 1;
var xpos = pos[id[x]];
var ypos = pos[id[y]];
var gid = fr.GetRoot(id[x]);
var xgroup = gw[gid];
if (xpos != ypos && xgroup.ContainsKey(ypos))
{
ans[i] = true;
--xgroup[xpos];
if (xgroup[xpos] == 0) xgroup.Remove(xpos);
fr.Unite(id[x], n + i);
id[x] = n + i;
++xgroup[ypos];
pos[id[x]] = ypos;
var newgid = fr.GetRoot(id[x]);
if (newgid != gid) gw[newgid] = xgroup;
}
}
WriteLine(string.Join("\n", ans.Select(f => f ? "Yes" : "No")));
}
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)];
}
}
}