結果

問題 No.3309 Aging Railway
コンテスト
ユーザー kakel-san
提出日時 2025-10-24 21:35:09
言語 C#
(.NET 8.0.404)
結果
TLE  
実行時間 -
コード長 2,258 bytes
コンパイル時間 9,254 ms
コンパイル使用メモリ 169,648 KB
実行使用メモリ 75,856 KB
最終ジャッジ日時 2025-10-24 21:35:25
合計ジャッジ時間 14,478 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 3 TLE * 1 -- * 16
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (109 ミリ秒)。
  main -> /home/judge/data/code/bin/Release/net8.0/main.dll
  main -> /home/judge/data/code/bin/Release/net8.0/publish/

ソースコード

diff #

using System;
using static System.Console;
using System.Linq;
using System.Collections.Generic;
using System.Globalization;

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();
    static string[] SList(long n) => Enumerable.Repeat(0, (int)n).Select(_ => ReadLine()).ToArray();
    static long[] LList(long n) => Enumerable.Repeat(0, (int)n).Select(_ => long.Parse(ReadLine())).ToArray();
    public static void Main()
    {
        Solve();
    }
    static void Solve()
    {
        var c = NList;
        var (n, m) = (c[0], c[1]);
        var map = NMap(n - 1);
        var query = NMap(m);

        var uf = new UnionFindTree(n);
        var ans = new int[n - 1];
        uf.Unite(map[n - 2][0], map[n - 2][1]);
        for (var i = n - 3; i >= 0; --i)
        {
            for (var j = 0; j < m; ++j) if (uf.IsSameTree(query[j][0], query[j][1])) ++ans[i];
            uf.Unite(map[i][0], map[i][1]);
        }
        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)];
        }
    }
}
0