結果

問題 No.416 旅行会社
ユーザー mbanmban
提出日時 2017-03-21 03:05:03
言語 C#(csc)
(csc 3.9.0)
結果
TLE  
実行時間 -
コード長 3,086 bytes
コンパイル時間 2,387 ms
コンパイル使用メモリ 107,156 KB
実行使用メモリ 40,880 KB
最終ジャッジ日時 2023-09-18 13:48:46
合計ジャッジ時間 13,375 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;
using System.Linq;
using System.IO;
using System.ComponentModel;


public class Magatro
{
    private static int N, M, Q;
    private static Bridge[] AB, CD;
    private static int[] Anser;
    private static int[] Par;
    private static List<int>[] Group;
    private static int[] Rank;

    private static void Scan()
    {
        string[] s = Console.ReadLine().Split(' ');
        N = int.Parse(s[0]);
        M = int.Parse(s[1]);
        Q = int.Parse(s[2]);
        CD = new Bridge[Q];
        AB = new Bridge[M];
        for (int i = 0; i < M; i++)
        {
            s = Console.ReadLine().Split(' ');
            AB[i] = new Bridge(int.Parse(s[0]) - 1, int.Parse(s[1]) - 1);
        }
        for (int i = 0; i < Q; i++)
        {
            s = Console.ReadLine().Split(' ');
            CD[i] = new Bridge(int.Parse(s[0]) - 1, int.Parse(s[1]) - 1);
        }
    }

    private static bool Same(int x, int y)
    {
        return Root(x) == Root(y);
    }

    private static void Union(int x, int y, int i)
    {
        int rX = Root(x);
        int rY = Root(y);
        if (rX == rY)
        {
            return;
        }
        int rZero = Root(0);
        if (rZero == rX)
        {
            foreach (int j in Group[rY])
            {
                Anser[j] = i;
            }
        }
        else if (rZero == rY)
        {
            foreach (int j in Group[rX])
            {
                Anser[j] = i;
            }
        }
        if (Group[rX].Count < Group[rY].Count)
        {
            Swap(ref rX, ref rY);
        }

        Group[rX].AddRange(Group[rY]);
        Group[rY] = Group[rX];
        if (Rank[rX] < Rank[rY])
        {
            Par[rX] = rY;
        }
        else
        {
            Par[rY] = rX;
            if (Rank[rX] == Rank[rY])
            {
                Rank[rX]++;
            }
        }
    }

    private static void Swap(ref int a, ref int b)
    {
        a ^= b;
        b ^= a;
        a ^= b;
    }

    private static int Root(int x)
    {
        return Par[x] == x ? x : Root(Par[x]);
    }

    static void Main()
    {
        Scan();
        AB = AB.Except(CD).ToArray();
        Par = new int[N];
        Rank = new int[N];
        for (int i = 0; i < N; i++)
        {
            Par[i] = i;
        }
        Group = new List<int>[N];
        for (int i = 0; i < N; i++)
        {
            Group[i] = new List<int>();
            Group[i].Add(i);
        }
        Anser = new int[N];
        foreach (var b in AB)
        {
            Union(b.A, b.B, -1);
        }

        for (int i = Q - 1; i >= 0; i--)
        {
            var b = CD[i];
            Union(b.A, b.B, i + 1);
        }

        for (int i = 1; i < N; i++)
        {
            Console.WriteLine(Anser[i]);
        }
    }

}

struct Bridge
{
    public int A, B;
    public Bridge(int a, int b)
    {
        A = a;
        B = b;
    }
}
0