結果
| 問題 |
No.1639 最小通信路
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-08-06 22:10:57 |
| 言語 | C#(csc) (csc 3.9.0) |
| 結果 |
AC
|
| 実行時間 | 99 ms / 2,000 ms |
| コード長 | 2,282 bytes |
| コンパイル時間 | 4,261 ms |
| コンパイル使用メモリ | 112,152 KB |
| 実行使用メモリ | 21,760 KB |
| 最終ジャッジ日時 | 2024-09-17 02:11:46 |
| 合計ジャッジ時間 | 7,618 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 43 |
コンパイルメッセージ
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.Generic;
using static System.Console;
using System.Linq;
class yuki308
{
static int NN => int.Parse(ReadLine());
static int[] NList => ReadLine().Split().Select(int.Parse).ToArray();
static long[] LList => ReadLine().Split().Select(long.Parse).ToArray();
static string[] SList(int n) => Enumerable.Repeat(0, n).Select(_ => ReadLine()).ToArray();
static void Main()
{
var n = NN;
var map = new Pair[n * (n - 1) / 2];
for (var i = 0; i < map.Length; ++i)
{
var c = ReadLine().Split();
map[i] = new Pair(int.Parse(c[0]) - 1, int.Parse(c[1]) - 1, c[2]);
}
Array.Sort(map);
var uf = new UnionFindTree(n);
var r = "";
foreach (var way in map)
{
if (!uf.IsSameTree(way.TA, way.TB))
{
uf.Unite(way.TA, way.TB);
r = way.Str;
}
}
WriteLine(r);
}
class Pair : IComparable<Pair>
{
public int TA;
public int TB;
public string Str;
public Pair(int a, int b, string str)
{
TA = a; TB = b; Str = str;
}
public int CompareTo(Pair b)
{
var c = Str.Length.CompareTo(b.Str.Length);
if (c != 0) return c;
return Str.CompareTo(b.Str);
}
}
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)];
}
}
}