結果

問題 No.1843 Tree ANDistance
ユーザー 👑 kakel-sankakel-san
提出日時 2023-12-05 00:22:02
言語 C#
(.NET 8.0.203)
結果
AC  
実行時間 311 ms / 2,000 ms
コード長 2,017 bytes
コンパイル時間 7,238 ms
コンパイル使用メモリ 158,376 KB
実行使用メモリ 179,952 KB
最終ジャッジ日時 2023-12-05 00:22:23
合計ジャッジ時間 18,888 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 290 ms
67,380 KB
testcase_01 AC 294 ms
67,508 KB
testcase_02 AC 298 ms
67,380 KB
testcase_03 AC 296 ms
67,380 KB
testcase_04 AC 311 ms
67,376 KB
testcase_05 AC 295 ms
67,508 KB
testcase_06 AC 299 ms
67,384 KB
testcase_07 AC 278 ms
67,084 KB
testcase_08 AC 293 ms
67,192 KB
testcase_09 AC 292 ms
66,828 KB
testcase_10 AC 272 ms
67,072 KB
testcase_11 AC 290 ms
67,248 KB
testcase_12 AC 271 ms
67,056 KB
testcase_13 AC 302 ms
66,936 KB
testcase_14 AC 85 ms
34,084 KB
testcase_15 AC 83 ms
34,212 KB
testcase_16 AC 78 ms
33,316 KB
testcase_17 AC 79 ms
33,316 KB
testcase_18 AC 77 ms
32,932 KB
testcase_19 AC 85 ms
34,468 KB
testcase_20 AC 86 ms
33,572 KB
testcase_21 AC 81 ms
32,420 KB
testcase_22 AC 79 ms
32,420 KB
testcase_23 AC 78 ms
32,420 KB
testcase_24 AC 78 ms
32,420 KB
testcase_25 AC 76 ms
32,420 KB
testcase_26 AC 80 ms
32,420 KB
testcase_27 AC 77 ms
32,420 KB
testcase_28 AC 185 ms
60,752 KB
testcase_29 AC 149 ms
52,792 KB
testcase_30 AC 148 ms
52,952 KB
testcase_31 AC 205 ms
62,980 KB
testcase_32 AC 202 ms
61,168 KB
testcase_33 AC 113 ms
43,300 KB
testcase_34 AC 163 ms
55,880 KB
testcase_35 AC 75 ms
32,548 KB
testcase_36 AC 77 ms
32,548 KB
testcase_37 AC 77 ms
179,952 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
  復元対象のプロジェクトを決定しています...
  /home/judge/data/code/main.csproj を復元しました (127 ms)。
MSBuild のバージョン 17.7.3+8ec440e68 (.NET)
  main -> /home/judge/data/code/bin/Release/net7.0/main.dll
  main -> /home/judge/data/code/bin/Release/net7.0/publish/

ソースコード

diff #

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 n = NN;
        var map = NArr(n - 1);
        var mod = 1_000_000_007;
        var ans = 0L;
        for (var b = 0; b < 30; ++b)
        {
            var uf = new UnionFindTree(n);
            foreach (var edge in map) if (((edge[2] >> b) & 1) == 1)
            {
                uf.Unite(edge[0] - 1, edge[1] - 1);
            }
            var sub = 0L;
            for (var i = 0; i < n; ++i) if (uf.GetRoot(i) == i)
            {
                var size = (long)uf.GetSize(i);
                sub += size * (size - 1) / 2;
            }
            ans = (ans + sub % mod * (1 << b) % mod) % mod;
        }
        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)];
        }
    }
}
0