結果
| 問題 |
No.3093 Safe Infection
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2025-04-06 15:54:44 |
| 言語 | C# (.NET 8.0.404) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 4,083 bytes |
| コンパイル時間 | 9,529 ms |
| コンパイル使用メモリ | 171,912 KB |
| 実行使用メモリ | 234,552 KB |
| 最終ジャッジ日時 | 2025-04-06 15:55:07 |
| 合計ジャッジ時間 | 20,624 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 32 WA * 38 |
コンパイルメッセージ
復元対象のプロジェクトを決定しています... /home/judge/data/code/main.csproj を復元しました (94 ミリ秒)。 main -> /home/judge/data/code/bin/Release/net8.0/main.dll main -> /home/judge/data/code/bin/Release/net8.0/publish/
ソースコード
#nullable enable
using System.Numerics;
void Run() => Out(Solve() ? "Yes" : "No");
bool Solve()
{
var n = Int();
var m = Int();
var k = Int();
var az = n.Repeat(Int);
var edges = m.Repeat(() => (Int() - 1, Int() - 1));
var adjl = n.Repeat(() => new List<int>());
foreach (var (u, v) in edges)
{
adjl[u].Add(v);
adjl[v].Add(u);
}
var aiz = new (int, int)[n];
for (var i = 0; i < n; i++) aiz[i] = (az[i], i);
Array.Sort(aiz);
var uf = new UnionFind(n);
foreach (var (_, i) in aiz)
{
var pi = uf.Find(i);
var ai = az[pi];
foreach (var j in adjl[i])
{
var pj = uf.Find(j);
var aj = az[pj];
if (Math.Abs(aj - ai) <= k)
{
ai = Math.Max(ai, aj);
uf.Union(pi, pj);
pi = uf.Find(pi);
if (uf.Size(pi) == n) return true;
az[pi] = Math.Max(az[pi], ai);
}
}
}
return false;
}
#region
AtCoderIO _io_;
var _backend_ = new StandardIOBackend();
_io_ = new(){ Backend = _backend_ };
Run();
_backend_.Flush();
string String() => _io_.Next();
int Int() => int.Parse(String());
void Out(object? x, string? sep = null) => _io_.Out(x, sep);
class AtCoderIO
{
public required StandardIOBackend Backend { get; init; }
Memory<string> _input = Array.Empty<string>();
int _iter = 0;
public string Next()
{
while (_iter >= _input.Length) (_input, _iter) = (Backend.ReadLine().Split(' '), 0);
return _input.Span[_iter++];
}
public void Out(object? x, string? separator = null)
{
if (x == null) return;
separator ??= Environment.NewLine;
if (x is System.Collections.IEnumerable a and not string)
{
var objects = a.Cast<object>();
if (separator == Environment.NewLine && !objects.Any()) return;
x = string.Join(separator, objects);
}
Backend.WriteLine(x);
}
}
class StandardIOBackend
{
readonly StreamReader _sr = new(Console.OpenStandardInput());
readonly StreamWriter _sw = new(Console.OpenStandardOutput()) { AutoFlush = false };
public string ReadLine() => _sr.ReadLine()!;
public void WriteLine(object? value) => _sw.WriteLine(value);
public void Flush() => _sw.Flush();
}
#endregion
static class Extensions
{
public static T[] Repeat<T>(this int time, Func<T> F) => Enumerable.Range(0, time).Select(_ => F()).ToArray();
}
class UnionFind
{
(int parent, int edgeCount)[] Verticals { get; set; }
bool EnabledUndo { get; init; }
Stack<(int v, (int parent, int edges))> Histories { get; init; }
public UnionFind(int verticals, bool enableUndo = false)
{
Verticals = new (int, int)[verticals];
for (int i = 0; i < verticals; i++) Verticals[i] = (-1, 0);
Histories = new();
EnabledUndo = enableUndo;
}
public int Union(int x, int y)
{
(x, y) = (Find(x), Find(y));
var (sx, sy) = (Size(x), Size(y));
if (sx < sy) (x, y, sy) = (y, x, sx);
var (nx, ny) = (Verticals[x], Verticals[y]);
Histories.Push((x, nx));
Histories.Push((y, ny));
if (x == y) nx.edgeCount += 1;
else
{
ny.parent = x;
nx.parent -= sy;
nx.edgeCount += ny.edgeCount + 1;
}
(Verticals[y], Verticals[x]) = (ny, nx);
return x;
}
public int Find(int x)
{
var pid = Verticals[x].parent;
if (pid < 0) return x;
var aid = Find(pid);
if (!EnabledUndo) Verticals[x].parent = aid;
return aid;
}
public int Size(int x) => -Verticals[Find(x)].parent;
public int EdgeCount(int x) => Verticals[Find(x)].edgeCount;
public bool Undo()
{
if (!EnabledUndo || Histories.Count == 0) return false;
for (var i = 0; i < 2; i++)
{
var (v, h) = Histories.Pop();
Verticals[v] = h;
}
return true;
}
}